ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemSpawnerChanger.java

154 lines
5.8 KiB
Java
Raw Normal View History

2016-05-16 22:52:27 +02:00
/*
* This file ("ItemSpawnerChanger.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 Ellpeck
2016-05-16 22:52:27 +02:00
*/
2016-05-08 02:55:59 +02:00
package de.ellpeck.actuallyadditions.mod.items;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigStringListValues;
2016-05-08 02:55:59 +02:00
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
2016-07-03 20:57:00 +02:00
import de.ellpeck.actuallyadditions.mod.items.metalists.TheMiscItems;
2016-05-08 02:55:59 +02:00
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
2016-07-03 20:57:00 +02:00
import net.minecraft.init.Blocks;
2016-05-08 02:55:59 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.MobSpawnerBaseLogic;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityMobSpawner;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
2016-07-03 20:57:00 +02:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
2016-05-08 02:55:59 +02:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.List;
public class ItemSpawnerChanger extends ItemBase{
public ItemSpawnerChanger(String name){
super(name);
this.setMaxStackSize(1);
2016-07-03 20:57:00 +02:00
MinecraftForge.EVENT_BUS.register(this);
2016-05-08 02:55:59 +02:00
}
2016-07-03 20:57:00 +02:00
@SubscribeEvent
public void onBlockBreakEvent(BlockEvent.HarvestDropsEvent event){
IBlockState state = event.getState();
if(state != null && state.getBlock() == Blocks.MOB_SPAWNER){
event.getDrops().add(new ItemStack(InitItems.itemMisc, 1, TheMiscItems.SPAWNER_SHARD.ordinal()));
}
}
2016-05-08 02:55:59 +02:00
@Override
public EnumActionResult onItemUse(ItemStack aStack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ){
if(!world.isRemote){
ItemStack stack = player.getHeldItemMainhand();
if(player.canPlayerEdit(pos.offset(facing), facing, stack)){
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileEntityMobSpawner){
String entity = this.getStoredEntity(stack);
if(entity != null){
MobSpawnerBaseLogic logic = ((TileEntityMobSpawner)tile).getSpawnerBaseLogic();
//This is a hacky way to remove the spawn potentials that make the spawner reset from time to time
//Don't judge, there isn't a method for it and it's better than Reflection hackiness
NBTTagCompound compound = new NBTTagCompound();
logic.writeToNBT(compound);
compound.removeTag("SpawnPotentials");
compound.removeTag("SpawnData");
logic.readFromNBT(compound);
2016-05-08 02:55:59 +02:00
logic.setEntityName(entity);
tile.markDirty();
IBlockState state = world.getBlockState(pos);
world.notifyBlockUpdate(pos, state, state, 3);
ItemPhantomConnector.clearStorage(stack, "Entity");
2016-05-08 02:55:59 +02:00
if(!player.capabilities.isCreativeMode){
stack.stackSize--;
}
return EnumActionResult.SUCCESS;
}
}
}
}
return EnumActionResult.FAIL;
}
@Override
public boolean itemInteractionForEntity(ItemStack aStack, EntityPlayer player, EntityLivingBase entity, EnumHand hand){
if(!player.worldObj.isRemote){
ItemStack stack = player.getHeldItemMainhand();
if(this.getStoredEntity(stack) == null){
if(this.storeClickedEntity(stack, entity)){
entity.setDead();
}
}
return true;
}
return false;
}
2016-05-08 11:07:19 +02:00
private boolean storeClickedEntity(ItemStack stack, EntityLivingBase entity){
if(!stack.hasTagCompound()){
stack.setTagCompound(new NBTTagCompound());
}
2016-05-08 02:55:59 +02:00
2016-07-18 02:09:41 +02:00
if(!(entity instanceof EntityPlayer) && entity.isNonBoss()){
2016-05-08 02:55:59 +02:00
String entityName = EntityList.getEntityString(entity);
if(entityName != null && !entityName.isEmpty()){
for(String name : ConfigStringListValues.SPAWNER_CHANGER_BLACKLIST.getValue()){
2016-05-08 02:55:59 +02:00
if(entityName.equals(name)){
return false;
}
}
stack.getTagCompound().setString("Entity", entityName);
return true;
}
}
return false;
}
private String getStoredEntity(ItemStack stack){
if(stack.hasTagCompound()){
String entity = stack.getTagCompound().getString("Entity");
if(entity != null && !entity.isEmpty()){
return entity;
}
}
return null;
}
@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean isHeld){
String entity = this.getStoredEntity(stack);
if(entity != null){
list.add("Entity: "+entity);
list.add(TextFormatting.ITALIC+StringUtil.localize("tooltip."+ModUtil.MOD_ID+".clearStorage.desc"));
}
}
}