mirror of
https://github.com/GTNewHorizons/Baubles-Expanded.git
synced 2026-11-19 03:39:25 +00:00
New Curios-Inspired GUI (#13)
This commit is contained in:
1 parent
63833f2fbb
commit
b57a7a1dd6
13 files changed
+704
-322
No files matched your search
@@ -10,29 +10,51 @@ import cpw.mods.fml.relauncher.SideOnly;
|
||||
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.InventoryCraftResult;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.inventory.SlotCrafting;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemArmor;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.util.IIcon;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static baubles.common.BaublesConfig.useOldGuiRendering;
|
||||
|
||||
public class ContainerPlayerExpanded extends Container {
|
||||
|
||||
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
|
||||
public IInventory craftResult = new InventoryCraftResult();
|
||||
public InventoryBaubles baubles;
|
||||
|
||||
private final EntityPlayer thePlayer;
|
||||
|
||||
public ContainerPlayerExpanded(InventoryPlayer playerInv, boolean par2, EntityPlayer player) {
|
||||
public ContainerPlayerExpanded(InventoryPlayer playerInv, boolean isClient, EntityPlayer player) {
|
||||
this.thePlayer = player;
|
||||
baubles = new InventoryBaubles(player);
|
||||
baubles.setEventHandler(this);
|
||||
if (!player.worldObj.isRemote) {
|
||||
if (isClient) {
|
||||
baubles.stackList = PlayerHandler.getPlayerBaubles(player).stackList;
|
||||
}
|
||||
|
||||
int i;
|
||||
int j;
|
||||
|
||||
//armor
|
||||
if (!useOldGuiRendering) {
|
||||
this.addSlotToContainer(new SlotCrafting(playerInv.player, this.craftMatrix, this.craftResult, 0, 144, 36));
|
||||
|
||||
for (i = 0; i < 2; ++i) {
|
||||
for (j = 0; j < 2; ++j) {
|
||||
this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, 88 + j * 18, 26 + i * 18));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Armor slots
|
||||
for (i = 0; i < 4; ++i) {
|
||||
final int k = i;
|
||||
this.addSlotToContainer(new Slot(playerInv, playerInv.getSizeInventory() - 1 - i, 8, 8 + i * 18) {
|
||||
@@ -41,7 +63,7 @@ public class ContainerPlayerExpanded extends Container {
|
||||
|
||||
@Override
|
||||
public boolean isItemValid(ItemStack itemStack) {
|
||||
if (itemStack == null) return false;
|
||||
if (itemStack == null || itemStack.getItem() == null) return false;
|
||||
return itemStack.getItem().isValidArmor(itemStack, k, thePlayer);
|
||||
}
|
||||
|
||||
@@ -57,26 +79,93 @@ public class ContainerPlayerExpanded extends Container {
|
||||
final int slotStartX = 80;
|
||||
final int slotStartY = 8;
|
||||
|
||||
//bauble slots
|
||||
for(i = 0; i < BaubleExpandedSlots.slotLimit; i++) {
|
||||
String slotType = BaubleExpandedSlots.getSlotType(i);
|
||||
if(BaublesConfig.showUnusedSlots || !slotType.equals(BaubleExpandedSlots.unknownType)) {
|
||||
addSlotToContainer(new SlotBauble(baubles, slotType, i, slotStartX + (slotOffset * (i / 4)), slotStartY + (slotOffset * (i % 4))));
|
||||
}
|
||||
}
|
||||
|
||||
//inventory slots
|
||||
for (i = 0; i < 3; ++i) {
|
||||
for (j = 0; j < 9; ++j) {
|
||||
addSlotToContainer(new Slot(playerInv, j + (i + 1) * 9, slotStartY + j * slotOffset, 84 + i * 18));
|
||||
// Bauble slots
|
||||
for (i = 0; i < BaubleExpandedSlots.slotLimit; i++) {
|
||||
String slotType = BaubleExpandedSlots.getSlotType(i);
|
||||
if (BaublesConfig.showUnusedSlots || !slotType.equals(BaubleExpandedSlots.unknownType)) {
|
||||
if (useOldGuiRendering) {
|
||||
addSlotToContainer(new SlotBauble(baubles, slotType, i, slotStartX + (slotOffset * (i / 4)), slotStartY + (slotOffset * (i % 4))));
|
||||
} else {
|
||||
addSlotToContainer(new SlotBauble(baubles, slotType, i, -18, 12 + (slotOffset * i)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//hotbar slots
|
||||
for (i = 0; i < 9; ++i) {
|
||||
addSlotToContainer(new Slot(playerInv, i, slotStartY + i * slotOffset, 142));
|
||||
// Inventory slots
|
||||
for (i = 0; i < 3; ++i) {
|
||||
for (j = 0; j < 9; ++j) {
|
||||
this.addSlotToContainer(new Slot(playerInv, j + (i + 1) * 9, slotStartY + j * slotOffset, 84 + i * 18));
|
||||
}
|
||||
}
|
||||
|
||||
// Hotbar slots
|
||||
for (i = 0; i < 9; ++i) {
|
||||
this.addSlotToContainer(new Slot(playerInv, i, slotStartY + i * slotOffset, 142));
|
||||
}
|
||||
|
||||
if (!useOldGuiRendering) {
|
||||
this.onCraftMatrixChanged(this.craftMatrix);
|
||||
this.scrollTo(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCraftMatrixChanged(IInventory par1IInventory) {
|
||||
if (!useOldGuiRendering) {
|
||||
this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.thePlayer.worldObj));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContainerClosed(EntityPlayer player) {
|
||||
super.onContainerClosed(player);
|
||||
if (useOldGuiRendering) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
|
||||
|
||||
if (itemstack != null) {
|
||||
if (!player.inventory.addItemStackToInventory(itemstack)) {
|
||||
player.dropPlayerItemWithRandomChoice(itemstack, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.craftResult.setInventorySlotContents(0, null);
|
||||
if (!player.worldObj.isRemote) {
|
||||
PlayerHandler.setPlayerBaubles(player, baubles);
|
||||
}
|
||||
}
|
||||
|
||||
public void scrollTo(float offset) {
|
||||
if (!canScroll()) return;
|
||||
final int activeBaubleSlots = BaubleExpandedSlots.slotsCurrentlyUsed();
|
||||
|
||||
offset = Math.max(0, Math.min(1, offset));
|
||||
|
||||
int shownSlots = 8;
|
||||
int slotOffset = (int) (offset * (activeBaubleSlots - shownSlots) + 0.5F);
|
||||
|
||||
if (slotOffset < 0) {
|
||||
slotOffset = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < activeBaubleSlots && i < BaubleExpandedSlots.slotLimit; i++) {
|
||||
//TODO: Find a way to not use a magic number to get to the correct ID range for the Slot editing.
|
||||
Slot slot = (Slot) this.inventorySlots.get(9 + i);
|
||||
if (i >= 0) {
|
||||
slot.yDisplayPosition = (12 - (slotOffset * 18) + (i) * 18);
|
||||
if (slot.yDisplayPosition < 12 || slot.yDisplayPosition > 8 * 18) {
|
||||
// Hide the rest of the slots!
|
||||
slot.yDisplayPosition = -2000;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canScroll() {
|
||||
return BaubleExpandedSlots.slotsCurrentlyUsed() > 8 && !useOldGuiRendering;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,75 +174,96 @@ public class ContainerPlayerExpanded extends Container {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
|
||||
* Called when a player shift-clicks on a slot. You must override this, or you will crash when someone does that.
|
||||
*/
|
||||
@Override
|
||||
public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) {
|
||||
ItemStack returnStack = null;
|
||||
Slot slot = (Slot)inventorySlots.get(slotIndex);
|
||||
final int visibleBaubleSlots = BaublesConfig.showUnusedSlots ? BaubleExpandedSlots.slotLimit : BaubleExpandedSlots.slotsCurrentlyUsed();
|
||||
|
||||
if(slot != null && slot.getHasStack()) {
|
||||
ItemStack originalStack = slot.getStack();
|
||||
returnStack = originalStack.copy();
|
||||
Item item = returnStack.getItem();
|
||||
|
||||
if(item instanceof ItemArmor && !((Slot)inventorySlots.get(((ItemArmor)item).armorType)).getHasStack()) {
|
||||
int armorSlot = ((ItemArmor)item).armorType;
|
||||
if(!mergeItemStack(originalStack, armorSlot, armorSlot + 1, false)) {
|
||||
returnStack = null;
|
||||
}
|
||||
} else if(slotIndex >= 4 + visibleBaubleSlots && item instanceof IBauble && ((IBauble) item).canEquip(returnStack, thePlayer)) {
|
||||
for(int baubleSlot = 4; baubleSlot < 4 + visibleBaubleSlots; baubleSlot++) {
|
||||
if(returnStack == null) {
|
||||
break;
|
||||
}
|
||||
if(!((Slot)inventorySlots.get(baubleSlot)).getHasStack()) {
|
||||
String[] types;
|
||||
if(item instanceof IBaubleExpanded) {
|
||||
types = ((IBaubleExpanded)item).getBaubleTypes(returnStack);
|
||||
} else {
|
||||
types = new String[] {BaubleExpandedSlots.getTypeFromBaubleType(((IBauble)item).getBaubleType(returnStack))};
|
||||
}
|
||||
for(String type : types) {
|
||||
if((type.equals(BaubleExpandedSlots.universalType) || type.equals(BaubleExpandedSlots.getSlotType(baubleSlot - 4))) && !mergeItemStack(originalStack, baubleSlot, baubleSlot + 1, false)) {
|
||||
returnStack = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if(slotIndex >= 4 + visibleBaubleSlots && slotIndex < 31 + visibleBaubleSlots) {
|
||||
if(!mergeItemStack(originalStack, 31 + visibleBaubleSlots, 40 + visibleBaubleSlots, false)) {
|
||||
returnStack = null;
|
||||
}
|
||||
} else if(slotIndex >= 31 + visibleBaubleSlots && slotIndex < 40 + visibleBaubleSlots) {
|
||||
if(!mergeItemStack(originalStack, 4 + visibleBaubleSlots, 31 + visibleBaubleSlots, false)) {
|
||||
returnStack = null;
|
||||
}
|
||||
} else if(!mergeItemStack(originalStack, 4 + visibleBaubleSlots, 40 + visibleBaubleSlots, false, slot)) {
|
||||
returnStack = null;
|
||||
}
|
||||
|
||||
if(originalStack.stackSize <= 0) {
|
||||
slot.putStack(null);
|
||||
} else {
|
||||
slot.onSlotChanged();
|
||||
}
|
||||
|
||||
if(returnStack != null && originalStack.stackSize == returnStack.stackSize) {
|
||||
returnStack = null;
|
||||
}
|
||||
|
||||
slot.onPickupFromSlot(player, originalStack);
|
||||
Slot slot = (Slot) inventorySlots.get(slotIndex);
|
||||
final int visibleBaubleSlots = BaubleExpandedSlots.slotsCurrentlyUsed();
|
||||
int craftingActive = 5;
|
||||
if (useOldGuiRendering) {
|
||||
craftingActive = 0;
|
||||
}
|
||||
|
||||
if (slot == null || !slot.getHasStack()) {
|
||||
return returnStack;
|
||||
}
|
||||
ItemStack originalStack = slot.getStack();
|
||||
returnStack = originalStack.copy();
|
||||
Item item = returnStack.getItem();
|
||||
|
||||
if (!useOldGuiRendering) {
|
||||
if (slotIndex == 0) {
|
||||
if (!mergeItemStack(originalStack, 4 + craftingActive + visibleBaubleSlots, 40 + craftingActive + visibleBaubleSlots, true)) {
|
||||
return null;
|
||||
}
|
||||
slot.onSlotChange(originalStack, returnStack);
|
||||
} else if (slotIndex < 5) {
|
||||
if (!mergeItemStack(originalStack, 4 + craftingActive + visibleBaubleSlots, 40 + craftingActive + visibleBaubleSlots, false)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (item instanceof ItemArmor armor && !((Slot) inventorySlots.get(craftingActive + armor.armorType)).getHasStack()) {
|
||||
int armorSlot = craftingActive + armor.armorType;
|
||||
if (!mergeItemStack(originalStack, armorSlot, armorSlot + 1, false)) {
|
||||
returnStack = null;
|
||||
}
|
||||
} else if (slotIndex >= 4 + craftingActive + visibleBaubleSlots && item instanceof IBauble bauble && bauble.canEquip(returnStack, thePlayer)) {
|
||||
for (int baubleSlot = 4 + craftingActive; baubleSlot < 4 + craftingActive + visibleBaubleSlots; baubleSlot++) {
|
||||
if (returnStack == null) {
|
||||
break;
|
||||
}
|
||||
if (((Slot) inventorySlots.get(baubleSlot)).getHasStack()) {
|
||||
continue;
|
||||
}
|
||||
String[] types;
|
||||
if (item instanceof IBaubleExpanded) {
|
||||
types = ((IBaubleExpanded) item).getBaubleTypes(returnStack);
|
||||
} else {
|
||||
types = new String[] {BaubleExpandedSlots.getTypeFromBaubleType(bauble.getBaubleType(returnStack))};
|
||||
}
|
||||
for (String type : types) {
|
||||
if ((type.equals(BaubleExpandedSlots.universalType)
|
||||
|| type.equals(BaubleExpandedSlots.getSlotType(baubleSlot - 4 - craftingActive)))
|
||||
&& !mergeItemStack(originalStack, baubleSlot, baubleSlot + 1, false)) {
|
||||
returnStack = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (slotIndex >= 4 + craftingActive + visibleBaubleSlots && slotIndex < 31 + craftingActive + visibleBaubleSlots) {
|
||||
if (!mergeItemStack(originalStack, 31 + craftingActive + visibleBaubleSlots, 40 + craftingActive + visibleBaubleSlots, false)) {
|
||||
returnStack = null;
|
||||
}
|
||||
} else if (slotIndex >= 31 + craftingActive + visibleBaubleSlots && slotIndex < 40 + craftingActive + visibleBaubleSlots) {
|
||||
if (!mergeItemStack(originalStack, 4 + craftingActive + visibleBaubleSlots, 31 + craftingActive + visibleBaubleSlots, false)) {
|
||||
returnStack = null;
|
||||
}
|
||||
} else if (!mergeItemStack(originalStack, 4 + craftingActive + visibleBaubleSlots, 40 + craftingActive + visibleBaubleSlots, false, slot)) {
|
||||
returnStack = null;
|
||||
}
|
||||
|
||||
if (originalStack.stackSize <= 0) {
|
||||
slot.putStack(null);
|
||||
} else {
|
||||
slot.onSlotChanged();
|
||||
}
|
||||
|
||||
if (returnStack != null && originalStack.stackSize == returnStack.stackSize) {
|
||||
returnStack = null;
|
||||
}
|
||||
|
||||
slot.onPickupFromSlot(player, originalStack);
|
||||
|
||||
return returnStack;
|
||||
}
|
||||
|
||||
private void unequipBauble(ItemStack stack) {
|
||||
// if (stack.getItem() instanceof IBauble) {
|
||||
// ((IBauble)stack.getItem()).onUnequipped(stack, thePlayer);
|
||||
// }
|
||||
//if (stack.getItem() instanceof IBauble) {
|
||||
// ((IBauble)stack.getItem()).onUnequipped(stack, thePlayer);
|
||||
//}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -162,75 +272,67 @@ public class ContainerPlayerExpanded extends Container {
|
||||
super.putStacksInSlots(p_75131_1_);
|
||||
}
|
||||
|
||||
protected boolean mergeItemStack(ItemStack itemStack, int par2, int par3, boolean par4, Slot ss) {
|
||||
boolean flag1 = false;
|
||||
int k = par2;
|
||||
protected boolean mergeItemStack(ItemStack sourceStack, int startIndex, int endIndex, boolean reverse, Slot sourceSlot) {
|
||||
boolean merged = false;
|
||||
int index = reverse ? endIndex - 1 : startIndex;
|
||||
|
||||
if (par4) {
|
||||
k = par3 - 1;
|
||||
}
|
||||
Slot targetSlot;
|
||||
ItemStack targetStack;
|
||||
|
||||
Slot slot;
|
||||
ItemStack itemstack1;
|
||||
// First pass: try stacking into existing stacks
|
||||
if (sourceStack.isStackable()) {
|
||||
while (sourceStack.stackSize > 0 && (!reverse && index < endIndex || reverse && index >= startIndex)) {
|
||||
targetSlot = (Slot) inventorySlots.get(index);
|
||||
targetStack = targetSlot.getStack();
|
||||
|
||||
if (itemStack.isStackable()) {
|
||||
while (itemStack.stackSize > 0 && (!par4 && k < par3 || par4 && k >= par2)) {
|
||||
slot = (Slot)inventorySlots.get(k);
|
||||
itemstack1 = slot.getStack();
|
||||
if (targetStack != null && targetStack.getItem() == sourceStack.getItem() &&
|
||||
(!sourceStack.getHasSubtypes() || sourceStack.getItemDamage() == targetStack.getItemDamage()) &&
|
||||
ItemStack.areItemStackTagsEqual(sourceStack, targetStack)) {
|
||||
|
||||
if (itemstack1 != null && itemstack1.getItem() == itemStack.getItem() && (!itemStack.getHasSubtypes() || itemStack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(itemStack, itemstack1)) {
|
||||
int l = itemstack1.stackSize + itemStack.stackSize;
|
||||
if (l <= itemStack.getMaxStackSize()) {
|
||||
if (ss instanceof SlotBauble) unequipBauble(itemStack);
|
||||
itemStack.stackSize = 0;
|
||||
itemstack1.stackSize = l;
|
||||
slot.onSlotChanged();
|
||||
flag1 = true;
|
||||
} else if (itemstack1.stackSize < itemStack.getMaxStackSize()) {
|
||||
if (ss instanceof SlotBauble) unequipBauble(itemStack);
|
||||
itemStack.stackSize -= itemStack.getMaxStackSize() - itemstack1.stackSize;
|
||||
itemstack1.stackSize = itemStack.getMaxStackSize();
|
||||
slot.onSlotChanged();
|
||||
flag1 = true;
|
||||
int combinedSize = targetStack.stackSize + sourceStack.stackSize;
|
||||
|
||||
if (combinedSize <= sourceStack.getMaxStackSize()) {
|
||||
if (sourceSlot instanceof SlotBauble) {
|
||||
unequipBauble(sourceStack);
|
||||
}
|
||||
sourceStack.stackSize = 0;
|
||||
targetStack.stackSize = combinedSize;
|
||||
targetSlot.onSlotChanged();
|
||||
return true;
|
||||
} else if (targetStack.stackSize < sourceStack.getMaxStackSize()) {
|
||||
if (sourceSlot instanceof SlotBauble) {
|
||||
unequipBauble(sourceStack);
|
||||
}
|
||||
sourceStack.stackSize -= sourceStack.getMaxStackSize() - targetStack.stackSize;
|
||||
targetStack.stackSize = sourceStack.getMaxStackSize();
|
||||
targetSlot.onSlotChanged();
|
||||
merged = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (par4) {
|
||||
--k;
|
||||
} else {
|
||||
++k;
|
||||
}
|
||||
index = reverse ? index - 1 : index + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (itemStack.stackSize > 0) {
|
||||
if (par4) {
|
||||
k = par3 - 1;
|
||||
} else {
|
||||
k = par2;
|
||||
// Second pass: put into empty slots
|
||||
index = reverse ? endIndex - 1 : startIndex;
|
||||
while (!reverse && index < endIndex || reverse && index >= startIndex) {
|
||||
targetSlot = (Slot) inventorySlots.get(index);
|
||||
targetStack = targetSlot.getStack();
|
||||
|
||||
if (targetStack == null) {
|
||||
if (sourceSlot instanceof SlotBauble) {
|
||||
unequipBauble(sourceStack);
|
||||
}
|
||||
targetSlot.putStack(sourceStack.copy());
|
||||
targetSlot.onSlotChanged();
|
||||
sourceStack.stackSize = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
while (!par4 && k < par3 || par4 && k >= par2) {
|
||||
slot = (Slot)inventorySlots.get(k);
|
||||
itemstack1 = slot.getStack();
|
||||
|
||||
if (itemstack1 == null) {
|
||||
if (ss instanceof SlotBauble) unequipBauble(itemStack);
|
||||
slot.putStack(itemStack.copy());
|
||||
slot.onSlotChanged();
|
||||
itemStack.stackSize = 0;
|
||||
flag1 = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (par4) {
|
||||
--k;
|
||||
} else {
|
||||
++k;
|
||||
}
|
||||
}
|
||||
index = reverse ? index - 1 : index + 1;
|
||||
}
|
||||
return flag1;
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user