added ability to open backpacks enabled by NoHotbarNeeded (#105)

This commit is contained in:
Ed Rakhmankulov authored and GitHub committed 2026-05-24 14:59:08 +02:00
1 parent 3470c6c7d1
commit bbb374cd5e
22 files changed
+233 -82

No files matched your search

@@ -13,6 +13,7 @@ import net.minecraft.item.ItemStack;
import forestry.core.gui.ContainerItemInventory;
import forestry.core.gui.slots.SlotFilteredInventory;
import forestry.core.inventory.ItemLocation;
import forestry.storage.inventory.ItemInventoryBackpack;
public class ContainerBackpack extends ContainerItemInventory<ItemInventoryBackpack> {
@@ -39,9 +40,9 @@ public class ContainerBackpack extends ContainerItemInventory<ItemInventoryBackp
}
}
public ContainerBackpack(EntityPlayer player, Size size, ItemStack parent) {
public ContainerBackpack(EntityPlayer player, Size size, ItemStack parent, ItemLocation loc) {
super(
new ItemInventoryBackpack(player, size.getSize(), parent),
new ItemInventoryBackpack(player, size.getSize(), parent, loc),
player.inventory,
8,
11 + size.startY + size.rows * 18);
@@ -6,6 +6,7 @@ import net.minecraft.item.ItemStack;
import forestry.api.storage.IBackpackDefinition;
import forestry.core.inventory.ItemInventory;
import forestry.core.inventory.ItemLocation;
import forestry.storage.items.ItemBackpack;
public class ItemInventoryBackpack extends ItemInventory {
@@ -13,7 +14,11 @@ public class ItemInventoryBackpack extends ItemInventory {
private final IBackpackDefinition backpackDefinition;
public ItemInventoryBackpack(EntityPlayer player, int size, ItemStack parent) {
super(player, size, parent);
this(player, size, parent, ItemLocation.UNKNOWN);
}
public ItemInventoryBackpack(EntityPlayer player, int size, ItemStack parent, ItemLocation location) {
super(player, size, parent, location);
if (parent == null) {
throw new IllegalArgumentException("Parent cannot be null.");
@@ -13,6 +13,7 @@ import net.minecraft.item.ItemStack;
import forestry.core.gui.GuiHandler;
import forestry.core.gui.IPagedInventory;
import forestry.core.inventory.ItemLocation;
import forestry.storage.items.ItemBackpackNaturalist;
public class ItemInventoryBackpackPaged extends ItemInventoryBackpack implements IPagedInventory {
@@ -20,8 +21,8 @@ public class ItemInventoryBackpackPaged extends ItemInventoryBackpack implements
private final ItemBackpackNaturalist backpackNaturalist;
public ItemInventoryBackpackPaged(EntityPlayer player, int size, ItemStack itemstack,
ItemBackpackNaturalist backpackNaturalist) {
super(player, size, itemstack);
ItemBackpackNaturalist backpackNaturalist, ItemLocation loc) {
super(player, size, itemstack, loc);
this.backpackNaturalist = backpackNaturalist;
}
@@ -13,6 +13,7 @@ import java.util.Locale;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
@@ -20,7 +21,9 @@ import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import net.xonich.mc.nohotbarneeded.api.ActivatableFromInventoryServerSide;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -31,6 +34,7 @@ import forestry.core.config.Config;
import forestry.core.config.Constants;
import forestry.core.gui.GuiHandler;
import forestry.core.inventory.ItemInventory;
import forestry.core.inventory.ItemLocation;
import forestry.core.inventory.wrappers.IInvSlot;
import forestry.core.inventory.wrappers.InventoryIterator;
import forestry.core.items.ItemWithGui;
@@ -44,7 +48,10 @@ import forestry.storage.gui.GuiBackpack;
import forestry.storage.gui.GuiBackpackT2;
import forestry.storage.inventory.ItemInventoryBackpack;
public class ItemBackpack extends ItemWithGui {
@Optional.Interface(
iface = "net.xonich.mc.nohotbarneeded.api.ActivatableFromInventoryServerSide",
modid = "nohotbarneeded")
public class ItemBackpack extends ItemWithGui implements ActivatableFromInventoryServerSide {
private final IBackpackDefinition definition;
private final EnumBackpackType type;
@@ -318,32 +325,32 @@ public class ItemBackpack extends ItemWithGui {
}
@Override
public Object getGui(EntityPlayer player, ItemStack heldItem, int data) {
if (data > EnumBackpackType.values().length) {
public Object getGui(EntityPlayer player, ItemStack heldItem, int data, ItemLocation loc) {
if (data >= EnumBackpackType.values().length) {
return null;
}
EnumBackpackType type = EnumBackpackType.values()[data];
switch (type) {
case T1:
return new GuiBackpack(new ContainerBackpack(player, ContainerBackpack.Size.DEFAULT, heldItem));
return new GuiBackpack(new ContainerBackpack(player, ContainerBackpack.Size.DEFAULT, heldItem, loc));
case T2:
return new GuiBackpackT2(new ContainerBackpack(player, ContainerBackpack.Size.T2, heldItem));
return new GuiBackpackT2(new ContainerBackpack(player, ContainerBackpack.Size.T2, heldItem, loc));
default:
return null;
}
}
@Override
public Object getContainer(EntityPlayer player, ItemStack heldItem, int data) {
if (data > EnumBackpackType.values().length) {
public Object getContainer(EntityPlayer player, ItemStack heldItem, int data, ItemLocation loc) {
if (data >= EnumBackpackType.values().length) {
return null;
}
EnumBackpackType type = EnumBackpackType.values()[data];
switch (type) {
case T1:
return new ContainerBackpack(player, ContainerBackpack.Size.DEFAULT, heldItem);
return new ContainerBackpack(player, ContainerBackpack.Size.DEFAULT, heldItem, loc);
case T2:
return new ContainerBackpack(player, ContainerBackpack.Size.T2, heldItem);
return new ContainerBackpack(player, ContainerBackpack.Size.T2, heldItem, loc);
default:
return null;
}
@@ -361,4 +368,10 @@ public class ItemBackpack extends ItemWithGui {
public boolean showDurabilityBar(final ItemStack stack) {
return Config.showBackpackDurability && ItemInventory.getOccupiedSlotCount(stack) > 0;
}
@Override
@Optional.Method(modid = "nohotbarneeded")
public void activateFromInventory(EntityPlayerMP player, int slotIdx) {
GuiHandler.openGui(player, this, (short) type.ordinal(), slotIdx);
}
}
@@ -9,14 +9,17 @@
package forestry.storage.items;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.Optional;
import forestry.api.genetics.ISpeciesRoot;
import forestry.api.storage.EnumBackpackType;
import forestry.api.storage.IBackpackDefinition;
import forestry.core.config.Constants;
import forestry.core.gui.GuiHandler;
import forestry.core.gui.GuiNaturalistInventory;
import forestry.core.inventory.ItemLocation;
import forestry.storage.gui.ContainerNaturalistBackpack;
import forestry.storage.inventory.ItemInventoryBackpackPaged;
@@ -35,23 +38,31 @@ public class ItemBackpackNaturalist extends ItemBackpack {
}
@Override
public Object getGui(EntityPlayer player, ItemStack heldItem, int page) {
public Object getGui(EntityPlayer player, ItemStack heldItem, int page, ItemLocation loc) {
ItemInventoryBackpackPaged inventory = new ItemInventoryBackpackPaged(
player,
Constants.SLOTS_BACKPACK_APIARIST,
heldItem,
this);
this,
loc);
ContainerNaturalistBackpack container = new ContainerNaturalistBackpack(player, inventory, page);
return new GuiNaturalistInventory(speciesRoot, player, container, inventory, page, 5);
}
@Override
public Object getContainer(EntityPlayer player, ItemStack heldItem, int page) {
public Object getContainer(EntityPlayer player, ItemStack heldItem, int page, ItemLocation loc) {
ItemInventoryBackpackPaged inventory = new ItemInventoryBackpackPaged(
player,
Constants.SLOTS_BACKPACK_APIARIST,
heldItem,
this);
this,
loc);
return new ContainerNaturalistBackpack(player, inventory, page);
}
@Override
@Optional.Method(modid = "nohotbarneeded")
public void activateFromInventory(EntityPlayerMP player, int slotIdx) {
GuiHandler.openGui(player, this, /* page */(short) 0, slotIdx);
}
}