Merge pull request #2022 from BallOfEnergy1/Optimization

Toolbox Flixes!
This commit is contained in:
HbmMods 2025-03-31 08:01:00 +02:00 committed by GitHub
commit f43cf194cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 120 additions and 32 deletions

View File

@ -232,7 +232,7 @@ public class CompatHandler {
@Override
@Optional.Method(modid = "OpenComputers")
default boolean canConnectNode(ForgeDirection side) {
return true;
return !this.getComponentName().equals(nullComponent);
}
/**

View File

@ -20,55 +20,67 @@ public class HazardTransformerRadiationContainer extends HazardTransformerBase {
@Override
public void transformPost(ItemStack stack, List<HazardEntry> entries) {
boolean isCrate = Block.getBlockFromItem(stack.getItem()) instanceof BlockStorageCrate;
boolean isBox = stack.getItem() == ModItems.containment_box;
boolean isBag = stack.getItem() == ModItems.plastic_bag;
if(!isCrate && !isBox && !isBag) return;
boolean isContainer = stack.getItem() == ModItems.toolbox; // For anything using the standard ItemInventory shit.
if(!isCrate && !isBox && !isBag && !isContainer) return;
if(!stack.hasTagCompound()) return;
float radiation = 0;
if(isContainer) {
ItemStack[] items = ItemStackUtil.readStacksFromNBT(stack, 24); // Biggest container: toolbox; 24 slots.
if(items != null)
for(ItemStack held : items)
if(held != null)
radiation += HazardSystem.getHazardLevelFromStack(held, HazardRegistry.RADIATION) * held.stackSize;
// this indentation is killing me man
}
if(isCrate) {
for(int i = 0; i < 104; i++) {
ItemStack held = ItemStack.loadItemStackFromNBT(stack.stackTagCompound.getCompoundTag("slot" + i));
if(held != null) {
radiation += HazardSystem.getHazardLevelFromStack(held, HazardRegistry.RADIATION) * held.stackSize;
}
}
}
if(isBox) {
ItemStack[] fromNBT = ItemStackUtil.readStacksFromNBT(stack, 20);
if(fromNBT == null) return;
for(ItemStack held : fromNBT) {
if(held != null) {
radiation += HazardSystem.getHazardLevelFromStack(held, HazardRegistry.RADIATION) * held.stackSize;
}
}
radiation = (float) BobMathUtil.squirt(radiation);
}
if(isBag) {
ItemStack[] fromNBT = ItemStackUtil.readStacksFromNBT(stack, 1);
if(fromNBT == null) return;
for(ItemStack held : fromNBT) {
if(held != null) {
radiation += HazardSystem.getHazardLevelFromStack(held, HazardRegistry.RADIATION) * held.stackSize;
}
}
radiation *= 2F;
}
if(radiation > 0) {
entries.add(new HazardEntry(HazardRegistry.RADIATION, radiation));
}

View File

@ -2,6 +2,9 @@ package com.hbm.inventory;
import com.hbm.interfaces.NotableComments;
import com.hbm.inventory.container.ContainerCrateBase;
import com.hbm.items.block.ItemBlockStorageCrate;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
@ -24,7 +27,7 @@ public class SlotNonRetarded extends Slot {
public boolean isItemValid(ItemStack stack) {
return inventory.isItemValidForSlot(this.slotNumber, stack);
}
/**
* Because if slots have higher stacksizes than the maximum allowed by the tile, the display just stops working.
* Why was that necessary? Sure it's not intended but falsifying information isn't very cool.
@ -33,4 +36,17 @@ public class SlotNonRetarded extends Slot {
public int getSlotStackLimit() {
return Math.max(this.inventory.getInventoryStackLimit(), this.getHasStack() ? this.getStack().stackSize : 1);
}
/**
* This prevents the player from moving containers that are being held *at all*, fixing a decently big dupe.
* I hate that this has to be here but... It is what it is.
*/
@Override
public boolean canTakeStack(EntityPlayer player) {
if(player.inventory.currentItem == this.getSlotIndex() && // If this slot is the current held slot.
this.getStack() != null && this.getStack().getItem() instanceof ItemBlockStorageCrate && // If the slot contains a storage crate.
player.openContainer instanceof ContainerCrateBase) // If the player is currently inside a crate container.
return false;
return super.canTakeStack(player);
}
}

