diff --git a/pom.xml b/pom.xml index 9bfa516..79772fd 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.unldenis.corpse Corpse - 1.0.8-SNAPSHOT + 1.0.9-SNAPSHOT 8 diff --git a/src/main/java/com/github/unldenis/corpse/CorpseP.java b/src/main/java/com/github/unldenis/corpse/CorpseP.java index 29e6035..190a764 100644 --- a/src/main/java/com/github/unldenis/corpse/CorpseP.java +++ b/src/main/java/com/github/unldenis/corpse/CorpseP.java @@ -30,44 +30,45 @@ public class CorpseP extends JavaPlugin { - private static CorpseP instance; + private static CorpseP instance; - private DataManager configYml; - private CorpsePool pool; - @Override - public void onEnable() { - CorpseP.instance = this; + private DataManager configYml; + private CorpsePool pool; - //register commands - this.getCommand("spawncorpse").setExecutor(new SpawnCorpseCommand()); - this.getCommand("removecorpse").setExecutor(new RemoveCorpseCommand()); + @NotNull + public static CorpseP getInstance() { + return instance; + } - //load config - configYml = new DataManager(this, "config.yml"); + @Override + public void onEnable() { + CorpseP.instance = this; - //load instance - pool = CorpsePool.getInstance(); - } + //register commands + this.getCommand("spawncorpse").setExecutor(new SpawnCorpseCommand()); + this.getCommand("removecorpse").setExecutor(new RemoveCorpseCommand()); - @Override - public void onDisable() { - BukkitTask task = pool.getTickTask(); - if(task != null) { - task.cancel(); - } - for(Corpse c: pool.getCorpses()) { - c.getSeeingPlayers() - .forEach(c::hide); - } - } + //load config + configYml = new DataManager(this, "config.yml"); - @NotNull - public static CorpseP getInstance() { - return instance; - } + //load instance + pool = CorpsePool.getInstance(); + } - @NotNull - public FileConfiguration getConfigYml() { - return configYml.getConfig(); + @Override + public void onDisable() { + BukkitTask task = pool.getTickTask(); + if (task != null) { + task.cancel(); } + for (Corpse c : pool.getCorpses()) { + c.getSeeingPlayers() + .forEach(c::hide); + } + } + + @NotNull + public FileConfiguration getConfigYml() { + return configYml.getConfig(); + } } diff --git a/src/main/java/com/github/unldenis/corpse/api/CorpseAPI.java b/src/main/java/com/github/unldenis/corpse/api/CorpseAPI.java index c28ea3d..0f89e8c 100644 --- a/src/main/java/com/github/unldenis/corpse/api/CorpseAPI.java +++ b/src/main/java/com/github/unldenis/corpse/api/CorpseAPI.java @@ -29,114 +29,126 @@ public class CorpseAPI { - private static CorpseAPI instance; + private static CorpseAPI instance; - public CorpseAPI() { - throw new IllegalArgumentException(); - } + public CorpseAPI() { + throw new IllegalArgumentException(); + } - private CorpseAPI(Object dummy) { } + private CorpseAPI(Object dummy) { + } - /** - * Method that creates a corpse in the player's position and with its skin and inventory - * @param player The player to copy - * @return a new Corpse object - */ - public Corpse spawnCorpse(@NotNull Player player) { - Validate.notNull(player, "Player cannot be null"); - return new Corpse(player); + /** + * Class method that allows you to use the API. + * + * @return an instance of this class + */ + @NotNull + public static synchronized CorpseAPI getInstance() { + if (instance == null) { + instance = new CorpseAPI(null); } + return instance; + } - /** - * Method that creates a corpse in the given place and with the skin, name and inventory of the player - * @param player The player to copy - * @param location The location where to spawn the corpse - * @return a new Corpse object - */ - public Corpse spawnCorpse(@NotNull Player player, @NotNull Location location) { - Validate.notNull(player, "Player cannot be null"); - Validate.notNull(location, "Spawn location cannot be null"); - return new Corpse(location, WrappedGameProfile.fromPlayer(player), null, player.getName()); - } + /** + * Method that creates a corpse in the player's position and with its skin and inventory + * + * @param player The player to copy + * @return a new Corpse object + */ + public Corpse spawnCorpse(@NotNull Player player) { + Validate.notNull(player, "Player cannot be null"); + return new Corpse(player); + } - /** - * Method that creates a corpse in the given place and with the skin and name of the offlinePlayer - * @param offlinePlayer The offlinePlayer to copy - * @param location The location where to spawn the corpse - * @return a new Corpse object - */ - public Corpse spawnCorpse(@NotNull OfflinePlayer offlinePlayer, @NotNull Location location) { - Validate.notNull(offlinePlayer, "OfflinePlayer cannot be null"); - Validate.notNull(location, "Spawn location cannot be null"); - return new Corpse(location, offlinePlayer, null); - } + /** + * Method that creates a corpse in the given place and with the skin, name and inventory of the + * player + * + * @param player The player to copy + * @param location The location where to spawn the corpse + * @return a new Corpse object + */ + public Corpse spawnCorpse(@NotNull Player player, @NotNull Location location) { + Validate.notNull(player, "Player cannot be null"); + Validate.notNull(location, "Spawn location cannot be null"); + return new Corpse(location, WrappedGameProfile.fromPlayer(player), null, player.getName()); + } - /** - * Method that creates a corpse in the given place and with the skin and name of the player - * with a custom inventory. - * @param player The player to copy - * @param location The location where to spawn the corpse - * @param helmet The helmet to put on the corpse - * @param chestPlate The chestPlate to put on the corpse - * @param leggings The leggings to put on the corpse - * @param boots The boots to put on the corpse - * @return a new Corpse object - */ - public Corpse spawnCorpse( - @NotNull Player player, - @NotNull Location location, - @Nullable ItemStack helmet, - @Nullable ItemStack chestPlate, - @Nullable ItemStack leggings, - @Nullable ItemStack boots - ) { - Validate.notNull(player, "Player cannot be null"); - Validate.notNull(location, "Spawn location cannot be null"); - return new Corpse(location, WrappedGameProfile.fromPlayer(player), new ItemStack[]{boots, leggings, chestPlate, helmet}, player.getName()); - } + /** + * Method that creates a corpse in the given place and with the skin and name of the + * offlinePlayer + * + * @param offlinePlayer The offlinePlayer to copy + * @param location The location where to spawn the corpse + * @return a new Corpse object + */ + public Corpse spawnCorpse(@NotNull OfflinePlayer offlinePlayer, @NotNull Location location) { + Validate.notNull(offlinePlayer, "OfflinePlayer cannot be null"); + Validate.notNull(location, "Spawn location cannot be null"); + return new Corpse(location, offlinePlayer, null); + } - /** - * Method that creates a corpse in the given place and with the skin and name of the offlinePlayer - * with a custom inventory. - * @param offlinePlayer The offlinePlayer to copy - * @param location The location where to spawn the corpse - * @param helmet The helmet to put on the corpse - * @param chestPlate The chestPlate to put on the corpse - * @param leggings The leggings to put on the corpse - * @param boots The boots to put on the corpse - * @return a new Corpse object - */ - public Corpse spawnCorpse( - @NotNull OfflinePlayer offlinePlayer, - @NotNull Location location, - @Nullable ItemStack helmet, - @Nullable ItemStack chestPlate, - @Nullable ItemStack leggings, - @Nullable ItemStack boots - ) { - Validate.notNull(offlinePlayer, "OfflinePlayer cannot be null"); - Validate.notNull(location, "Spawn location cannot be null"); - return new Corpse(location, offlinePlayer, new ItemStack[]{boots, leggings, chestPlate, helmet}); - } + /** + * Method that creates a corpse in the given place and with the skin and name of the player with a + * custom inventory. + * + * @param player The player to copy + * @param location The location where to spawn the corpse + * @param helmet The helmet to put on the corpse + * @param chestPlate The chestPlate to put on the corpse + * @param leggings The leggings to put on the corpse + * @param boots The boots to put on the corpse + * @return a new Corpse object + */ + public Corpse spawnCorpse( + @NotNull Player player, + @NotNull Location location, + @Nullable ItemStack helmet, + @Nullable ItemStack chestPlate, + @Nullable ItemStack leggings, + @Nullable ItemStack boots + ) { + Validate.notNull(player, "Player cannot be null"); + Validate.notNull(location, "Spawn location cannot be null"); + return new Corpse(location, WrappedGameProfile.fromPlayer(player), + new ItemStack[]{boots, leggings, chestPlate, helmet}, player.getName()); + } - /** - * Method that removes a corpse - * @param corpse The corpse to be removed - */ - public void removeCorpse(@NotNull Corpse corpse) { - Validate.notNull(corpse, "Corpse cannot be null"); - CorpsePool.getInstance().remove(corpse.getId()); - } + /** + * Method that creates a corpse in the given place and with the skin and name of the offlinePlayer + * with a custom inventory. + * + * @param offlinePlayer The offlinePlayer to copy + * @param location The location where to spawn the corpse + * @param helmet The helmet to put on the corpse + * @param chestPlate The chestPlate to put on the corpse + * @param leggings The leggings to put on the corpse + * @param boots The boots to put on the corpse + * @return a new Corpse object + */ + public Corpse spawnCorpse( + @NotNull OfflinePlayer offlinePlayer, + @NotNull Location location, + @Nullable ItemStack helmet, + @Nullable ItemStack chestPlate, + @Nullable ItemStack leggings, + @Nullable ItemStack boots + ) { + Validate.notNull(offlinePlayer, "OfflinePlayer cannot be null"); + Validate.notNull(location, "Spawn location cannot be null"); + return new Corpse(location, offlinePlayer, + new ItemStack[]{boots, leggings, chestPlate, helmet}); + } - /** - * Class method that allows you to use the API. - * @return an instance of this class - */ - @NotNull - public static synchronized CorpseAPI getInstance() { - if(instance == null) { - instance = new CorpseAPI(null); - } - return instance; - } + /** + * Method that removes a corpse + * + * @param corpse The corpse to be removed + */ + public void removeCorpse(@NotNull Corpse corpse) { + Validate.notNull(corpse, "Corpse cannot be null"); + CorpsePool.getInstance().remove(corpse.getId()); + } } diff --git a/src/main/java/com/github/unldenis/corpse/command/RemoveCorpseCommand.java b/src/main/java/com/github/unldenis/corpse/command/RemoveCorpseCommand.java index d127ec1..a5a1423 100644 --- a/src/main/java/com/github/unldenis/corpse/command/RemoveCorpseCommand.java +++ b/src/main/java/com/github/unldenis/corpse/command/RemoveCorpseCommand.java @@ -28,38 +28,40 @@ public class RemoveCorpseCommand implements CommandExecutor { - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) { - if(sender instanceof Player) { - Player player = (Player) sender; - if(player.hasPermission("corpses.remove")) { - if(args.length==1) { - try { - double radius = Math.pow(Double.parseDouble(args[0]), 2); + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, + @NotNull String label, @NotNull String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if (player.hasPermission("corpses.remove")) { + if (args.length == 1) { + try { + double radius = Math.pow(Double.parseDouble(args[0]), 2); - CorpsePool pool = CorpsePool.getInstance(); - AtomicInteger count = new AtomicInteger(0); - pool.getCorpses() - .stream() - .filter(corpse -> corpse.getLocation().distanceSquared(player.getLocation()) <= radius) - .forEach(corpse -> { - pool.remove(corpse.getId()); - count.incrementAndGet(); - }); - player.sendMessage("(" + count.get() + ") " + ChatColor.GREEN+"Corpses deleted"); - return true; - }catch (NumberFormatException e) { - player.sendMessage(ChatColor.RED + "Radius must be a number"); - } - return true; - } - sender.sendMessage(ChatColor.RED+"/removecorpse [radius] - Removes any coprse(s) in a radius of you."); - } - } else { - sender.sendMessage(ChatColor.RED+"Only players can run this command"); + CorpsePool pool = CorpsePool.getInstance(); + AtomicInteger count = new AtomicInteger(0); + pool.getCorpses() + .stream() + .filter( + corpse -> corpse.getLocation().distanceSquared(player.getLocation()) <= radius) + .forEach(corpse -> { + pool.remove(corpse.getId()); + count.incrementAndGet(); + }); + player.sendMessage("(" + count.get() + ") " + ChatColor.GREEN + "Corpses deleted"); + return true; + } catch (NumberFormatException e) { + player.sendMessage(ChatColor.RED + "Radius must be a number"); + } + return true; } - - - return false; + sender.sendMessage( + ChatColor.RED + "/removecorpse [radius] - Removes any coprse(s) in a radius of you."); + } + } else { + sender.sendMessage(ChatColor.RED + "Only players can run this command"); } + + return false; + } } diff --git a/src/main/java/com/github/unldenis/corpse/command/SpawnCorpseCommand.java b/src/main/java/com/github/unldenis/corpse/command/SpawnCorpseCommand.java index b579976..e39891b 100644 --- a/src/main/java/com/github/unldenis/corpse/command/SpawnCorpseCommand.java +++ b/src/main/java/com/github/unldenis/corpse/command/SpawnCorpseCommand.java @@ -28,30 +28,32 @@ public class SpawnCorpseCommand implements CommandExecutor { - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) { - if(sender instanceof Player) { - Player player = (Player) sender; - if(player.hasPermission("corpses.spawn")) { - if(args.length==0) { - new Corpse(player); - player.sendMessage(ChatColor.GREEN+"Corpse created"); - return true; - }else if(args.length==1) { - OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]); - if(target.isOnline()) { - CorpseAPI.getInstance().spawnCorpse((Player) target, player.getLocation()); - } else { - new Corpse(player.getLocation(), target, null); - } - player.sendMessage(ChatColor.GREEN+"Corpse created"); - return true; - } - sender.sendMessage(ChatColor.RED+"/spawncorpse [Player] - Spawns a corpse of a player if the name is given else it just spawns a corpse of yourself."); - } - } else { - sender.sendMessage(ChatColor.RED+"Only players can run this command"); + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, + @NotNull String label, @NotNull String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if (player.hasPermission("corpses.spawn")) { + if (args.length == 0) { + new Corpse(player); + player.sendMessage(ChatColor.GREEN + "Corpse created"); + return true; + } else if (args.length == 1) { + OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]); + if (target.isOnline()) { + CorpseAPI.getInstance().spawnCorpse((Player) target, player.getLocation()); + } else { + new Corpse(player.getLocation(), target, null); + } + player.sendMessage(ChatColor.GREEN + "Corpse created"); + return true; } - return false; + sender.sendMessage(ChatColor.RED + + "/spawncorpse [Player] - Spawns a corpse of a player if the name is given else it just spawns a corpse of yourself."); + } + } else { + sender.sendMessage(ChatColor.RED + "Only players can run this command"); } + return false; + } } diff --git a/src/main/java/com/github/unldenis/corpse/data/DataManager.java b/src/main/java/com/github/unldenis/corpse/data/DataManager.java index a6062c9..61c4213 100644 --- a/src/main/java/com/github/unldenis/corpse/data/DataManager.java +++ b/src/main/java/com/github/unldenis/corpse/data/DataManager.java @@ -27,58 +27,58 @@ import java.io.*; import java.util.logging.Level; -public class DataManager -{ - private Plugin plugin; - private FileConfiguration dataConfig; - private File configFile; - private String nameFile; +public class DataManager { - public DataManager(@NotNull Plugin plugin, @NotNull String nameFile) { - this.dataConfig = null; - this.configFile = null; - this.plugin = plugin; - this.nameFile = nameFile; - this.saveDefaultConfig(); - } + private Plugin plugin; + private FileConfiguration dataConfig; + private File configFile; + private String nameFile; + + public DataManager(@NotNull Plugin plugin, @NotNull String nameFile) { + this.dataConfig = null; + this.configFile = null; + this.plugin = plugin; + this.nameFile = nameFile; + this.saveDefaultConfig(); + } - public void reloadConfig() { - if (this.configFile == null) { - this.configFile = new File(this.plugin.getDataFolder(), this.nameFile); - } - this.dataConfig = (FileConfiguration)YamlConfiguration.loadConfiguration(this.configFile); - final InputStream defaultStream = this.plugin.getResource(this.nameFile); - if (defaultStream != null) { - final YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration((Reader)new InputStreamReader(defaultStream)); - this.dataConfig.setDefaults((Configuration)defaultConfig); - } + public void reloadConfig() { + if (this.configFile == null) { + this.configFile = new File(this.plugin.getDataFolder(), this.nameFile); + } + this.dataConfig = (FileConfiguration) YamlConfiguration.loadConfiguration(this.configFile); + final InputStream defaultStream = this.plugin.getResource(this.nameFile); + if (defaultStream != null) { + final YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration( + (Reader) new InputStreamReader(defaultStream)); + this.dataConfig.setDefaults((Configuration) defaultConfig); } + } - public FileConfiguration getConfig() { - if (this.dataConfig == null) { - this.reloadConfig(); - } - return this.dataConfig; + public FileConfiguration getConfig() { + if (this.dataConfig == null) { + this.reloadConfig(); } + return this.dataConfig; + } - public void saveConfig() { - if (this.dataConfig == null || this.configFile == null) { - return; - } - try { - this.getConfig().save(this.configFile); - } - catch (IOException e) { - this.plugin.getLogger().log(Level.SEVERE, "Could not save config to " + this.configFile, e); - } + public void saveConfig() { + if (this.dataConfig == null || this.configFile == null) { + return; } + try { + this.getConfig().save(this.configFile); + } catch (IOException e) { + this.plugin.getLogger().log(Level.SEVERE, "Could not save config to " + this.configFile, e); + } + } - public void saveDefaultConfig() { - if (this.configFile == null) { - this.configFile = new File(this.plugin.getDataFolder(), this.nameFile); - } - if (!this.configFile.exists()) { - this.plugin.saveResource(this.nameFile, false); - } + public void saveDefaultConfig() { + if (this.configFile == null) { + this.configFile = new File(this.plugin.getDataFolder(), this.nameFile); + } + if (!this.configFile.exists()) { + this.plugin.saveResource(this.nameFile, false); } + } } \ No newline at end of file diff --git a/src/main/java/com/github/unldenis/corpse/logic/Corpse.java b/src/main/java/com/github/unldenis/corpse/logic/Corpse.java index da45681..d6a3cfb 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/Corpse.java +++ b/src/main/java/com/github/unldenis/corpse/logic/Corpse.java @@ -36,171 +36,169 @@ public class Corpse { - protected final int id; - - protected final UUID uuid; - - protected final String name; - - protected final Location location; - - protected final WrappedGameProfile profile; - - private final Collection seeingPlayers = new CopyOnWriteArraySet<>(); - - private final PacketLoader packetLoader; - - private final CorpsePool pool; - - private boolean armor = false; - protected ItemStack[] armorContents; - - - @ApiStatus.Internal - public Corpse( - @NotNull Location location, - @NotNull WrappedGameProfile wrappedGameProfile, - @Nullable ItemStack[] armorContents, - @Nullable String name - ) { - pool = CorpsePool.getInstance(); - - this.id = pool.getFreeEntityId(); - this.uuid = new UUID(new Random().nextLong(), 0); - this.name = name == null ? ProfileUtils.randomName() : name; - this.location = location; - this.profile = new WrappedGameProfile(this.uuid, this.name); - //set skin to profile WrappedGameProfile - wrappedGameProfile.getProperties().get("textures").forEach(property -> profile.getProperties().put("textures", property)); - - if(pool.isRenderArmor() && armorContents != null) { - this.armorContents = armorContents.clone(); - this.armor = this.armorContents[0] != null || this.armorContents[1] != null || this.armorContents[2] != null || this.armorContents[3] != null; - } - //load packets - packetLoader = new PacketLoader(this); - packetLoader.load(); - - //pool take care - pool.takeCareOf(this); - - //remove eventually corpse after X seconds - int time = pool.getTimeRemove(); - if(time > -1) { - Bukkit.getScheduler().runTaskLaterAsynchronously(CorpseP.getInstance(), ()-> pool.remove(this.id), 20L*time); - } - + private static final String TEAM_NAME = "corpse-lib"; + protected final int id; + protected final UUID uuid; + protected final String name; + protected final Location location; + protected final WrappedGameProfile profile; + private final Collection seeingPlayers = new CopyOnWriteArraySet<>(); + private final PacketLoader packetLoader; + private final CorpsePool pool; + protected ItemStack[] armorContents; + private boolean armor = false; + + @ApiStatus.Internal + public Corpse( + @NotNull Location location, + @NotNull WrappedGameProfile wrappedGameProfile, + @Nullable ItemStack[] armorContents, + @Nullable String name + ) { + pool = CorpsePool.getInstance(); + + this.id = pool.getFreeEntityId(); + this.uuid = new UUID(new Random().nextLong(), 0); + this.name = name == null ? ProfileUtils.randomName() : name; + this.location = location; + this.profile = new WrappedGameProfile(this.uuid, this.name); + //set skin to profile WrappedGameProfile + wrappedGameProfile.getProperties().get("textures") + .forEach(property -> profile.getProperties().put("textures", property)); + + if (pool.isRenderArmor() && armorContents != null) { + this.armorContents = armorContents.clone(); + this.armor = this.armorContents[0] != null || this.armorContents[1] != null + || this.armorContents[2] != null || this.armorContents[3] != null; } - - public Corpse(@NotNull Player player) { - this(player.getLocation(), WrappedGameProfile.fromPlayer(player), player.getInventory().getArmorContents(), player.getName()); + //load packets + packetLoader = new PacketLoader(this); + packetLoader.load(); + + //pool take care + pool.takeCareOf(this); + + //remove eventually corpse after X seconds + int time = pool.getTimeRemove(); + if (time > -1) { + Bukkit.getScheduler() + .runTaskLaterAsynchronously(CorpseP.getInstance(), () -> pool.remove(this.id), + 20L * time); } - public Corpse( - @NotNull Location location, - @NotNull OfflinePlayer offlinePlayer, - @Nullable ItemStack[] armorContents - ) { - this(location, WrappedGameProfile.fromOfflinePlayer(offlinePlayer), armorContents, offlinePlayer.getName()); - } + } - @ApiStatus.Internal - public void show(@NotNull Player player) { - this.seeingPlayers.add(player); - - if(!this.pool.isShowTags()) { - hideNameTag(player); - } - sendPackets(player, - this.packetLoader.getWrapperPlayerInfoAdd().get(), - this.packetLoader.getWrapperNamedEntitySpawn().get(), - this.packetLoader.getWrapperEntityMetadata().get()); // Set sleep - - if (VersionUtil.isBelow(VersionUtil.VersionEnum.V1_12)) { - player.sendBlockChange(BedUtil.getBedLocation(location), Material.valueOf("BED_BLOCK"), (byte) BedUtil.yawToFacing(location.getYaw())); - sendPackets(player, - this.packetLoader.getWrapperBed().get(), - this.packetLoader.getWrapperEntityTeleport().get()); // Set the correct height of the player lying down - } - - if(armor) { - sendPackets(player, this.packetLoader.getWrapperEntityEquipment().getMore()); - } - - Bukkit.getScheduler().runTaskLaterAsynchronously( - CorpseP.getInstance(), - () -> sendPackets(player, this.packetLoader.getWrapperPlayerInfoRemove().get()), - 2L); + public Corpse(@NotNull Player player) { + this(player.getLocation(), WrappedGameProfile.fromPlayer(player), + player.getInventory().getArmorContents(), player.getName()); + } - } + public Corpse( + @NotNull Location location, + @NotNull OfflinePlayer offlinePlayer, + @Nullable ItemStack[] armorContents + ) { + this(location, WrappedGameProfile.fromOfflinePlayer(offlinePlayer), armorContents, + offlinePlayer.getName()); + } - @ApiStatus.Internal - public void hide(@NotNull Player player) { - sendPackets(player, this.packetLoader.getWrapperEntityDestroy().get()); - if(!this.pool.isShowTags()) { - showNameTag(player); - } - this.seeingPlayers.remove(player); - } + @ApiStatus.Internal + public void show(@NotNull Player player) { + this.seeingPlayers.add(player); - public boolean isShownFor(@NotNull Player player) { - return this.seeingPlayers.contains(player); + if (!this.pool.isShowTags()) { + hideNameTag(player); } - - private static final String TEAM_NAME = "corpse-lib"; - - private void hideNameTag(@NotNull Player player) { - // hide nametag to player - org.bukkit.scoreboard.Scoreboard scoreboard = player.getScoreboard(); - Team npcs = null; - for(Team team : scoreboard.getTeams()) { - if(team.getName().equals(Corpse.TEAM_NAME)) { - npcs = team; - break; - } - } - if(npcs == null) { - npcs = scoreboard.registerNewTeam(Corpse.TEAM_NAME); - } - npcs.setNameTagVisibility(NameTagVisibility.NEVER); - npcs.addEntry(this.name); + sendPackets(player, + this.packetLoader.getWrapperPlayerInfoAdd().get(), + this.packetLoader.getWrapperNamedEntitySpawn().get(), + this.packetLoader.getWrapperEntityMetadata().get()); // Set sleep + + if (VersionUtil.isBelow(VersionUtil.VersionEnum.V1_12)) { + player.sendBlockChange(BedUtil.getBedLocation(location), Material.valueOf("BED_BLOCK"), + (byte) BedUtil.yawToFacing(location.getYaw())); + sendPackets(player, + this.packetLoader.getWrapperBed().get(), + this.packetLoader.getWrapperEntityTeleport() + .get()); // Set the correct height of the player lying down } - private void showNameTag(@NotNull Player player) { - // show nametag to player - player.getScoreboard().getTeams() - .stream() - .filter(team -> team.getName().equals(Corpse.TEAM_NAME)) - .forEach(team -> team.removeEntry(this.name)); + if (armor) { + sendPackets(player, this.packetLoader.getWrapperEntityEquipment().getMore()); } - private void sendPackets(Player player, PacketContainer... packets) { - for(PacketContainer packet: packets) { - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - } + Bukkit.getScheduler().runTaskLaterAsynchronously( + CorpseP.getInstance(), + () -> sendPackets(player, this.packetLoader.getWrapperPlayerInfoRemove().get()), + 2L); - public int getId() { - return id; - } + } - @NotNull - public String getName() { - return name; + @ApiStatus.Internal + public void hide(@NotNull Player player) { + sendPackets(player, this.packetLoader.getWrapperEntityDestroy().get()); + if (!this.pool.isShowTags()) { + showNameTag(player); } - - @NotNull - public Location getLocation() { - return location; + this.seeingPlayers.remove(player); + } + + public boolean isShownFor(@NotNull Player player) { + return this.seeingPlayers.contains(player); + } + + private void hideNameTag(@NotNull Player player) { + // hide nametag to player + org.bukkit.scoreboard.Scoreboard scoreboard = player.getScoreboard(); + Team npcs = null; + for (Team team : scoreboard.getTeams()) { + if (team.getName().equals(Corpse.TEAM_NAME)) { + npcs = team; + break; + } } - - @NotNull - public Collection getSeeingPlayers() { - return Collections.unmodifiableCollection(this.seeingPlayers); + if (npcs == null) { + npcs = scoreboard.registerNewTeam(Corpse.TEAM_NAME); + } + npcs.setNameTagVisibility(NameTagVisibility.NEVER); + npcs.addEntry(this.name); + } + + private void showNameTag(@NotNull Player player) { + // show nametag to player + player.getScoreboard().getTeams() + .stream() + .filter(team -> team.getName().equals(Corpse.TEAM_NAME)) + .forEach(team -> team.removeEntry(this.name)); + } + + private void sendPackets(Player player, PacketContainer... packets) { + for (PacketContainer packet : packets) { + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } } + } + + public int getId() { + return id; + } + + @NotNull + public String getName() { + return name; + } + + @NotNull + public Location getLocation() { + return location; + } + + @NotNull + public Collection getSeeingPlayers() { + return Collections.unmodifiableCollection(this.seeingPlayers); + } } diff --git a/src/main/java/com/github/unldenis/corpse/logic/PacketLoader.java b/src/main/java/com/github/unldenis/corpse/logic/PacketLoader.java index f33655b..4451d48 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/PacketLoader.java +++ b/src/main/java/com/github/unldenis/corpse/logic/PacketLoader.java @@ -28,74 +28,75 @@ public class PacketLoader { - private final Collection packets = new CopyOnWriteArraySet<>(); - private final WrapperEntityDestroy wrapperEntityDestroy; - private final WrapperEntityEquipment wrapperEntityEquipment; - private final WrapperEntityMetadata wrapperEntityMetadata; - private final WrapperNamedEntitySpawn wrapperNamedEntitySpawn; - private final WrapperPlayerInfo wrapperPlayerInfoAdd; - private final WrapperPlayerInfo wrapperPlayerInfoRemove; - private final WrapperBed wrapperBed; - private final WrapperEntityTeleport wrapperEntityTeleport; - - public PacketLoader(@NotNull Corpse corpse) { - //init packets - this.wrapperEntityDestroy = new WrapperEntityDestroy(corpse.id); - this.wrapperEntityEquipment = new WrapperEntityEquipment(corpse.id, corpse.armorContents); - this.wrapperEntityMetadata = new WrapperEntityMetadata(corpse.id); - this.wrapperNamedEntitySpawn = new WrapperNamedEntitySpawn(corpse.id, corpse.uuid, corpse.location); - this.wrapperPlayerInfoAdd = new WrapperPlayerInfo(true, corpse.profile, corpse.name); - this.wrapperPlayerInfoRemove = new WrapperPlayerInfo(false, corpse.profile, corpse.name); - this.wrapperBed = new WrapperBed(corpse.id, corpse.location); - this.wrapperEntityTeleport = new WrapperEntityTeleport(corpse.id, corpse.location); + private final Collection packets = new CopyOnWriteArraySet<>(); + private final WrapperEntityDestroy wrapperEntityDestroy; + private final WrapperEntityEquipment wrapperEntityEquipment; + private final WrapperEntityMetadata wrapperEntityMetadata; + private final WrapperNamedEntitySpawn wrapperNamedEntitySpawn; + private final WrapperPlayerInfo wrapperPlayerInfoAdd; + private final WrapperPlayerInfo wrapperPlayerInfoRemove; + private final WrapperBed wrapperBed; + private final WrapperEntityTeleport wrapperEntityTeleport; + + public PacketLoader(@NotNull Corpse corpse) { + //init packets + this.wrapperEntityDestroy = new WrapperEntityDestroy(corpse.id); + this.wrapperEntityEquipment = new WrapperEntityEquipment(corpse.id, corpse.armorContents); + this.wrapperEntityMetadata = new WrapperEntityMetadata(corpse.id); + this.wrapperNamedEntitySpawn = new WrapperNamedEntitySpawn(corpse.id, corpse.uuid, + corpse.location); + this.wrapperPlayerInfoAdd = new WrapperPlayerInfo(true, corpse.profile, corpse.name); + this.wrapperPlayerInfoRemove = new WrapperPlayerInfo(false, corpse.profile, corpse.name); + this.wrapperBed = new WrapperBed(corpse.id, corpse.location); + this.wrapperEntityTeleport = new WrapperEntityTeleport(corpse.id, corpse.location); + } + + public void load() { + //load packets + packets.add(wrapperEntityDestroy); + packets.add(wrapperEntityEquipment); + packets.add(wrapperEntityMetadata); + packets.add(wrapperNamedEntitySpawn); + packets.add(wrapperPlayerInfoAdd); + packets.add(wrapperPlayerInfoRemove); + + //Fix 'Caused by: java.lang.IllegalArgumentException: Could not find packet for type BED' for last versions + if (VersionUtil.isBelow(VersionUtil.VersionEnum.V1_12)) { + packets.add(wrapperBed); + packets.add(wrapperEntityTeleport); } + this.packets.forEach(IPacket::load); + } - public void load() { - //load packets - packets.add(wrapperEntityDestroy); - packets.add(wrapperEntityEquipment); - packets.add(wrapperEntityMetadata); - packets.add(wrapperNamedEntitySpawn); - packets.add(wrapperPlayerInfoAdd); - packets.add(wrapperPlayerInfoRemove); - - //Fix 'Caused by: java.lang.IllegalArgumentException: Could not find packet for type BED' for last versions - if(VersionUtil.isBelow(VersionUtil.VersionEnum.V1_12)) { - packets.add(wrapperBed); - packets.add(wrapperEntityTeleport); - } - this.packets.forEach(IPacket::load); - } + public WrapperEntityDestroy getWrapperEntityDestroy() { + return wrapperEntityDestroy; + } - public WrapperEntityDestroy getWrapperEntityDestroy() { - return wrapperEntityDestroy; - } + public WrapperEntityEquipment getWrapperEntityEquipment() { + return wrapperEntityEquipment; + } - public WrapperEntityEquipment getWrapperEntityEquipment() { - return wrapperEntityEquipment; - } - - public WrapperEntityMetadata getWrapperEntityMetadata() { - return wrapperEntityMetadata; - } + public WrapperEntityMetadata getWrapperEntityMetadata() { + return wrapperEntityMetadata; + } - public WrapperNamedEntitySpawn getWrapperNamedEntitySpawn() { - return wrapperNamedEntitySpawn; - } + public WrapperNamedEntitySpawn getWrapperNamedEntitySpawn() { + return wrapperNamedEntitySpawn; + } - public WrapperPlayerInfo getWrapperPlayerInfoAdd() { - return wrapperPlayerInfoAdd; - } + public WrapperPlayerInfo getWrapperPlayerInfoAdd() { + return wrapperPlayerInfoAdd; + } - public WrapperPlayerInfo getWrapperPlayerInfoRemove() { - return wrapperPlayerInfoRemove; - } + public WrapperPlayerInfo getWrapperPlayerInfoRemove() { + return wrapperPlayerInfoRemove; + } - public WrapperBed getWrapperBed() { - return wrapperBed; - } + public WrapperBed getWrapperBed() { + return wrapperBed; + } - public WrapperEntityTeleport getWrapperEntityTeleport() { - return wrapperEntityTeleport; - } + public WrapperEntityTeleport getWrapperEntityTeleport() { + return wrapperEntityTeleport; + } } diff --git a/src/main/java/com/github/unldenis/corpse/logic/packet/IPacket.java b/src/main/java/com/github/unldenis/corpse/logic/packet/IPacket.java index 7ca13f8..a19f007 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/packet/IPacket.java +++ b/src/main/java/com/github/unldenis/corpse/logic/packet/IPacket.java @@ -23,9 +23,9 @@ public interface IPacket { - void load(); + void load(); - @Nullable - PacketContainer get(); + @Nullable + PacketContainer get(); } diff --git a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperBed.java b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperBed.java index 688a99b..d93c7fc 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperBed.java +++ b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperBed.java @@ -7,25 +7,25 @@ public class WrapperBed implements IPacket { - private PacketContainer packet; - private final int id; - private final Location loc; + private final int id; + private final Location loc; + private PacketContainer packet; - public WrapperBed(int entityID, Location location) { - this.id = entityID; - this.loc = location; - } + public WrapperBed(int entityID, Location location) { + this.id = entityID; + this.loc = location; + } - @Override - public void load() { - packet = new PacketContainer(PacketType.Play.Server.BED); - packet.getIntegers().write(0, this.id); - packet.getBlockPositionModifier().write(0, - new BlockPosition(loc.getBlockX(), 1, loc.getBlockZ())); - } + @Override + public void load() { + packet = new PacketContainer(PacketType.Play.Server.BED); + packet.getIntegers().write(0, this.id); + packet.getBlockPositionModifier().write(0, + new BlockPosition(loc.getBlockX(), 1, loc.getBlockZ())); + } - @Override - public PacketContainer get() { - return packet; - } + @Override + public PacketContainer get() { + return packet; + } } \ No newline at end of file diff --git a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityDestroy.java b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityDestroy.java index ce3e6ea..cca1028 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityDestroy.java +++ b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityDestroy.java @@ -26,26 +26,25 @@ public class WrapperEntityDestroy implements IPacket { - private PacketContainer packet; - - private final int id; - - public WrapperEntityDestroy(int entityID) { - this.id = entityID; + private final int id; + private PacketContainer packet; + + public WrapperEntityDestroy(int entityID) { + this.id = entityID; + } + + @Override + public void load() { + packet = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY); + if (VersionUtil.isBelow(VersionUtil.VersionEnum.V1_16)) { + packet.getIntegerArrays().write(0, new int[]{this.id}); + } else { + packet.getIntLists().write(0, Collections.singletonList(this.id)); } + } - @Override - public void load() { - packet = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY); - if(VersionUtil.isBelow(VersionUtil.VersionEnum.V1_16)) { - packet.getIntegerArrays().write(0, new int[] { this.id }); - }else{ - packet.getIntLists().write(0, Collections.singletonList(this.id)); - } - } - - @Override - public PacketContainer get() { - return packet; - } + @Override + public PacketContainer get() { + return packet; + } } diff --git a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityEquipment.java b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityEquipment.java index ff3e685..6a0c231 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityEquipment.java +++ b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityEquipment.java @@ -29,93 +29,94 @@ public class WrapperEntityEquipment implements IPacket { - private PacketContainer[] packetContainers; + private final int id; + private final ItemStack[] armorContents; + private PacketContainer[] packetContainers; - private final int id; - private final ItemStack[] armorContents; + public WrapperEntityEquipment(int entityID, @Nullable ItemStack[] armorContents) { + this.id = entityID; + this.armorContents = armorContents; + } - public WrapperEntityEquipment(int entityID, @Nullable ItemStack[] armorContents) { - this.id = entityID; - this.armorContents = armorContents; - } - - @Override - public void load() { - PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_EQUIPMENT); - packet.getIntegers().write(0, this.id); - if(this.armorContents != null) { - if (VersionUtil.isCompatible(VersionUtil.VersionEnum.V1_8)) { - packetContainers = new PacketContainer[4]; - ItemStack temp; - for (int j = 0; j < this.armorContents.length; j++) { - temp = this.armorContents[j]; - if (temp != null) { - PacketContainer cloned = packet.deepClone(); - cloned.getIntegers().write(1, j + 1); - cloned.getItemModifier().write(0, this.armorContents[j]); - packetContainers[j] = cloned; - } - } - //remove null packets - packetContainers = Arrays.stream(packetContainers).filter(Objects::nonNull).toArray(PacketContainer[]::new); - } else if (VersionUtil.isBelow(VersionUtil.VersionEnum.V1_13)) { - packetContainers = new PacketContainer[4]; - if (this.armorContents[3] != null) { - PacketContainer cloned = packet.deepClone(); - cloned.getItemSlots().write(0, EnumWrappers.ItemSlot.HEAD); - cloned.getItemModifier().write(0, this.armorContents[3]); - packetContainers[3] = cloned; - } - if (this.armorContents[2] != null) { - PacketContainer cloned = packet.deepClone(); - cloned.getItemSlots().write(0, EnumWrappers.ItemSlot.CHEST); - cloned.getItemModifier().write(0, this.armorContents[2]); - packetContainers[2] = cloned; - } - if (this.armorContents[1] != null) { - PacketContainer cloned = packet.deepClone(); - cloned.getItemSlots().write(0, EnumWrappers.ItemSlot.LEGS); - cloned.getItemModifier().write(0, this.armorContents[1]); - packetContainers[1] = cloned; - } - if (this.armorContents[0] != null) { - PacketContainer cloned = packet.deepClone(); - cloned.getItemSlots().write(0, EnumWrappers.ItemSlot.FEET); - cloned.getItemModifier().write(0, this.armorContents[0]); - packetContainers[0] = cloned; - } - //remove null packets - packetContainers = Arrays.stream(packetContainers).filter(Objects::nonNull).toArray(PacketContainer[]::new); - } else { - packetContainers = new PacketContainer[1]; - List> pairList = new ArrayList<>(); - if (this.armorContents[3] != null) { - pairList.add(new Pair<>(EnumWrappers.ItemSlot.HEAD, this.armorContents[3])); - } - if (this.armorContents[2] != null) { - pairList.add(new Pair<>(EnumWrappers.ItemSlot.CHEST, this.armorContents[2])); - } - if (this.armorContents[1] != null) { - pairList.add(new Pair<>(EnumWrappers.ItemSlot.LEGS, this.armorContents[1])); - } - if (this.armorContents[0] != null) { - pairList.add(new Pair<>(EnumWrappers.ItemSlot.FEET, this.armorContents[0])); - } - packet.getSlotStackPairLists().write(0, pairList); - packetContainers[0] = packet; - } - } else { - packetContainers = new PacketContainer[0]; + @Override + public void load() { + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_EQUIPMENT); + packet.getIntegers().write(0, this.id); + if (this.armorContents != null) { + if (VersionUtil.isCompatible(VersionUtil.VersionEnum.V1_8)) { + packetContainers = new PacketContainer[4]; + ItemStack temp; + for (int j = 0; j < this.armorContents.length; j++) { + temp = this.armorContents[j]; + if (temp != null) { + PacketContainer cloned = packet.deepClone(); + cloned.getIntegers().write(1, j + 1); + cloned.getItemModifier().write(0, this.armorContents[j]); + packetContainers[j] = cloned; + } + } + //remove null packets + packetContainers = Arrays.stream(packetContainers).filter(Objects::nonNull) + .toArray(PacketContainer[]::new); + } else if (VersionUtil.isBelow(VersionUtil.VersionEnum.V1_13)) { + packetContainers = new PacketContainer[4]; + if (this.armorContents[3] != null) { + PacketContainer cloned = packet.deepClone(); + cloned.getItemSlots().write(0, EnumWrappers.ItemSlot.HEAD); + cloned.getItemModifier().write(0, this.armorContents[3]); + packetContainers[3] = cloned; + } + if (this.armorContents[2] != null) { + PacketContainer cloned = packet.deepClone(); + cloned.getItemSlots().write(0, EnumWrappers.ItemSlot.CHEST); + cloned.getItemModifier().write(0, this.armorContents[2]); + packetContainers[2] = cloned; + } + if (this.armorContents[1] != null) { + PacketContainer cloned = packet.deepClone(); + cloned.getItemSlots().write(0, EnumWrappers.ItemSlot.LEGS); + cloned.getItemModifier().write(0, this.armorContents[1]); + packetContainers[1] = cloned; + } + if (this.armorContents[0] != null) { + PacketContainer cloned = packet.deepClone(); + cloned.getItemSlots().write(0, EnumWrappers.ItemSlot.FEET); + cloned.getItemModifier().write(0, this.armorContents[0]); + packetContainers[0] = cloned; } + //remove null packets + packetContainers = Arrays.stream(packetContainers).filter(Objects::nonNull) + .toArray(PacketContainer[]::new); + } else { + packetContainers = new PacketContainer[1]; + List> pairList = new ArrayList<>(); + if (this.armorContents[3] != null) { + pairList.add(new Pair<>(EnumWrappers.ItemSlot.HEAD, this.armorContents[3])); + } + if (this.armorContents[2] != null) { + pairList.add(new Pair<>(EnumWrappers.ItemSlot.CHEST, this.armorContents[2])); + } + if (this.armorContents[1] != null) { + pairList.add(new Pair<>(EnumWrappers.ItemSlot.LEGS, this.armorContents[1])); + } + if (this.armorContents[0] != null) { + pairList.add(new Pair<>(EnumWrappers.ItemSlot.FEET, this.armorContents[0])); + } + packet.getSlotStackPairLists().write(0, pairList); + packetContainers[0] = packet; + } + } else { + packetContainers = new PacketContainer[0]; } + } - @Override - public PacketContainer get() { - return null; - } + @Override + public PacketContainer get() { + return null; + } - @NotNull - public PacketContainer[] getMore() { - return this.packetContainers; - } + @NotNull + public PacketContainer[] getMore() { + return this.packetContainers; + } } diff --git a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityMetadata.java b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityMetadata.java index 13e44a0..4677ac0 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityMetadata.java +++ b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityMetadata.java @@ -25,38 +25,40 @@ public class WrapperEntityMetadata implements IPacket { - private PacketContainer packet; - private final int id; + private final int id; + private PacketContainer packet; - public WrapperEntityMetadata(int entityID) { - this.id = entityID; - } + public WrapperEntityMetadata(int entityID) { + this.id = entityID; + } - @Override - public void load() { - packet = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); - packet.getIntegers().write(0, this.id); - WrappedDataWatcher watcher = new WrappedDataWatcher(); + @Override + public void load() { + packet = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); + packet.getIntegers().write(0, this.id); + WrappedDataWatcher watcher = new WrappedDataWatcher(); - if (VersionUtil.isAbove(VersionUtil.VersionEnum.V1_13)) { - WrappedDataWatcher.WrappedDataWatcherObject visible = new WrappedDataWatcher.WrappedDataWatcherObject(6, WrappedDataWatcher.Registry.get(EnumWrappers.getEntityPoseClass())); - watcher.setObject(visible, EnumWrappers.EntityPose.SLEEPING.toNms()); + if (VersionUtil.isAbove(VersionUtil.VersionEnum.V1_13)) { + WrappedDataWatcher.WrappedDataWatcherObject visible = new WrappedDataWatcher.WrappedDataWatcherObject( + 6, WrappedDataWatcher.Registry.get(EnumWrappers.getEntityPoseClass())); + watcher.setObject(visible, EnumWrappers.EntityPose.SLEEPING.toNms()); /* //Works and set location a little higher WrappedDataWatcher.WrappedDataWatcherObject bed = new WrappedDataWatcher.WrappedDataWatcherObject(14, WrappedDataWatcher.Registry.getBlockPositionSerializer(true)); watcher.setObject(bed, Optional.of(BlockPosition.getConverter().getGeneric(new BlockPosition(corpse.location.toVector())))); */ - int indexSkinLayer = VersionUtil.isAbove(VersionUtil.VersionEnum.V1_17) ? 17 : 16; - WrappedDataWatcher.WrappedDataWatcherObject skinLayers = new WrappedDataWatcher.WrappedDataWatcherObject(indexSkinLayer, WrappedDataWatcher.Registry.get(Byte.class)); - watcher.setObject(skinLayers, (byte) (0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80)); - } else { - watcher.setObject(10, (byte) (0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80)); - } - packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects()); + int indexSkinLayer = VersionUtil.isAbove(VersionUtil.VersionEnum.V1_17) ? 17 : 16; + WrappedDataWatcher.WrappedDataWatcherObject skinLayers = new WrappedDataWatcher.WrappedDataWatcherObject( + indexSkinLayer, WrappedDataWatcher.Registry.get(Byte.class)); + watcher.setObject(skinLayers, (byte) (0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80)); + } else { + watcher.setObject(10, (byte) (0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80)); } + packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects()); + } - @Override - public PacketContainer get() { - return packet; - } + @Override + public PacketContainer get() { + return packet; + } } diff --git a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityTeleport.java b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityTeleport.java index 174768f..7d6e8c3 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityTeleport.java +++ b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperEntityTeleport.java @@ -8,33 +8,33 @@ public class WrapperEntityTeleport implements IPacket { - private PacketContainer packet; - private final int id; - private final Location loc; + private final int id; + private final Location loc; + private PacketContainer packet; - public WrapperEntityTeleport(int entityID, Location location) { - this.id = entityID; - this.loc = location; - } + public WrapperEntityTeleport(int entityID, Location location) { + this.id = entityID; + this.loc = location; + } - @Override - public void load() { - packet = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT); - packet.getIntegers().write(0, this.id); - if (VersionUtil.isAbove(VersionUtil.VersionEnum.V1_9)) { - packet.getDoubles().write(0, loc.getX()); - packet.getDoubles().write(1, loc.getY() + 0.15); - packet.getDoubles().write(2, loc.getZ()); - } else { - packet.getIntegers().write(1, (int) Math.floor(loc.getX() * 32.0D)); - packet.getIntegers().write(2, (int) Math.floor((loc.getY() + 0.15) * 32.0D)); - packet.getIntegers().write(3, (int) Math.floor(loc.getZ() * 32.0D)); - } + @Override + public void load() { + packet = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT); + packet.getIntegers().write(0, this.id); + if (VersionUtil.isAbove(VersionUtil.VersionEnum.V1_9)) { + packet.getDoubles().write(0, loc.getX()); + packet.getDoubles().write(1, loc.getY() + 0.15); + packet.getDoubles().write(2, loc.getZ()); + } else { + packet.getIntegers().write(1, (int) Math.floor(loc.getX() * 32.0D)); + packet.getIntegers().write(2, (int) Math.floor((loc.getY() + 0.15) * 32.0D)); + packet.getIntegers().write(3, (int) Math.floor(loc.getZ() * 32.0D)); } + } - @Nullable - @Override - public PacketContainer get() { - return this.packet; - } + @Nullable + @Override + public PacketContainer get() { + return this.packet; + } } diff --git a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperNamedEntitySpawn.java b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperNamedEntitySpawn.java index 1d816de..85195fb 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperNamedEntitySpawn.java +++ b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperNamedEntitySpawn.java @@ -28,22 +28,21 @@ public class WrapperNamedEntitySpawn implements IPacket { - private PacketContainer packet; - - - private final int id; - private final UUID uuid; - private final Location location; - - public WrapperNamedEntitySpawn(int entityID, @NotNull UUID uuid, @NotNull Location location) { - this.id = entityID; - this.uuid = uuid; - this.location = location; - } - - @Override - public void load() { - packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.NAMED_ENTITY_SPAWN); + private final int id; + private final UUID uuid; + private final Location location; + private PacketContainer packet; + + public WrapperNamedEntitySpawn(int entityID, @NotNull UUID uuid, @NotNull Location location) { + this.id = entityID; + this.uuid = uuid; + this.location = location; + } + + @Override + public void load() { + packet = ProtocolLibrary.getProtocolManager() + .createPacket(PacketType.Play.Server.NAMED_ENTITY_SPAWN); /* Unknown reason @@ -51,38 +50,37 @@ public void load() { packet = new PacketContainer(PacketType.Play.Server.NAMED_ENTITY_SPAWN); */ - if(VersionUtil.isCompatible(VersionUtil.VersionEnum.V1_8)) { - packet.getModifier().writeDefaults(); - packet.getIntegers(). - write(0, this.id). - write(1, (int) (this.location.getX() * 32)). - write(2, (int) (this.location.getY() * 32)). - write(3, (int) (this.location.getZ() * 32)); - - packet.getUUIDs() - .write(0, this.uuid); - } else { - packet.getIntegers() - .write(0, this.id); - packet.getUUIDs() - .write(0, this.uuid); - packet.getDoubles() - .write(0, this.location.getX()) - .write(1, this.location.getY()) - .write(2, this.location.getZ()); - packet.getBytes() - .write(0, (byte) (this.location.getYaw() * 256.0F / 360.0F)) - .write(1, (byte) (this.location.getPitch() * 256.0F / 360.0F)); - } - + if (VersionUtil.isCompatible(VersionUtil.VersionEnum.V1_8)) { + packet.getModifier().writeDefaults(); + packet.getIntegers(). + write(0, this.id). + write(1, (int) (this.location.getX() * 32)). + write(2, (int) (this.location.getY() * 32)). + write(3, (int) (this.location.getZ() * 32)); + + packet.getUUIDs() + .write(0, this.uuid); + } else { + packet.getIntegers() + .write(0, this.id); + packet.getUUIDs() + .write(0, this.uuid); + packet.getDoubles() + .write(0, this.location.getX()) + .write(1, this.location.getY()) + .write(2, this.location.getZ()); + packet.getBytes() + .write(0, (byte) (this.location.getYaw() * 256.0F / 360.0F)) + .write(1, (byte) (this.location.getPitch() * 256.0F / 360.0F)); } - @Nullable - @Override - public PacketContainer get() { - return packet; - } + } + @Nullable + @Override + public PacketContainer get() { + return packet; + } } diff --git a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperPlayerInfo.java b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperPlayerInfo.java index e38ded4..143b852 100644 --- a/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperPlayerInfo.java +++ b/src/main/java/com/github/unldenis/corpse/logic/packet/WrapperPlayerInfo.java @@ -27,33 +27,36 @@ public class WrapperPlayerInfo implements IPacket { - private PacketContainer packet; - private final boolean add; - private final WrappedGameProfile gameProfile; - private final String name; - - public WrapperPlayerInfo(boolean add, @NotNull WrappedGameProfile gameProfile, @NotNull String name) { - this.add = add; - this.gameProfile = gameProfile; - this.name = name; - } - - @Override - public void load() { - packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO); - packet.getPlayerInfoAction() - .write(0, this.add ? EnumWrappers.PlayerInfoAction.ADD_PLAYER : EnumWrappers.PlayerInfoAction.REMOVE_PLAYER ); - - PlayerInfoData data = new PlayerInfoData(this.gameProfile, 1, EnumWrappers.NativeGameMode.CREATIVE, WrappedChatComponent.fromText(this.name)); - List dataList = new ArrayList(); - dataList.add(data); - - packet.getPlayerInfoDataLists() - .write(0, dataList); - } - - @Override - public PacketContainer get() { - return packet; - } + private final boolean add; + private final WrappedGameProfile gameProfile; + private final String name; + private PacketContainer packet; + + public WrapperPlayerInfo(boolean add, @NotNull WrappedGameProfile gameProfile, + @NotNull String name) { + this.add = add; + this.gameProfile = gameProfile; + this.name = name; + } + + @Override + public void load() { + packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO); + packet.getPlayerInfoAction() + .write(0, this.add ? EnumWrappers.PlayerInfoAction.ADD_PLAYER + : EnumWrappers.PlayerInfoAction.REMOVE_PLAYER); + + PlayerInfoData data = new PlayerInfoData(this.gameProfile, 1, + EnumWrappers.NativeGameMode.CREATIVE, WrappedChatComponent.fromText(this.name)); + List dataList = new ArrayList(); + dataList.add(data); + + packet.getPlayerInfoDataLists() + .write(0, dataList); + } + + @Override + public PacketContainer get() { + return packet; + } } diff --git a/src/main/java/com/github/unldenis/corpse/manager/CorpsePool.java b/src/main/java/com/github/unldenis/corpse/manager/CorpsePool.java index 41b0d47..a4753b4 100644 --- a/src/main/java/com/github/unldenis/corpse/manager/CorpsePool.java +++ b/src/main/java/com/github/unldenis/corpse/manager/CorpsePool.java @@ -35,165 +35,164 @@ public class CorpsePool implements Listener { - private static CorpsePool instance; - - private static final Random RANDOM = new Random(); - private final CorpseP plugin; - - //config options - private final double spawnDistance; - private final int timeRemove; - private final boolean onDeath; - private final boolean showTags; - private final boolean renderArmor; - - - private final Map corpseMap = new ConcurrentHashMap<>(); - - private BukkitTask tickTask; - - @ApiStatus.Internal - private CorpsePool() { - this.plugin = CorpseP.getInstance(); - - FileConfiguration config = plugin.getConfigYml(); - this.spawnDistance = Math.pow(config.getInt("corpse-distance"), 2); - this.timeRemove = config.getInt("corpse-time"); - this.onDeath = config.getBoolean("on-death"); - this.showTags = config.getBoolean("show-tags"); - this.renderArmor = config.getBoolean("render-armor"); - - Bukkit.getPluginManager().registerEvents(this, plugin); - this.corpseTick(); - } - - private void corpseTick() { - tickTask = Bukkit.getScheduler().runTaskTimerAsynchronously(this.plugin, () -> { - for (Player player : ImmutableList.copyOf(Bukkit.getOnlinePlayers())) { - for (Corpse corpse : this.corpseMap.values()) { - Location holoLoc = corpse.getLocation(); - Location playerLoc = player.getLocation(); - boolean isShown = corpse.isShownFor(player); - - if (!holoLoc.getWorld().equals(playerLoc.getWorld())) { - if(isShown) { - corpse.hide(player); - } - continue; - } else if (!holoLoc.getWorld() - .isChunkLoaded(holoLoc.getBlockX() >> 4, holoLoc.getBlockZ() >> 4) && isShown) { - corpse.hide(player); - continue; - } - boolean inRange = holoLoc.distanceSquared(playerLoc) <= this.spawnDistance; - - if (!inRange && isShown) { - corpse.hide(player); - } else if (inRange && !isShown) { - corpse.show(player); - } - } + private static final Random RANDOM = new Random(); + private static CorpsePool instance; + private final CorpseP plugin; + + //config options + private final double spawnDistance; + private final int timeRemove; + private final boolean onDeath; + private final boolean showTags; + private final boolean renderArmor; + + + private final Map corpseMap = new ConcurrentHashMap<>(); + + private BukkitTask tickTask; + + @ApiStatus.Internal + private CorpsePool() { + this.plugin = CorpseP.getInstance(); + + FileConfiguration config = plugin.getConfigYml(); + this.spawnDistance = Math.pow(config.getInt("corpse-distance"), 2); + this.timeRemove = config.getInt("corpse-time"); + this.onDeath = config.getBoolean("on-death"); + this.showTags = config.getBoolean("show-tags"); + this.renderArmor = config.getBoolean("render-armor"); + + Bukkit.getPluginManager().registerEvents(this, plugin); + this.corpseTick(); + } + + @NotNull + public static synchronized CorpsePool getInstance() { + if (instance == null) { + instance = new CorpsePool(); + } + return instance; + } + + private void corpseTick() { + tickTask = Bukkit.getScheduler().runTaskTimerAsynchronously(this.plugin, () -> { + for (Player player : ImmutableList.copyOf(Bukkit.getOnlinePlayers())) { + for (Corpse corpse : this.corpseMap.values()) { + Location holoLoc = corpse.getLocation(); + Location playerLoc = player.getLocation(); + boolean isShown = corpse.isShownFor(player); + + if (!holoLoc.getWorld().equals(playerLoc.getWorld())) { + if (isShown) { + corpse.hide(player); } - }, 20, 2); - } - - @NotNull - public Optional getCorpse(int entityId) { - return Optional.ofNullable(this.corpseMap.get(entityId)); - } - - @NotNull - public Optional getCorpse(String name) { - return this.getCorpses() - .stream() - .filter(corpse -> corpse.getName().equals(name)) - .findFirst(); - } - - public void remove(int entityId) { - this.getCorpse(entityId).ifPresent(corpse -> { - this.corpseMap.remove(entityId); - corpse.getSeeingPlayers() - .forEach(corpse::hide); - }); - } - - public int getFreeEntityId() { - int id; - - do { - id = RANDOM.nextInt(Integer.MAX_VALUE); - } while (this.corpseMap.containsKey(id)); - - return id; - } - - @NotNull - public Collection getCorpses() { - return Collections.unmodifiableCollection(this.corpseMap.values()); - } - - public void takeCareOf(@NotNull Corpse corpse) { - // Prevent two corpses with same name and showTags is enabled - if(this.showTags) { - this.getCorpse(corpse.getName()).ifPresent(c -> { - this.corpseMap.remove(c.getId()); - c.getSeeingPlayers() - .forEach(c::hide); - }); + continue; + } else if (!holoLoc.getWorld() + .isChunkLoaded(holoLoc.getBlockX() >> 4, holoLoc.getBlockZ() >> 4) && isShown) { + corpse.hide(player); + continue; + } + boolean inRange = holoLoc.distanceSquared(playerLoc) <= this.spawnDistance; + + if (!inRange && isShown) { + corpse.hide(player); + } else if (inRange && !isShown) { + corpse.show(player); + } } - this.corpseMap.put(corpse.getId(), corpse); - } - - public int getTimeRemove() { - return timeRemove; - } - - public boolean isRenderArmor() { - return renderArmor; - } - - public boolean isShowTags() { - return showTags; - } - - @Nullable - public BukkitTask getTickTask() { - return tickTask; - } - - @EventHandler - public void handleQuit(PlayerQuitEvent event) { - Player player = event.getPlayer(); - - this.corpseMap.values().stream() - .filter(corpse -> corpse.isShownFor(player)) - .forEach(corpse -> corpse.hide(player)); - } - - @EventHandler - public void handleRespawn(PlayerRespawnEvent event) { - Player player = event.getPlayer(); - - this.corpseMap.values().stream() - .filter(corpse -> corpse.isShownFor(player)) - .forEach(corpse -> corpse.hide(player)); - } - - - @EventHandler - public void handleDeath(PlayerDeathEvent event) { - event.setDeathMessage(null); - if(onDeath) { - new Corpse(event.getEntity()); - } - } - - @NotNull - public static synchronized CorpsePool getInstance() { - if(instance == null) { - instance = new CorpsePool(); - } - return instance; - } + } + }, 20, 2); + } + + @NotNull + public Optional getCorpse(int entityId) { + return Optional.ofNullable(this.corpseMap.get(entityId)); + } + + @NotNull + public Optional getCorpse(String name) { + return this.getCorpses() + .stream() + .filter(corpse -> corpse.getName().equals(name)) + .findFirst(); + } + + public void remove(int entityId) { + this.getCorpse(entityId).ifPresent(corpse -> { + this.corpseMap.remove(entityId); + corpse.getSeeingPlayers() + .forEach(corpse::hide); + }); + } + + public int getFreeEntityId() { + int id; + + do { + id = RANDOM.nextInt(Integer.MAX_VALUE); + } while (this.corpseMap.containsKey(id)); + + return id; + } + + @NotNull + public Collection getCorpses() { + return Collections.unmodifiableCollection(this.corpseMap.values()); + } + + public void takeCareOf(@NotNull Corpse corpse) { + // Prevent two corpses with same name and showTags is enabled + if (this.showTags) { + this.getCorpse(corpse.getName()).ifPresent(c -> { + this.corpseMap.remove(c.getId()); + c.getSeeingPlayers() + .forEach(c::hide); + }); + } + this.corpseMap.put(corpse.getId(), corpse); + } + + public int getTimeRemove() { + return timeRemove; + } + + public boolean isRenderArmor() { + return renderArmor; + } + + public boolean isShowTags() { + return showTags; + } + + @Nullable + public BukkitTask getTickTask() { + return tickTask; + } + + @EventHandler + public void handleQuit(PlayerQuitEvent event) { + Player player = event.getPlayer(); + + this.corpseMap.values().stream() + .filter(corpse -> corpse.isShownFor(player)) + .forEach(corpse -> corpse.hide(player)); + } + + @EventHandler + public void handleRespawn(PlayerRespawnEvent event) { + Player player = event.getPlayer(); + + this.corpseMap.values().stream() + .filter(corpse -> corpse.isShownFor(player)) + .forEach(corpse -> corpse.hide(player)); + } + + @EventHandler + public void handleDeath(PlayerDeathEvent event) { + //Fix player death message disappear +// event.setDeathMessage(null); + if (onDeath) { + new Corpse(event.getEntity()); + } + } } diff --git a/src/main/java/com/github/unldenis/corpse/util/BedUtil.java b/src/main/java/com/github/unldenis/corpse/util/BedUtil.java index d70181c..b52aab2 100644 --- a/src/main/java/com/github/unldenis/corpse/util/BedUtil.java +++ b/src/main/java/com/github/unldenis/corpse/util/BedUtil.java @@ -22,52 +22,46 @@ public class BedUtil { - /** - * Get bed location. - * @param location corpse's location - * @return fake bed location - */ - public static Location getBedLocation(Location location) { - Location loc = location.clone(); - loc.setY(1); - return loc; - } + /** + * Get bed location. + * + * @param location corpse's location + * @return fake bed location + */ + public static Location getBedLocation(Location location) { + Location loc = location.clone(); + loc.setY(1); + return loc; + } - /** - * Get corpse facing. - * @param yaw location yaw - * @return bed block facing - */ - public static int yawToFacing(float yaw) { - int facing = 2; - if(yaw >= -45 && yaw <= 45) { - facing = 0; - } - else if(yaw >= 45 && yaw <=135) { - facing = 1; - } - else if(yaw <= -45 && yaw >=-135) { - facing = 3; - } - else if(yaw <= -135 && yaw >=-225) { - facing = 2; - } - else if(yaw <= -225 && yaw >=-315) { - facing = 1; - } - else if(yaw >= 135 && yaw <= 225) { - facing = 2; - } - else if(yaw >= 225 && yaw <= 315) { - facing = 3; - } - else if (yaw >= 315) { - facing = 0; - } - else if (yaw <= -315) { - facing = 0; - } - return facing; + /** + * Get corpse facing. + * + * @param yaw location yaw + * @return bed block facing + */ + public static int yawToFacing(float yaw) { + int facing = 2; + if (yaw >= -45 && yaw <= 45) { + facing = 0; + } else if (yaw >= 45 && yaw <= 135) { + facing = 1; + } else if (yaw <= -45 && yaw >= -135) { + facing = 3; + } else if (yaw <= -135 && yaw >= -225) { + facing = 2; + } else if (yaw <= -225 && yaw >= -315) { + facing = 1; + } else if (yaw >= 135 && yaw <= 225) { + facing = 2; + } else if (yaw >= 225 && yaw <= 315) { + facing = 3; + } else if (yaw >= 315) { + facing = 0; + } else if (yaw <= -315) { + facing = 0; } + return facing; + } } diff --git a/src/main/java/com/github/unldenis/corpse/util/ProfileUtils.java b/src/main/java/com/github/unldenis/corpse/util/ProfileUtils.java index 438daaa..13f5c31 100644 --- a/src/main/java/com/github/unldenis/corpse/util/ProfileUtils.java +++ b/src/main/java/com/github/unldenis/corpse/util/ProfileUtils.java @@ -24,14 +24,15 @@ public class ProfileUtils { - /** - * Creates a random name which is exactly 16 chars long and only contains alphabetic and numeric - * chars. - * @return a randomly created minecraft name. - */ - @NotNull - public static String randomName() { - return UUID.randomUUID().toString().replace("-", "").substring(0, 16); - } + /** + * Creates a random name which is exactly 16 chars long and only contains alphabetic and numeric + * chars. + * + * @return a randomly created minecraft name. + */ + @NotNull + public static String randomName() { + return UUID.randomUUID().toString().replace("-", "").substring(0, 16); + } } diff --git a/src/main/java/com/github/unldenis/corpse/util/VersionUtil.java b/src/main/java/com/github/unldenis/corpse/util/VersionUtil.java index f688882..8d9f0e2 100644 --- a/src/main/java/com/github/unldenis/corpse/util/VersionUtil.java +++ b/src/main/java/com/github/unldenis/corpse/util/VersionUtil.java @@ -24,59 +24,59 @@ public class VersionUtil { - public static String VERSION; + public static String VERSION; - public static String CLEAN_VERSION; + public static String CLEAN_VERSION; - static { - String bpName = Bukkit.getServer().getClass().getPackage().getName(); - VERSION = bpName.substring(bpName.lastIndexOf(".") + 1); + static { + String bpName = Bukkit.getServer().getClass().getPackage().getName(); + VERSION = bpName.substring(bpName.lastIndexOf(".") + 1); - CLEAN_VERSION = VERSION.substring(0, VERSION.length() - 3); - } - - public static boolean isCompatible(@NotNull VersionEnum ve){ - return VERSION.toLowerCase().contains(ve.toString().toLowerCase()); - } + CLEAN_VERSION = VERSION.substring(0, VERSION.length() - 3); + } - public static boolean isAbove(@NotNull VersionEnum ve) { - return VersionEnum.valueOf(CLEAN_VERSION.toUpperCase()).getOrder() >= ve.getOrder(); - } + public static boolean isCompatible(@NotNull VersionEnum ve) { + return VERSION.toLowerCase().contains(ve.toString().toLowerCase()); + } - public static boolean isBelow(@NotNull VersionEnum ve) { - return VersionEnum.valueOf(CLEAN_VERSION.toUpperCase()).getOrder() <= ve.getOrder(); - } + public static boolean isAbove(@NotNull VersionEnum ve) { + return VersionEnum.valueOf(CLEAN_VERSION.toUpperCase()).getOrder() >= ve.getOrder(); + } - public static boolean isBetween(@NotNull VersionEnum ve1, @NotNull VersionEnum ve2) { - return isAbove(ve1) && isBelow(ve2); - } + public static boolean isBelow(@NotNull VersionEnum ve) { + return VersionEnum.valueOf(CLEAN_VERSION.toUpperCase()).getOrder() <= ve.getOrder(); + } + public static boolean isBetween(@NotNull VersionEnum ve1, @NotNull VersionEnum ve2) { + return isAbove(ve1) && isBelow(ve2); + } - public enum VersionEnum { - V1_8(1), - V1_9(2), - V1_10(3), - V1_11(4), - V1_12(5), - V1_13(6), - V1_14(7), - V1_15(8), - V1_16(9), - V1_17(10), - V1_18(11), - V1_19(12); + public enum VersionEnum { - private final int order; + V1_8(1), + V1_9(2), + V1_10(3), + V1_11(4), + V1_12(5), + V1_13(6), + V1_14(7), + V1_15(8), + V1_16(9), + V1_17(10), + V1_18(11), + V1_19(12); - VersionEnum(int order) { - this.order = order; - } + private final int order; - public int getOrder() { - return order; - } + VersionEnum(int order) { + this.order = order; + } + public int getOrder() { + return order; } + + } }