Merge pull request #2005 from BallOfEnergy1/Optimization

Toolboxes!
This commit is contained in:
HbmMods 2025-03-24 08:09:17 +01:00 committed by GitHub
commit 4c58d9a084
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 636 additions and 354 deletions

View File

@ -150,7 +150,7 @@ public class ToolRecipes {
CraftingManager.addRecipeAuto(new ItemStack(ModItems.power_net_tool), new Object[] { "WRW", " I ", " B ", 'W', MINGRADE.wireFine(), 'R', REDSTONE.dust(), 'I', IRON.ingot(), 'B', ModItems.battery_generic }); CraftingManager.addRecipeAuto(new ItemStack(ModItems.power_net_tool), new Object[] { "WRW", " I ", " B ", 'W', MINGRADE.wireFine(), 'R', REDSTONE.dust(), 'I', IRON.ingot(), 'B', ModItems.battery_generic });
CraftingManager.addRecipeAuto(new ItemStack(ModItems.analysis_tool), new Object[] { " G", " S ", "S ", 'G', KEY_ANYPANE, 'S', STEEL.ingot() }); CraftingManager.addRecipeAuto(new ItemStack(ModItems.analysis_tool), new Object[] { " G", " S ", "S ", 'G', KEY_ANYPANE, 'S', STEEL.ingot() });
CraftingManager.addRecipeAuto(new ItemStack(ModItems.kit_toolbox_empty), new Object[] { "CCC", "CIC", 'C', CU.plate(), 'I', IRON.ingot() }); CraftingManager.addRecipeAuto(new ItemStack(ModItems.toolbox), new Object[] { "CCC", "CIC", 'C', CU.plate(), 'I', IRON.ingot() });
CraftingManager.addRecipeAuto(new ItemStack(ModItems.screwdriver, 1), new Object[] { " I", " I ", "S ", 'S', STEEL.ingot(), 'I', IRON.ingot() }); CraftingManager.addRecipeAuto(new ItemStack(ModItems.screwdriver, 1), new Object[] { " I", " I ", "S ", 'S', STEEL.ingot(), 'I', IRON.ingot() });
CraftingManager.addRecipeAuto(new ItemStack(ModItems.screwdriver_desh, 1), new Object[] { " I", " I ", "S ", 'S', ANY_PLASTIC.ingot(), 'I', DESH.ingot() }); CraftingManager.addRecipeAuto(new ItemStack(ModItems.screwdriver_desh, 1), new Object[] { " I", " I ", "S ", 'S', ANY_PLASTIC.ingot(), 'I', DESH.ingot() });

View File

@ -1,68 +0,0 @@
package com.hbm.crafting.handlers;
import java.util.ArrayList;
import java.util.List;
import com.hbm.items.ModItems;
import com.hbm.items.special.ItemKitNBT;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
public class ToolboxCraftingHandler implements IRecipe {
@Override
public boolean matches(InventoryCrafting inventory, World world) {
int itemCount = 0;
int kitCount = 0;
for(int i = 0; i < 9; i++) {
ItemStack stack = inventory.getStackInRowAndColumn(i % 3, i / 3);
if(stack != null) {
if(stack.getItem().hasContainerItem(stack) || !stack.getItem().doesContainerItemLeaveCraftingGrid(stack))
return false;
itemCount++;
if(stack.getItem() == ModItems.kit_toolbox_empty) {
kitCount++;
}
}
}
return itemCount > 1 && kitCount == 1;
}
@Override
public ItemStack getCraftingResult(InventoryCrafting inventory) {
List<ItemStack> stacks = new ArrayList();
for(int i = 0; i < 9; i++) {
ItemStack stack = inventory.getStackInRowAndColumn(i % 3, i / 3);
if(stack != null && stack.getItem() != ModItems.kit_toolbox_empty) {
ItemStack copy = stack.copy();
copy.stackSize = 1;
stacks.add(copy);
}
}
return ItemKitNBT.create(stacks.toArray(new ItemStack[0]));
}
@Override
public int getRecipeSize() {
return 9;
}
@Override
public ItemStack getRecipeOutput() {
return new ItemStack(ModItems.kit_toolbox);
}
}

View File

@ -0,0 +1,85 @@
package com.hbm.inventory.container;
import com.hbm.inventory.SlotNonRetarded;
import com.hbm.util.InventoryUtil;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import static com.hbm.items.tool.ItemToolBox.*;
public class ContainerToolBox extends Container {
private final InventoryToolBox box;
public ContainerToolBox(InventoryPlayer invPlayer, InventoryToolBox box) {
this.box = box;
this.box.openInventory();
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 8; j++) {
this.addSlotToContainer(new SlotNonRetarded(box, j + i * 8, 17 + j * 18, 49 + i * 18));
}
}
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 9; j++) {
this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, 8 + j * 18, 129 + i * 18));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 187));
}
}
@Override
public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int par2) {
ItemStack var3 = null;
Slot var4 = (Slot) this.inventorySlots.get(par2);
if(var4 != null && var4.getHasStack()) {
ItemStack var5 = var4.getStack();
var3 = var5.copy();
if(par2 <= box.getSizeInventory() - 1) {
if(!InventoryUtil.mergeItemStack(this.inventorySlots, var5, box.getSizeInventory(), this.inventorySlots.size(), true)) {
return null;
}
} else if(!InventoryUtil.mergeItemStack(this.inventorySlots, var5, 0, box.getSizeInventory(), false)) {
return null;
}
if(var5.stackSize == 0) {
var4.putStack(null);
} else {
var4.onSlotChanged();
}
var4.onPickupFromSlot(p_82846_1_, var5);
}
return var3;
}
@Override
public ItemStack slotClick(int index, int button, int mode, EntityPlayer player) {
// prevents the player from moving around the currently open box
if(mode == 2 && button == player.inventory.currentItem) return null;
if(index == player.inventory.currentItem + 51) return null;
return super.slotClick(index, button, mode, player);
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return box.isUseableByPlayer(player);
}
@Override
public void onContainerClosed(EntityPlayer player) {
super.onContainerClosed(player);
this.box.closeInventory();
}
}

