From 72b8774731f199bf7be7e07f1effa9e5973272b9 Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Thu, 7 Apr 2022 00:41:29 -0400 Subject: [PATCH] add dispenser filter & touch up spawn egg filter --- .../dev/plex/listener/impl/BlockListener.java | 2 +- .../plex/listener/impl/SpawnEggListener.java | 57 ------------ .../dev/plex/listener/impl/SpawnListener.java | 87 +++++++++++++++++++ 3 files changed, 88 insertions(+), 58 deletions(-) delete mode 100644 src/main/java/dev/plex/listener/impl/SpawnEggListener.java create mode 100644 src/main/java/dev/plex/listener/impl/SpawnListener.java diff --git a/src/main/java/dev/plex/listener/impl/BlockListener.java b/src/main/java/dev/plex/listener/impl/BlockListener.java index 95fa716..c8993ed 100644 --- a/src/main/java/dev/plex/listener/impl/BlockListener.java +++ b/src/main/java/dev/plex/listener/impl/BlockListener.java @@ -43,7 +43,7 @@ public class BlockListener extends PlexListener if (blockedBlocks.contains(event.getBlock().getType())) { - event.getBlock().setType(Material.COBWEB); + event.getBlock().setType(Material.CAKE); Particle.CLOUD.builder().location(event.getBlock().getLocation().add(0.5,0.5,0.5)).receivers(event.getPlayer()).extra(0).offset(0.5,0.5,0.5).count(5).spawn(); Particle.FLAME.builder().location(event.getBlock().getLocation().add(0.5,0.5,0.5)).receivers(event.getPlayer()).extra(0).offset(0.5,0.5,0.5).count(3).spawn(); Particle.SOUL_FIRE_FLAME.builder().location(event.getBlock().getLocation().add(0.5,0.5,0.5)).receivers(event.getPlayer()).offset(0.5,0.5,0.5).extra(0).count(2).spawn(); diff --git a/src/main/java/dev/plex/listener/impl/SpawnEggListener.java b/src/main/java/dev/plex/listener/impl/SpawnEggListener.java deleted file mode 100644 index e12e72e..0000000 --- a/src/main/java/dev/plex/listener/impl/SpawnEggListener.java +++ /dev/null @@ -1,57 +0,0 @@ -package dev.plex.listener.impl; - -import dev.plex.listener.PlexListener; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.entity.EntityType; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.block.Action; -import org.bukkit.event.player.PlayerInteractEvent; - -import java.util.Arrays; -import java.util.List; - -public class SpawnEggListener extends PlexListener -{ - public static final List SPAWN_EGGS = Arrays.stream(Material.values()).filter((mat) -> mat.name().endsWith("_SPAWN_EGG")).toList(); - - @EventHandler(priority = EventPriority.HIGH) - public void onPlayerInteract(PlayerInteractEvent event) - { - if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) - { - if (SPAWN_EGGS.contains(event.getMaterial())) - { - event.setCancelled(true); - Block clickedBlock = event.getClickedBlock(); - if (clickedBlock == null) - { - return; - } - EntityType eggType = null; - try - { - Material mat = event.getMaterial(); - if (mat == Material.MOOSHROOM_SPAWN_EGG) - { - eggType = EntityType.MUSHROOM_COW; - } - else - { - eggType = EntityType.valueOf(mat.name().substring(0, mat.name().length() - 10)); - } - } - catch (IllegalArgumentException ignored) - { - // - } - if (eggType != null) - { - clickedBlock.getWorld().spawnEntity(clickedBlock.getLocation().add(event.getBlockFace().getDirection()).add(0.5, 0.5, 0.5), eggType); - } - return; - } - } - } -} diff --git a/src/main/java/dev/plex/listener/impl/SpawnListener.java b/src/main/java/dev/plex/listener/impl/SpawnListener.java new file mode 100644 index 0000000..48ab832 --- /dev/null +++ b/src/main/java/dev/plex/listener/impl/SpawnListener.java @@ -0,0 +1,87 @@ +package dev.plex.listener.impl; + +import dev.plex.listener.PlexListener; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.data.Directional; +import org.bukkit.entity.EntityType; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockDispenseEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.Arrays; +import java.util.List; + +public class SpawnListener extends PlexListener +{ + // TODO: CONFIGURABILITY!!! + + public static final List SPAWN_EGGS = Arrays.stream(Material.values()).filter((mat) -> mat.name().endsWith("_SPAWN_EGG")).toList(); + + @EventHandler + public void onDispense(BlockDispenseEvent event) + { + ItemStack item = event.getItem(); + Material itemType = item.getType(); + if (SPAWN_EGGS.contains(itemType)) + { + Block block = event.getBlock(); + Location blockLoc = block.getLocation().add(0.5,0.5,0.5).add(((Directional) block.getBlockData()).getFacing().getDirection().multiply(0.8)); + event.setCancelled(true); + item.setAmount(item.getAmount() - 1); + event.setItem(item); + EntityType eggType = spawnEggToEntityType(itemType); + if (eggType != null) + { + blockLoc.getWorld().spawnEntity(blockLoc, eggType); + } + } + } + + @EventHandler(priority = EventPriority.HIGH) + public void onPlayerInteract(PlayerInteractEvent event) + { + if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) + { + if (SPAWN_EGGS.contains(event.getMaterial())) + { + event.setCancelled(true); + Block clickedBlock = event.getClickedBlock(); + if (clickedBlock == null) + { + return; + } + EntityType eggType = spawnEggToEntityType(event.getMaterial()); + if (eggType != null) + { + clickedBlock.getWorld().spawnEntity(clickedBlock.getLocation().add(event.getBlockFace().getDirection().multiply(0.8)).add(0.5, 0.5, 0.5), eggType); + } + return; + } + } + } + + private static EntityType spawnEggToEntityType(Material mat){ + EntityType eggType; + try + { + if (mat == Material.MOOSHROOM_SPAWN_EGG) + { + eggType = EntityType.MUSHROOM_COW; + } + else + { + eggType = EntityType.valueOf(mat.name().substring(0, mat.name().length() - 10)); + } + } + catch (IllegalArgumentException ignored) + { + return null; + } + return eggType; + } +}