SpigotSilkBlocks/src/main/java/dev/xoy/silkblocks/BlockListener.java

140 lines
5.4 KiB
Java

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<EntityType, Material> 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);
}
}
}
}
}
}
}