Skip to content

Commit

Permalink
better look
Browse files Browse the repository at this point in the history
  • Loading branch information
AltronMaxX committed Jan 4, 2024
1 parent 5a92b66 commit cbfedaa
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 111 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ 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-platform-bukkit:4.3.2")
implementation("net.kyori:adventure-text-minimessage:4.15.0")

compileOnly("com.google.guava:guava:32.0.0-android")
Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/edenor/minimap/MinimapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class MinimapConfig{
public static JMWorldConfig defaultWorldConfig = new JMWorldConfig();
public static VoxelMapConfig globalVoxelConfig = new VoxelMapConfig();
private static final Map<String, WorldConfig> worlds = new HashMap<>();

private static FileConfiguration config;
public static void initConfig(){
config = MinimapPlugin.getInstance().getConfig();
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/net/edenor/minimap/MinimapEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.edenor.minimap;

import net.edenor.minimap.api.MinimapPlayer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.jetbrains.annotations.NotNull;

public class MinimapEvents implements @NotNull Listener {

private MinimapPlugin plugin;

public MinimapEvents(MinimapPlugin plugin){
this.plugin = 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
}

@EventHandler
public void onWorldChange(PlayerChangedWorldEvent event) {
handlePackets(new MinimapPlayer(event.getPlayer()));
}

public void handlePackets(MinimapPlayer player) {
plugin.xaerosHandler.sendXaerosHandshake(player);
plugin.xaerosHandler.sendXaerosConfig(player);
}
}
50 changes: 50 additions & 0 deletions src/main/java/net/edenor/minimap/MinimapListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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;

import java.util.Arrays;
import java.util.List;

public class MinimapListener implements PluginMessageListener {

private static final List<String> listenChannels = Arrays.asList(
"journeymap:version",
"journeymap:perm_req",
"journeymap:admin_req",
"journeymap:admin_save",
"journeymap:teleport_req",
"journeymap:common",
"worldinfo:world_id",
"xaerominimap:main",
"xaeroworldmap:main"
);

private MinimapPlugin plugin;

public MinimapListener(MinimapPlugin plugin){
this.plugin = plugin;

listenChannels.forEach(this::registerChannel);
}

public void registerChannel(String channel) {
plugin.getServer().getMessenger().registerOutgoingPluginChannel(MinimapPlugin.getInstance(), channel);
plugin.getServer().getMessenger().registerIncomingPluginChannel(MinimapPlugin.getInstance(), channel, this);
}

public void onPluginMessage(String channel, MinimapPlayer 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);
}
}

@Override
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
this.onPluginMessage(channel, new MinimapPlayer(player), message);
}
}
107 changes: 7 additions & 100 deletions src/main/java/net/edenor/minimap/MinimapPlugin.java
Original file line number Diff line number Diff line change
@@ -1,81 +1,33 @@
package net.edenor.minimap;

import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import net.edenor.minimap.api.MinimapPlayer;
import net.edenor.minimap.jm.JMHandler;
import net.edenor.minimap.worldinfo.WorldInfoHandler;
import net.edenor.minimap.xaeros.XaerosHandler;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
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.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MinimapPlugin extends JavaPlugin implements @NotNull Listener, @NotNull PluginMessageListener {
public class MinimapPlugin extends JavaPlugin {
private static MinimapPlugin instance;
private static final List<String> listenChannels = Arrays.asList(
"journeymap:version",
"journeymap:perm_req",
"journeymap:admin_req",
"journeymap:admin_save",
"journeymap:teleport_req",
"journeymap:common",
"worldinfo:world_id",
"xaerominimap:main",
"xaeroworldmap:main"
);

private final JMHandler jmHandler = new JMHandler(this);
private final XaerosHandler xaerosHandler = new XaerosHandler(this);
private final WorldInfoHandler worldInfoHandler = new WorldInfoHandler(this);
public final JMHandler jmHandler = new JMHandler(this);
public final XaerosHandler xaerosHandler = new XaerosHandler();
public final WorldInfoHandler worldInfoHandler = new WorldInfoHandler();

public MinimapPlugin plugin;

public void registerChannel(String channel) {
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, channel);
plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, channel, this);
}

private BukkitAudiences adventure;

public BukkitAudiences adventure() {
if(this.adventure == null) {
throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!");
}
return this.adventure;
}

@Override
public void onEnable() {
// Plugin startup logic
instance = plugin = this;
this.adventure = BukkitAudiences.create(this);
getServer().getPluginManager().registerEvents(this, this);
listenChannels.forEach(this::registerChannel);
getServer().getPluginManager().registerEvents(new MinimapEvents(this), this);
MinimapConfig.initConfig();
new MinimapListener(this);
}

