From 86675bb808b6bdcd3ac777387c41d5bb5cda7431 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sun, 26 May 2024 14:03:41 +0200 Subject: [PATCH] Fix `/wandOops` giving back wrong amount (#12) * fix wandoops giving back wrong amount * address reviews --- .../portablejim/bbw/core/OopsCommand.java | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/src/main/java/portablejim/bbw/core/OopsCommand.java b/src/main/java/portablejim/bbw/core/OopsCommand.java index 8f66b85..cd1c60d 100644 --- a/src/main/java/portablejim/bbw/core/OopsCommand.java +++ b/src/main/java/portablejim/bbw/core/OopsCommand.java @@ -2,6 +2,7 @@ package portablejim.bbw.core; import java.util.ArrayList; +import net.minecraft.block.Block; import net.minecraft.command.CommandBase; import net.minecraft.command.ICommandSender; import net.minecraft.command.WrongUsageException; @@ -9,6 +10,7 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; import net.minecraftforge.common.util.Constants; import cpw.mods.fml.common.registry.GameRegistry; @@ -39,15 +41,9 @@ public class OopsCommand extends CommandBase { if (currentItemstack != null && currentItemstack.getItem() != null && currentItemstack.getItem() instanceof IWandItem) { NBTTagCompound tagComponent = currentItemstack.getTagCompound(); - - NBTTagCompound bbwCompound; if (tagComponent != null && tagComponent.hasKey("bbw", Constants.NBT.TAG_COMPOUND) && tagComponent.getCompoundTag("bbw").hasKey("lastPlaced", Constants.NBT.TAG_INT_ARRAY)) { - bbwCompound = tagComponent.getCompoundTag("bbw"); - ArrayList pointList = unpackNbt(bbwCompound.getIntArray("lastPlaced")); - for (Point3d point : pointList) { - player.getEntityWorld().setBlockToAir(point.x, point.y, point.z); - } + NBTTagCompound bbwCompound = tagComponent.getCompoundTag("bbw"); if (bbwCompound.hasKey("lastBlock", Constants.NBT.TAG_STRING) && bbwCompound.hasKey("lastPerBlock", Constants.NBT.TAG_INT)) { GameRegistry.UniqueIdentifier lastBlock = new GameRegistry.UniqueIdentifier( @@ -55,34 +51,19 @@ public class OopsCommand extends CommandBase { int damageValue = bbwCompound.getInteger("lastDamage"); ItemStack itemStack = GameRegistry.findItemStack(lastBlock.modId, lastBlock.name, 1); itemStack.setItemDamage(damageValue); - int count = bbwCompound.getInteger("lastPerBlock") * pointList.size(); - int stackSize = itemStack.getMaxStackSize(); - int fullStacks = count / stackSize; - for (int i = 0; i < fullStacks; i++) { - ItemStack newStack = itemStack.copy(); - newStack.stackSize = stackSize; - player.worldObj.spawnEntityInWorld( - new EntityItem( - player.getEntityWorld(), - player.posX, - player.posY, - player.posZ, - newStack)); + Block block = Block.getBlockFromItem(itemStack.getItem()); + World world = player.worldObj; + ArrayList pointList = unpackNbt(bbwCompound.getIntArray("lastPlaced")); + int count = 0; + for (Point3d point : pointList) { + if (world.getBlock(point.x, point.y, point.z) == block + && world.getBlockMetadata(point.x, point.y, point.z) == damageValue) { + world.setBlockToAir(point.x, point.y, point.z); + count++; + } } - ItemStack finalStack = itemStack.copy(); - finalStack.stackSize = count % stackSize; - player.worldObj.spawnEntityInWorld( - new EntityItem( - player.getEntityWorld(), - player.posX, - player.posY, - player.posZ, - finalStack)); - bbwCompound.removeTag("lastPlaced"); - bbwCompound.removeTag("lastBlock"); - bbwCompound.removeTag("lastDamage"); - bbwCompound.removeTag("lastPerBlock"); + dropItems(bbwCompound, player, itemStack, count); } } else { throw new WrongUsageException(BetterBuildersWandsMod.LANGID + ".chat.error.noundo"); @@ -96,7 +77,7 @@ public class OopsCommand extends CommandBase { } protected ArrayList unpackNbt(int[] placedBlocks) { - ArrayList output = new ArrayList(); + ArrayList output = new ArrayList<>(); int countPoints = placedBlocks.length / 3; for (int i = 0; i < countPoints * 3; i += 3) { output.add(new Point3d(placedBlocks[i], placedBlocks[i + 1], placedBlocks[i + 2])); @@ -105,6 +86,27 @@ public class OopsCommand extends CommandBase { return output; } + private void dropItems(NBTTagCompound bbwCompound, EntityPlayerMP player, ItemStack itemStack, int amount) { + if (amount <= 0) return; + int count = bbwCompound.getInteger("lastPerBlock") * amount; + int stackSize = itemStack.getMaxStackSize(); + int fullStacks = count / stackSize; + for (int i = 0; i < fullStacks; i++) { + ItemStack newStack = itemStack.copy(); + newStack.stackSize = stackSize; + player.worldObj.spawnEntityInWorld( + new EntityItem(player.getEntityWorld(), player.posX, player.posY, player.posZ, newStack)); + } + ItemStack finalStack = itemStack.copy(); + finalStack.stackSize = count % stackSize; + player.worldObj.spawnEntityInWorld( + new EntityItem(player.getEntityWorld(), player.posX, player.posY, player.posZ, finalStack)); + bbwCompound.removeTag("lastPlaced"); + bbwCompound.removeTag("lastBlock"); + bbwCompound.removeTag("lastDamage"); + bbwCompound.removeTag("lastPerBlock"); + } + @Override public int getRequiredPermissionLevel() { return 0;