diff --git a/src/main/java/net/coreprotect/command/TeleportCommand.java b/src/main/java/net/coreprotect/command/TeleportCommand.java index e352326..fc4420a 100644 --- a/src/main/java/net/coreprotect/command/TeleportCommand.java +++ b/src/main/java/net/coreprotect/command/TeleportCommand.java @@ -11,6 +11,7 @@ import net.coreprotect.language.Phrase; import net.coreprotect.utility.Chat; import net.coreprotect.utility.ChatMessage; import net.coreprotect.utility.Color; +import net.coreprotect.utility.Teleport; import net.coreprotect.utility.Util; public class TeleportCommand { @@ -103,7 +104,7 @@ public class TeleportCommand { } // Teleport the player to a safe location - Util.performSafeTeleport(((Player) player), location, true); + Teleport.performSafeTeleport(((Player) player), location, true); ConfigHandler.teleportThrottle.put(player.getName(), new Object[] { false, System.currentTimeMillis() }); } diff --git a/src/main/java/net/coreprotect/consumer/process/EntitySpawnProcess.java b/src/main/java/net/coreprotect/consumer/process/EntitySpawnProcess.java index 8bb77b9..f9dbbe4 100644 --- a/src/main/java/net/coreprotect/consumer/process/EntitySpawnProcess.java +++ b/src/main/java/net/coreprotect/consumer/process/EntitySpawnProcess.java @@ -8,7 +8,7 @@ import org.bukkit.entity.EntityType; import net.coreprotect.config.ConfigHandler; import net.coreprotect.database.statement.EntityStatement; -import net.coreprotect.utility.Util; +import net.coreprotect.utility.entity.EntityUtil; class EntitySpawnProcess { @@ -18,7 +18,7 @@ class EntitySpawnProcess { EntityType type = (EntityType) ((Object[]) object)[1]; String query = "SELECT data FROM " + ConfigHandler.prefix + "entity WHERE rowid='" + rowId + "' LIMIT 0, 1"; List data = EntityStatement.getData(statement, block, query); - Util.spawnEntity(block, type, data); + EntityUtil.spawnEntity(block, type, data); } } } diff --git a/src/main/java/net/coreprotect/consumer/process/HangingRemoveProcess.java b/src/main/java/net/coreprotect/consumer/process/HangingRemoveProcess.java index 952a493..4bd1f51 100644 --- a/src/main/java/net/coreprotect/consumer/process/HangingRemoveProcess.java +++ b/src/main/java/net/coreprotect/consumer/process/HangingRemoveProcess.java @@ -2,14 +2,14 @@ package net.coreprotect.consumer.process; import org.bukkit.block.BlockState; -import net.coreprotect.utility.Util; +import net.coreprotect.utility.entity.HangingUtil; class HangingRemoveProcess { static void process(Object object, int delay) { if (object instanceof BlockState) { BlockState block = (BlockState) object; - Util.removeHanging(block, delay); + HangingUtil.removeHanging(block, delay); } } } diff --git a/src/main/java/net/coreprotect/consumer/process/HangingSpawnProcess.java b/src/main/java/net/coreprotect/consumer/process/HangingSpawnProcess.java index 56a7ce7..f32f7e5 100644 --- a/src/main/java/net/coreprotect/consumer/process/HangingSpawnProcess.java +++ b/src/main/java/net/coreprotect/consumer/process/HangingSpawnProcess.java @@ -3,14 +3,14 @@ package net.coreprotect.consumer.process; import org.bukkit.Material; import org.bukkit.block.BlockState; -import net.coreprotect.utility.Util; +import net.coreprotect.utility.entity.HangingUtil; class HangingSpawnProcess { static void process(Object object, Material type, int data, int delay) { if (object instanceof BlockState) { BlockState block = (BlockState) object; - Util.spawnHanging(block, type, data, delay); + HangingUtil.spawnHanging(block, type, data, delay); } } } diff --git a/src/main/java/net/coreprotect/database/Rollback.java b/src/main/java/net/coreprotect/database/Rollback.java index 1924bce..daa02ee 100644 --- a/src/main/java/net/coreprotect/database/Rollback.java +++ b/src/main/java/net/coreprotect/database/Rollback.java @@ -84,6 +84,7 @@ import net.coreprotect.thread.CacheHandler; import net.coreprotect.utility.Chat; import net.coreprotect.utility.ChestTool; import net.coreprotect.utility.Color; +import net.coreprotect.utility.Teleport; import net.coreprotect.utility.Util; public class Rollback extends Queue { @@ -1227,7 +1228,7 @@ public class Rollback extends Queue { int chunkZ = playerLocation.getBlockZ() >> 4; if (chunkX == finalChunkX && chunkZ == finalChunkZ) { - Util.performSafeTeleport(player, playerLocation, false); + Teleport.performSafeTeleport(player, playerLocation, false); } } } diff --git a/src/main/java/net/coreprotect/utility/Teleport.java b/src/main/java/net/coreprotect/utility/Teleport.java new file mode 100644 index 0000000..4595462 --- /dev/null +++ b/src/main/java/net/coreprotect/utility/Teleport.java @@ -0,0 +1,105 @@ +package net.coreprotect.utility; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; + +import net.coreprotect.language.Phrase; +import net.coreprotect.model.BlockGroup; + +public class Teleport { + + private Teleport() { + throw new IllegalStateException("Utility class"); + } + + public static void performSafeTeleport(Player player, Location location, boolean enforceTeleport) { + try { + Set unsafeBlocks = new HashSet<>(Arrays.asList(Material.LAVA)); + unsafeBlocks.addAll(BlockGroup.FIRE); + + int worldHeight = location.getWorld().getMaxHeight(); + int playerX = location.getBlockX(); + int playerY = location.getBlockY(); + int playerZ = location.getBlockZ(); + int checkY = playerY - 1; + boolean safeBlock = false; + boolean placeSafe = false; + boolean alert = false; + + while (!safeBlock) { + int above = checkY + 1; + if (above > worldHeight) { + above = worldHeight; + } + + Block block1 = location.getWorld().getBlockAt(playerX, checkY, playerZ); + Block block2 = location.getWorld().getBlockAt(playerX, above, playerZ); + Material type1 = block1.getType(); + Material type2 = block2.getType(); + + if (!Util.solidBlock(type1) && !Util.solidBlock(type2)) { + if (unsafeBlocks.contains(type1)) { + placeSafe = true; + } + else { + safeBlock = true; + if (placeSafe) { + int below = checkY - 1; + Block blockBelow = location.getWorld().getBlockAt(playerX, below, playerZ); + + if (checkY < worldHeight && unsafeBlocks.contains(blockBelow.getType())) { + alert = true; + block1.setType(Material.DIRT); + checkY++; + } + } + } + } + + if (checkY >= worldHeight || player.getGameMode() == GameMode.SPECTATOR) { + safeBlock = true; + + if (checkY < worldHeight) { + checkY++; + } + } + + if (safeBlock && (checkY > playerY || enforceTeleport)) { + if (checkY > worldHeight) { + checkY = worldHeight; + } + + double oldY = location.getY(); + location.setY(checkY); + player.teleport(location); + + if (!enforceTeleport) { + // Only send a message if the player was moved by at least 1 block + if (location.getY() >= (oldY + 1.00)) { + Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.TELEPORTED_SAFETY)); + } + } + else { + Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.TELEPORTED, "x" + playerX + "/y" + checkY + "/z" + playerZ + "/" + location.getWorld().getName())); + } + if (alert) { + Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + Color.ITALIC + "- " + Phrase.build(Phrase.DIRT_BLOCK)); + } + } + + checkY++; + } + } + catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/net/coreprotect/utility/Util.java b/src/main/java/net/coreprotect/utility/Util.java index 993fe2d..287f9bd 100755 --- a/src/main/java/net/coreprotect/utility/Util.java +++ b/src/main/java/net/coreprotect/utility/Util.java @@ -12,7 +12,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -22,19 +21,11 @@ import java.util.TreeSet; import org.bukkit.Art; import org.bukkit.Bukkit; -import org.bukkit.DyeColor; -import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.OfflinePlayer; import org.bukkit.World; -import org.bukkit.attribute.Attributable; -import org.bukkit.attribute.Attribute; -import org.bukkit.attribute.AttributeInstance; -import org.bukkit.attribute.AttributeModifier; import org.bukkit.block.Banner; import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; import org.bukkit.block.Chest; import org.bukkit.block.CommandBlock; @@ -46,50 +37,14 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.configuration.serialization.DelegateDeserialization; -import org.bukkit.entity.AbstractHorse; -import org.bukkit.entity.AbstractVillager; -import org.bukkit.entity.Ageable; -import org.bukkit.entity.Cat; -import org.bukkit.entity.ChestedHorse; -import org.bukkit.entity.Creeper; -import org.bukkit.entity.Enderman; -import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; -import org.bukkit.entity.Fox; -import org.bukkit.entity.Horse; -import org.bukkit.entity.Horse.Style; -import org.bukkit.entity.IronGolem; -import org.bukkit.entity.ItemFrame; import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Llama; -import org.bukkit.entity.MushroomCow; -import org.bukkit.entity.Painting; -import org.bukkit.entity.Panda; -import org.bukkit.entity.Panda.Gene; -import org.bukkit.entity.Parrot; -import org.bukkit.entity.Parrot.Variant; -import org.bukkit.entity.Phantom; -import org.bukkit.entity.Pig; import org.bukkit.entity.Player; -import org.bukkit.entity.Raider; -import org.bukkit.entity.Sheep; -import org.bukkit.entity.Slime; -import org.bukkit.entity.Spellcaster; -import org.bukkit.entity.Spellcaster.Spell; -import org.bukkit.entity.Tameable; -import org.bukkit.entity.TropicalFish; -import org.bukkit.entity.Villager; -import org.bukkit.entity.Villager.Profession; -import org.bukkit.entity.Wolf; -import org.bukkit.entity.Zombie; -import org.bukkit.entity.ZombieVillager; import org.bukkit.inventory.BlockInventoryHolder; import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.MerchantRecipe; import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.inventory.meta.LeatherArmorMeta; import org.bukkit.plugin.Plugin; import org.bukkit.util.io.BukkitObjectOutputStream; @@ -569,84 +524,6 @@ public class Util extends Queue { return artname; } - public static int getBlockFace(BlockFace rotation) { - switch (rotation) { - case NORTH: - return 0; - case NORTH_NORTH_EAST: - return 1; - case NORTH_EAST: - return 2; - case EAST_NORTH_EAST: - return 3; - case EAST: - return 4; - case EAST_SOUTH_EAST: - return 5; - case SOUTH_EAST: - return 6; - case SOUTH_SOUTH_EAST: - return 7; - case SOUTH: - return 8; - case SOUTH_SOUTH_WEST: - return 9; - case SOUTH_WEST: - return 10; - case WEST_SOUTH_WEST: - return 11; - case WEST: - return 12; - case WEST_NORTH_WEST: - return 13; - case NORTH_WEST: - return 14; - case NORTH_NORTH_WEST: - return 15; - default: - throw new IllegalArgumentException("Invalid BlockFace rotation: " + rotation); - } - } - - public static BlockFace getBlockFace(int rotation) { - switch (rotation) { - case 0: - return BlockFace.NORTH; - case 1: - return BlockFace.NORTH_NORTH_EAST; - case 2: - return BlockFace.NORTH_EAST; - case 3: - return BlockFace.EAST_NORTH_EAST; - case 4: - return BlockFace.EAST; - case 5: - return BlockFace.EAST_SOUTH_EAST; - case 6: - return BlockFace.SOUTH_EAST; - case 7: - return BlockFace.SOUTH_SOUTH_EAST; - case 8: - return BlockFace.SOUTH; - case 9: - return BlockFace.SOUTH_SOUTH_WEST; - case 10: - return BlockFace.SOUTH_WEST; - case 11: - return BlockFace.WEST_SOUTH_WEST; - case 12: - return BlockFace.WEST; - case 13: - return BlockFace.WEST_NORTH_WEST; - case 14: - return BlockFace.NORTH_WEST; - case 15: - return BlockFace.NORTH_NORTH_WEST; - default: - throw new AssertionError(rotation); - } - } - public static ItemStack[] getArmorStandContents(EntityEquipment equipment) { ItemStack[] contents = new ItemStack[6]; if (equipment != null) { @@ -1077,89 +954,6 @@ public class Util extends Queue { Chat.sendComponent(consoleSender, Color.RESET + "[CoreProtect] " + string + Chat.COMPONENT_TAG_OPEN + Chat.COMPONENT_POPUP + "| | " + Chat.COMPONENT_TAG_CLOSE); } - public static void performSafeTeleport(Player player, Location location, boolean enforceTeleport) { - try { - Set unsafeBlocks = new HashSet<>(Arrays.asList(Material.LAVA)); - unsafeBlocks.addAll(BlockGroup.FIRE); - - int worldHeight = location.getWorld().getMaxHeight(); - int playerX = location.getBlockX(); - int playerY = location.getBlockY(); - int playerZ = location.getBlockZ(); - int checkY = playerY - 1; - boolean safeBlock = false; - boolean placeSafe = false; - boolean alert = false; - - while (!safeBlock) { - int above = checkY + 1; - if (above > worldHeight) { - above = worldHeight; - } - - Block block1 = location.getWorld().getBlockAt(playerX, checkY, playerZ); - Block block2 = location.getWorld().getBlockAt(playerX, above, playerZ); - Material type1 = block1.getType(); - Material type2 = block2.getType(); - - if (!Util.solidBlock(type1) && !Util.solidBlock(type2)) { - if (unsafeBlocks.contains(type1)) { - placeSafe = true; - } - else { - safeBlock = true; - if (placeSafe) { - int below = checkY - 1; - Block blockBelow = location.getWorld().getBlockAt(playerX, below, playerZ); - - if (checkY < worldHeight && unsafeBlocks.contains(blockBelow.getType())) { - alert = true; - block1.setType(Material.DIRT); - checkY++; - } - } - } - } - - if (checkY >= worldHeight || player.getGameMode() == GameMode.SPECTATOR) { - safeBlock = true; - - if (checkY < worldHeight) { - checkY++; - } - } - - if (safeBlock && (checkY > playerY || enforceTeleport)) { - if (checkY > worldHeight) { - checkY = worldHeight; - } - - double oldY = location.getY(); - location.setY(checkY); - player.teleport(location); - - if (!enforceTeleport) { - // Only send a message if the player was moved by at least 1 block - if (location.getY() >= (oldY + 1.00)) { - Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.TELEPORTED_SAFETY)); - } - } - else { - Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.TELEPORTED, "x" + playerX + "/y" + checkY + "/z" + playerZ + "/" + location.getWorld().getName())); - } - if (alert) { - Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + Color.ITALIC + "- " + Phrase.build(Phrase.DIRT_BLOCK)); - } - } - - checkY++; - } - } - catch (Exception e) { - e.printStackTrace(); - } - } - public static String nameFilter(String name, int data) { if (name.equals("stone")) { switch (data) { @@ -1381,24 +1175,6 @@ public class Util extends Queue { return meta; } - public static void removeHanging(final BlockState block, int delay) { - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> { - try { - for (Entity e : block.getChunk().getEntities()) { - if (e instanceof ItemFrame || e instanceof Painting) { - Location el = e.getLocation(); - if (el.getBlockX() == block.getX() && el.getBlockY() == block.getY() && el.getBlockZ() == block.getZ()) { - e.remove(); - } - } - } - } - catch (Exception e) { - e.printStackTrace(); - } - }, delay); - } - public static void sendBlockChange(Player player, Location location, BlockData blockData) { player.sendBlockChange(location, blockData); } @@ -1428,607 +1204,6 @@ public class Util extends Queue { block.setBlockData(blockData, update); } - public static void spawnEntity(final BlockState block, final EntityType type, final List list) { - if (type == null) { - return; - } - Bukkit.getServer().getScheduler().runTask(CoreProtect.getInstance(), () -> { - try { - Location location = block.getLocation(); - location.setX(location.getX() + 0.50); - location.setZ(location.getZ() + 0.50); - Entity entity = block.getLocation().getWorld().spawnEntity(location, type); - - if (list.isEmpty()) { - return; - } - - @SuppressWarnings("unchecked") - List age = (List) list.get(0); - @SuppressWarnings("unchecked") - List tame = (List) list.get(1); - @SuppressWarnings("unchecked") - List data = (List) list.get(2); - - if (list.size() >= 5) { - entity.setCustomNameVisible((Boolean) list.get(3)); - entity.setCustomName((String) list.get(4)); - } - - int unixtimestamp = (int) (System.currentTimeMillis() / 1000L); - int wid = Util.getWorldId(block.getWorld().getName()); - String token = "" + block.getX() + "." + block.getY() + "." + block.getZ() + "." + wid + "." + type.name() + ""; - CacheHandler.entityCache.put(token, new Object[] { unixtimestamp, entity.getEntityId() }); - - if (entity instanceof Ageable) { - int count = 0; - Ageable ageable = (Ageable) entity; - for (Object value : age) { - if (count == 0) { - int set = (Integer) value; - ageable.setAge(set); - } - else if (count == 1) { - boolean set = (Boolean) value; - ageable.setAgeLock(set); - } - else if (count == 2) { - boolean set = (Boolean) value; - if (set) { - ageable.setAdult(); - } - else { - ageable.setBaby(); - } - } - else if (count == 3) { - boolean set = (Boolean) value; - ageable.setBreed(set); - } - else if (count == 4 && value != null) { - // deprecated - double set = (Double) value; - ageable.setMaxHealth(set); - } - count++; - } - } - if (entity instanceof Tameable) { - int count = 0; - Tameable tameable = (Tameable) entity; - for (Object value : tame) { - if (count == 0) { - boolean set = (Boolean) value; - tameable.setTamed(set); - } - else if (count == 1) { - String set = (String) value; - if (set.length() > 0) { - Player owner = Bukkit.getServer().getPlayer(set); - if (owner == null) { - OfflinePlayer offlinePlayer = Bukkit.getServer().getOfflinePlayer(set); - if (offlinePlayer != null) { - tameable.setOwner(offlinePlayer); - } - } - else { - tameable.setOwner(owner); - } - } - } - count++; - } - } - if (entity instanceof Attributable && list.size() >= 6) { - Attributable attributable = (Attributable) entity; - @SuppressWarnings("unchecked") - List attributes = (List) list.get(5); - for (Object value : attributes) { - @SuppressWarnings("unchecked") - List attributeData = (List) value; - Attribute attribute = (Attribute) attributeData.get(0); - Double baseValue = (Double) attributeData.get(1); - @SuppressWarnings("unchecked") - List attributeModifiers = (List) attributeData.get(2); - - AttributeInstance entityAttribute = attributable.getAttribute(attribute); - if (entityAttribute != null) { - entityAttribute.setBaseValue(baseValue); - for (AttributeModifier modifier : entityAttribute.getModifiers()) { - entityAttribute.removeModifier(modifier); - } - for (Object modifier : attributeModifiers) { - @SuppressWarnings("unchecked") - Map serializedModifier = (Map) modifier; - entityAttribute.addModifier(AttributeModifier.deserialize(serializedModifier)); - } - } - } - } - - if (entity instanceof LivingEntity && list.size() >= 7) { - LivingEntity livingEntity = (LivingEntity) entity; - @SuppressWarnings("unchecked") - List details = (List) list.get(6); - int count = 0; - for (Object value : details) { - if (count == 0) { - boolean set = (Boolean) value; - livingEntity.setRemoveWhenFarAway(set); - } - else if (count == 1) { - boolean set = (Boolean) value; - livingEntity.setCanPickupItems(set); - } - count++; - } - } - - int count = 0; - for (Object value : data) { - if (entity instanceof Creeper) { - Creeper creeper = (Creeper) entity; - if (count == 0) { - boolean set = (Boolean) value; - creeper.setPowered(set); - } - } - else if (entity instanceof Enderman) { - Enderman enderman = (Enderman) entity; - if (count == 1) { - String blockDataString = (String) value; - BlockData blockData = Bukkit.getServer().createBlockData(blockDataString); - enderman.setCarriedBlock(blockData); - } - } - else if (entity instanceof IronGolem) { - IronGolem irongolem = (IronGolem) entity; - if (count == 0) { - boolean set = (Boolean) value; - irongolem.setPlayerCreated(set); - } - } - else if (entity instanceof Cat) { - Cat cat = (Cat) entity; - if (count == 0) { - Cat.Type set = (Cat.Type) value; - cat.setCatType(set); - } - else if (count == 1) { - DyeColor set = (DyeColor) value; - cat.setCollarColor(set); - } - } - else if (entity instanceof Fox) { - Fox fox = (Fox) entity; - if (count == 0) { - Fox.Type set = (Fox.Type) value; - fox.setFoxType(set); - } - else if (count == 1) { - boolean set = (Boolean) value; - fox.setSitting(set); - } - } - else if (entity instanceof Panda) { - Panda panda = (Panda) entity; - if (count == 0) { - Gene set = (Gene) value; - panda.setMainGene(set); - } - else if (count == 1) { - Gene set = (Gene) value; - panda.setHiddenGene(set); - } - } - else if (entity instanceof Pig) { - Pig pig = (Pig) entity; - if (count == 0) { - boolean set = (Boolean) value; - pig.setSaddle(set); - } - } - else if (entity instanceof Sheep) { - Sheep sheep = (Sheep) entity; - if (count == 0) { - boolean set = (Boolean) value; - sheep.setSheared(set); - } - else if (count == 1) { - DyeColor set = (DyeColor) value; - sheep.setColor(set); - } - } - else if (entity instanceof MushroomCow) { - MushroomCow mushroomCow = (MushroomCow) entity; - if (count == 0) { - MushroomCow.Variant set = (MushroomCow.Variant) value; - mushroomCow.setVariant(set); - } - } - else if (entity instanceof Slime) { - Slime slime = (Slime) entity; - if (count == 0) { - int set = (Integer) value; - slime.setSize(set); - } - } - else if (entity instanceof Parrot) { - Parrot parrot = (Parrot) entity; - if (count == 0) { - Variant set = (Variant) value; - parrot.setVariant(set); - } - } - else if (entity instanceof TropicalFish) { - TropicalFish tropicalFish = (TropicalFish) entity; - if (count == 0) { - DyeColor set = (DyeColor) value; - tropicalFish.setBodyColor(set); - } - else if (count == 1) { - TropicalFish.Pattern set = (TropicalFish.Pattern) value; - tropicalFish.setPattern(set); - } - else if (count == 2) { - DyeColor set = (DyeColor) value; - tropicalFish.setPatternColor(set); - } - } - else if (entity instanceof Phantom) { - Phantom phantom = (Phantom) entity; - if (count == 0) { - int set = (Integer) value; - phantom.setSize(set); - } - } - else if (entity instanceof AbstractVillager) { - AbstractVillager abstractVillager = (AbstractVillager) entity; - if (count == 0) { - if (abstractVillager instanceof Villager) { - Villager villager = (Villager) abstractVillager; - Profession set = (Profession) value; - villager.setProfession(set); - } - } - else if (count == 1) { - if (abstractVillager instanceof Villager && value instanceof Villager.Type) { - Villager villager = (Villager) abstractVillager; - Villager.Type set = (Villager.Type) value; - villager.setVillagerType(set); - } - } - else if (count == 2) { - List merchantRecipes = new ArrayList<>(); - @SuppressWarnings("unchecked") - List set = (List) value; - for (Object recipes : set) { - @SuppressWarnings("unchecked") - List recipe = (List) recipes; - @SuppressWarnings("unchecked") - List itemMap = (List) recipe.get(0); - @SuppressWarnings("unchecked") - ItemStack result = ItemStack.deserialize((Map) itemMap.get(0)); - @SuppressWarnings("unchecked") - List>> metadata = (List>>) itemMap.get(1); - Object[] populatedStack = Rollback.populateItemStack(result, metadata); - result = (ItemStack) populatedStack[1]; - int uses = (int) recipe.get(1); - int maxUses = (int) recipe.get(2); - boolean experienceReward = (boolean) recipe.get(3); - List merchantIngredients = new ArrayList<>(); - @SuppressWarnings("unchecked") - List ingredients = (List) recipe.get(4); - for (Object ingredient : ingredients) { - @SuppressWarnings("unchecked") - List ingredientMap = (List) ingredient; - @SuppressWarnings("unchecked") - ItemStack item = ItemStack.deserialize((Map) ingredientMap.get(0)); - @SuppressWarnings("unchecked") - List>> itemMetaData = (List>>) ingredientMap.get(1); - populatedStack = Rollback.populateItemStack(item, itemMetaData); - item = (ItemStack) populatedStack[1]; - merchantIngredients.add(item); - } - MerchantRecipe merchantRecipe = new MerchantRecipe(result, uses, maxUses, experienceReward); - if (recipe.size() > 6) { - int villagerExperience = (int) recipe.get(5); - float priceMultiplier = (float) recipe.get(6); - merchantRecipe = new MerchantRecipe(result, uses, maxUses, experienceReward, villagerExperience, priceMultiplier); - } - merchantRecipe.setIngredients(merchantIngredients); - merchantRecipes.add(merchantRecipe); - } - if (!merchantRecipes.isEmpty()) { - abstractVillager.setRecipes(merchantRecipes); - } - } - else { - Villager villager = (Villager) abstractVillager; - - if (count == 3) { - int set = (int) value; - villager.setVillagerLevel(set); - } - else if (count == 4) { - int set = (int) value; - villager.setVillagerExperience(set); - } - } - } - else if (entity instanceof Raider) { - Raider raider = (Raider) entity; - if (count == 0) { - boolean set = (Boolean) value; - raider.setPatrolLeader(set); - } - - if (entity instanceof Spellcaster && count == 1) { - Spellcaster spellcaster = (Spellcaster) entity; - Spell set = (Spell) value; - spellcaster.setSpell(set); - } - } - else if (entity instanceof Wolf) { - Wolf wolf = (Wolf) entity; - if (count == 0) { - boolean set = (Boolean) value; - wolf.setSitting(set); - } - else if (count == 1) { - DyeColor set = (DyeColor) value; - wolf.setCollarColor(set); - } - } - else if (entity instanceof ZombieVillager) { - ZombieVillager zombieVillager = (ZombieVillager) entity; - if (count == 0) { - boolean set = (Boolean) value; - zombieVillager.setBaby(set); - } - else if (count == 1) { - Profession set = (Profession) value; - zombieVillager.setVillagerProfession(set); - } - } - else if (entity instanceof Zombie) { - Zombie zombie = (Zombie) entity; - if (count == 0) { - boolean set = (Boolean) value; - zombie.setBaby(set); - } - } - else if (entity instanceof AbstractHorse) { - AbstractHorse abstractHorse = (AbstractHorse) entity; - if (count == 0 && value != null) { - // deprecated - boolean set = (Boolean) value; - if (entity instanceof ChestedHorse) { - ChestedHorse chestedHorse = (ChestedHorse) entity; - chestedHorse.setCarryingChest(set); - } - } - else if (count == 1 && value != null) { - // deprecated - org.bukkit.entity.Horse.Color set = (org.bukkit.entity.Horse.Color) value; - if (entity instanceof Horse) { - Horse horse = (Horse) entity; - horse.setColor(set); - } - } - else if (count == 2) { - int set = (Integer) value; - abstractHorse.setDomestication(set); - } - else if (count == 3) { - double set = (Double) value; - abstractHorse.setJumpStrength(set); - } - else if (count == 4) { - int set = (Integer) value; - abstractHorse.setMaxDomestication(set); - } - else if (count == 5 && value != null) { - // deprecated - Style set = (Style) value; - Horse horse = (Horse) entity; - horse.setStyle(set); - } - if (entity instanceof Horse) { - Horse horse = (Horse) entity; - if (count == 8) { - if (value != null) { - @SuppressWarnings("unchecked") - ItemStack set = ItemStack.deserialize((Map) value); - horse.getInventory().setSaddle(set); - } - } - else if (count == 9) { - org.bukkit.entity.Horse.Color set = (org.bukkit.entity.Horse.Color) value; - horse.setColor(set); - } - else if (count == 10) { - Style set = (Style) value; - horse.setStyle(set); - } - else if (count == 11) { - if (value != null) { - @SuppressWarnings("unchecked") - ItemStack set = ItemStack.deserialize((Map) value); - horse.getInventory().setArmor(set); - } - } - else if (count == 12 && value != null) { - @SuppressWarnings("unchecked") - org.bukkit.Color set = org.bukkit.Color.deserialize((Map) value); - ItemStack armor = horse.getInventory().getArmor(); - if (armor != null) { - ItemMeta itemMeta = armor.getItemMeta(); - if (itemMeta instanceof LeatherArmorMeta) { - LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemMeta; - leatherArmorMeta.setColor(set); - armor.setItemMeta(leatherArmorMeta); - } - } - } - } - else if (entity instanceof ChestedHorse) { - if (count == 7) { - ChestedHorse chestedHorse = (ChestedHorse) entity; - boolean set = (Boolean) value; - chestedHorse.setCarryingChest(set); - } - if (entity instanceof Llama) { - Llama llama = (Llama) entity; - if (count == 8) { - if (value != null) { - @SuppressWarnings("unchecked") - ItemStack set = ItemStack.deserialize((Map) value); - llama.getInventory().setDecor(set); - } - } - else if (count == 9) { - Llama.Color set = (Llama.Color) value; - llama.setColor(set); - } - } - } - } - else { - BukkitAdapter.ADAPTER.setEntityMeta(entity, value, count); - } - count++; - } - } - catch (Exception e) { - e.printStackTrace(); - } - }); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - public static void spawnHanging(final BlockState blockstate, final Material rowType, final int rowData, int delay) { - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> { - try { - Block block = blockstate.getBlock(); - int x = block.getX(); - int y = block.getY(); - int z = block.getZ(); - - for (Entity e : block.getChunk().getEntities()) { - if ((BukkitAdapter.ADAPTER.isItemFrame(rowType) && e instanceof ItemFrame) || (rowType.equals(Material.PAINTING) && e instanceof Painting)) { - Location el = e.getLocation(); - if (el.getBlockX() == x && el.getBlockY() == y && el.getBlockZ() == z) { - e.remove(); - break; - } - } - } - - Block c1 = block.getWorld().getBlockAt((x + 1), y, z); - Block c2 = block.getWorld().getBlockAt((x - 1), y, z); - Block c3 = block.getWorld().getBlockAt(x, y, (z + 1)); - Block c4 = block.getWorld().getBlockAt(x, y, (z - 1)); - - BlockFace faceSet = null; - if (!BlockGroup.NON_ATTACHABLE.contains(c1.getType())) { - faceSet = BlockFace.WEST; - block = c1; - } - else if (!BlockGroup.NON_ATTACHABLE.contains(c2.getType())) { - faceSet = BlockFace.EAST; - block = c2; - } - else if (!BlockGroup.NON_ATTACHABLE.contains(c3.getType())) { - faceSet = BlockFace.NORTH; - block = c3; - } - else if (!BlockGroup.NON_ATTACHABLE.contains(c4.getType())) { - faceSet = BlockFace.SOUTH; - block = c4; - } - - BlockFace face = null; - if (!solidBlock(Util.getType(block.getRelative(BlockFace.EAST)))) { - face = BlockFace.EAST; - } - else if (!solidBlock(Util.getType(block.getRelative(BlockFace.NORTH)))) { - face = BlockFace.NORTH; - } - else if (!solidBlock(Util.getType(block.getRelative(BlockFace.WEST)))) { - face = BlockFace.WEST; - } - else if (!solidBlock(Util.getType(block.getRelative(BlockFace.SOUTH)))) { - face = BlockFace.SOUTH; - } - - if (faceSet != null && face != null) { - if (rowType.equals(Material.PAINTING)) { - String name = Util.getArtName(rowData); - Art painting = Art.getByName(name.toUpperCase(Locale.ROOT)); - int height = painting.getBlockHeight(); - int width = painting.getBlockWidth(); - int paintingX = x; - int paintingY = y; - int paintingZ = z; - if (height != 1 || width != 1) { - if (height > 1) { - if (height != 3) { - paintingY = paintingY - 1; - } - } - if (width > 1) { - if (faceSet.equals(BlockFace.WEST)) { - paintingZ--; - } - else if (faceSet.equals(BlockFace.SOUTH)) { - paintingX--; - } - } - } - Block spawnBlock = block.getRelative(face); - Util.setTypeAndData(spawnBlock, Material.AIR, null, true); - Painting hanging = null; - try { - hanging = block.getWorld().spawn(spawnBlock.getLocation(), Painting.class); - } - catch (Exception e) { - } - if (hanging != null) { - hanging.teleport(block.getWorld().getBlockAt(paintingX, paintingY, paintingZ).getLocation()); - hanging.setFacingDirection(faceSet, true); - hanging.setArt(painting, true); - } - } - else if (BukkitAdapter.ADAPTER.isItemFrame(rowType)) { - try { - Block spawnBlock = block.getRelative(face); - Util.setTypeAndData(spawnBlock, Material.AIR, null, true); - Class itemFrame = BukkitAdapter.ADAPTER.getFrameClass(rowType); - Entity entity = block.getWorld().spawn(spawnBlock.getLocation(), itemFrame); - if (entity instanceof ItemFrame) { - ItemFrame hanging = (ItemFrame) entity; - hanging.teleport(block.getWorld().getBlockAt(x, y, z).getLocation()); - hanging.setFacingDirection(faceSet, true); - - Material type = Util.getType(rowData); - if (type != null) { - ItemStack istack = new ItemStack(type, 1); - hanging.setItem(istack); - } - } - } - catch (Exception e) { - } - } - } - } - catch (Exception e) { - e.printStackTrace(); - } - }, delay); - } - public static boolean successfulQuery(Connection connection, String query) { boolean result = false; try { diff --git a/src/main/java/net/coreprotect/utility/entity/EntityUtil.java b/src/main/java/net/coreprotect/utility/entity/EntityUtil.java new file mode 100644 index 0000000..cf23678 --- /dev/null +++ b/src/main/java/net/coreprotect/utility/entity/EntityUtil.java @@ -0,0 +1,547 @@ +package net.coreprotect.utility.entity; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.DyeColor; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.attribute.Attributable; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; +import org.bukkit.attribute.AttributeModifier; +import org.bukkit.block.BlockState; +import org.bukkit.block.data.BlockData; +import org.bukkit.entity.AbstractHorse; +import org.bukkit.entity.AbstractVillager; +import org.bukkit.entity.Ageable; +import org.bukkit.entity.Cat; +import org.bukkit.entity.ChestedHorse; +import org.bukkit.entity.Creeper; +import org.bukkit.entity.Enderman; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Fox; +import org.bukkit.entity.Horse; +import org.bukkit.entity.Horse.Style; +import org.bukkit.entity.IronGolem; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Llama; +import org.bukkit.entity.MushroomCow; +import org.bukkit.entity.Panda; +import org.bukkit.entity.Panda.Gene; +import org.bukkit.entity.Parrot; +import org.bukkit.entity.Parrot.Variant; +import org.bukkit.entity.Phantom; +import org.bukkit.entity.Pig; +import org.bukkit.entity.Player; +import org.bukkit.entity.Raider; +import org.bukkit.entity.Sheep; +import org.bukkit.entity.Slime; +import org.bukkit.entity.Spellcaster; +import org.bukkit.entity.Spellcaster.Spell; +import org.bukkit.entity.Tameable; +import org.bukkit.entity.TropicalFish; +import org.bukkit.entity.Villager; +import org.bukkit.entity.Villager.Profession; +import org.bukkit.entity.Wolf; +import org.bukkit.entity.Zombie; +import org.bukkit.entity.ZombieVillager; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.MerchantRecipe; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.LeatherArmorMeta; + +import net.coreprotect.CoreProtect; +import net.coreprotect.bukkit.BukkitAdapter; +import net.coreprotect.database.Rollback; +import net.coreprotect.thread.CacheHandler; +import net.coreprotect.utility.Util; + +public class EntityUtil { + + private EntityUtil() { + throw new IllegalStateException("Utility class"); + } + + public static void spawnEntity(final BlockState block, final EntityType type, final List list) { + if (type == null) { + return; + } + Bukkit.getServer().getScheduler().runTask(CoreProtect.getInstance(), () -> { + try { + Location location = block.getLocation(); + location.setX(location.getX() + 0.50); + location.setZ(location.getZ() + 0.50); + Entity entity = block.getLocation().getWorld().spawnEntity(location, type); + + if (list.isEmpty()) { + return; + } + + @SuppressWarnings("unchecked") + List age = (List) list.get(0); + @SuppressWarnings("unchecked") + List tame = (List) list.get(1); + @SuppressWarnings("unchecked") + List data = (List) list.get(2); + + if (list.size() >= 5) { + entity.setCustomNameVisible((Boolean) list.get(3)); + entity.setCustomName((String) list.get(4)); + } + + int unixtimestamp = (int) (System.currentTimeMillis() / 1000L); + int wid = Util.getWorldId(block.getWorld().getName()); + String token = "" + block.getX() + "." + block.getY() + "." + block.getZ() + "." + wid + "." + type.name() + ""; + CacheHandler.entityCache.put(token, new Object[] { unixtimestamp, entity.getEntityId() }); + + if (entity instanceof Ageable) { + int count = 0; + Ageable ageable = (Ageable) entity; + for (Object value : age) { + if (count == 0) { + int set = (Integer) value; + ageable.setAge(set); + } + else if (count == 1) { + boolean set = (Boolean) value; + ageable.setAgeLock(set); + } + else if (count == 2) { + boolean set = (Boolean) value; + if (set) { + ageable.setAdult(); + } + else { + ageable.setBaby(); + } + } + else if (count == 3) { + boolean set = (Boolean) value; + ageable.setBreed(set); + } + else if (count == 4 && value != null) { + // deprecated + double set = (Double) value; + ageable.setMaxHealth(set); + } + count++; + } + } + if (entity instanceof Tameable) { + int count = 0; + Tameable tameable = (Tameable) entity; + for (Object value : tame) { + if (count == 0) { + boolean set = (Boolean) value; + tameable.setTamed(set); + } + else if (count == 1) { + String set = (String) value; + if (set.length() > 0) { + Player owner = Bukkit.getServer().getPlayer(set); + if (owner == null) { + OfflinePlayer offlinePlayer = Bukkit.getServer().getOfflinePlayer(set); + if (offlinePlayer != null) { + tameable.setOwner(offlinePlayer); + } + } + else { + tameable.setOwner(owner); + } + } + } + count++; + } + } + if (entity instanceof Attributable && list.size() >= 6) { + Attributable attributable = (Attributable) entity; + @SuppressWarnings("unchecked") + List attributes = (List) list.get(5); + for (Object value : attributes) { + @SuppressWarnings("unchecked") + List attributeData = (List) value; + Attribute attribute = (Attribute) attributeData.get(0); + Double baseValue = (Double) attributeData.get(1); + @SuppressWarnings("unchecked") + List attributeModifiers = (List) attributeData.get(2); + + AttributeInstance entityAttribute = attributable.getAttribute(attribute); + if (entityAttribute != null) { + entityAttribute.setBaseValue(baseValue); + for (AttributeModifier modifier : entityAttribute.getModifiers()) { + entityAttribute.removeModifier(modifier); + } + for (Object modifier : attributeModifiers) { + @SuppressWarnings("unchecked") + Map serializedModifier = (Map) modifier; + entityAttribute.addModifier(AttributeModifier.deserialize(serializedModifier)); + } + } + } + } + + if (entity instanceof LivingEntity && list.size() >= 7) { + LivingEntity livingEntity = (LivingEntity) entity; + @SuppressWarnings("unchecked") + List details = (List) list.get(6); + int count = 0; + for (Object value : details) { + if (count == 0) { + boolean set = (Boolean) value; + livingEntity.setRemoveWhenFarAway(set); + } + else if (count == 1) { + boolean set = (Boolean) value; + livingEntity.setCanPickupItems(set); + } + count++; + } + } + + int count = 0; + for (Object value : data) { + if (entity instanceof Creeper) { + Creeper creeper = (Creeper) entity; + if (count == 0) { + boolean set = (Boolean) value; + creeper.setPowered(set); + } + } + else if (entity instanceof Enderman) { + Enderman enderman = (Enderman) entity; + if (count == 1) { + String blockDataString = (String) value; + BlockData blockData = Bukkit.getServer().createBlockData(blockDataString); + enderman.setCarriedBlock(blockData); + } + } + else if (entity instanceof IronGolem) { + IronGolem irongolem = (IronGolem) entity; + if (count == 0) { + boolean set = (Boolean) value; + irongolem.setPlayerCreated(set); + } + } + else if (entity instanceof Cat) { + Cat cat = (Cat) entity; + if (count == 0) { + Cat.Type set = (Cat.Type) value; + cat.setCatType(set); + } + else if (count == 1) { + DyeColor set = (DyeColor) value; + cat.setCollarColor(set); + } + } + else if (entity instanceof Fox) { + Fox fox = (Fox) entity; + if (count == 0) { + Fox.Type set = (Fox.Type) value; + fox.setFoxType(set); + } + else if (count == 1) { + boolean set = (Boolean) value; + fox.setSitting(set); + } + } + else if (entity instanceof Panda) { + Panda panda = (Panda) entity; + if (count == 0) { + Gene set = (Gene) value; + panda.setMainGene(set); + } + else if (count == 1) { + Gene set = (Gene) value; + panda.setHiddenGene(set); + } + } + else if (entity instanceof Pig) { + Pig pig = (Pig) entity; + if (count == 0) { + boolean set = (Boolean) value; + pig.setSaddle(set); + } + } + else if (entity instanceof Sheep) { + Sheep sheep = (Sheep) entity; + if (count == 0) { + boolean set = (Boolean) value; + sheep.setSheared(set); + } + else if (count == 1) { + DyeColor set = (DyeColor) value; + sheep.setColor(set); + } + } + else if (entity instanceof MushroomCow) { + MushroomCow mushroomCow = (MushroomCow) entity; + if (count == 0) { + MushroomCow.Variant set = (MushroomCow.Variant) value; + mushroomCow.setVariant(set); + } + } + else if (entity instanceof Slime) { + Slime slime = (Slime) entity; + if (count == 0) { + int set = (Integer) value; + slime.setSize(set); + } + } + else if (entity instanceof Parrot) { + Parrot parrot = (Parrot) entity; + if (count == 0) { + Variant set = (Variant) value; + parrot.setVariant(set); + } + } + else if (entity instanceof TropicalFish) { + TropicalFish tropicalFish = (TropicalFish) entity; + if (count == 0) { + DyeColor set = (DyeColor) value; + tropicalFish.setBodyColor(set); + } + else if (count == 1) { + TropicalFish.Pattern set = (TropicalFish.Pattern) value; + tropicalFish.setPattern(set); + } + else if (count == 2) { + DyeColor set = (DyeColor) value; + tropicalFish.setPatternColor(set); + } + } + else if (entity instanceof Phantom) { + Phantom phantom = (Phantom) entity; + if (count == 0) { + int set = (Integer) value; + phantom.setSize(set); + } + } + else if (entity instanceof AbstractVillager) { + AbstractVillager abstractVillager = (AbstractVillager) entity; + if (count == 0) { + if (abstractVillager instanceof Villager) { + Villager villager = (Villager) abstractVillager; + Profession set = (Profession) value; + villager.setProfession(set); + } + } + else if (count == 1) { + if (abstractVillager instanceof Villager && value instanceof Villager.Type) { + Villager villager = (Villager) abstractVillager; + Villager.Type set = (Villager.Type) value; + villager.setVillagerType(set); + } + } + else if (count == 2) { + List merchantRecipes = new ArrayList<>(); + @SuppressWarnings("unchecked") + List set = (List) value; + for (Object recipes : set) { + @SuppressWarnings("unchecked") + List recipe = (List) recipes; + @SuppressWarnings("unchecked") + List itemMap = (List) recipe.get(0); + @SuppressWarnings("unchecked") + ItemStack result = ItemStack.deserialize((Map) itemMap.get(0)); + @SuppressWarnings("unchecked") + List>> metadata = (List>>) itemMap.get(1); + Object[] populatedStack = Rollback.populateItemStack(result, metadata); + result = (ItemStack) populatedStack[1]; + int uses = (int) recipe.get(1); + int maxUses = (int) recipe.get(2); + boolean experienceReward = (boolean) recipe.get(3); + List merchantIngredients = new ArrayList<>(); + @SuppressWarnings("unchecked") + List ingredients = (List) recipe.get(4); + for (Object ingredient : ingredients) { + @SuppressWarnings("unchecked") + List ingredientMap = (List) ingredient; + @SuppressWarnings("unchecked") + ItemStack item = ItemStack.deserialize((Map) ingredientMap.get(0)); + @SuppressWarnings("unchecked") + List>> itemMetaData = (List>>) ingredientMap.get(1); + populatedStack = Rollback.populateItemStack(item, itemMetaData); + item = (ItemStack) populatedStack[1]; + merchantIngredients.add(item); + } + MerchantRecipe merchantRecipe = new MerchantRecipe(result, uses, maxUses, experienceReward); + if (recipe.size() > 6) { + int villagerExperience = (int) recipe.get(5); + float priceMultiplier = (float) recipe.get(6); + merchantRecipe = new MerchantRecipe(result, uses, maxUses, experienceReward, villagerExperience, priceMultiplier); + } + merchantRecipe.setIngredients(merchantIngredients); + merchantRecipes.add(merchantRecipe); + } + if (!merchantRecipes.isEmpty()) { + abstractVillager.setRecipes(merchantRecipes); + } + } + else { + Villager villager = (Villager) abstractVillager; + + if (count == 3) { + int set = (int) value; + villager.setVillagerLevel(set); + } + else if (count == 4) { + int set = (int) value; + villager.setVillagerExperience(set); + } + } + } + else if (entity instanceof Raider) { + Raider raider = (Raider) entity; + if (count == 0) { + boolean set = (Boolean) value; + raider.setPatrolLeader(set); + } + + if (entity instanceof Spellcaster && count == 1) { + Spellcaster spellcaster = (Spellcaster) entity; + Spell set = (Spell) value; + spellcaster.setSpell(set); + } + } + else if (entity instanceof Wolf) { + Wolf wolf = (Wolf) entity; + if (count == 0) { + boolean set = (Boolean) value; + wolf.setSitting(set); + } + else if (count == 1) { + DyeColor set = (DyeColor) value; + wolf.setCollarColor(set); + } + } + else if (entity instanceof ZombieVillager) { + ZombieVillager zombieVillager = (ZombieVillager) entity; + if (count == 0) { + boolean set = (Boolean) value; + zombieVillager.setBaby(set); + } + else if (count == 1) { + Profession set = (Profession) value; + zombieVillager.setVillagerProfession(set); + } + } + else if (entity instanceof Zombie) { + Zombie zombie = (Zombie) entity; + if (count == 0) { + boolean set = (Boolean) value; + zombie.setBaby(set); + } + } + else if (entity instanceof AbstractHorse) { + AbstractHorse abstractHorse = (AbstractHorse) entity; + if (count == 0 && value != null) { + // deprecated + boolean set = (Boolean) value; + if (entity instanceof ChestedHorse) { + ChestedHorse chestedHorse = (ChestedHorse) entity; + chestedHorse.setCarryingChest(set); + } + } + else if (count == 1 && value != null) { + // deprecated + org.bukkit.entity.Horse.Color set = (org.bukkit.entity.Horse.Color) value; + if (entity instanceof Horse) { + Horse horse = (Horse) entity; + horse.setColor(set); + } + } + else if (count == 2) { + int set = (Integer) value; + abstractHorse.setDomestication(set); + } + else if (count == 3) { + double set = (Double) value; + abstractHorse.setJumpStrength(set); + } + else if (count == 4) { + int set = (Integer) value; + abstractHorse.setMaxDomestication(set); + } + else if (count == 5 && value != null) { + // deprecated + Style set = (Style) value; + Horse horse = (Horse) entity; + horse.setStyle(set); + } + if (entity instanceof Horse) { + Horse horse = (Horse) entity; + if (count == 8) { + if (value != null) { + @SuppressWarnings("unchecked") + ItemStack set = ItemStack.deserialize((Map) value); + horse.getInventory().setSaddle(set); + } + } + else if (count == 9) { + org.bukkit.entity.Horse.Color set = (org.bukkit.entity.Horse.Color) value; + horse.setColor(set); + } + else if (count == 10) { + Style set = (Style) value; + horse.setStyle(set); + } + else if (count == 11) { + if (value != null) { + @SuppressWarnings("unchecked") + ItemStack set = ItemStack.deserialize((Map) value); + horse.getInventory().setArmor(set); + } + } + else if (count == 12 && value != null) { + @SuppressWarnings("unchecked") + org.bukkit.Color set = org.bukkit.Color.deserialize((Map) value); + ItemStack armor = horse.getInventory().getArmor(); + if (armor != null) { + ItemMeta itemMeta = armor.getItemMeta(); + if (itemMeta instanceof LeatherArmorMeta) { + LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemMeta; + leatherArmorMeta.setColor(set); + armor.setItemMeta(leatherArmorMeta); + } + } + } + } + else if (entity instanceof ChestedHorse) { + if (count == 7) { + ChestedHorse chestedHorse = (ChestedHorse) entity; + boolean set = (Boolean) value; + chestedHorse.setCarryingChest(set); + } + if (entity instanceof Llama) { + Llama llama = (Llama) entity; + if (count == 8) { + if (value != null) { + @SuppressWarnings("unchecked") + ItemStack set = ItemStack.deserialize((Map) value); + llama.getInventory().setDecor(set); + } + } + else if (count == 9) { + Llama.Color set = (Llama.Color) value; + llama.setColor(set); + } + } + } + } + else { + BukkitAdapter.ADAPTER.setEntityMeta(entity, value, count); + } + count++; + } + } + catch (Exception e) { + e.printStackTrace(); + } + }); + } + +} diff --git a/src/main/java/net/coreprotect/utility/entity/HangingUtil.java b/src/main/java/net/coreprotect/utility/entity/HangingUtil.java new file mode 100644 index 0000000..8074caa --- /dev/null +++ b/src/main/java/net/coreprotect/utility/entity/HangingUtil.java @@ -0,0 +1,169 @@ +package net.coreprotect.utility.entity; + +import java.util.Locale; + +import org.bukkit.Art; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockState; +import org.bukkit.entity.Entity; +import org.bukkit.entity.ItemFrame; +import org.bukkit.entity.Painting; +import org.bukkit.inventory.ItemStack; + +import net.coreprotect.CoreProtect; +import net.coreprotect.bukkit.BukkitAdapter; +import net.coreprotect.model.BlockGroup; +import net.coreprotect.utility.Util; + +public class HangingUtil { + + private HangingUtil() { + throw new IllegalStateException("Utility class"); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static void spawnHanging(final BlockState blockstate, final Material rowType, final int rowData, int delay) { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> { + try { + Block block = blockstate.getBlock(); + int x = block.getX(); + int y = block.getY(); + int z = block.getZ(); + + for (Entity e : block.getChunk().getEntities()) { + if ((BukkitAdapter.ADAPTER.isItemFrame(rowType) && e instanceof ItemFrame) || (rowType.equals(Material.PAINTING) && e instanceof Painting)) { + Location el = e.getLocation(); + if (el.getBlockX() == x && el.getBlockY() == y && el.getBlockZ() == z) { + e.remove(); + break; + } + } + } + + Block c1 = block.getWorld().getBlockAt((x + 1), y, z); + Block c2 = block.getWorld().getBlockAt((x - 1), y, z); + Block c3 = block.getWorld().getBlockAt(x, y, (z + 1)); + Block c4 = block.getWorld().getBlockAt(x, y, (z - 1)); + + BlockFace faceSet = null; + if (!BlockGroup.NON_ATTACHABLE.contains(c1.getType())) { + faceSet = BlockFace.WEST; + block = c1; + } + else if (!BlockGroup.NON_ATTACHABLE.contains(c2.getType())) { + faceSet = BlockFace.EAST; + block = c2; + } + else if (!BlockGroup.NON_ATTACHABLE.contains(c3.getType())) { + faceSet = BlockFace.NORTH; + block = c3; + } + else if (!BlockGroup.NON_ATTACHABLE.contains(c4.getType())) { + faceSet = BlockFace.SOUTH; + block = c4; + } + + BlockFace face = null; + if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.EAST)))) { + face = BlockFace.EAST; + } + else if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.NORTH)))) { + face = BlockFace.NORTH; + } + else if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.WEST)))) { + face = BlockFace.WEST; + } + else if (!Util.solidBlock(Util.getType(block.getRelative(BlockFace.SOUTH)))) { + face = BlockFace.SOUTH; + } + + if (faceSet != null && face != null) { + if (rowType.equals(Material.PAINTING)) { + String name = Util.getArtName(rowData); + Art painting = Art.getByName(name.toUpperCase(Locale.ROOT)); + int height = painting.getBlockHeight(); + int width = painting.getBlockWidth(); + int paintingX = x; + int paintingY = y; + int paintingZ = z; + if (height != 1 || width != 1) { + if (height > 1) { + if (height != 3) { + paintingY = paintingY - 1; + } + } + if (width > 1) { + if (faceSet.equals(BlockFace.WEST)) { + paintingZ--; + } + else if (faceSet.equals(BlockFace.SOUTH)) { + paintingX--; + } + } + } + Block spawnBlock = block.getRelative(face); + Util.setTypeAndData(spawnBlock, Material.AIR, null, true); + Painting hanging = null; + try { + hanging = block.getWorld().spawn(spawnBlock.getLocation(), Painting.class); + } + catch (Exception e) { + } + if (hanging != null) { + hanging.teleport(block.getWorld().getBlockAt(paintingX, paintingY, paintingZ).getLocation()); + hanging.setFacingDirection(faceSet, true); + hanging.setArt(painting, true); + } + } + else if (BukkitAdapter.ADAPTER.isItemFrame(rowType)) { + try { + Block spawnBlock = block.getRelative(face); + Util.setTypeAndData(spawnBlock, Material.AIR, null, true); + Class itemFrame = BukkitAdapter.ADAPTER.getFrameClass(rowType); + Entity entity = block.getWorld().spawn(spawnBlock.getLocation(), itemFrame); + if (entity instanceof ItemFrame) { + ItemFrame hanging = (ItemFrame) entity; + hanging.teleport(block.getWorld().getBlockAt(x, y, z).getLocation()); + hanging.setFacingDirection(faceSet, true); + + Material type = Util.getType(rowData); + if (type != null) { + ItemStack istack = new ItemStack(type, 1); + hanging.setItem(istack); + } + } + } + catch (Exception e) { + } + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + }, delay); + } + + public static void removeHanging(final BlockState block, int delay) { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> { + try { + for (Entity e : block.getChunk().getEntities()) { + if (e instanceof ItemFrame || e instanceof Painting) { + Location el = e.getLocation(); + if (el.getBlockX() == block.getX() && el.getBlockY() == block.getY() && el.getBlockZ() == block.getZ()) { + e.remove(); + } + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + }, delay); + } + +}