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
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-05-16 22:52:27 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-08 02:55:59 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.items;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2018-05-10 11:38:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2016-06-05 12:15:02 +02:00
|
|
|
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-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-05-08 02:55:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
2017-06-17 00:48:49 +02:00
|
|
|
import net.minecraft.client.util.ITooltipFlag;
|
2016-05-08 02:55:59 +02:00
|
|
|
import net.minecraft.entity.EntityList;
|
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
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;
|
2016-11-19 21:11:17 +01:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2016-05-08 02:55:59 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.text.TextFormatting;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class ItemSpawnerChanger extends ItemBase {
|
2016-05-08 02:55:59 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public ItemSpawnerChanger(String name) {
|
2016-05-08 02:55:59 +02:00
|
|
|
super(name);
|
|
|
|
this.setMaxStackSize(1);
|
2016-07-03 20:57:00 +02:00
|
|
|
}
|
2016-05-29 23:49:35 +02:00
|
|
|
|
2016-05-08 02:55:59 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
|
|
|
|
if (!world.isRemote) {
|
2016-05-08 02:55:59 +02:00
|
|
|
ItemStack stack = player.getHeldItemMainhand();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (player.canPlayerEdit(pos.offset(facing), facing, stack)) {
|
2016-05-08 02:55:59 +02:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityMobSpawner) {
|
2016-05-08 02:55:59 +02:00
|
|
|
String entity = this.getStoredEntity(stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (entity != null) {
|
|
|
|
MobSpawnerBaseLogic logic = ((TileEntityMobSpawner) tile).getSpawnerBaseLogic();
|
2016-07-30 17:20:07 +02:00
|
|
|
|
|
|
|
//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");
|
2016-10-19 16:09:50 +02:00
|
|
|
compound.removeTag("SpawnData");
|
2016-07-30 17:20:07 +02:00
|
|
|
logic.readFromNBT(compound);
|
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
logic.setEntityId(new ResourceLocation(entity));
|
2016-05-08 02:55:59 +02:00
|
|
|
|
|
|
|
tile.markDirty();
|
|
|
|
|
|
|
|
IBlockState state = world.getBlockState(pos);
|
|
|
|
world.notifyBlockUpdate(pos, state, state, 3);
|
|
|
|
|
2016-08-10 21:04:44 +02:00
|
|
|
ItemPhantomConnector.clearStorage(stack, "Entity");
|
2016-05-08 02:55:59 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!player.capabilities.isCreativeMode) {
|
2018-06-23 01:39:30 +02:00
|
|
|
player.setHeldItem(hand, StackUtil.shrink(stack, 1));
|
2016-05-08 02:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return EnumActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean itemInteractionForEntity(ItemStack aStack, EntityPlayer player, EntityLivingBase entity, EnumHand hand) {
|
|
|
|
if (!player.world.isRemote) {
|
2016-05-08 02:55:59 +02:00
|
|
|
ItemStack stack = player.getHeldItemMainhand();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.getStoredEntity(stack) == null) {
|
|
|
|
if (this.storeClickedEntity(stack, entity)) {
|
2016-05-08 02:55:59 +02:00
|
|
|
entity.setDead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private boolean storeClickedEntity(ItemStack stack, EntityLivingBase entity) {
|
|
|
|
if (!stack.hasTagCompound()) {
|
2016-05-08 11:07:19 +02:00
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
}
|
2016-05-08 02:55:59 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!(entity instanceof EntityPlayer) && entity.isNonBoss()) {
|
2016-12-23 15:33:45 +01:00
|
|
|
ResourceLocation entityLoc = EntityList.getKey(entity.getClass());
|
2019-05-02 09:10:29 +02:00
|
|
|
if (entityLoc != null) {
|
2016-12-23 15:33:45 +01:00
|
|
|
String entityName = entityLoc.toString();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (entityName != null && !entityName.isEmpty()) {
|
|
|
|
for (String name : ConfigStringListValues.SPAWNER_CHANGER_BLACKLIST.getValue()) {
|
|
|
|
if (entityName.equals(name)) { return false; }
|
2016-05-08 02:55:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-23 15:33:45 +01:00
|
|
|
stack.getTagCompound().setString("Entity", entityName);
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-08 02:55:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private String getStoredEntity(ItemStack stack) {
|
|
|
|
if (stack.hasTagCompound()) {
|
2016-05-08 02:55:59 +02:00
|
|
|
String entity = stack.getTagCompound().getString("Entity");
|
2019-05-02 09:10:29 +02:00
|
|
|
if (entity != null && !entity.isEmpty()) { return entity; }
|
2016-05-08 02:55:59 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void addInformation(ItemStack stack, World playerIn, List<String> list, ITooltipFlag advanced) {
|
2016-05-08 02:55:59 +02:00
|
|
|
String entity = this.getStoredEntity(stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (entity != null) {
|
|
|
|
list.add("Entity: " + entity);
|
|
|
|
list.add(TextFormatting.ITALIC + StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".clearStorage.desc"));
|
2016-05-08 02:55:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|