View File

@ -47,7 +47,7 @@ public class GUILeadBox extends GuiContainer {
String name = I18n.format(this.inventory.getInventoryName()); String name = I18n.format(this.inventory.getInventoryName());
if(inventory.hasCustomInventoryName()) { if(inventory.hasCustomInventoryName()) {
name = inventory.box.getDisplayName(); name = inventory.target.getDisplayName();
} }
this.fontRendererObj.drawString(name, this.xSize / 2 - this.fontRendererObj.getStringWidth(name) / 2, 6, 4210752); this.fontRendererObj.drawString(name, this.xSize / 2 - this.fontRendererObj.getStringWidth(name) / 2, 6, 4210752);

View File

@ -0,0 +1,63 @@
package com.hbm.inventory.gui;
import com.hbm.inventory.container.ContainerToolBox;
import com.hbm.lib.RefStrings;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
import static com.hbm.items.tool.ItemToolBox.*;
public class GUIToolBox extends GuiContainer {
private final static ResourceLocation texture = new ResourceLocation(RefStrings.MODID + ":textures/gui/gui_toolbox.png");
private final InventoryToolBox inventory;
private ItemStack firstHeld;
public GUIToolBox(InventoryPlayer invPlayer, InventoryToolBox box) {
super(new ContainerToolBox(invPlayer, box));
this.inventory = box;
this.xSize = 176;
this.ySize = 211;
}
@Override
public void drawScreen(int x, int y, float interp) {
if(firstHeld == null) {
// *very* unlikely to be incorrect on the first frame after opening, so doing this is good enough
firstHeld = this.mc.thePlayer.getHeldItem();
// if the open box has changed or disappeared, close the inventory
} else if(this.mc.thePlayer.getHeldItem() != firstHeld) {
//this.mc.thePlayer.closeScreen();
//return;
}
super.drawScreen(x, y, interp);
}
@Override
protected void drawGuiContainerForegroundLayer(int i, int j) {
String name = I18n.format(this.inventory.getInventoryName());
if(inventory.hasCustomInventoryName()) {
name = inventory.target.getDisplayName();
}
this.fontRendererObj.drawString(name, this.xSize / 2 - this.fontRendererObj.getStringWidth(name) / 2, 37, 4210752);
this.fontRendererObj.drawString(I18n.format("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
}
@Override
protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
}
}

View File

@ -0,0 +1,154 @@
package com.hbm.items;
import com.hbm.util.ItemStackUtil;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import java.io.IOException;
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 EntityPlayer player;
public ItemStack[] slots;
public ItemStack target;
public boolean toMarkDirty = false;
@Override
public void markDirty() {
if(!toMarkDirty || player.getEntityWorld().isRemote)
return;
for(int i = 0; i < getSizeInventory(); ++i) {
if(getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0) {
slots[i] = null;
}
}
ItemStackUtil.addStacksToNBT(target, slots); // Maintain compatibility with the containment boxes.
target.setTagCompound(checkNBT(target.getTagCompound()));
}
public NBTTagCompound checkNBT(NBTTagCompound nbt) {
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!"));
for (int i1 = 0; i1 < this.getSizeInventory(); ++i1) {
ItemStack itemstack = this.getStackInSlot(i1);
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);
}
}
}
return new NBTTagCompound(); // Reset.
}
} catch (IOException ignored) {}
}
return nbt;
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
if (stack.stackSize > amount) {
stack = stack.splitStack(amount);
} else {
setInventorySlotContents(slot, null);
}
}
return stack;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
if(stack != null) {
stack.stackSize = Math.min(stack.stackSize, this.getInventoryStackLimit());
}
slots[slot] = stack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack stack = getStackInSlot(slot);
setInventorySlotContents(slot, null);
return stack;
}
@Override
public ItemStack getStackInSlot(int slot) {
return slots[slot];
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return true;
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return true;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public void openInventory() {
player.worldObj.playSoundEffect(player.posX, player.posY, player.posZ, "hbm:block.crateOpen", 1.0F, 0.8F);
}
@Override
public void closeInventory() {
toMarkDirty = true;
markDirty();
toMarkDirty = false;
player.worldObj.playSoundEffect(player.posX, player.posY, player.posZ, "hbm:block.crateClose", 1.0F, 0.8F);
}
}