View File

@ -1,5 +1,6 @@
package com.hbm.inventory.container;
import com.hbm.inventory.SlotNonRetarded;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
@ -12,7 +13,7 @@ import net.minecraft.item.ItemStack;
* implementation, because I really needed to get the te from a container But
* you should very much use this to kill the giant amount of boilerplate in
* container classes
*
*
* @author 70k
**/
public class ContainerBase extends Container {
@ -61,12 +62,12 @@ public class ContainerBase extends Container {
public void playerInv(InventoryPlayer invPlayer, int playerInvX, int playerInvY, int playerHotbarY) {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 9; j++) {
this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, playerInvX + j * 18, playerInvY + i * 18));
this.addSlotToContainer(new SlotNonRetarded(invPlayer, j + i * 9 + 9, playerInvX + j * 18, playerInvY + i * 18));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, playerInvX + i * 18, playerHotbarY));
this.addSlotToContainer(new SlotNonRetarded(invPlayer, i, playerInvX + i * 18, playerHotbarY));
}
}
@ -75,7 +76,7 @@ public class ContainerBase extends Container {
// - Mellow, 1884
/**
* Used to add several conventional inventory slots at a time
*
*
* @param inv the inventory to add the slots to
* @param from the slot index to start from
*/
@ -83,7 +84,7 @@ public class ContainerBase extends Container {
int slotSize = 18;
for(int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
this.addSlotToContainer(new Slot(inv, col + row * cols + from, x + col * slotSize, y + row * slotSize));
this.addSlotToContainer(new SlotNonRetarded(inv, col + row * cols + from, x + col * slotSize, y + row * slotSize));
}
}
}

View File

