// SpawnerEventHandler.java
// All imports
@Mod.EventBusSubscriber
public class SpawnerEventHandler
{
/**
* When we harvest a block
* Return spawner block when harvested with silk touch
*/
@SubscribeEvent
public void onBlockHarvestDrops(BlockEvent.HarvestDropsEvent event)
{
if(event.getState().getBlock() == Blocks.SPAWNER)
{
NBTTagList list = event.getHarvester().getHeldItemMainhand().getEnchantmentTagList();
// Check if silk touch enchant is on the tool
if(CheckSilkTouch(list))
event.getDrops().add(new ItemStack(Blocks.SPAWNER, 1));
}
}
/**
* When a block is destroyed
* Prevent XP drop when spawner is harvested with silk touch
*/
@SubscribeEvent
public void onBlockBreakEvent(BlockEvent.BreakEvent event)
{
// Check if a spawner broke
if(event.getState().getBlock() == Blocks.SPAWNER)
{
NBTTagList list = event.getPlayer().getHeldItemMainhand().getEnchantmentTagList();
// Return 0 EXP when harvested with silk touch
if(CheckSilkTouch(list))
event.setExpToDrop(0);
}
}
/**
* Called when player places a block
*
* -> This does not happen
*/
@SubscribeEvent
public void onBlockPlaced(BlockEvent.PlaceEvent event)
{
System.out.println("This does not happen");
}
/**
* Check a tools item enchantment list contains Silk Touch enchant
*
* @param NBTTagList of enchantment
* @return true/false
*/
private boolean CheckSilkTouch(NBTTagList list)
{
// Check list string contains silk touch
if(list.getString().indexOf("minecraft:silk_touch") != -1)
return true;
else
return false;
}
}
// SpawnerMod.java
// all imports ...
@Mod("spawnermod")
public class SpawnerMod
{
public SpawnerMod()
{
SpawnerEventHandler spawnerEventHandler = new SpawnerEventHandler();
MinecraftForge.EVENT_BUS.register(spawnerEventHandler);
}
}
This is the code for both of my files