Container base class to make life easier

This commit is contained in:
70000hp 2024-06-01 15:23:47 -04:00
parent 9f567135d2
commit eb8155b11b
10 changed files with 98 additions and 127 deletions

View File

@ -0,0 +1,67 @@
package com.hbm.inventory.container;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**For now, only used for stuff with filters and crates as a reference 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 {
protected IInventory te;
public ContainerBase (InventoryPlayer invPlayer, IInventory tedf){
te = tedf;
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return te.isUseableByPlayer(player);
}
@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 <= te.getSizeInventory() - 1) {
if(!this.mergeItemStack(var5, te.getSizeInventory(), this.inventorySlots.size(), true)) {
return null;
}
} else if(!this.mergeItemStack(var5, 0, te.getSizeInventory(), false)) {
return null;
}
if(var5.stackSize == 0) {
var4.putStack(null);
} else {
var4.onSlotChanged();
}
var4.onPickupFromSlot(p_82846_1_, var5);
}
return var3;
}
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));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, playerInvX + i * 18, playerHotbarY));
}
}
}

View File

@ -1,57 +1,25 @@
package com.hbm.inventory.container;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
public class ContainerCrateBase extends Container {
protected IInventory crate;
public class ContainerCrateBase extends ContainerBase {
public ContainerCrateBase(IInventory tedf) {
crate = tedf;
crate.openInventory();
//just there so prev stuff doesnt break
protected IInventory crate = te;
public ContainerCrateBase(InventoryPlayer invPlayer, IInventory tedf) {
super(invPlayer, tedf);
te.openInventory();
}
@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 <= crate.getSizeInventory() - 1) {
if(!this.mergeItemStack(var5, crate.getSizeInventory(), this.inventorySlots.size(), true)) {
return null;
}
} else if(!this.mergeItemStack(var5, 0, crate.getSizeInventory(), false)) {
return null;
}
if(var5.stackSize == 0) {
var4.putStack((ItemStack) null);
} else {
var4.onSlotChanged();
}
var4.onPickupFromSlot(p_82846_1_, var5);
}
return var3;
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return crate.isUseableByPlayer(player);
}
@Override
public void onContainerClosed(EntityPlayer p_75134_1_) {
super.onContainerClosed(p_75134_1_);
this.crate.closeInventory();
te.closeInventory();
}
}

View File

@ -7,7 +7,7 @@ import net.minecraft.inventory.Slot;
public class ContainerCrateDesh extends ContainerCrateBase {
public ContainerCrateDesh(InventoryPlayer invPlayer, IInventory tedf) {
super(tedf);
super(invPlayer,tedf);
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 13; j++) {
@ -15,14 +15,7 @@ public class ContainerCrateDesh extends ContainerCrateBase {
}
}
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 9; j++) {
this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, 44 + j * 18, 174 + i * 18));
}
}
this.playerInv(invPlayer,44, 174, 232);
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, 44 + i * 18, 232));
}
}
}

View File

@ -9,22 +9,13 @@ import net.minecraft.inventory.Slot;
public class ContainerCrateIron extends ContainerCrateBase {
public ContainerCrateIron(InventoryPlayer invPlayer, TileEntityCrateIron tedf) {
super(tedf);
super(invPlayer,tedf);
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 9; j++) {
this.addSlotToContainer(new Slot(tedf, j + i * 9, 8 + j * 18, 18 + 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, 84 + i * 18 + 20));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142 + 20));
}
this.playerInv(invPlayer, 8, 84 + 20, 142 + 20);
}
}

View File

@ -8,7 +8,7 @@ import net.minecraft.inventory.Slot;
public class ContainerCrateSteel extends ContainerCrateBase {
public ContainerCrateSteel(InventoryPlayer invPlayer, IInventory tedf) {
super(tedf);
super(invPlayer,tedf);
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 9; j++) {
@ -16,14 +16,6 @@ public class ContainerCrateSteel extends ContainerCrateBase {
}
}
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, 84 + i * 18 + (18 * 3) + 2));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142 + (18 * 3) + 2));
}
this.playerInv(invPlayer,8, 84 + (18 * 3) + 2, 142 + (18 * 3) + 2);
}
}

