Skip to content

Commit

Permalink
Fix /wandOops giving back wrong amount (#12)
Browse files Browse the repository at this point in the history
* fix wandoops giving back wrong amount

* address reviews
  • Loading branch information
Lyfts authored May 26, 2024
1 parent 4a802db commit 86675bb
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions src/main/java/portablejim/bbw/core/OopsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.util.ArrayList;

import net.minecraft.block.Block;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
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;
Expand Down Expand Up @@ -39,50 +41,29 @@ public void processCommand(ICommandSender sender, String[] arguments) {
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<Point3d> 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(
bbwCompound.getString("lastBlock"));
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<Point3d> 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");
Expand All @@ -96,7 +77,7 @@ public void processCommand(ICommandSender sender, String[] arguments) {
}

protected ArrayList<Point3d> unpackNbt(int[] placedBlocks) {
ArrayList<Point3d> output = new ArrayList<Point3d>();
ArrayList<Point3d> 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]));
Expand All @@ -105,6 +86,27 @@ protected ArrayList<Point3d> unpackNbt(int[] placedBlocks) {
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;
Expand Down

0 comments on commit 86675bb

Please sign in to comment.