From 21672e846828f862677f16698167869b0d0dfc8a Mon Sep 17 00:00:00 2001 From: xoy Date: Thu, 19 Oct 2023 13:17:33 +0200 Subject: [PATCH] [First Commit] First version of the plugin. Get spawners and budding amethysts dropped with silk touch. --- .gitignore | 38 +++++ .idea/.gitignore | 8 + .idea/artifacts/SilkBlocks_0_1.xml | 8 + .idea/encodings.xml | 7 + .idea/misc.xml | 14 ++ .idea/vcs.xml | 6 + README.md | 5 + pom.xml | 17 +++ .../dev/xoy/silkblocks/BlockListener.java | 139 ++++++++++++++++++ .../java/dev/xoy/silkblocks/SilkBlocks.java | 19 +++ src/main/resources/plugin.yml | 6 + 11 files changed, 267 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/artifacts/SilkBlocks_0_1.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/dev/xoy/silkblocks/BlockListener.java create mode 100644 src/main/java/dev/xoy/silkblocks/SilkBlocks.java create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/artifacts/SilkBlocks_0_1.xml b/.idea/artifacts/SilkBlocks_0_1.xml new file mode 100644 index 0000000..ae92e8e --- /dev/null +++ b/.idea/artifacts/SilkBlocks_0_1.xml @@ -0,0 +1,8 @@ + + + $USER_HOME$/Desktop/Minecraft Test Server/plugins + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ecfa09c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f369bbe --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Spigot SilkBlocks Plugin + +Spigot plugin to get spawners and other blocks dropped with silk touch. + +Tested version: 1.20.2 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c4a4e0c --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + dev.xoy.silkblocks + SilkBlocks + 0.1 + + + 21 + 21 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/dev/xoy/silkblocks/BlockListener.java b/src/main/java/dev/xoy/silkblocks/BlockListener.java new file mode 100644 index 0000000..fb64e48 --- /dev/null +++ b/src/main/java/dev/xoy/silkblocks/BlockListener.java @@ -0,0 +1,139 @@ +package dev.xoy.silkblocks; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.CreatureSpawner; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class BlockListener implements Listener { + public void sendMessageToChat(Player player, String msg) { + String formattedMsg = ChatColor.YELLOW + msg; + + player.sendMessage(formattedMsg); + } +/* + public ItemStack getSpawnerItemStack(Block block) { + CreatureSpawner spawner = (CreatureSpawner) block.getState(); + + ItemStack spawnerItem = new ItemStack(Material.SPAWNER); + + BlockStateMeta blockStateMeta = (BlockStateMeta) spawnerItem.getItemMeta(); + if ( blockStateMeta != null ) { + blockStateMeta.setBlockState(spawner); + spawnerItem.setItemMeta(blockStateMeta); + + + return spawnerItem; + } + + return null; + } +*/ + + public ItemStack[] getItemStack(Block block) { + Map mobList = new HashMap<>(); + mobList.put(EntityType.ZOMBIE, Material.ZOMBIE_SPAWN_EGG); + mobList.put(EntityType.SKELETON, Material.SKELETON_SPAWN_EGG); + mobList.put(EntityType.CREEPER, Material.CREEPER_SPAWN_EGG); + mobList.put(EntityType.SPIDER, Material.SPIDER_SPAWN_EGG); + mobList.put(EntityType.WITCH, Material.WITCH_SPAWN_EGG); + mobList.put(EntityType.ENDERMAN, Material.ENDERMAN_SPAWN_EGG); + mobList.put(EntityType.PIG, Material.PIG_SPAWN_EGG); + mobList.put(EntityType.COW, Material.COW_SPAWN_EGG); + mobList.put(EntityType.CHICKEN, Material.CHICKEN_SPAWN_EGG); + mobList.put(EntityType.HORSE, Material.HORSE_SPAWN_EGG); + mobList.put(EntityType.SHEEP, Material.SHEEP_SPAWN_EGG); + mobList.put(EntityType.OCELOT, Material.OCELOT_SPAWN_EGG); + mobList.put(EntityType.BAT, Material.BAT_SPAWN_EGG); + mobList.put(EntityType.VILLAGER, Material.VILLAGER_SPAWN_EGG); + mobList.put(EntityType.IRON_GOLEM, Material.IRON_GOLEM_SPAWN_EGG); + mobList.put(EntityType.WOLF, Material.WOLF_SPAWN_EGG); + mobList.put(EntityType.POLAR_BEAR, Material.POLAR_BEAR_SPAWN_EGG); + mobList.put(EntityType.RABBIT, Material.RABBIT_SPAWN_EGG); + mobList.put(EntityType.PARROT, Material.PARROT_SPAWN_EGG); + mobList.put(EntityType.SQUID, Material.SQUID_SPAWN_EGG); + mobList.put(EntityType.DOLPHIN, Material.DOLPHIN_SPAWN_EGG); + mobList.put(EntityType.TRADER_LLAMA, Material.TRADER_LLAMA_SPAWN_EGG); + mobList.put(EntityType.PANDA, Material.PANDA_SPAWN_EGG); + mobList.put(EntityType.FOX, Material.FOX_SPAWN_EGG); + mobList.put(EntityType.BEE, Material.BEE_SPAWN_EGG); + mobList.put(EntityType.PIGLIN, Material.PIGLIN_SPAWN_EGG); + mobList.put(EntityType.ZOGLIN, Material.ZOGLIN_SPAWN_EGG); + mobList.put(EntityType.COD, Material.COD_SPAWN_EGG); + mobList.put(EntityType.SALMON, Material.SALMON_SPAWN_EGG); + mobList.put(EntityType.PUFFERFISH, Material.PUFFERFISH_SPAWN_EGG); + mobList.put(EntityType.TROPICAL_FISH, Material.TROPICAL_FISH_SPAWN_EGG); + mobList.put(EntityType.STRIDER, Material.STRIDER_SPAWN_EGG); + mobList.put(EntityType.BLAZE, Material.BLAZE_SPAWN_EGG); + mobList.put(EntityType.MAGMA_CUBE, Material.MAGMA_CUBE_SPAWN_EGG); + mobList.put(EntityType.SLIME, Material.SLIME_SPAWN_EGG); + mobList.put(EntityType.GHAST, Material.GHAST_SPAWN_EGG); + mobList.put(EntityType.WITHER_SKELETON, Material.WITHER_SKELETON_SPAWN_EGG); + mobList.put(EntityType.HOGLIN, Material.HOGLIN_SPAWN_EGG); + mobList.put(EntityType.PILLAGER, Material.PILLAGER_SPAWN_EGG); + mobList.put(EntityType.VEX, Material.VEX_SPAWN_EGG); + mobList.put(EntityType.EVOKER, Material.EVOKER_SPAWN_EGG); + mobList.put(EntityType.RAVAGER, Material.RAVAGER_SPAWN_EGG); + mobList.put(EntityType.SNIFFER, Material.SNIFFER_SPAWN_EGG); + mobList.put(EntityType.CAVE_SPIDER, Material.CAVE_SPIDER_SPAWN_EGG); + + ItemStack blockItemStack = new ItemStack(block.getType()); + ItemStack additionalItem = null; + + if (block.getState() instanceof CreatureSpawner spawner) { + if ( spawner.getSpawnedType() != null ) { + additionalItem = new ItemStack(mobList.get(spawner.getSpawnedType())); + } + } + + return new ItemStack[]{ + blockItemStack, + additionalItem + }; + } + + @EventHandler + public void blockBreakEvent(BlockBreakEvent event) { + Player player = event.getPlayer(); + + if ( player.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH) ) { + Block block = event.getBlock(); + + Material[] allowedMaterials = { + Material.SPAWNER, + Material.BUDDING_AMETHYST + }; + + if (Arrays.asList(allowedMaterials).contains(block.getType())) { + Location blockLocation = block.getLocation(); + + if (blockLocation.getWorld() != null) { + if ( block.getType() == Material.SPAWNER ) { + event.setExpToDrop(0); + } + + ItemStack[] itemStacks = getItemStack(block); + + for (ItemStack itemStack : itemStacks) { + if (itemStack != null) { + blockLocation.getWorld().dropItemNaturally(blockLocation, itemStack); + } + } + } + } + } + } +} diff --git a/src/main/java/dev/xoy/silkblocks/SilkBlocks.java b/src/main/java/dev/xoy/silkblocks/SilkBlocks.java new file mode 100644 index 0000000..90b2d13 --- /dev/null +++ b/src/main/java/dev/xoy/silkblocks/SilkBlocks.java @@ -0,0 +1,19 @@ +package dev.xoy.silkblocks; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.plugin.java.JavaPlugin; + +public class SilkBlocks extends JavaPlugin { + @Override + public void onEnable() { + Bukkit.getLogger().info(ChatColor.GREEN + "[" + this.getName() + "] enabled!"); + + Bukkit.getPluginManager().registerEvents(new BlockListener(), this); + } + + @Override + public void onDisable() { + Bukkit.getLogger().info(ChatColor.RED + "[" + this.getName() + "] disabled!"); + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..a6a215e --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,6 @@ + +name: SilkBlocks +version: 0.1 +author: xoydev +main: dev.xoy.silkblocks.SilkBlocks +api-version: 1.20 \ No newline at end of file