Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix /wandOops giving back wrong amount #12

Merged
merged 2 commits into from
May 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 34 additions & 35 deletions src/main/java/portablejim/bbw/core/OopsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

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 +40,28 @@ 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) {
Lyfts marked this conversation as resolved.
Show resolved Hide resolved
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 +75,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 +84,26 @@ 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.entityDropItem(newStack, 0.0F);
}
ItemStack finalStack = itemStack.copy();
finalStack.stackSize = count % stackSize;
player.entityDropItem(finalStack, 0.0F);

bbwCompound.removeTag("lastPlaced");
bbwCompound.removeTag("lastBlock");
bbwCompound.removeTag("lastDamage");
bbwCompound.removeTag("lastPerBlock");
}

@Override
public int getRequiredPermissionLevel() {
return 0;
Expand Down