View File

@ -2136,8 +2136,9 @@ public class ModItems {
public static Item hazmat_red_kit; public static Item hazmat_red_kit;
public static Item hazmat_grey_kit; public static Item hazmat_grey_kit;
public static Item kit_custom; public static Item kit_custom;
public static Item kit_toolbox_empty;
public static Item kit_toolbox; public static Item legacy_toolbox;
public static Item toolbox;
public static Item loot_10; public static Item loot_10;
public static Item loot_15; public static Item loot_15;
@ -4128,8 +4129,8 @@ public class ModItems {
hazmat_red_kit = new ItemStarterKit().setUnlocalizedName("hazmat_red_kit").setMaxStackSize(1).setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":hazmat_red_kit"); hazmat_red_kit = new ItemStarterKit().setUnlocalizedName("hazmat_red_kit").setMaxStackSize(1).setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":hazmat_red_kit");
hazmat_grey_kit = new ItemStarterKit().setUnlocalizedName("hazmat_grey_kit").setMaxStackSize(1).setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":hazmat_grey_kit"); hazmat_grey_kit = new ItemStarterKit().setUnlocalizedName("hazmat_grey_kit").setMaxStackSize(1).setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":hazmat_grey_kit");
kit_custom = new ItemKitCustom().setUnlocalizedName("kit_custom").setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":kit"); kit_custom = new ItemKitCustom().setUnlocalizedName("kit_custom").setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":kit");
kit_toolbox_empty = new Item().setUnlocalizedName("kit_toolbox_empty").setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":kit_toolbox_empty"); toolbox = new ItemToolBox().setUnlocalizedName("toolbox").setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":kit_toolbox");
kit_toolbox = new ItemKitNBT().setUnlocalizedName("kit_toolbox").setCreativeTab(MainRegistry.consumableTab).setContainerItem(kit_toolbox_empty).setTextureName(RefStrings.MODID + ":kit_toolbox"); legacy_toolbox = new ItemKitNBT().setUnlocalizedName("toolbox_legacy").setContainerItem(toolbox).setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":kit_toolbox");
loot_10 = new ItemLootCrate().setUnlocalizedName("loot_10").setMaxStackSize(1).setCreativeTab(MainRegistry.missileTab).setTextureName(RefStrings.MODID + ":loot_10"); loot_10 = new ItemLootCrate().setUnlocalizedName("loot_10").setMaxStackSize(1).setCreativeTab(MainRegistry.missileTab).setTextureName(RefStrings.MODID + ":loot_10");
loot_15 = new ItemLootCrate().setUnlocalizedName("loot_15").setMaxStackSize(1).setCreativeTab(MainRegistry.missileTab).setTextureName(RefStrings.MODID + ":loot_15"); loot_15 = new ItemLootCrate().setUnlocalizedName("loot_15").setMaxStackSize(1).setCreativeTab(MainRegistry.missileTab).setTextureName(RefStrings.MODID + ":loot_15");
@ -7167,8 +7168,8 @@ public class ModItems {
GameRegistry.registerItem(hazmat_grey_kit, hazmat_grey_kit.getUnlocalizedName()); GameRegistry.registerItem(hazmat_grey_kit, hazmat_grey_kit.getUnlocalizedName());
GameRegistry.registerItem(kit_custom, kit_custom.getUnlocalizedName()); GameRegistry.registerItem(kit_custom, kit_custom.getUnlocalizedName());
GameRegistry.registerItem(euphemium_kit, euphemium_kit.getUnlocalizedName()); GameRegistry.registerItem(euphemium_kit, euphemium_kit.getUnlocalizedName());
GameRegistry.registerItem(kit_toolbox_empty, kit_toolbox_empty.getUnlocalizedName()); GameRegistry.registerItem(legacy_toolbox, legacy_toolbox.getUnlocalizedName());
GameRegistry.registerItem(kit_toolbox, kit_toolbox.getUnlocalizedName()); GameRegistry.registerItem(toolbox, toolbox.getUnlocalizedName());
GameRegistry.registerItem(letter, letter.getUnlocalizedName()); GameRegistry.registerItem(letter, letter.getUnlocalizedName());
//Misile Loot Boxes //Misile Loot Boxes

View File

@ -3,6 +3,7 @@ package com.hbm.items.block;
import com.hbm.blocks.ModBlocks; import com.hbm.blocks.ModBlocks;
import com.hbm.inventory.container.*; import com.hbm.inventory.container.*;
import com.hbm.inventory.gui.*; import com.hbm.inventory.gui.*;
import com.hbm.items.IItemInventory;
import com.hbm.items.tool.ItemKey; import com.hbm.items.tool.ItemKey;
import com.hbm.main.MainRegistry; import com.hbm.main.MainRegistry;
import com.hbm.tileentity.IGUIProvider; import com.hbm.tileentity.IGUIProvider;
@ -10,21 +11,14 @@ import com.hbm.tileentity.machine.storage.*;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World; import net.minecraft.world.World;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Random;
public class ItemBlockStorageCrate extends ItemBlockBase implements IGUIProvider { public class ItemBlockStorageCrate extends ItemBlockBase implements IGUIProvider {
@ -86,18 +80,12 @@ public class ItemBlockStorageCrate extends ItemBlockBase implements IGUIProvider
throw new NullPointerException(); throw new NullPointerException();
} }
public static class InventoryCrate implements IInventory { public static class InventoryCrate extends IItemInventory {
public final EntityPlayer player;
public final ItemStack crate;
public ItemStack[] slots;
private boolean toMarkDirty = false;
public InventoryCrate(EntityPlayer player, ItemStack crate) { public InventoryCrate(EntityPlayer player, ItemStack crate) {
this.player = player; this.player = player;
this.crate = crate; this.target = crate;
slots = new ItemStack[this.getSizeInventory()]; slots = new ItemStack[this.getSizeInventory()];
if(crate.stackTagCompound == null) if(crate.stackTagCompound == null)
@ -125,66 +113,23 @@ public class ItemBlockStorageCrate extends ItemBlockBase implements IGUIProvider
@Override @Override
public int getSizeInventory() { public int getSizeInventory() {
return findCrateType(crate.getItem()).getSizeInventory(); return findCrateType(target.getItem()).getSizeInventory();
} }
@Override @Override
public String getInventoryName() { public String getInventoryName() {
return findCrateType(crate.getItem()).getInventoryName(); return findCrateType(target.getItem()).getInventoryName();
}
@Override
public ItemStack getStackInSlot(int slot) {
return slots[slot];
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
if (stack.stackSize > amount) {
stack = stack.splitStack(amount);
} else {
setInventorySlotContents(slot, null);
}
}
return stack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack stack = getStackInSlot(slot);
setInventorySlotContents(slot, null);
return stack;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
if(stack != null) {
stack.stackSize = Math.min(stack.stackSize, this.getInventoryStackLimit());
}
slots[slot] = stack;
} }
@Override @Override
public boolean hasCustomInventoryName() { public boolean hasCustomInventoryName() {
return crate.hasDisplayName(); return target.hasDisplayName();
}
@Override
public int getInventoryStackLimit() {
return 64;
} }
@Override @Override
public void markDirty() { // I HATE THIS SO MUCH public void markDirty() { // I HATE THIS SO MUCH
if(player.worldObj.isRemote) { // go the fuck away if(player.getEntityWorld().isRemote || !toMarkDirty) { // go the fuck away
return;
}
if(!toMarkDirty) { // ok fuck you too
return; return;
} }
@ -203,88 +148,18 @@ public class ItemBlockStorageCrate extends ItemBlockBase implements IGUIProvider
nbt.setTag("slot" + i, slot); nbt.setTag("slot" + i, slot);
} }
if(crate.stackTagCompound != null) { // yes it's a bit jank, but it wants to clear otherwise so... if(target.stackTagCompound != null) { // yes it's a bit jank, but it wants to clear otherwise so...
if(crate.stackTagCompound.hasKey("lock")) if(target.stackTagCompound.hasKey("lock"))
nbt.setInteger("lock", crate.stackTagCompound.getInteger("lock")); nbt.setInteger("lock", target.stackTagCompound.getInteger("lock"));
if(crate.stackTagCompound.hasKey("lockMod")) if(target.stackTagCompound.hasKey("lockMod"))
nbt.setDouble("lockMod", crate.stackTagCompound.getDouble("lockMod")); nbt.setDouble("lockMod", target.stackTagCompound.getDouble("lockMod"));
if(crate.stackTagCompound.hasKey("spiders")) if(target.stackTagCompound.hasKey("spiders"))
nbt.setBoolean("spiders", crate.stackTagCompound.getBoolean("spiders")); // fuck you!! nbt.setBoolean("spiders", target.stackTagCompound.getBoolean("spiders")); // fuck you!!
} }
if(!nbt.hasNoTags()) { target.setTagCompound(checkNBT(nbt));
Random random = new Random(); player.inventory.setInventorySlotContents(player.inventory.currentItem, target);
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!"));
for(int i1 = 0; i1 < invSize; ++i1) {
ItemStack itemstack = this.getStackInSlot(i1);
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);
}
}
}
crate.setTagCompound(null); // Wipe tag compound to clear crate.
player.inventory.setInventorySlotContents(player.inventory.currentItem, crate);
return;
}
} catch(IOException ignored) { }
}
crate.setTagCompound(nbt);
player.inventory.setInventorySlotContents(player.inventory.currentItem, crate);
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return true;
}
@Override
public void openInventory() {
player.worldObj.playSoundEffect(player.posX, player.posY, player.posZ, "hbm:block.crateOpen", 1.0F, 0.8F);
}
@Override
public void closeInventory() {
toMarkDirty = true;
markDirty();
toMarkDirty = false;
player.worldObj.playSoundEffect(player.posX, player.posY, player.posZ, "hbm:block.crateClose", 1.0F, 0.8F);
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return true;
} }
} }
} }

