2022-04-07 04:41:29 +00:00
package dev.plex.listener.impl ;
import dev.plex.listener.PlexListener ;
2022-04-07 14:59:04 +00:00
import dev.plex.util.PlexUtils ;
2022-04-07 04:41:29 +00:00
import org.bukkit.Location ;
import org.bukkit.Material ;
import org.bukkit.block.Block ;
2022-04-07 23:09:23 +00:00
import org.bukkit.block.Dispenser ;
2022-04-07 04:41:29 +00:00
import org.bukkit.block.data.Directional ;
import org.bukkit.entity.EntityType ;
2022-04-07 14:59:04 +00:00
import org.bukkit.entity.Player ;
2022-04-07 04:41:29 +00:00
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
import org.bukkit.event.block.Action ;
import org.bukkit.event.block.BlockDispenseEvent ;
2022-04-07 23:09:23 +00:00
import org.bukkit.event.entity.CreatureSpawnEvent ;
2022-04-07 14:59:04 +00:00
import org.bukkit.event.entity.EntitySpawnEvent ;
2022-04-07 04:41:29 +00:00
import org.bukkit.event.player.PlayerInteractEvent ;
2022-04-07 23:09:23 +00:00
import org.bukkit.inventory.Inventory ;
2022-04-07 04:41:29 +00:00
import org.bukkit.inventory.ItemStack ;
import java.util.Arrays ;
2022-04-07 17:12:38 +00:00
import java.util.Collection ;
2022-04-07 04:41:29 +00:00
import java.util.List ;
public class SpawnListener extends PlexListener
{
2022-04-07 18:05:15 +00:00
private static final List < Material > SPAWN_EGGS = Arrays . stream ( Material . values ( ) ) . filter ( ( mat ) - > mat . name ( ) . endsWith ( " _SPAWN_EGG " ) ) . toList ( ) ;
2022-04-07 04:41:29 +00:00
2022-04-07 14:59:04 +00:00
@EventHandler
public void onEntitySpawn ( EntitySpawnEvent event )
{
2022-04-07 23:09:23 +00:00
if ( event . getEntity ( ) . getEntitySpawnReason ( ) = = CreatureSpawnEvent . SpawnReason . SPAWNER_EGG )
{
// for the future, we can instead filter and restrict nbt tags right here.
// currently, however, the entity from spawn eggs are spawned by other event handlers
event . setCancelled ( true ) ;
return ;
}
if ( plugin . config . getStringList ( " blockedEntities " ) . stream ( ) . anyMatch ( type - > type . equalsIgnoreCase ( event . getEntityType ( ) . name ( ) ) ) )
2022-04-07 14:59:04 +00:00
{
event . setCancelled ( true ) ;
Location location = event . getLocation ( ) ;
2022-04-07 17:12:38 +00:00
Collection < Player > coll = location . getNearbyEntitiesByType ( Player . class , 10 ) ;
PlexUtils . disabledEffectMultiple ( coll . toArray ( new Player [ coll . size ( ) ] ) , location ) ; // dont let intellij auto correct toArray to an empty array (for efficiency)
2022-04-07 14:59:04 +00:00
}
}
2022-04-07 04:41:29 +00:00
@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 ) ) ;
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 ;
}
}