Merge pull request #11 from plexusorg/spawneggfilter

add spawn egg filtering
This commit is contained in:
Telesphoreo 2022-04-06 21:50:36 -05:00 committed by GitHub
commit 00d00eb4e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 0 deletions

View File

@ -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<Material> 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;
}
}
}
}

View File

@ -5,6 +5,7 @@ import dev.plex.Plex;
import dev.plex.services.impl.AutoWipeService; import dev.plex.services.impl.AutoWipeService;
import dev.plex.services.impl.BanService; import dev.plex.services.impl.BanService;
import dev.plex.services.impl.GameRuleService; import dev.plex.services.impl.GameRuleService;
import dev.plex.services.impl.SpawnEggService;
import dev.plex.services.impl.UpdateCheckerService; import dev.plex.services.impl.UpdateCheckerService;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
@ -20,6 +21,7 @@ public class ServiceManager
registerService(new BanService()); registerService(new BanService());
registerService(new GameRuleService()); registerService(new GameRuleService());
registerService(new UpdateCheckerService()); registerService(new UpdateCheckerService());
registerService(new SpawnEggService());
registerService(new AutoWipeService()); registerService(new AutoWipeService());
} }

View File

@ -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;
}
}