remove more meta usage

This commit is contained in:
Shadows_of_Fire 2018-03-22 23:49:50 -04:00
parent 88daf6683d
commit 54e5cdd5cf

View file

@ -31,26 +31,29 @@ import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import java.util.List; import java.util.List;
public class ItemFillingWand extends ItemEnergy{ import org.apache.commons.lang3.tuple.Pair;
public ItemFillingWand(String name){ public class ItemFillingWand extends ItemEnergy {
public ItemFillingWand(String name) {
super(500000, 1000, name); super(500000, 1000, name);
} }
private static boolean removeFittingItem(IBlockState state, EntityPlayer player){ private static boolean removeFittingItem(IBlockState state, EntityPlayer player) {
Block block = state.getBlock(); Block block = state.getBlock();
ItemStack stack = new ItemStack(block, 1, block.damageDropped(state)); ItemStack stack = new ItemStack(block, 1, block.damageDropped(state));
if(StackUtil.isValid(stack)){ if (StackUtil.isValid(stack)) {
for(int i = 0; i < player.inventory.getSizeInventory(); i++){ for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
ItemStack slot = player.inventory.getStackInSlot(i); ItemStack slot = player.inventory.getStackInSlot(i);
if(StackUtil.isValid(slot) && slot.isItemEqual(stack)){ if (StackUtil.isValid(slot) && slot.isItemEqual(stack)) {
slot = StackUtil.addStackSize(slot, -1); slot = StackUtil.addStackSize(slot, -1);
if(!StackUtil.isValid(slot)){ if (!StackUtil.isValid(slot)) {
player.inventory.setInventorySlotContents(i, StackUtil.getEmpty()); player.inventory.setInventorySlotContents(i, StackUtil.getEmpty());
} }
@ -62,37 +65,34 @@ public class ItemFillingWand extends ItemEnergy{
return false; return false;
} }
private static void saveBlock(IBlockState state, ItemStack stack){ private static void saveData(ItemStack pickBlock, IBlockState state, ItemStack wand) {
if(!stack.hasTagCompound()){ if (!wand.hasTagCompound()) wand.setTagCompound(new NBTTagCompound());
stack.setTagCompound(new NBTTagCompound()); wand.getTagCompound().setInteger("state", Block.getStateId(state));
} wand.getTagCompound().setString("name", pickBlock.getDisplayName());
stack.getTagCompound().setInteger("state", Block.getStateId(state));
} }
private static IBlockState loadBlock(ItemStack stack){ private static Pair<IBlockState, String> loadData(ItemStack stack) {
if(stack.hasTagCompound()){ if (stack.hasTagCompound()) return Pair.of(Block.getStateById(stack.getTagCompound().getInteger("state")), stack.getTagCompound().getString("name"));
return Block.getStateById(stack.getTagCompound().getInteger("state"));
}
return null; return null;
} }
@Override @Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ){ public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
ItemStack stack = player.getHeldItem(hand); ItemStack stack = player.getHeldItem(hand);
if(!world.isRemote && player.getItemInUseCount() <= 0){ if (!world.isRemote && player.getItemInUseCount() <= 0) {
if(player.isSneaking()){ if (player.isSneaking()) {
IBlockState state = world.getBlockState(pos); IBlockState state = world.getBlockState(pos);
saveBlock(state, stack); ItemStack pick = state.getBlock().getPickBlock(state, world.rayTraceBlocks(player.getPositionVector(), new Vec3d(pos.getX() + hitX, pos.getY() + hitY, pos.getZ() + hitZ)), world, pos, player);
saveData(pick, state, stack);
return EnumActionResult.SUCCESS; return EnumActionResult.SUCCESS;
} } else if (loadData(stack) != null) {
else if(loadBlock(stack) != null){ if (!stack.hasTagCompound()) {
if(!stack.hasTagCompound()){
stack.setTagCompound(new NBTTagCompound()); stack.setTagCompound(new NBTTagCompound());
} }
NBTTagCompound compound = stack.getTagCompound(); NBTTagCompound compound = stack.getTagCompound();
if(compound.getInteger("CurrX") == 0 && compound.getInteger("CurrY") == 0 && compound.getInteger("CurrZ") == 0){ if (compound.getInteger("CurrX") == 0 && compound.getInteger("CurrY") == 0 && compound.getInteger("CurrZ") == 0) {
compound.setInteger("FirstX", pos.getX()); compound.setInteger("FirstX", pos.getX());
compound.setInteger("FirstY", pos.getY()); compound.setInteger("FirstY", pos.getY());
compound.setInteger("FirstZ", pos.getZ()); compound.setInteger("FirstZ", pos.getZ());
@ -106,13 +106,13 @@ public class ItemFillingWand extends ItemEnergy{
} }
@Override @Override
public void onPlayerStoppedUsing(ItemStack stack, World world, EntityLivingBase entity, int timeLeft){ public void onPlayerStoppedUsing(ItemStack stack, World world, EntityLivingBase entity, int timeLeft) {
if(!world.isRemote){ if (!world.isRemote) {
boolean clear = true; boolean clear = true;
if(entity instanceof EntityPlayer){ if (entity instanceof EntityPlayer) {
RayTraceResult result = WorldUtil.getNearestBlockWithDefaultReachDistance(world, (EntityPlayer)entity); RayTraceResult result = WorldUtil.getNearestBlockWithDefaultReachDistance(world, (EntityPlayer) entity);
if(result != null && result.getBlockPos() != null){ if (result != null && result.getBlockPos() != null) {
if(!stack.hasTagCompound()){ if (!stack.hasTagCompound()) {
stack.setTagCompound(new NBTTagCompound()); stack.setTagCompound(new NBTTagCompound());
} }
NBTTagCompound compound = stack.getTagCompound(); NBTTagCompound compound = stack.getTagCompound();
@ -126,7 +126,7 @@ public class ItemFillingWand extends ItemEnergy{
} }
} }
if(clear){ if (clear) {
ItemPhantomConnector.clearStorage(stack, "FirstX", "FirstY", "FirstZ"); ItemPhantomConnector.clearStorage(stack, "FirstX", "FirstY", "FirstZ");
} }
} }
@ -135,15 +135,15 @@ public class ItemFillingWand extends ItemEnergy{
} }
@Override @Override
public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected){ public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
super.onUpdate(stack, world, entity, itemSlot, isSelected); super.onUpdate(stack, world, entity, itemSlot, isSelected);
if(!world.isRemote){ if (!world.isRemote) {
boolean shouldClear = false; boolean shouldClear = false;
if(isSelected){ if (isSelected) {
if(entity instanceof EntityPlayer && stack.hasTagCompound()){ if (entity instanceof EntityPlayer && stack.hasTagCompound()) {
EntityPlayer player = (EntityPlayer)entity; EntityPlayer player = (EntityPlayer) entity;
boolean creative = player.capabilities.isCreativeMode; boolean creative = player.capabilities.isCreativeMode;
NBTTagCompound compound = stack.getTagCompound(); NBTTagCompound compound = stack.getTagCompound();
@ -151,11 +151,12 @@ public class ItemFillingWand extends ItemEnergy{
BlockPos firstPos = new BlockPos(compound.getInteger("FirstX"), compound.getInteger("FirstY"), compound.getInteger("FirstZ")); BlockPos firstPos = new BlockPos(compound.getInteger("FirstX"), compound.getInteger("FirstY"), compound.getInteger("FirstZ"));
BlockPos secondPos = new BlockPos(compound.getInteger("SecondX"), compound.getInteger("SecondY"), compound.getInteger("SecondZ")); BlockPos secondPos = new BlockPos(compound.getInteger("SecondX"), compound.getInteger("SecondY"), compound.getInteger("SecondZ"));
if(!BlockPos.ORIGIN.equals(firstPos) && !BlockPos.ORIGIN.equals(secondPos)){ if (!BlockPos.ORIGIN.equals(firstPos) && !BlockPos.ORIGIN.equals(secondPos)) {
int energyUse = 1500; int energyUse = 1500;
IBlockState replaceState = loadBlock(stack); Pair<IBlockState, String> data = loadData(stack);
if(replaceState != null && (creative || this.getEnergyStored(stack) >= energyUse)){ if (data != null && (creative || this.getEnergyStored(stack) >= energyUse)) {
IBlockState replaceState = data.getLeft();
int lowestX = Math.min(firstPos.getX(), secondPos.getX()); int lowestX = Math.min(firstPos.getX(), secondPos.getX());
int lowestY = Math.min(firstPos.getY(), secondPos.getY()); int lowestY = Math.min(firstPos.getY(), secondPos.getY());
int lowestZ = Math.min(firstPos.getZ(), secondPos.getZ()); int lowestZ = Math.min(firstPos.getZ(), secondPos.getZ());
@ -164,89 +165,82 @@ public class ItemFillingWand extends ItemEnergy{
int currY = compound.getInteger("CurrY"); int currY = compound.getInteger("CurrY");
int currZ = compound.getInteger("CurrZ"); int currZ = compound.getInteger("CurrZ");
BlockPos pos = new BlockPos(lowestX+currX, lowestY+currY, lowestZ+currZ); BlockPos pos = new BlockPos(lowestX + currX, lowestY + currY, lowestZ + currZ);
IBlockState state = world.getBlockState(pos); IBlockState state = world.getBlockState(pos);
if(state.getBlock().isReplaceable(world, pos) && replaceState.getBlock().canPlaceBlockAt(world, pos)){ if (state.getBlock().isReplaceable(world, pos) && replaceState.getBlock().canPlaceBlockAt(world, pos)) {
if(creative || removeFittingItem(replaceState, player)){ if (creative || removeFittingItem(replaceState, player)) {
world.setBlockState(pos, replaceState, 2); world.setBlockState(pos, replaceState, 2);
SoundType sound = replaceState.getBlock().getSoundType(replaceState, world, pos, player); SoundType sound = replaceState.getBlock().getSoundType(replaceState, world, pos, player);
world.playSound(null, pos, sound.getPlaceSound(), SoundCategory.BLOCKS, sound.getVolume()/2F + .5F, sound.getPitch()*0.8F); world.playSound(null, pos, sound.getPlaceSound(), SoundCategory.BLOCKS, sound.getVolume() / 2F + .5F, sound.getPitch() * 0.8F);
if(!creative){ if (!creative) {
this.extractEnergyInternal(stack, energyUse, false); this.extractEnergyInternal(stack, energyUse, false);
} }
} } else {
else{
shouldClear = true; shouldClear = true;
} }
} }
int distX = Math.abs(secondPos.getX()-firstPos.getX()); int distX = Math.abs(secondPos.getX() - firstPos.getX());
int distY = Math.abs(secondPos.getY()-firstPos.getY()); int distY = Math.abs(secondPos.getY() - firstPos.getY());
int distZ = Math.abs(secondPos.getZ()-firstPos.getZ()); int distZ = Math.abs(secondPos.getZ() - firstPos.getZ());
currX++; currX++;
if(currX > distX){ if (currX > distX) {
currX = 0; currX = 0;
currY++; currY++;
if(currY > distY){ if (currY > distY) {
currY = 0; currY = 0;
currZ++; currZ++;
if(currZ > distZ){ if (currZ > distZ) {
shouldClear = true; shouldClear = true;
} }
} }
} }
if(!shouldClear){ if (!shouldClear) {
compound.setInteger("CurrX", currX); compound.setInteger("CurrX", currX);
compound.setInteger("CurrY", currY); compound.setInteger("CurrY", currY);
compound.setInteger("CurrZ", currZ); compound.setInteger("CurrZ", currZ);
} }
} } else {
else{
shouldClear = true; shouldClear = true;
} }
} }
} }
} } else {
else{
shouldClear = true; shouldClear = true;
} }
if(shouldClear){ if (shouldClear) {
ItemPhantomConnector.clearStorage(stack, "FirstX", "FirstY", "FirstZ", "SecondX", "SecondY", "SecondZ", "CurrX", "CurrY", "CurrZ"); ItemPhantomConnector.clearStorage(stack, "FirstX", "FirstY", "FirstZ", "SecondX", "SecondY", "SecondZ", "CurrX", "CurrY", "CurrZ");
} }
} }
} }
@Override @Override
public void addInformation(ItemStack stack, World playerIn, List<String> tooltip, ITooltipFlag advanced){ public void addInformation(ItemStack stack, World playerIn, List<String> tooltip, ITooltipFlag advanced) {
super.addInformation(stack, playerIn, tooltip, advanced); super.addInformation(stack, playerIn, tooltip, advanced);
String display = StringUtil.localize("tooltip."+ModUtil.MOD_ID+".item_filling_wand.selectedBlock.none"); String display = StringUtil.localize("tooltip." + ModUtil.MOD_ID + ".item_filling_wand.selectedBlock.none");
IBlockState state = loadBlock(stack); Pair<IBlockState, String> data = loadData(stack);
if(state != null){ if (data != null) {
Block block = state.getBlock(); display = data.getRight();
ItemStack blockStack = new ItemStack(block, 1, block.getMetaFromState(state));
if(StackUtil.isValid(blockStack)){
display = blockStack.getDisplayName();
}
} }
tooltip.add(String.format("%s: %s", StringUtil.localize("tooltip."+ModUtil.MOD_ID+".item_filling_wand.selectedBlock"), display)); tooltip.add(String.format("%s: %s", StringUtil.localize("tooltip." + ModUtil.MOD_ID + ".item_filling_wand.selectedBlock"), display));
} }
@Override @Override
public int getMaxItemUseDuration(ItemStack stack){ public int getMaxItemUseDuration(ItemStack stack) {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
@Override @Override
public EnumRarity getRarity(ItemStack stack){ public EnumRarity getRarity(ItemStack stack) {
return EnumRarity.EPIC; return EnumRarity.EPIC;
} }
} }