View File

@ -68,7 +68,7 @@ public class ItemKitNBT extends Item {
} }
public static ItemStack create(ItemStack... contents) { public static ItemStack create(ItemStack... contents) {
ItemStack stack = new ItemStack(ModItems.kit_toolbox); ItemStack stack = new ItemStack(ModItems.legacy_toolbox);
stack.stackTagCompound = new NBTTagCompound(); stack.stackTagCompound = new NBTTagCompound();
ItemStackUtil.addStacksToNBT(stack, contents); ItemStackUtil.addStacksToNBT(stack, contents);

View File

@ -2,6 +2,7 @@ package com.hbm.items.tool;
import com.hbm.inventory.container.ContainerLeadBox; import com.hbm.inventory.container.ContainerLeadBox;
import com.hbm.inventory.gui.GUILeadBox; import com.hbm.inventory.gui.GUILeadBox;
import com.hbm.items.IItemInventory;
import com.hbm.main.MainRegistry; import com.hbm.main.MainRegistry;
import com.hbm.tileentity.IGUIProvider; import com.hbm.tileentity.IGUIProvider;
import com.hbm.util.ItemStackUtil; import com.hbm.util.ItemStackUtil;
@ -10,7 +11,6 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -45,15 +45,11 @@ public class ItemLeadBox extends Item implements IGUIProvider {
return new GUILeadBox(player.inventory, new InventoryLeadBox(player, player.getHeldItem())); return new GUILeadBox(player.inventory, new InventoryLeadBox(player, player.getHeldItem()));
} }
public static class InventoryLeadBox implements IInventory { public static class InventoryLeadBox extends IItemInventory {
public final EntityPlayer player;
public final ItemStack box;
public ItemStack[] slots;
public InventoryLeadBox(EntityPlayer player, ItemStack box) { public InventoryLeadBox(EntityPlayer player, ItemStack box) {
this.player = player; this.player = player;
this.box = box; this.target = box;
slots = new ItemStack[this.getSizeInventory()]; slots = new ItemStack[this.getSizeInventory()];
if(!box.hasTagCompound()) if(!box.hasTagCompound())
@ -66,6 +62,9 @@ public class ItemLeadBox extends Item implements IGUIProvider {
slots[i] = fromNBT[i]; slots[i] = fromNBT[i];
} }
} }
toMarkDirty = true;
this.markDirty();
toMarkDirty = false;
} }
@Override @Override
@ -73,43 +72,6 @@ public class ItemLeadBox extends Item implements IGUIProvider {
return 20; return 20;
} }
@Override
public ItemStack getStackInSlot(int slot) {
return slots[slot];
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
if (stack.stackSize > amount) {
stack = stack.splitStack(amount);
markDirty();
} else {
setInventorySlotContents(slot, null);
}
}
return stack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack stack = getStackInSlot(slot);
setInventorySlotContents(slot, null);
return stack;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
if(stack != null) {
stack.stackSize = Math.min(stack.stackSize, this.getInventoryStackLimit());
}
slots[slot] = stack;
markDirty();
}
@Override @Override
public String getInventoryName() { public String getInventoryName() {
return "container.leadBox"; return "container.leadBox";
@ -117,44 +79,12 @@ public class ItemLeadBox extends Item implements IGUIProvider {
@Override @Override
public boolean hasCustomInventoryName() { public boolean hasCustomInventoryName() {
return box.hasDisplayName(); return target.hasDisplayName();
} }
@Override @Override
public int getInventoryStackLimit() { public int getInventoryStackLimit() {
return 1; return 1;
} }
@Override
public void markDirty() {
for(int i = 0; i < getSizeInventory(); ++i) {
if(getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0) {
slots[i] = null;
}
}
ItemStackUtil.addStacksToNBT(box, slots);
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return true;
}
@Override
public void openInventory() {
player.worldObj.playSoundEffect(player.posX, player.posY, player.posZ, "hbm:block.crateOpen", 1.0F, 0.8F);
}
@Override
public void closeInventory() {
player.worldObj.playSoundEffect(player.posX, player.posY, player.posZ, "hbm:block.crateClose", 1.0F, 0.8F);
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return true;
}
} }
} }

