From d2b4772242ee2b831ff5b3f4ccf5db63e1b3b6ac Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 6 May 2020 17:48:04 +0200 Subject: [PATCH] feat: Drop Items of FakePlayer and Ban real Player on Death --- .../main/java/de/cliffbreak/varo/Varo.java | 3 + .../cliffbreak/varo/commands/VaroCommand.java | 10 +-- .../varo/listeners/PlayerDeathListener.java | 6 +- .../listeners/PlayerJoinQuitListener.java | 1 + .../listeners/PlayerPreLoginListener.java | 7 +- .../cliffbreak/varo/managers/NPCManager.java | 73 +++++++++++++++++++ .../de/cliffbreak/varo/uitls/BanUtils.java | 42 +++++++++++ 7 files changed, 124 insertions(+), 18 deletions(-) create mode 100644 varo/src/main/java/de/cliffbreak/varo/uitls/BanUtils.java diff --git a/varo/src/main/java/de/cliffbreak/varo/Varo.java b/varo/src/main/java/de/cliffbreak/varo/Varo.java index f413b6f..b3ad3f1 100644 --- a/varo/src/main/java/de/cliffbreak/varo/Varo.java +++ b/varo/src/main/java/de/cliffbreak/varo/Varo.java @@ -24,6 +24,7 @@ import de.cliffbreak.varo.listeners.PlayerDeathListener; import de.cliffbreak.varo.listeners.PlayerJoinQuitListener; import de.cliffbreak.varo.listeners.PlayerPreLoginListener; import de.cliffbreak.varo.managers.NPCManager; +import de.cliffbreak.varo.uitls.BanUtils; public class Varo extends JavaPlugin { @@ -31,6 +32,7 @@ public class Varo extends JavaPlugin { public FileConfiguration config = YamlConfiguration.loadConfiguration(this.configurationFile); public NPCManager npcManager; public PlayerCache playerCache; + public BanUtils banUtils; @Override public void onEnable() { @@ -39,6 +41,7 @@ public class Varo extends JavaPlugin { this.npcManager = new NPCManager(this); this.playerCache = new PlayerCache(this); + this.banUtils = new BanUtils(this); // this.config.addDefault("Varo.Start", "TODO: StartDate"); this.config.addDefault("Varo.Debug", false); diff --git a/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java b/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java index 5ea01ed..bcd958e 100644 --- a/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java +++ b/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java @@ -1,7 +1,6 @@ package de.cliffbreak.varo.commands; import java.io.IOException; -import java.util.ArrayList; import com.destroystokyo.paper.Title; @@ -36,8 +35,7 @@ public class VaroCommand implements CommandExecutor { return true; } if (args.length == 2 && args[0].equals("unban")) { - @SuppressWarnings("unchecked") - final ArrayList bans = (ArrayList) this.plugin.config.get("Varo.Bans"); + try { String result = WebUtils.GET("https://api.mojang.com/users/profiles/minecraft/" + args[1]); if (result.isEmpty()) { @@ -45,13 +43,11 @@ public class VaroCommand implements CommandExecutor { return true; } JSONObject uuidObject = (JSONObject) JSONValue.parseWithException(result); - if (!bans.contains(uuidObject.get("id").toString())) { + if (!plugin.banUtils.isBanned(uuidObject.get("id").toString())) { sender.sendMessage("§c§lFehler: §r§cDer Spieler ist nicht gebannt."); return true; } - bans.remove(uuidObject.get("id").toString()); - this.plugin.config.set("Varo.Bans", bans); - this.plugin.saveConfiguration(); + plugin.banUtils.removeBan(uuidObject.get("id").toString()); sender.sendMessage("§a§lErfolg: §r§aDer Spieler ist nun nicht mehr gebannt."); } catch (IOException | ParseException e) { sender.sendMessage( diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java index 93f04a1..1d5362d 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java @@ -29,11 +29,7 @@ public class PlayerDeathListener implements Listener { e.getEntity().setHealth(20d); // Reset health to prevent death dialog after rejoin (if unbanned) e.getEntity().setGameMode(GameMode.SPECTATOR); - @SuppressWarnings("unchecked") - final ArrayList bans = (ArrayList) this.plugin.config.get("Varo.Bans"); - bans.add(e.getEntity().getUniqueId().toString().replace("-", "")); - this.plugin.config.set("Varo.Bans", bans); - this.plugin.saveConfiguration(); + this.plugin.banUtils.addBann(e.getEntity().getUniqueId()); } } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java index 05a8b58..6bee126 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java @@ -52,6 +52,7 @@ public class PlayerJoinQuitListener implements Listener { final Stack changes = new Stack(); + changes.push(" §7• §r§lADD: §rFakePlayer now drops his items and gets real player banned on death"); changes.push(" §7• §r§lADD: §rFakePlayer is no attackable (can also be killed)"); changes.push(" §7• §r§lADD: §rAdd a FakePlayer if Player is logged out"); changes.push(" §7• §r§lADD: §rAdd /varo test && /varo testremove"); diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java index e414577..c09d991 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java @@ -1,7 +1,5 @@ package de.cliffbreak.varo.listeners; -import java.util.ArrayList; - import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerPreLoginEvent; @@ -19,10 +17,7 @@ public class PlayerPreLoginListener implements Listener { @EventHandler public void onPlayerPreLogin(final AsyncPlayerPreLoginEvent e) { - @SuppressWarnings("unchecked") - final ArrayList bans = (ArrayList) this.plugin.config.get("Varo.Bans"); - if (bans.contains(e.getPlayerProfile().getId().toString().replace("-", ""))) { + if (plugin.banUtils.isBanned(e.getPlayerProfile().getId())) e.disallow(Result.KICK_BANNED, "§4Du bist gestorben.\n \n§cDamit bist du aus §eVaro§c ausgeschieden."); - } } } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java b/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java index 05736f3..ded1207 100644 --- a/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java +++ b/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java @@ -2,6 +2,8 @@ package de.cliffbreak.varo.managers; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.UUID; import com.mojang.authlib.GameProfile; import com.mojang.authlib.properties.Property; @@ -14,7 +16,10 @@ import org.bukkit.World; import org.bukkit.craftbukkit.v1_15_R1.CraftServer; import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; @@ -28,8 +33,10 @@ import de.cliffbreak.varo.uitls.WebUtils; import net.minecraft.server.v1_15_R1.DataWatcherObject; import net.minecraft.server.v1_15_R1.DataWatcherRegistry; import net.minecraft.server.v1_15_R1.EntityPlayer; +import net.minecraft.server.v1_15_R1.EnumItemSlot; import net.minecraft.server.v1_15_R1.MinecraftServer; import net.minecraft.server.v1_15_R1.PacketPlayOutEntityDestroy; +import net.minecraft.server.v1_15_R1.PacketPlayOutEntityEquipment; import net.minecraft.server.v1_15_R1.PacketPlayOutEntityHeadRotation; import net.minecraft.server.v1_15_R1.PacketPlayOutEntityMetadata; import net.minecraft.server.v1_15_R1.PacketPlayOutEntityStatus; @@ -44,6 +51,7 @@ public class NPCManager { private final Varo plugin; private final ArrayList npcs = new ArrayList(); + private final HashMap inventories = new HashMap(); private Scoreboard scoreboard; private Team afkTeam; @@ -65,6 +73,7 @@ public class NPCManager { final MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer(); final WorldServer nmsWorld = ((CraftWorld) player.getWorld()).getHandle(); final GameProfile gameProfile = new GameProfile(player.getUniqueId(), player.getName()); + inventories.put(player.getUniqueId(), player.getInventory()); try { final String result = WebUtils.GET("https://sessionserver.mojang.com/session/minecraft/profile/" + player.getUniqueId().toString().replace("-", "") + "?unsigned=false"); @@ -105,9 +114,25 @@ public class NPCManager { npc.getDataWatcher().set(new DataWatcherObject<>(17, DataWatcherRegistry.a), plugin.playerCache.getMainHandAsByte(player.getUniqueId().toString().replace("-", ""))); connection.sendPacket(new PacketPlayOutEntityMetadata(npc.getId(), npc.getDataWatcher(), true)); + sendEquipmentPackets(connection, npc.getId(), player.getInventory()); } } + private void sendEquipmentPackets(PlayerConnection connection, int id, PlayerInventory inventory) { + connection.sendPacket(new PacketPlayOutEntityEquipment(id, EnumItemSlot.MAINHAND, + CraftItemStack.asNMSCopy(inventory.getItemInMainHand()))); + connection.sendPacket(new PacketPlayOutEntityEquipment(id, EnumItemSlot.OFFHAND, + CraftItemStack.asNMSCopy(inventory.getItemInOffHand()))); + connection.sendPacket(new PacketPlayOutEntityEquipment(id, EnumItemSlot.FEET, + CraftItemStack.asNMSCopy(inventory.getBoots()))); + connection.sendPacket(new PacketPlayOutEntityEquipment(id, EnumItemSlot.LEGS, + CraftItemStack.asNMSCopy(inventory.getLeggings()))); + connection.sendPacket(new PacketPlayOutEntityEquipment(id, EnumItemSlot.CHEST, + CraftItemStack.asNMSCopy(inventory.getChestplate()))); + connection.sendPacket(new PacketPlayOutEntityEquipment(id, EnumItemSlot.HEAD, + CraftItemStack.asNMSCopy(inventory.getHelmet()))); + } + public void syncClones(Player player) { for (EntityPlayer npc : this.npcs) { final PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection; @@ -119,6 +144,7 @@ public class NPCManager { npc.getDataWatcher().set(new DataWatcherObject<>(17, DataWatcherRegistry.a), plugin.playerCache.getMainHandAsByte(player.getUniqueId().toString().replace("-", ""))); connection.sendPacket(new PacketPlayOutEntityMetadata(npc.getId(), npc.getDataWatcher(), true)); + sendEquipmentPackets(connection, npc.getId(), this.inventories.get(npc.getUniqueID())); } } @@ -148,6 +174,7 @@ public class NPCManager { if (isCritical) world.playSound(loc, Sound.ENTITY_PLAYER_ATTACK_CRIT, 1.0F, 1.0F); } else { + // Send Death Animation Packet for (Player connectionPlayer : Bukkit.getOnlinePlayers()) { final PlayerConnection connection = ((CraftPlayer) connectionPlayer) .getHandle().playerConnection; @@ -156,12 +183,34 @@ public class NPCManager { if (isCritical) world.playSound(loc, Sound.ENTITY_PLAYER_ATTACK_CRIT, 1.0F, 1.0F); world.playSound(loc, Sound.ENTITY_PLAYER_DEATH, 1.0F, 1.0F); + + new BukkitRunnable() { + @Override + public void run() { + for (ItemStack item : inventories.get(npc.getUniqueID())) { + if (item != null) + world.dropItem(loc, item); + } + + } + }.runTask(plugin); // Run Task in sync to be thread safe + + this.plugin.banUtils.addBann(npc.getUniqueID()); + Bukkit.broadcastMessage("TODO: Player(" + "??" + ") killed AFK Player (" + npc.getName() + ")"); + + new BukkitRunnable() { + @Override + public void run() { + removeClone(npc.getUniqueID()); + } + }.runTaskLater(plugin, 20); } } } } + // FIXME: Only for debug reasons -- Remove public void removeClone(Player player) { this.afkTeam.removeEntry(player.getName()); for (int i = 0; i < this.npcs.size(); i++) { @@ -178,4 +227,28 @@ public class NPCManager { } } } + + public void removeClone(UUID uuid) { + for (int i = 0; i < this.npcs.size(); i++) { + EntityPlayer npc = this.npcs.get(i); + if (npc.getUniqueID().equals(uuid)) { + final WorldServer nmsWorld = npc.getWorldServer(); + + for (Player connectionPlayer : Bukkit.getOnlinePlayers()) { + final PlayerConnection connection = ((CraftPlayer) connectionPlayer).getHandle().playerConnection; + connection.sendPacket(new PacketPlayOutEntityDestroy(npc.getId())); + connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.REMOVE_PLAYER, npc)); + } + + new BukkitRunnable() { + @Override + public void run() { + nmsWorld.removeEntity(npc); + afkTeam.removeEntry(npc.getName()); + npcs.remove(npc); + } + }.runTask(this.plugin); + } + } + } } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/uitls/BanUtils.java b/varo/src/main/java/de/cliffbreak/varo/uitls/BanUtils.java new file mode 100644 index 0000000..d193072 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/uitls/BanUtils.java @@ -0,0 +1,42 @@ +package de.cliffbreak.varo.uitls; + +import java.util.ArrayList; +import java.util.UUID; + +import de.cliffbreak.varo.Varo; + +public class BanUtils { + + private final Varo plugin; + + public BanUtils(Varo plugin) { + this.plugin = plugin; + } + + public boolean isBanned(UUID uuid) { + return isBanned(uuid.toString().replace("-", "")); + } + + public boolean isBanned(String compressedUuid) { + @SuppressWarnings("unchecked") + final ArrayList bans = (ArrayList) this.plugin.config.get("Varo.Bans"); + return bans.contains(compressedUuid); + } + + public void addBann(UUID uuid) { + @SuppressWarnings("unchecked") + final ArrayList bans = (ArrayList) this.plugin.config.get("Varo.Bans"); + bans.add(uuid.toString().replace("-", "")); + this.plugin.config.set("Varo.Bans", bans); + this.plugin.saveConfiguration(); + } + + public void removeBan(String compressedUuid) { + @SuppressWarnings("unchecked") + final ArrayList bans = (ArrayList) this.plugin.config.get("Varo.Bans"); + bans.remove(compressedUuid); + this.plugin.config.set("Varo.Bans", bans); + this.plugin.saveConfiguration(); + } + +} \ No newline at end of file