@ -17,7 +17,7 @@ import java.util.Random;
* Base class for items containing an inventory. This can be seen in crates, containment boxes, and the toolbox.
* @author BallOfEnergy/Gammawave
*/
public abstract class IItemInventory implements IInventory {
public abstract class ItemInventory implements IInventory {
public EntityPlayer player;
public ItemStack[] slots;

View File

@ -3,7 +3,7 @@ package com.hbm.items.block;
import com.hbm.blocks.ModBlocks;
import com.hbm.inventory.container.*;
import com.hbm.inventory.gui.*;
import com.hbm.items.IItemInventory;
import com.hbm.items.ItemInventory;
import com.hbm.items.tool.ItemKey;
import com.hbm.main.MainRegistry;
import com.hbm.tileentity.IGUIProvider;
@ -80,7 +80,7 @@ public class ItemBlockStorageCrate extends ItemBlockBase implements IGUIProvider
throw new NullPointerException();
}
public static class InventoryCrate extends IItemInventory {
public static class InventoryCrate extends ItemInventory {
public InventoryCrate(EntityPlayer player, ItemStack crate) {

View File

@ -2,7 +2,7 @@ package com.hbm.items.tool;
import com.hbm.inventory.container.ContainerLeadBox;
import com.hbm.inventory.gui.GUILeadBox;
import com.hbm.items.IItemInventory;
import com.hbm.items.ItemInventory;
import com.hbm.main.MainRegistry;
import com.hbm.tileentity.IGUIProvider;
import com.hbm.util.ItemStackUtil;
@ -45,7 +45,7 @@ public class ItemLeadBox extends Item implements IGUIProvider {
return new GUILeadBox(player.inventory, new InventoryLeadBox(player, player.getHeldItem()));
}
public static class InventoryLeadBox extends IItemInventory {
public static class InventoryLeadBox extends ItemInventory {
public InventoryLeadBox(EntityPlayer player, ItemStack box) {
this.player = player;
@ -58,9 +58,7 @@ public class ItemLeadBox extends Item implements IGUIProvider {
ItemStack[] fromNBT = ItemStackUtil.readStacksFromNBT(box, slots.length);
if(fromNBT != null) {
for(int i = 0; i < slots.length; i++) {
slots[i] = fromNBT[i];
}
System.arraycopy(fromNBT, 0, slots, 0, slots.length);
}
toMarkDirty = true;
this.markDirty();

View File

@ -2,7 +2,7 @@ package com.hbm.items.tool;
import com.hbm.inventory.container.ContainerToolBox;
import com.hbm.inventory.gui.GUIToolBox;
import com.hbm.items.IItemInventory;
import com.hbm.items.ItemInventory;
import com.hbm.items.ModItems;
import com.hbm.lib.RefStrings;
import com.hbm.main.MainRegistry;
@ -11,16 +11,20 @@ import com.hbm.util.ItemStackUtil;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.*;
import net.minecraft.world.World;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ItemToolBox extends Item implements IGUIProvider {
@ -53,6 +57,12 @@ public class ItemToolBox extends Item implements IGUIProvider {
return renderPass == 1 ? this.iconClosed : getIconFromDamageForRenderPass(stack.getItemDamage(), renderPass);
}
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool) {
list.add("Click with the toolbox to swap hotbars in/out of the toolbox.");
list.add("Shift-click with the toolbox to open the toolbox.");
}
// Finds active rows in the toolbox (rows with items inside them).
public List<Integer> getActiveRows(ItemStack box) {
ItemStack[] stacks = ItemStackUtil.readStacksFromNBT(box, 24);
@ -151,6 +161,9 @@ public class ItemToolBox extends Item implements IGUIProvider {
}
}
if(stacks == null)
lowestInactiveIndex = 0; // Fix crash relating to a null NBT causing this value to be Integer.MAX_VALUE.
// Finally, move all temporary arrays into their respective locations.
System.arraycopy(stacksToTransferToBox, 0, endingStacks, lowestInactiveIndex * 8, 8);
@ -160,13 +173,61 @@ public class ItemToolBox extends Item implements IGUIProvider {
box.setTagCompound(new NBTTagCompound());
ItemStackUtil.addStacksToNBT(box, endingStacks);
NBTTagCompound nbt = box.getTagCompound();
if(!nbt.hasNoTags()) {
Random random = new Random();
try {
byte[] abyte = CompressedStreamTools.compress(nbt);
if (abyte.length > 6000) {
player.addChatComponentMessage(new ChatComponentText(EnumChatFormatting.RED + "Warning: Container NBT exceeds 6kB, contents will be ejected!"));
ItemStack[] stacks1 = ItemStackUtil.readStacksFromNBT(box, 24 /* Toolbox inv size. */);
if(stacks1 == null)
return;
for (ItemStack itemstack : stacks1) {
if (itemstack != null) {
float f = random.nextFloat() * 0.8F + 0.1F;
float f1 = random.nextFloat() * 0.8F + 0.1F;
float f2 = random.nextFloat() * 0.8F + 0.1F;
while (itemstack.stackSize > 0) {
int j1 = random.nextInt(21) + 10;
if (j1 > itemstack.stackSize) {
j1 = itemstack.stackSize;
}
itemstack.stackSize -= j1;
EntityItem entityitem = new EntityItem(player.worldObj, player.posX + f, player.posY + f1, player.posZ + f2, new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
if (itemstack.hasTagCompound()) {
entityitem.getEntityItem().setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
}
float f3 = 0.05F;
entityitem.motionX = (float) random.nextGaussian() * f3 + player.motionX;
entityitem.motionY = (float) random.nextGaussian() * f3 + 0.2F + player.motionY;
entityitem.motionZ = (float) random.nextGaussian() * f3 + player.motionZ;
player.worldObj.spawnEntityInWorld(entityitem);
}
}
}
box.setTagCompound(new NBTTagCompound()); // Reset.
}
} catch (IOException ignored) {}
}
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
if(!world.isRemote) {
if (player.isSneaking()) {
if (!player.isSneaking()) {
moveRows(stack, player);
player.inventoryContainer.detectAndSendChanges();
} else {
@ -190,7 +251,7 @@ public class ItemToolBox extends Item implements IGUIProvider {
return new GUIToolBox(player.inventory, new InventoryToolBox(player, player.getHeldItem()));
}
public static class InventoryToolBox extends IItemInventory {
public static class InventoryToolBox extends ItemInventory {
public InventoryToolBox(EntityPlayer player, ItemStack box) {
this.player = player;