View File

@ -0,0 +1,240 @@
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.ModItems;
import com.hbm.lib.RefStrings;
import com.hbm.main.MainRegistry;
import com.hbm.tileentity.IGUIProvider;
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.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.*;
import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.List;
public class ItemToolBox extends Item implements IGUIProvider {
@SideOnly(Side.CLIENT) protected IIcon iconOpen;
@SideOnly(Side.CLIENT) protected IIcon iconClosed;
public ItemToolBox() {
this.setMaxStackSize(1);
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister iconRegister) {
super.registerIcons(iconRegister);
this.iconOpen = iconRegister.registerIcon(RefStrings.MODID + ":kit_toolbox_empty");
this.iconClosed = iconRegister.registerIcon(RefStrings.MODID + ":kit_toolbox");
}
@Override
@SideOnly(Side.CLIENT)
public boolean requiresMultipleRenderPasses() {
return true;
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getIcon(ItemStack stack, int renderPass) {
if(stack.getTagCompound() != null && stack.getTagCompound().getBoolean("isOpen") && renderPass == 1) return this.iconOpen;
return renderPass == 1 ? this.iconClosed : getIconFromDamageForRenderPass(stack.getItemDamage(), renderPass);
}
// Finds active rows in the toolbox (rows with items inside them).
public List<Integer> getActiveRows(ItemStack box) {
ItemStack[] stacks = ItemStackUtil.readStacksFromNBT(box, 24);
if(stacks == null)
return new ArrayList<>();
List<Integer> activeRows = new ArrayList<>();
for (int row = 0; row < 3; row++) {
for (int slot = 0; slot < 8; slot++) {
if(stacks[row * 8 + slot] != null) {
activeRows.add(row);
break;
}
}
}
return activeRows;
}
// This function genuinely hurts my soul, but it works...
public void moveRows(ItemStack box, EntityPlayer player) {
// Move from hotbar into array in preparation for boxing.
ItemStack[] endingHotBar = new ItemStack[9];
ItemStack[] stacksToTransferToBox = new ItemStack[8];
boolean hasToolbox = false;
int extraToolboxes = 0;
for (int i = 0; i < 9; i++) { // Maximum allowed HotBar size is 9.
ItemStack slot = player.inventory.getStackInSlot(i);
if(slot != null && slot.getItem() == ModItems.toolbox && i != player.inventory.currentItem) {
extraToolboxes++;
player.dropPlayerItemWithRandomChoice(slot, true);
player.inventory.setInventorySlotContents(i, null);
} else if(i == player.inventory.currentItem) {
hasToolbox = true;
endingHotBar[i] = slot;
} else {
stacksToTransferToBox[i - (hasToolbox ? 1 : 0)] = slot;
}
}
if(extraToolboxes > 0) {
if(extraToolboxes == 1)
player.addChatComponentMessage(new ChatComponentText("You can't toolbox a toolbox... ").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED))); // TODO: tell someone else to do i18n stuff; i don't want to
else
player.addChatComponentMessage(new ChatComponentText("You can't toolbox a toolbox... (x" + extraToolboxes + ")").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED))); // TODO: this too :ayo:
}
// Move stacks around inside the box, mostly shifts rows to other rows and shifts the top row to the hotbar.
ItemStack[] stacks = ItemStackUtil.readStacksFromNBT(box, 24);
ItemStack[] endingStacks = new ItemStack[24];
int lowestActiveIndex = Integer.MAX_VALUE; // Lowest active index to find which row to move *to* the hotbar.
int lowestInactiveIndex = Integer.MAX_VALUE; // Lowest *inactive* index to find which row to move the hotbar to.
if(stacks != null) {
List<Integer> activeRows = getActiveRows(box);
{ // despair
for (int i = 0; i < 3; i++) {
if(activeRows.contains(i))
lowestActiveIndex = Math.min(i, lowestActiveIndex);
else
lowestInactiveIndex = Math.min(i, lowestInactiveIndex);
}
if(lowestInactiveIndex > 2) // No inactive rows...
lowestInactiveIndex = 2; // Set to the last possible row; the items will be moved out of the way in time.
else
lowestInactiveIndex = Math.max(0, lowestInactiveIndex - 1); // A little shittery to make items pop into the row that's *going* to be empty.
}
// This entire section sucks, but honestly it's not actually that bad; it works so....
for (Integer activeRowIndex : activeRows) {
int activeIndex = 8 * activeRowIndex;
if (activeRowIndex == lowestActiveIndex) { // Items to "flow" to the hotbar.
hasToolbox = false;
for (int i = 0; i < 9; i++) {
if(i == player.inventory.currentItem) {
hasToolbox = true;
continue;
}
endingHotBar[i] = stacks[activeIndex + i - (hasToolbox ? 1 : 0)];
}
continue;
}
int targetIndex = 8 * (activeRowIndex - 1);
System.arraycopy(stacks, activeIndex, endingStacks, targetIndex, 8);
}
}
// Finally, move all temporary arrays into their respective locations.
System.arraycopy(stacksToTransferToBox, 0, endingStacks, lowestInactiveIndex * 8, 8);
for (int i = 0; i < endingHotBar.length; i++) {
player.inventory.setInventorySlotContents(i, endingHotBar[i]);
}
box.setTagCompound(new NBTTagCompound());
ItemStackUtil.addStacksToNBT(box, endingStacks);
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
if(!world.isRemote) {
if (player.isSneaking()) {
moveRows(stack, player);
player.inventoryContainer.detectAndSendChanges();
} else {
if(stack.getTagCompound() == null)
stack.setTagCompound(new NBTTagCompound());
stack.getTagCompound().setBoolean("isOpen", true);
player.openGui(MainRegistry.instance, 0, world, 0, 0, 0);
}
}
return stack;
}
@Override
public Container provideContainer(int ID, EntityPlayer player, World world, int x, int y, int z) {
return new ContainerToolBox(player.inventory, new InventoryToolBox(player, player.getHeldItem()));
}
@Override
@SideOnly(Side.CLIENT)
public Object provideGUI(int ID, EntityPlayer player, World world, int x, int y, int z) {
return new GUIToolBox(player.inventory, new InventoryToolBox(player, player.getHeldItem()));
}
public static class InventoryToolBox extends IItemInventory {
public InventoryToolBox(EntityPlayer player, ItemStack box) {
this.player = player;
this.target = box;
slots = new ItemStack[this.getSizeInventory()];
if(!box.hasTagCompound())
box.setTagCompound(new NBTTagCompound());
ItemStack[] fromNBT = ItemStackUtil.readStacksFromNBT(box, slots.length);
if(fromNBT != null) {
System.arraycopy(fromNBT, 0, slots, 0, slots.length);
}
toMarkDirty = true;
this.markDirty();
toMarkDirty = false;
}
@Override
public int getSizeInventory() {
return 24;
}
@Override
public String getInventoryName() {
return "container.toolBox";
}
@Override
public boolean hasCustomInventoryName() {
return target.hasDisplayName();
}
@Override
public void closeInventory() {
this.target.getTagCompound().removeTag("isOpen");
this.player.inventory.setInventorySlotContents(this.player.inventory.currentItem, this.target);
super.closeInventory();
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return stack.getItem() != ModItems.toolbox;
}
}
}