View File

@ -8,7 +8,7 @@ import net.minecraft.inventory.Slot;
public class ContainerCrateTemplate extends ContainerCrateBase {
public ContainerCrateTemplate(InventoryPlayer invPlayer, TileEntityCrateTemplate tedf) {
super(tedf);
super(invPlayer,tedf);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 9; j++) {
@ -16,14 +16,6 @@ public class ContainerCrateTemplate extends ContainerCrateBase {
}
}
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, 86 + i * 18));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 144));
}
this.playerInv(invPlayer,8, 86, 144);
}
}

View File

@ -9,7 +9,7 @@ import net.minecraft.inventory.Slot;
public class ContainerCrateTungsten extends ContainerCrateBase {
public ContainerCrateTungsten(InventoryPlayer invPlayer, TileEntityCrateTungsten te) {
super(te);
super(invPlayer,te);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 9; j++) {
@ -17,14 +17,6 @@ public class ContainerCrateTungsten extends ContainerCrateBase {
}
}
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, 32 + i * 18 + (18 * 3)));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 90 + (18 * 3)));
}
this.playerInv(invPlayer,8, 32 + 18 * 3, 90 + (18 * 3));
}
}

View File

@ -8,7 +8,7 @@ import net.minecraft.inventory.Slot;
public class ContainerDroneDock extends ContainerCrateBase {
public ContainerDroneDock(InventoryPlayer invPlayer, TileEntityDroneDock tedf) {
super(tedf);
super(invPlayer,tedf);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
@ -16,14 +16,6 @@ public class ContainerDroneDock extends ContainerCrateBase {
}
}
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, 103 + i * 18));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 161));
}
this.playerInv(invPlayer, 8, 103, 161);
}
}

View File

@ -8,7 +8,7 @@ import net.minecraft.inventory.Slot;
public class ContainerDroneProvider extends ContainerCrateBase {
public ContainerDroneProvider(InventoryPlayer invPlayer, TileEntityDroneProvider tedf) {
super(tedf);
super(invPlayer,tedf);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
@ -16,14 +16,6 @@ public class ContainerDroneProvider extends ContainerCrateBase {
}
}
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, 103 + i * 18));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 161));
}
this.playerInv(invPlayer, 8, 103, 161);
}
}

View File

@ -9,10 +9,10 @@ import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
public class ContainerDroneRequester extends ContainerCrateBase {
public ContainerDroneRequester(InventoryPlayer invPlayer, TileEntityDroneRequester tedf) {
super(tedf);
super(invPlayer,tedf);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
this.addSlotToContainer(new SlotPattern(tedf, j + i * 3, 98 + j * 18, 17 + i * 18));
@ -25,15 +25,7 @@ public class ContainerDroneRequester extends ContainerCrateBase {
}
}
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, 103 + i * 18));
}
}
for(int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 161));
}
this.playerInv(invPlayer, 8, 103, 161);
}
@Override
@ -44,7 +36,7 @@ public class ContainerDroneRequester extends ContainerCrateBase {
if(var4 != null && var4.getHasStack()) {
ItemStack var5 = var4.getStack();
var3 = var5.copy();
if(par2 < 9) return null; //ignore filters
if(par2 <= crate.getSizeInventory() - 1) {
@ -69,18 +61,18 @@ public class ContainerDroneRequester extends ContainerCrateBase {
@Override
public ItemStack slotClick(int index, int button, int mode, EntityPlayer player) {
//L/R: 0
//M3: 3
//SHIFT: 1
//DRAG: 5
if(index < 0 || index > 8) {
return super.slotClick(index, button, mode, player);
}
Slot slot = this.getSlot(index);
ItemStack ret = null;
ItemStack held = player.inventory.getItemStack();
TileEntityDroneRequester requester = (TileEntityDroneRequester) crate;