@Override
public void onDisable() {
saveConfig();
// Plugin shutdown logic
System.out.println("Disabled");
if(this.adventure != null) {
this.adventure.close();
this.adventure = null;
}
getLogger().info("Disabled");
}
public static MinimapPlugin getInstance() {
return instance;
Expand All @@ -88,49 +40,4 @@ public void saveConfig() {
throw new RuntimeException(e);
}
}

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

public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
this.onPluginMessage(channel, new MinimapPlayer(player), message);
}

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

@EventHandler
public void onJoin(PlayerJoinEvent event) {
plugin.getServer().getGlobalRegionScheduler().runDelayed(plugin,
v->this.handlePackets(new MinimapPlayer(event.getPlayer())), 20L);

ScheduledTask task = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin,
v->this.handlePackets(new MinimapPlayer(event.getPlayer())), 40L, 100L);
playerTaskMap.put(event.getPlayer().getName(), task);
}

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

@EventHandler
public void onWorldChange(PlayerChangedWorldEvent event) {
handlePackets(new MinimapPlayer(event.getPlayer()));
}

public void handlePackets(MinimapPlayer player) {
//if (!MinimapConfig.globalVoxelConfig.enabled)
//player.nativePlayer.sendPluginMessage(this, "voxel" Component.text("§3 §6 §3 §6 §3 §6 §e §r §3 §6 §3 §6 §3 §6 §d "));
xaerosHandler.sendXaerosHandshake(player);
xaerosHandler.sendXaerosConfig(player);
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/edenor/minimap/api/MinimapPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void sendPluginMessage(byte[] message, String channel) {
}

public void sendMessage(Component message) {
MinimapPlugin.getInstance().adventure().player(nativePlayer).sendMessage(message);
nativePlayer.sendMessage(message);
}

public void teleport(MinimapLocation location) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/edenor/minimap/jm/JMHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void handleAdminReq(MinimapPlayer player, byte[] message, String replyCha
}
}

public void handleAdminSave(MinimapPlayer player, byte[] message, String replyChannel, int replyByte) {
public void handleAdminSave(MinimapPlayer player, byte[] message, String replyChannel) {
if (!player.hasPermission("minimap.jm.admin")) return;

ByteArrayDataInput in = ByteStreams.newDataInput(message);
Expand Down Expand Up @@ -163,14 +163,14 @@ public void onPluginMessage(String channel, MinimapPlayer player, byte[] message
case "version" -> handleVersion(player, message, channel);
case "perm_req" -> handlePerm(player.nativePlayer, message, channel, 0);
case "admin_req" -> handleAdminReq(player, message, channel, 0);
case "admin_save" -> handleAdminSave(player, message, channel, 0);
case "admin_save" -> handleAdminSave(player, message, channel);
case "teleport_req" -> handleTeleport(player, message, channel, 0);
case "common" -> {
ByteArrayDataInput in = ByteStreams.newDataInput(message);
byte type = in.readByte();
switch (type) {
case 0 -> handleAdminReq(player, message, channel, type);
case 1 -> handleAdminSave(player, message, channel, type);
case 1 -> handleAdminSave(player, message, channel);
case 2 -> handlePerm(player.nativePlayer, message, channel, type);
case 4 -> handleTeleport(player, message, channel, type);
case 5 -> handleMPOptions(player, message, channel, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public int getId() {

static {
ServerPropType[] types = values();
int len = types.length;

for (ServerPropType propertyType : types) {
map.put(propertyType.id, propertyType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ public class WorldInfoHandler implements MessageHandler {

public static String WORLDINFO_CHANNEL = "worldinfo:world_id";

public WorldInfoHandler(MinimapPlugin plugin) {
}
public WorldInfoHandler() {}

@Override
public void onPluginMessage(String channel, MinimapPlayer player, byte[] message) {
/*if (!MinimapConfig.globalVoxelConfig.enabled){ //TODO Fix voxel map
player.sendPluginMessage("§3 §6 §3 §6 §3 §6 §d §3 §6 §3 §6 §3 §6 §e".getBytes(), WORLDINFO_CHANNEL);
}*/

UUID worldId = MinimapConfig.getWorldConfig(player.getLocation().getWorld().getName()).worldId;
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeByte(0);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/edenor/minimap/xaeros/XaerosHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class XaerosHandler implements MessageHandler {
public static String XAEROS_MAP_CHANNEL = "xaeroworldmap:main";


public XaerosHandler(MinimapPlugin plugin) {
}
public XaerosHandler() {}

public void sendXaerosHandshake(MinimapPlayer player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
Expand Down

0 comments on commit cbfedaa

Please sign in to comment.