View File

@ -66,12 +66,10 @@ public class CraftingManager {
GameRegistry.addRecipe(new RBMKFuelCraftingHandler()); GameRegistry.addRecipe(new RBMKFuelCraftingHandler());
GameRegistry.addRecipe(new MKUCraftingHandler()); GameRegistry.addRecipe(new MKUCraftingHandler());
GameRegistry.addRecipe(new ToolboxCraftingHandler());
GameRegistry.addRecipe(new CargoShellCraftingHandler()); GameRegistry.addRecipe(new CargoShellCraftingHandler());
GameRegistry.addRecipe(new ScrapsCraftingHandler()); GameRegistry.addRecipe(new ScrapsCraftingHandler());
RecipeSorter.register("hbm:rbmk", RBMKFuelCraftingHandler.class, RecipeSorter.Category.SHAPELESS, "after:minecraft:shapeless"); RecipeSorter.register("hbm:rbmk", RBMKFuelCraftingHandler.class, RecipeSorter.Category.SHAPELESS, "after:minecraft:shapeless");
RecipeSorter.register("hbm:toolbox", ToolboxCraftingHandler.class, RecipeSorter.Category.SHAPELESS, "after:minecraft:shapeless");
RecipeSorter.register("hbm:cargo", CargoShellCraftingHandler.class, RecipeSorter.Category.SHAPELESS, "after:minecraft:shapeless"); RecipeSorter.register("hbm:cargo", CargoShellCraftingHandler.class, RecipeSorter.Category.SHAPELESS, "after:minecraft:shapeless");
RecipeSorter.register("hbm:scraps", ScrapsCraftingHandler.class, RecipeSorter.Category.SHAPELESS, "after:minecraft:shapeless"); RecipeSorter.register("hbm:scraps", ScrapsCraftingHandler.class, RecipeSorter.Category.SHAPELESS, "after:minecraft:shapeless");
RecipeSorter.register("hbm:mku", MKUCraftingHandler.class, RecipeSorter.Category.SHAPED, "after:minecraft:shaped before:minecraft:shapeless"); RecipeSorter.register("hbm:mku", MKUCraftingHandler.class, RecipeSorter.Category.SHAPED, "after:minecraft:shaped before:minecraft:shapeless");

View File

@ -1672,6 +1672,9 @@ public class MainRegistry {
remapItems.put("hbm:item.briquette_lignite", ModItems.briquette); remapItems.put("hbm:item.briquette_lignite", ModItems.briquette);
remapItems.put("hbm:item.antiknock", ModItems.fuel_additive); remapItems.put("hbm:item.antiknock", ModItems.fuel_additive);
remapItems.put("hbm:item.kit_toolbox_empty", ModItems.toolbox);
remapItems.put("hbm:item.kit_toolbox", ModItems.legacy_toolbox);
for(MissingMapping mapping : event.get()) { for(MissingMapping mapping : event.get()) {
// ignore all ammo prefixes because those are from the time we threw out all the ammo items // ignore all ammo prefixes because those are from the time we threw out all the ammo items

View File

@ -849,6 +849,7 @@ container.soyuzLauncher=Soyuz Launch Platform
container.storageDrum=Nuclear Waste Disposal Drum container.storageDrum=Nuclear Waste Disposal Drum
container.teleLinker=TelLink Device container.teleLinker=TelLink Device
container.teleporter=Teleporter container.teleporter=Teleporter
container.toolBox=Toolbox
container.trainTram=Electric Flat Bed Tram container.trainTram=Electric Flat Bed Tram
container.turbinegas=Combined Cycle Gas Turbine container.turbinegas=Combined Cycle Gas Turbine
container.turretArty=Greg container.turretArty=Greg
@ -3295,8 +3296,8 @@ item.key_red.desc.P11=§4e§r
item.key_red_cracked.name=Cracked Key item.key_red_cracked.name=Cracked Key
item.key_red_cracked.desc=??? item.key_red_cracked.desc=???
item.key_red.key_red_cracked.P11=§4???§r item.key_red.key_red_cracked.P11=§4???§r
item.kit_toolbox.name=Toolbox item.toolbox.name=Toolbox
item.kit_toolbox_empty.name=Empty Toolbox item.toolbox_legacy.name=Toolbox (LEGACY)
item.laser_crystal_bismuth.desc=Bismuth-Samarium-Uranium-Thorium crystal matrix item.laser_crystal_bismuth.desc=Bismuth-Samarium-Uranium-Thorium crystal matrix
item.laser_crystal_bismuth.name=BiSmUTh Laser Crystal item.laser_crystal_bismuth.name=BiSmUTh Laser Crystal
item.laser_crystal_cmb.desc=Antischrabidium Suspended in a CMB-Schrabidate Alloy Lattice item.laser_crystal_cmb.desc=Antischrabidium Suspended in a CMB-Schrabidate Alloy Lattice

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB