From ce995dd030116e8cc7526ce8b621a33ffef2b764 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 29 May 2020 14:18:54 +0200 Subject: [PATCH] feat: add GameManager --- .vscode/settings.json | 3 + .../main/java/de/cliffbreak/varo/Varo.java | 65 +++++---- .../cliffbreak/varo/commands/InfoCommand.java | 26 ++++ .../de/cliffbreak/varo/enums/GameState.java | 5 + .../cliffbreak/varo/managers/GameManager.java | 112 +++++++++++++++ .../varo/managers/GameTimeManager.java | 132 +++++++++++++++--- .../cliffbreak/varo/managers/NPCManager.java | 84 ++++++++--- .../cliffbreak/varo/managers/TeamManager.java | 24 +++- .../varo/utils/ImprovedOfflinePlayer.java | 87 ++++++++++++ .../cliffbreak/varo/utils/PacketReader.java | 4 +- varo/src/main/resources/plugin.yml | 6 +- 11 files changed, 479 insertions(+), 69 deletions(-) create mode 100644 varo/src/main/java/de/cliffbreak/varo/commands/InfoCommand.java create mode 100644 varo/src/main/java/de/cliffbreak/varo/enums/GameState.java create mode 100644 varo/src/main/java/de/cliffbreak/varo/managers/GameManager.java create mode 100644 varo/src/main/java/de/cliffbreak/varo/utils/ImprovedOfflinePlayer.java diff --git a/.vscode/settings.json b/.vscode/settings.json index e38425f..9d16c36 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,11 +7,14 @@ "Aragur", "DRYOUT", "Elytra", + "Enderman", "Gamemode", "Gamerules", "Gson", + "INGAME", "Minecraft", "Mojang", + "NOTIME", "addspawns", "cliffbreak", "koords", diff --git a/varo/src/main/java/de/cliffbreak/varo/Varo.java b/varo/src/main/java/de/cliffbreak/varo/Varo.java index 61bbea5..0bf2c66 100644 --- a/varo/src/main/java/de/cliffbreak/varo/Varo.java +++ b/varo/src/main/java/de/cliffbreak/varo/Varo.java @@ -10,6 +10,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.java.JavaPlugin; import de.cliffbreak.varo.commands.CoordsCommand; +import de.cliffbreak.varo.commands.InfoCommand; import de.cliffbreak.varo.commands.VaroCommand; import de.cliffbreak.varo.commands.VaroTabCompleter; import de.cliffbreak.varo.database.Database; @@ -26,6 +27,7 @@ import de.cliffbreak.varo.listeners.PlayerJoinQuitListener; import de.cliffbreak.varo.listeners.PlayerLoginListener; import de.cliffbreak.varo.listeners.PlayerResourcePackStatusListener; import de.cliffbreak.varo.managers.BorderManager; +import de.cliffbreak.varo.managers.GameManager; import de.cliffbreak.varo.managers.GameTimeManager; import de.cliffbreak.varo.managers.NPCManager; import de.cliffbreak.varo.managers.TeamManager; @@ -40,18 +42,19 @@ public class Varo extends JavaPlugin { private File configurationFile; private FileConfiguration config; - private NPCManager npcManager; + private Database database; + private TeamManager teamManager; private PlayerCache playerCache; + private NPCManager npcManager; private BanUtils banUtils; private VaroUtils varoUtils; - private TeamManager teamManager; private MessageUtils messageUtils; - private DiscordBot discordBot; - private Database database; + private GameManager gameManager; private GameTimeManager gameTimeManager; private PlayerUtils playerUtils; private BorderManager borderManager; private LangUtils langUtils; + private DiscordBot discordBot; private boolean canCreatePortal = false; @@ -84,6 +87,7 @@ public class Varo extends JavaPlugin { this.banUtils = new BanUtils(this); this.varoUtils = new VaroUtils(this); this.messageUtils = new MessageUtils(this); + this.gameManager = new GameManager(this); this.gameTimeManager = new GameTimeManager(this); this.playerUtils = new PlayerUtils(this); this.borderManager = new BorderManager(this); @@ -104,6 +108,7 @@ public class Varo extends JavaPlugin { this.getCommand("varo").setExecutor(new VaroCommand(this)); this.getCommand("varo").setTabCompleter(new VaroTabCompleter(this)); this.getCommand("coords").setExecutor(new CoordsCommand(this)); + this.getCommand("info").setExecutor(new InfoCommand(this)); this.varoUtils.init(); @@ -136,40 +141,32 @@ public class Varo extends JavaPlugin { } } - public NPCManager getNPCManager() { - return this.npcManager; - } - - public PlayerCache getPlayerCache() { - return this.playerCache; - } - - public BanUtils getBanUtils() { - return this.banUtils; + public Database getDatabase() { + return this.database; } public TeamManager getTeamManager() { return this.teamManager; } + public PlayerCache getPlayerCache() { + return this.playerCache; + } + + public NPCManager getNPCManager() { + return this.npcManager; + } + + public BanUtils getBanUtils() { + return this.banUtils; + } + public MessageUtils getMessageUtils() { return this.messageUtils; } - public boolean canCreatePortal() { - return this.canCreatePortal; - } - - public void setCanCreatePortal(final boolean canCreatePortal) { - this.canCreatePortal = canCreatePortal; - } - - public DiscordBot getDiscordBot() { - return this.discordBot; - } - - public Database getDatabase() { - return this.database; + public GameManager getGameManager() { + return this.gameManager; } public GameTimeManager getGameTimeManager() { @@ -187,4 +184,16 @@ public class Varo extends JavaPlugin { public LangUtils getLangUtils() { return this.langUtils; } + + public DiscordBot getDiscordBot() { + return this.discordBot; + } + + public void setCanCreatePortal(final boolean canCreatePortal) { + this.canCreatePortal = canCreatePortal; + } + + public boolean canCreatePortal() { + return this.canCreatePortal; + } } diff --git a/varo/src/main/java/de/cliffbreak/varo/commands/InfoCommand.java b/varo/src/main/java/de/cliffbreak/varo/commands/InfoCommand.java new file mode 100644 index 0000000..1fdc02b --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/commands/InfoCommand.java @@ -0,0 +1,26 @@ +package de.cliffbreak.varo.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +import de.cliffbreak.varo.Varo; + +public class InfoCommand implements CommandExecutor { + + private final Varo plugin; + + public InfoCommand(final Varo plugin) { + this.plugin = plugin; + } + + public boolean onCommand(final CommandSender sender, final Command command, final String label, + final String[] args) { + sender.sendMessage("\n§6========== §b§lCliffbreak.de VARO §r§6============\n "); + sender.sendMessage("Current GameState: " + this.plugin.getGameManager().getGameState()); + sender.sendMessage("Current Day: " + this.plugin.getGameTimeManager().currentDay()); + sender.sendMessage("Current TimeDelta: " + this.plugin.getGameTimeManager().getPlayTimeDeltaSeconds()); + sender.sendMessage("\n§6=========================================="); + return true; + } +} \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/enums/GameState.java b/varo/src/main/java/de/cliffbreak/varo/enums/GameState.java new file mode 100644 index 0000000..231f7a2 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/enums/GameState.java @@ -0,0 +1,5 @@ +package de.cliffbreak.varo.enums; + +public enum GameState { + LOGIN, PRESTART, PROTECTION, INGAME, NOTIME, +} \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/managers/GameManager.java b/varo/src/main/java/de/cliffbreak/varo/managers/GameManager.java new file mode 100644 index 0000000..4f5eabb --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/managers/GameManager.java @@ -0,0 +1,112 @@ +package de.cliffbreak.varo.managers; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.World.Environment; +import org.bukkit.entity.Creeper; +import org.bukkit.entity.Drowned; +import org.bukkit.entity.Enderman; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.entity.Skeleton; +import org.bukkit.entity.Spider; +import org.bukkit.entity.Zombie; + +import de.cliffbreak.varo.Varo; +import de.cliffbreak.varo.enums.GameState; + +public class GameManager { + + private final Varo plugin; + private GameState gameState; + + public GameManager(final Varo plugin) { + this.plugin = plugin; + this.gameState = GameState.NOTIME; + } + + /** + * @return current GameState + */ + public GameState getGameState() { + return this.gameState; + } + + /** + * Set this to allow all players to login and show them a loading screen + */ + public void setLoginPhase() { + if (this.gameState.equals(GameState.LOGIN)) + return; + this.gameState = GameState.LOGIN; + this.plugin.getLogger().info("[GameManager] Set GameState to " + this.gameState); + } + + /** + * Set to prestart phase (teleport all players to their start locations and show 1 minute countdown) + */ + public void setPreStart() { + if (this.gameState.equals(GameState.PRESTART)) + return; + this.gameState = GameState.PRESTART; + this.plugin.getLogger().info("[GameManager] Set GameState to " + this.gameState); + for (World world : Bukkit.getWorlds()) { + world.setPVP(false); + world.setTime(0); + world.setStorm(false); + + if (world.getEnvironment().equals(Environment.NORMAL)) { + for (Entity entity : world.getEntitiesByClasses(Spider.class, Zombie.class, Skeleton.class, + Creeper.class, Drowned.class, Enderman.class)) { + entity.remove(); + // TODO: test + } + } + } + for (Player player : Bukkit.getOnlinePlayers()) { + player.setHealth(20.0d); + player.setFoodLevel(20); + player.setSaturation(5.0f); + } + // Telport handled from PlayerUtils.java + } + + /** + * Set this on day one to disable PVP + */ + public void setProtectionPhase() { + if (this.gameState.equals(GameState.PROTECTION)) + return; + this.gameState = GameState.PROTECTION; + this.plugin.getLogger().info("[GameManager] Set GameState to " + this.gameState); + for (World world : Bukkit.getWorlds()) { + world.setPVP(false); + } + } + + /** + * Set this to teleport all players to the normal world and start the game as usual (for days > 1) + */ + public void setIngame() { + if (this.gameState.equals(GameState.INGAME)) + return; + this.gameState = GameState.INGAME; + this.plugin.getLogger().info("[GameManager] Set GameState to " + this.gameState); + for (World world : Bukkit.getWorlds()) { + world.setPVP(true); + } + } + + /** + * Set this if the PlayTime is over, this will also kick the Players + */ + public void setGameTimeOver() { + if (this.gameState.equals(GameState.NOTIME)) + return; + this.gameState = GameState.NOTIME; + this.plugin.getLogger().info("[GameManager] Set GameState to " + this.gameState); + for (Player player : Bukkit.getOnlinePlayers()) { + System.out.println(player.getName() + " kick.."); + } + } +} \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/managers/GameTimeManager.java b/varo/src/main/java/de/cliffbreak/varo/managers/GameTimeManager.java index 01bc1be..76c390c 100644 --- a/varo/src/main/java/de/cliffbreak/varo/managers/GameTimeManager.java +++ b/varo/src/main/java/de/cliffbreak/varo/managers/GameTimeManager.java @@ -8,38 +8,53 @@ import java.time.ZoneId; import java.time.temporal.ChronoUnit; import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.World.Environment; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import de.cliffbreak.varo.Varo; -//TODO: Fix times after 24:00 public class GameTimeManager { private final Varo plugin; - private static final int firstPlayTimeMinutes = 50; + // private static final int firstPlayTimeMinutes = 50; private static final int firstPlayTimeLoginMinutes = 10; - private static final LocalTime firstPlayStart = LocalTime.of(19, 10, 00); - private static final int playTimeMinutes = 30; - private static final int playTimeLoginMinutes = 5; + private static final int protectionPeriodMinutes = 2; + private static final int firstPlayTimeMinutes = 2; + // private static final int firstPlayTimeMinutes = 360; + // private static final int protectionPeriodMinutes = 11; + // private static final LocalTime firstPlayStart = LocalTime.of(19, 20, 00); + private static final LocalTime firstPlayStart = LocalTime.now().plusMinutes(1); + private static final int firstRoundPreStartMinutes = 1; + // private static final int playTimeMinutes = 30; + // private static final int playTimeLoginMinutes = 5; + private static final int playTimeMinutes = 1; + private static final int playTimeLoginMinutes = 1; // private static final LocalTime playStart = LocalTime.of(19, 30, 00); // private static final LocalTime playStart = LocalTime.now().plusMinutes(30); - // private static final LocalTime playStart = LocalTime.now().plusMinutes(1); - private static final LocalTime playStart = LocalTime.now(); + private static final LocalTime playStart = LocalTime.now().plusSeconds(10); + // private static final LocalTime playStart = LocalTime.now(); public GameTimeManager(final Varo plugin) { this.plugin = plugin; - startPlayTimeTimer(); - startBorderTimer(); + this.startPlayTimeTimer(); + this.startBorderTimer(); } + /** + * @return current Day the Server is running + */ public int currentDay() { - final Instant instant = Instant.ofEpochMilli(this.plugin.getConfig().getLong("Varo.Start")); - final LocalDateTime start = instant.atZone(ZoneId.systemDefault()).toLocalDateTime(); - final LocalDateTime current = LocalDateTime.now(); - return (int) Duration.between(start, current).toDays() + 1; + return 1; + // TODO: uncomment + // final Instant instant = Instant.ofEpochMilli(this.plugin.getConfig().getLong("Varo.Start")); + // final LocalDateTime start = instant.atZone(ZoneId.systemDefault()).toLocalDateTime(); + // final LocalDateTime current = LocalDateTime.now(); + // return (int) Duration.between(start, current).toDays() + 1; } + // TODO: Rewrite public boolean canLogin() { final LocalTime currentTime = LocalTime.now(); if (this.currentDay() == 1) { @@ -59,6 +74,7 @@ public class GameTimeManager { } } + // TODO: Rewrite public boolean isLoginTimeOver() { final LocalTime currentTime = LocalTime.now(); if (this.currentDay() == 1) { @@ -76,11 +92,27 @@ public class GameTimeManager { } } + // TODO: Rewrite public int getLoginTimeLeft() { final LocalTime currentTime = LocalTime.now(); - return (int) currentTime.until(playStart, ChronoUnit.SECONDS); + if (this.currentDay() != 1) { + return (int) currentTime.until(playStart, ChronoUnit.SECONDS); + } else { + return (int) currentTime.until(firstPlayStart, ChronoUnit.SECONDS); + } } + // TODO: Rewrite + public int getGameTimeLeft() { + final LocalTime currentTime = LocalTime.now(); + if (this.currentDay() != 1) { + return (int) currentTime.until(playStart.plusMinutes(playTimeMinutes), ChronoUnit.MINUTES); + } else { + return (int) currentTime.until(firstPlayStart.plusMinutes(firstPlayTimeMinutes), ChronoUnit.MINUTES); + } + } + + // TODO: Rewrite private boolean isPlayTimeOver() { final LocalTime currentTime = LocalTime.now(); if (this.currentDay() == 1) { @@ -98,15 +130,77 @@ public class GameTimeManager { } } + // TODO: BREAKING handle times after 24:00 + public int getPlayTimeDeltaSeconds() { + final LocalTime currentTime = LocalTime.now(); + final LocalTime start = this.currentDay() == 1 ? firstPlayStart : playStart; + return (int) currentTime.until(start, ChronoUnit.SECONDS); + } + + // TODO: Rewrite + // private void startPlayTimeTimer() { + // new BukkitRunnable() { + // boolean broadcast1 = false; + // boolean broadcast2 = false; + + // @Override + // public void run() { + // if (currentDay() == 1) { + // for (final Player p : Bukkit.getOnlinePlayers()) { + // if (getGameTimeLeft() < 50 && getGameTimeLeft() > 45) { + // if (!p.getLocation().getWorld().getEnvironment().equals(Environment.THE_END)) { + // p.setGameMode(GameMode.ADVENTURE); + // } + // } + // if (getGameTimeLeft() == 45) { + // if (!p.getLocation().getWorld().getEnvironment().equals(Environment.THE_END)) { + // p.setGameMode(GameMode.SURVIVAL); + // } + // if (!broadcast1) { + // Bukkit.broadcastMessage("§aEs geht Los!"); + // Bukkit.broadcastMessage("Genießt die 10 minütige Schutzzeit."); + // } + // broadcast1 = true; + // } + // if (getGameTimeLeft() == 35) { + // if (!broadcast2) { + // Bukkit.broadcastMessage("Die Schutzzeit ist nur vorbei."); + // } + // broadcast2 = true; + // } + // } + // } + // if (isPlayTimeOver()) { + // for (final Player p : Bukkit.getOnlinePlayers()) { + // p.kickPlayer(plugin.getLangUtils().get("Service.KickPlayTimeOver")); + // } + // this.cancel(); + // } + // } + // }.runTaskTimer(this.plugin, 20l, 20l); + // } private void startPlayTimeTimer() { new BukkitRunnable() { @Override public void run() { - if (isPlayTimeOver()) { - for (final Player p : Bukkit.getOnlinePlayers()) { - p.kickPlayer(plugin.getLangUtils().get("Service.KickPlayTimeOver")); - } - this.cancel(); + int timeDelta = getPlayTimeDeltaSeconds(); + int loginTimeSeconds = currentDay() == 1 ? firstPlayTimeLoginMinutes * 60 : playTimeLoginMinutes * 60; + int playTimeSeconds = currentDay() == 1 ? firstPlayTimeMinutes * 60 : playTimeMinutes * 60; + // System.out.println(timeDelta); + if (timeDelta <= loginTimeSeconds && timeDelta > 0) { + plugin.getGameManager().setLoginPhase(); + } else if (currentDay() == 1 && (timeDelta <= 0 && timeDelta > firstRoundPreStartMinutes * -60)) { + plugin.getGameManager().setPreStart(); + } else if (currentDay() == 1 && (timeDelta <= firstRoundPreStartMinutes * -60 + && timeDelta > protectionPeriodMinutes * -60)) { + plugin.getGameManager().setProtectionPhase(); + } else if (currentDay() > 1 && (timeDelta <= 0 && timeDelta > playTimeSeconds * -1)) { + plugin.getGameManager().setIngame(); + } else if (currentDay() == 1 + && (timeDelta <= protectionPeriodMinutes * -60 && timeDelta > playTimeSeconds * -1)) { + plugin.getGameManager().setIngame(); + } else if (timeDelta <= playTimeSeconds * -1) { + plugin.getGameManager().setGameTimeOver(); } } }.runTaskTimer(this.plugin, 20l, 20l); 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 34833a6..dc16d60 100644 --- a/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java +++ b/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java @@ -21,12 +21,14 @@ import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scoreboard.Team; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; import org.json.simple.parser.ParseException; import de.cliffbreak.varo.Varo; +import de.cliffbreak.varo.utils.ImprovedOfflinePlayer; import de.cliffbreak.varo.utils.WebUtils; import net.minecraft.server.v1_15_R1.DataWatcherObject; import net.minecraft.server.v1_15_R1.DataWatcherRegistry; @@ -54,6 +56,7 @@ public class NPCManager { public NPCManager(final Varo plugin) { this.plugin = plugin; + addAllClones(); } public void createClone(final Player player) { @@ -100,6 +103,61 @@ public class NPCManager { } } + public void createOfflineClone(final UUID uuid, final String name) { + final String compressedUuid = uuid.toString().replace("-", ""); + final Location location = this.plugin.getPlayerCache().getPlayerLocationCache(compressedUuid); + final MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer(); + final WorldServer nmsWorld = ((CraftWorld) location.getWorld()).getHandle(); + final GameProfile gameProfile = new GameProfile(uuid, name); + + ImprovedOfflinePlayer iop = new ImprovedOfflinePlayer(uuid.toString()); + iop.getInventoryItems(); + + // final Inventory inventory = this.plugin.getPlayerCache().getInventory(compressedUuid); + // for (ItemStack item : inventory.getContents()) { + // System.out.println(item); + // } + + // inventories.put(uuid, inventory); + try { + final String result = WebUtils.GET( + "https://sessionserver.mojang.com/session/minecraft/profile/" + compressedUuid + "?unsigned=false"); + final JSONObject resultObject = (JSONObject) JSONValue.parseWithException(result); + final JSONObject properties = (JSONObject) ((JSONArray) resultObject.get("properties")).get(0); + + gameProfile.getProperties().put("textures", new Property("textures", properties.get("value").toString(), + properties.get("signature").toString())); + } catch (IOException | ParseException e) { + this.plugin.getLogger().info(e.getMessage()); + } + + final PlayerInteractManager playerInteractManager = new PlayerInteractManager(nmsWorld); + final EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, gameProfile, playerInteractManager); + + npc.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); + // npc.setHealth(this.plugin.getPlayerCache().getHealth(compressedUuid)); + // npcs.add(npc); + + // for (final Player connectionPlayer : Bukkit.getOnlinePlayers()) { + // final PlayerConnection connection = ((CraftPlayer) connectionPlayer).getHandle().playerConnection; + // new BukkitRunnable() { + // @Override + // public void run() { + // connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, npc)); + // } + // }.runTaskLater(this.plugin, 20); + // connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc)); + // connection.sendPacket( + // new PacketPlayOutEntityHeadRotation(npc, (byte) ((location.getYaw() * 256.0F) / 360.0F))); + // npc.getDataWatcher().set(new DataWatcherObject<>(16, DataWatcherRegistry.a), + // (byte) this.plugin.getPlayerCache().getSkinParts(compressedUuid)); + // npc.getDataWatcher().set(new DataWatcherObject<>(17, DataWatcherRegistry.a), + // this.plugin.getPlayerCache().getMainHandAsByte(compressedUuid)); + // connection.sendPacket(new PacketPlayOutEntityMetadata(npc.getId(), npc.getDataWatcher(), true)); + // // sendEquipmentPackets(connection, npc.getId(), inventory); + // } + } + private void sendEquipmentPackets(final PlayerConnection connection, final int id, final PlayerInventory inventory) { connection.sendPacket(new PacketPlayOutEntityEquipment(id, EnumItemSlot.MAINHAND, @@ -139,7 +197,7 @@ public class NPCManager { return false; } - public void addDamage(Player damager, final int id, final double damage, final boolean isCritical) { + public void addDamage(final Player damager, final int id, final double damage, final boolean isCritical) { for (final EntityPlayer npc : this.npcs) { if (npc.getId() == id) { npc.setHealth((float) (npc.getHealth() - damage)); @@ -203,22 +261,12 @@ public class NPCManager { } } - // FIXME: Only for debug reasons -- Remove - public void removeClone(final Player player) { - for (int i = 0; i < this.npcs.size(); i++) { - final EntityPlayer npc = this.npcs.get(i); - if (npc.getUniqueID().equals(player.getUniqueId())) { - final WorldServer nmsWorld = ((CraftWorld) player.getWorld()).getHandle(); - nmsWorld.removeEntity(npc); - - for (final Player connectionPlayer : Bukkit.getOnlinePlayers()) { - final PlayerConnection connection = ((CraftPlayer) connectionPlayer).getHandle().playerConnection; - connection.sendPacket(new PacketPlayOutEntityDestroy(npc.getId())); - } - this.npcs.remove(npc); - if (this.isDying.contains(npc.getId())) { - this.isDying.remove(npc.getId()); - } + private void addAllClones() { + final ArrayList teams = this.plugin.getTeamManager().getTeams(); + for (final Team team : teams) { + for (String player : team.getEntries()) { + UUID uuid = this.plugin.getTeamManager().getUuidFromPlayerName(player); + this.createOfflineClone(uuid, player); } } } @@ -232,7 +280,7 @@ public class NPCManager { for (final 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)); + // connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.REMOVE_PLAYER, npc)); } new BukkitRunnable() { diff --git a/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java b/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java index 7206cb9..52e40db 100644 --- a/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java +++ b/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java @@ -4,6 +4,9 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.UUID; + +import com.mojang.util.UUIDTypeAdapter; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -96,10 +99,29 @@ public class TeamManager { } } - public JSONArray getTeams() { + public JSONArray getJSONTeams() { return this.config.getArray("teams"); } + public ArrayList getTeams() { + return this.teams; + } + + public UUID getUuidFromPlayerName(final String playerName) { + final JSONArray jsonTeams = config.getArray("teams"); + for (final Object jsonRawTeam : jsonTeams) { + final JSONObject jsonTeam = (JSONObject) jsonRawTeam; + for (final Object rawPlayer : ((JSONArray) jsonTeam.get("players"))) { + final JSONObject jsonPlayer = (JSONObject) rawPlayer; + if (jsonPlayer.get("name").equals(playerName)) { + System.out.println(jsonPlayer.get("uuid")); + return UUIDTypeAdapter.fromString((String) jsonPlayer.get("uuid")); + } + } + } + return null; + } + public Team getTeamByPlayer(final String player) { for (final Team team : this.teams) { if (team.hasEntry(player)) { diff --git a/varo/src/main/java/de/cliffbreak/varo/utils/ImprovedOfflinePlayer.java b/varo/src/main/java/de/cliffbreak/varo/utils/ImprovedOfflinePlayer.java new file mode 100644 index 0000000..7731b18 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/utils/ImprovedOfflinePlayer.java @@ -0,0 +1,87 @@ +package de.cliffbreak.varo.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; + +import net.minecraft.server.v1_15_R1.NBTCompressedStreamTools; +import net.minecraft.server.v1_15_R1.NBTTagCompound; +import net.minecraft.server.v1_15_R1.NBTTagList; + +public class ImprovedOfflinePlayer { + private String player; + private File file; + private NBTTagCompound compound; + private boolean exists = true; + private String pluginName; + + public ImprovedOfflinePlayer(String name) { + this.player = name; + loadOfflinePlayer(); + } + + private void loadOfflinePlayer() { + try { + for (World w : Bukkit.getWorlds()) { + File check = new File(w.getWorldFolder(), "playerdata" + File.separator + this.player + ".dat"); + if (check.exists()) { + this.exists = true; + this.file = check; + this.compound = NBTCompressedStreamTools.a(new FileInputStream(this.file)); + this.pluginName = ImprovedOfflinePlayer.class.getProtectionDomain().getCodeSource().toString(); + this.pluginName = this.pluginName.substring(this.pluginName.lastIndexOf("/") + 1, + this.pluginName.lastIndexOf(".")); + return; + } + this.exists = false; + } + } catch (Exception e) { + this.exists = false; + } + } + + public ItemStack[] getInventoryItems() { + ItemStack[] items = new ItemStack[36]; + if (!this.exists) { + return items; + } + NBTTagList list = this.compound.getList("Inventory", 10); + System.out.println(list); + for (int i = 0; i < list.size(); i++) { + NBTTagCompound item = (NBTTagCompound) list.get(i); + byte slot = item.getByte("Slot"); + String id = item.getString("id"); + byte count = item.getByte("Count"); + short damage = item.getShort("Damage"); + System.out.println(id); + System.out.println(slot); + + if (slot < 100 && slot >= 0) { + items[slot] = new ItemStack(Material.matchMaterial(id), count); + items[slot].setDurability(damage); + if (item.hasKey("tag")) { + Map enchantments = new HashMap(); + NBTTagCompound tCompound = item.getCompound("tag"); + if (tCompound.hasKey("ench")) { + NBTTagList enchTL = tCompound.getList("ench", 10); + for (int f = 0; f < enchTL.size(); f++) { + NBTTagCompound enchTC = (NBTTagCompound) tCompound.getList("ench", 10).get(f); + String idTC = enchTC.getString("id"); + short lvlTC = enchTC.getShort("lvl"); + enchantments.put(Enchantment.getByName(idTC), (int) lvlTC); + } + } + items[slot].addUnsafeEnchantments(enchantments); + } + } + } + return items; + } +} \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/utils/PacketReader.java b/varo/src/main/java/de/cliffbreak/varo/utils/PacketReader.java index 910a91e..f8d5bdd 100644 --- a/varo/src/main/java/de/cliffbreak/varo/utils/PacketReader.java +++ b/varo/src/main/java/de/cliffbreak/varo/utils/PacketReader.java @@ -13,8 +13,8 @@ import org.bukkit.entity.Player; import org.bukkit.potion.PotionEffectType; import de.cliffbreak.varo.Varo; +import de.cliffbreak.varo.enums.PlayerInteractType; import de.cliffbreak.varo.events.PlayerInteractNPCEvent; -import de.cliffbreak.varo.events.PlayerInteractNPCEvent.Type; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; @@ -64,7 +64,7 @@ public class PacketReader { } try { - final Type type = Type.valueOf(getValue(packet, "action").toString()); + final PlayerInteractType type = PlayerInteractType.valueOf(getValue(packet, "action").toString()); final PlayerInteractNPCEvent event = new PlayerInteractNPCEvent(type, player, id, calculateDamage(this.player), isCriticalHit(this.player)); Bukkit.getServer().getPluginManager().callEvent(event); diff --git a/varo/src/main/resources/plugin.yml b/varo/src/main/resources/plugin.yml index b1aacae..552ecdd 100644 --- a/varo/src/main/resources/plugin.yml +++ b/varo/src/main/resources/plugin.yml @@ -10,4 +10,8 @@ commands: coords: description: Show the Coords. usage: / - aliases: [c, koords, k] \ No newline at end of file + aliases: [c, koords, k] + info: + description: Show information from the VaroPlugin + usage: / + aliases: [i] \ No newline at end of file