Skip to content

Commit

Permalink
refactoring + resend packets every 2 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
AltronMaxX committed Jan 14, 2024
1 parent 0e5f3a7 commit 4490028
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 177 deletions.
9 changes: 6 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id("com.github.johnrengelman.shadow") version "7.1.2"
}

val versionStr = "1.1.0"
val versionStr = "1.2.0"

group = "net.edenor.minimap"
version = versionStr
Expand Down Expand Up @@ -32,8 +32,6 @@ dependencies {
compileOnly("dev.folia:folia-api:1.20.1-R0.1-SNAPSHOT")

// Common Dependencies
implementation("org.spongepowered:configurate-core:4.1.2")
implementation("org.spongepowered:configurate-yaml:4.1.2")
implementation("net.kyori:adventure-api:4.15.0")
implementation("net.kyori:adventure-text-minimessage:4.15.0")

Expand All @@ -44,6 +42,11 @@ dependencies {
}

tasks {
processResources {
outputs.upToDateWhen { false }
eachFile { expand("version" to project.version) }
}

build {
dependsOn(shadowJar)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/edenor/minimap/DefaultWorldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.edenor.minimap.jm.data.JMWorldConfig;
import net.edenor.minimap.voxel.data.VoxelWorldConfig;
import net.edenor.minimap.xaeros.XaerosWorldConfig;
import net.edenor.minimap.xaeros.data.XaerosWorldConfig;
import org.bukkit.configuration.file.FileConfiguration;

public class DefaultWorldConfig {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/net/edenor/minimap/MinimapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import net.edenor.minimap.jm.data.JMWorldConfig;
import net.edenor.minimap.voxel.data.VoxelConfig;
import net.edenor.minimap.voxel.data.VoxelWorldConfig;
import net.edenor.minimap.xaeros.XaerosConfig;
import net.edenor.minimap.xaeros.XaerosWorldConfig;
import net.edenor.minimap.xaeros.data.XaerosConfig;
import net.edenor.minimap.xaeros.data.XaerosWorldConfig;
import org.bukkit.World;
import org.bukkit.configuration.MemoryConfiguration;
import org.bukkit.configuration.file.FileConfiguration;

import java.io.IOException;
Expand Down Expand Up @@ -67,7 +66,7 @@ public static void saveConfig() throws IOException {
public static void addWorld(World world){
if (config.contains("worlds." + world.getName())){
WorldConfig worldConfig = new WorldConfig();
worldConfig.fromConfig(config, world.getName());
worldConfig.fromConfig(world);
worlds.put(world.getName(), worldConfig);
}
else {
Expand All @@ -82,13 +81,14 @@ public static WorldConfig getWorldConfig(String world) {
WorldConfig conf = worlds.get(world);

if (conf == null) {
conf = new WorldConfig();
throw new RuntimeException("This world does not exists!");
/*conf = new WorldConfig();
worlds.put(world, conf);
try {
saveConfig();
} catch (IOException e) {
throw new RuntimeException(e);
}
}*/
}

return conf;
Expand All @@ -100,11 +100,11 @@ public static class WorldConfig {
public XaerosWorldConfig xaerosConfig = new XaerosWorldConfig();
public VoxelWorldConfig voxelWorldConfig = new VoxelWorldConfig();

public void fromConfig(FileConfiguration cfg, String worldName) {
this.worldId = cfg.getObject("worlds." + worldName + ".worldId", UUID.class);
this.journeymapConfig.loadFromConfig(worldName);
this.xaerosConfig.loadFromConfig(worldName);
this.voxelWorldConfig.loadFromConfig(worldName);
public void fromConfig(World world) {
this.worldId = world.getUID();
this.journeymapConfig.loadFromConfig(world.getName());
this.xaerosConfig.loadFromConfig(world.getName());
this.voxelWorldConfig.loadFromConfig(world.getName());
}

public Map<String, Object> toMap() {
Expand Down
40 changes: 25 additions & 15 deletions src/main/java/net/edenor/minimap/MinimapEvents.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package net.edenor.minimap;

import net.edenor.minimap.api.MinimapPlayer;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;

public class MinimapEvents implements @NotNull Listener {
import java.util.HashMap;
import java.util.Map;

private MinimapPlugin plugin;
public class MinimapEvents implements Listener {

private final MinimapPlugin plugin;
private final Map<String, ScheduledTask> playerTaskMap = new HashMap<>();

public MinimapEvents(MinimapPlugin plugin){
this.plugin = plugin;
Expand All @@ -18,24 +24,28 @@ public MinimapEvents(MinimapPlugin plugin){
@EventHandler
public void onJoin(PlayerJoinEvent event) {
plugin.getServer().getGlobalRegionScheduler().runDelayed(plugin,
v->this.handlePackets(new MinimapPlayer(event.getPlayer())), 20L);
plugin.getServer().getGlobalRegionScheduler().runDelayed(plugin,
v->this.handlePackets(new MinimapPlayer(event.getPlayer())), 100L); //Resend later
plugin.getServer().getGlobalRegionScheduler().runDelayed(plugin,
v->this.handlePackets(new MinimapPlayer(event.getPlayer())), 200L); //Resend later
v->this.handlePackets(event.getPlayer()), 20L);
ScheduledTask task = this.plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(this.plugin,
(v) -> this.handlePackets(event.getPlayer()), 40L, 40L);
this.playerTaskMap.put(event.getPlayer().getName(), task);
}

@EventHandler
public void onQuit(PlayerQuitEvent event) {
ScheduledTask task = this.playerTaskMap.get(event.getPlayer().getName());
if (task != null) {
this.playerTaskMap.get(event.getPlayer().getName()).cancel();
this.playerTaskMap.remove(event.getPlayer().getName());
}

}

@EventHandler
public void onWorldChange(PlayerChangedWorldEvent event) {
plugin.getServer().getGlobalRegionScheduler().runDelayed(plugin,
v->this.handlePackets(new MinimapPlayer(event.getPlayer())), 20L);
plugin.getServer().getGlobalRegionScheduler().runDelayed(plugin,
v->this.handlePackets(new MinimapPlayer(event.getPlayer())), 100L); //Resend later
plugin.getServer().getGlobalRegionScheduler().runDelayed(plugin,
v->this.handlePackets(new MinimapPlayer(event.getPlayer())), 200L); //Resend later
this.handlePackets(event.getPlayer());
}

public void handlePackets(MinimapPlayer player) {
public void handlePackets(@NotNull Player player) {
plugin.xaerosHandler.sendXaerosHandshake(player);
plugin.xaerosHandler.sendXaerosConfig(player);
plugin.voxelHandler.sendSettings(player);
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/net/edenor/minimap/MinimapListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.edenor.minimap;

import net.edenor.minimap.api.MinimapPlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.jetbrains.annotations.NotNull;
Expand All @@ -23,7 +22,7 @@ public class MinimapListener implements PluginMessageListener {
"voxelmap:settings"
);

private MinimapPlugin plugin;
private final MinimapPlugin plugin;

public MinimapListener(MinimapPlugin plugin){
this.plugin = plugin;
Expand All @@ -36,16 +35,16 @@ public void registerChannel(String channel) {
plugin.getServer().getMessenger().registerIncomingPluginChannel(MinimapPlugin.getInstance(), channel, this);
}

public void onPluginMessage(String channel, MinimapPlayer player, byte[] message) {
public void onPluginMessage(String channel, Player player, byte[] message) {
switch (channel.split(":")[0]) {
case "journeymap" -> plugin.jmHandler.onPluginMessage(channel, player, message);
case "xaerominimap", "xaeroworldmap" -> plugin.xaerosHandler.onPluginMessage(channel, player, message);
case "worldinfo" -> plugin.worldInfoHandler.onPluginMessage(channel, player, message);
case "xaerominimap", "xaeroworldmap" -> plugin.xaerosHandler.onPluginMessage(player, message);
case "worldinfo" -> plugin.worldInfoHandler.onPluginMessage(player);
}
}

@Override
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
this.onPluginMessage(channel, new MinimapPlayer(player), message);
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte @NotNull [] message) {
this.onPluginMessage(channel, player, message);
}
}
4 changes: 1 addition & 3 deletions src/main/java/net/edenor/minimap/MinimapPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import net.edenor.minimap.xaeros.XaerosHandler;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
public class MinimapPlugin extends JavaPlugin {
private static MinimapPlugin instance;
public final JMHandler jmHandler = new JMHandler(this);
public final XaerosHandler xaerosHandler = new XaerosHandler();
public final WorldInfoHandler worldInfoHandler = new WorldInfoHandler();
public final VoxelHandler voxelHandler = new VoxelHandler(this);

public final VoxelHandler voxelHandler = new VoxelHandler();
public MinimapPlugin plugin;

@Override
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/edenor/minimap/api/MessageHandler.java

This file was deleted.

9 changes: 0 additions & 9 deletions src/main/java/net/edenor/minimap/api/MinimapLocation.java

This file was deleted.

38 changes: 0 additions & 38 deletions src/main/java/net/edenor/minimap/api/MinimapPlayer.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/net/edenor/minimap/api/MinimapWorld.java

This file was deleted.

Loading

0 comments on commit 4490028

Please sign in to comment.