diff --git a/src/main/java/dev/plex/listener/impl/SpawnEggListener.java b/src/main/java/dev/plex/listener/impl/SpawnEggListener.java new file mode 100644 index 0000000..c490573 --- /dev/null +++ b/src/main/java/dev/plex/listener/impl/SpawnEggListener.java @@ -0,0 +1,56 @@ +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.List; + +public class SpawnEggListener extends PlexListener +{ + public static List SPAWN_EGGS; + + @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/services/ServiceManager.java b/src/main/java/dev/plex/services/ServiceManager.java index 0faf424..e3362ab 100644 --- a/src/main/java/dev/plex/services/ServiceManager.java +++ b/src/main/java/dev/plex/services/ServiceManager.java @@ -5,6 +5,7 @@ import dev.plex.Plex; import dev.plex.services.impl.AutoWipeService; import dev.plex.services.impl.BanService; import dev.plex.services.impl.GameRuleService; +import dev.plex.services.impl.SpawnEggService; import dev.plex.services.impl.UpdateCheckerService; import org.bukkit.Bukkit; import org.bukkit.scheduler.BukkitTask; @@ -20,6 +21,7 @@ public class ServiceManager registerService(new BanService()); registerService(new GameRuleService()); registerService(new UpdateCheckerService()); + registerService(new SpawnEggService()); registerService(new AutoWipeService()); } diff --git a/src/main/java/dev/plex/services/impl/SpawnEggService.java b/src/main/java/dev/plex/services/impl/SpawnEggService.java new file mode 100644 index 0000000..50e88ea --- /dev/null +++ b/src/main/java/dev/plex/services/impl/SpawnEggService.java @@ -0,0 +1,24 @@ +package dev.plex.services.impl; + +import dev.plex.listener.impl.SpawnEggListener; +import dev.plex.services.AbstractService; +import org.bukkit.Material; + +import java.util.Arrays; + +public class SpawnEggService extends AbstractService +{ + public SpawnEggService() { + super(false, true); + } + + @Override + public void run() { + SpawnEggListener.SPAWN_EGGS = Arrays.stream(Material.values()).filter((mat) -> mat.name().endsWith("_SPAWN_EGG")).toList(); + } + + @Override + public int repeatInSeconds() { + return 0; + } +}