Some work on randomizerbag

This commit is contained in:
Christian Knaapen
2018-09-16 14:21:05 +02:00
parent f8f76e8f24
commit d01fe52aba
5 changed files with 257 additions and 47 deletions

View File

@@ -1,11 +1,104 @@
package nl.requios.effortlessbuilding.inventory; package nl.requios.effortlessbuilding.inventory;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
public class RandomizerBagContainer extends Container { public class RandomizerBagContainer extends Container {
private final IInventory bagInventory;
private final int sizeInventory;
public RandomizerBagContainer(InventoryPlayer parInventoryPlayer, IInventory parIInventory) {
bagInventory = parIInventory;
sizeInventory = bagInventory.getSizeInventory();
for (int i = 0; i < RandomizerBagInventory.INV_SIZE; ++i) {
this.addSlotToContainer(new Slot(bagInventory, i, 80 + (18 * (i / 4)), 8 + (18 * (i % 4))));
}
// add player inventory slots
int i;
for (i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
addSlotToContainer(new Slot(parInventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
// add hotbar slots
for (i = 0; i < 9; ++i) {
addSlotToContainer(new Slot(parInventoryPlayer, i, 8 + i * 18, 142));
}
}
@Override @Override
public boolean canInteractWith(EntityPlayer playerIn) { public boolean canInteractWith(EntityPlayer playerIn) {
return false; return bagInventory.isUsableByPlayer(playerIn);
}
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn,
int slotIndex) {
ItemStack itemStack1 = null;
Slot slot = inventorySlots.get(slotIndex);
if (slot != null && slot.getHasStack()) {
ItemStack itemStack2 = slot.getStack();
itemStack1 = itemStack2.copy();
if (slotIndex == 0) {
if (!mergeItemStack(itemStack2, sizeInventory,
sizeInventory + 36, true)) {
return null;
}
slot.onSlotChange(itemStack2, itemStack1);
} else if (slotIndex != 1) {
if (slotIndex >= sizeInventory
&& slotIndex < sizeInventory + 27) // player inventory slots
{
if (!mergeItemStack(itemStack2, sizeInventory + 27,
sizeInventory + 36, false)) {
return null;
}
} else if (slotIndex >= sizeInventory + 27
&& slotIndex < sizeInventory + 36
&& !mergeItemStack(itemStack2, sizeInventory + 1,
sizeInventory + 27, false)) // hotbar slots
{
return null;
}
} else if (!mergeItemStack(itemStack2, sizeInventory,
sizeInventory + 36, false)) {
return null;
}
if (itemStack2.getCount() == 0) {
slot.putStack(null);
} else {
slot.onSlotChanged();
}
if (itemStack2.getCount() == itemStack1.getCount()) {
return null;
}
slot.onTake(playerIn, itemStack2);
}
return itemStack1;
}
@Override
public ItemStack slotClick(int slot, int dragType, ClickType clickTypeIn, EntityPlayer player) {
// this will prevent the player from interacting with the item that opened the inventory:
if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItem(EnumHand.MAIN_HAND)) {
return null;
}
return super.slotClick(slot, dragType, clickTypeIn, player);
} }
} }

View File

@@ -1,15 +1,60 @@
package nl.requios.effortlessbuilding.inventory; package nl.requios.effortlessbuilding.inventory;
import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import nl.requios.effortlessbuilding.EffortlessBuilding;
@SideOnly(Side.CLIENT)
public class RandomizerBagGuiContainer extends GuiContainer { public class RandomizerBagGuiContainer extends GuiContainer {
public RandomizerBagGuiContainer(Container inventorySlotsIn) { private static final ResourceLocation grinderGuiTextures =
super(inventorySlotsIn); new ResourceLocation(EffortlessBuilding.MODID
+ ":textures/gui/container/bagrandomizer.png");
private final InventoryPlayer inventoryPlayer;
private final IInventory tileGrinder;
public RandomizerBagGuiContainer(InventoryPlayer parInventoryPlayer,
IInventory parInventoryGrinder) {
super(new RandomizerBagContainer(parInventoryPlayer,
parInventoryGrinder));
inventoryPlayer = parInventoryPlayer;
tileGrinder = parInventoryGrinder;
} }
@Override @Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
String s = tileGrinder.getDisplayName().getUnformattedText();
fontRenderer.drawString(s, xSize / 2 - fontRenderer.getStringWidth(s) / 2, 6, 4210752);
fontRenderer.drawString(inventoryPlayer.getDisplayName().getUnformattedText(), 8, ySize - 96 + 2, 4210752);
}
/**
* Args : renderPartialTicks, mouseX, mouseY
*/
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks,
int mouseX, int mouseY) {
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
mc.getTextureManager().bindTexture(grinderGuiTextures);
int marginHorizontal = (width - xSize) / 2;
int marginVertical = (height - ySize) / 2;
drawTexturedModalRect(marginHorizontal, marginVertical, 0, 0, xSize, ySize);
// Draw progress indicator
int progressLevel = getProgressLevel(24);
drawTexturedModalRect(marginHorizontal + 79, marginVertical + 34, 176, 14, progressLevel + 1, 16);
}
private int getProgressLevel(int progressIndicatorPixelWidth) {
int ticksGrindingItemSoFar = tileGrinder.getField(2);
int ticksPerItem = tileGrinder.getField(3);
return ticksPerItem != 0 && ticksGrindingItemSoFar != 0 ?
ticksGrindingItemSoFar * progressIndicatorPixelWidth / ticksPerItem
: 0;
} }
} }

View File

@@ -1,13 +1,17 @@
package nl.requios.effortlessbuilding.inventory; package nl.requios.effortlessbuilding.inventory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class RandomizerBagInventory implements IItemHandler { public class RandomizerBagInventory implements IInventory {
//Reference to NBT data //Reference to NBT data
private final ItemStack invItem; private final ItemStack invItem;
@@ -27,10 +31,15 @@ public class RandomizerBagInventory implements IItemHandler {
} }
@Override @Override
public int getSlots() { public int getSizeInventory() {
return INV_SIZE; return INV_SIZE;
} }
@Override
public boolean isEmpty() {
return false;
}
@Nonnull @Nonnull
@Override @Override
public ItemStack getStackInSlot(int slot) { public ItemStack getStackInSlot(int slot) {
@@ -39,48 +48,42 @@ public class RandomizerBagInventory implements IItemHandler {
@Nonnull @Nonnull
@Override @Override
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) { public ItemStack decrStackSize(int slot, int amount) {
ItemStack slotStack = getStackInSlot(slot); if (inventory[slot] != null)
if (slotStack.getCount() == 0) { {
setInventorySlotContents(slot, stack); ItemStack itemstack;
return null;
if (inventory[slot].getCount() <= amount)
{
itemstack = inventory[slot];
inventory[slot] = null;
return itemstack;
} }
if (getSlotLimit(slot) - slotStack.getCount() < stack.getCount()) { else
//Not enough place remaining, split stack {
slotStack.setCount(getSlotLimit(slot)); itemstack = inventory[slot].splitStack(amount);
onInventoryChanged();
stack.copy().shrink(getSlotLimit(slot) - slotStack.getCount()); if (inventory[slot].getCount() == 0)
//TODO make proper {
return stack; inventory[slot] = null;
} else { }
slotStack.grow(stack.getCount());
onInventoryChanged(); return itemstack;
}
}
else
{
return null; return null;
} }
} }
@Nonnull
@Override @Override
public ItemStack extractItem(int slot, int amount, boolean simulate) { public ItemStack removeStackFromSlot(int index) {
ItemStack stack = getStackInSlot(slot); return null;
if (stack == null) return stack;
if (stack.getCount() > amount) {
stack = stack.splitStack(amount);
onInventoryChanged();
} else {
setInventorySlotContents(slot, null);
}
return stack;
}
@Override
public int getSlotLimit(int slot) {
return 64;
} }
public void onInventoryChanged() { public void onInventoryChanged() {
for (int i = 0; i < getSlotLimit(0); ++i) { for (int i = 0; i < getFieldCount(); ++i) {
if (getStackInSlot(i) != null && getStackInSlot(i).getCount() == 0) { if (getStackInSlot(i) != null && getStackInSlot(i).getCount() == 0) {
inventory[i] = null; inventory[i] = null;
} }
@@ -92,12 +95,66 @@ public class RandomizerBagInventory implements IItemHandler {
public void setInventorySlotContents(int slot, ItemStack stack) { public void setInventorySlotContents(int slot, ItemStack stack) {
inventory[slot] = stack; inventory[slot] = stack;
if (stack != null && stack.getCount() > getSlotLimit(slot)) { if (stack != null && stack.getCount() > getInventoryStackLimit())
stack.setCount(getSlotLimit(slot)); {
stack.setCount(getInventoryStackLimit());
} }
// Don't forget this line or your inventory will not be saved! markDirty();
onInventoryChanged(); }
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public void markDirty() {
}
@Override
public boolean isUsableByPlayer(EntityPlayer player) {
return true;
}
@Override
public void openInventory(EntityPlayer player) {
}
@Override
public void closeInventory(EntityPlayer player) {
}
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
return true;
}
@Override
public int getField(int id) {
//variables
return 0;
}
@Override
public void setField(int id, int value) {
}
@Override
public int getFieldCount() {
return 0;
}
@Override
public void clear() {
for (int i = 0; i < inventory.length; ++i)
{
inventory[i] = null;
}
} }
public void readFromNBT(NBTTagCompound compound) { public void readFromNBT(NBTTagCompound compound) {
@@ -111,7 +168,7 @@ public class RandomizerBagInventory implements IItemHandler {
int slot = item.getInteger("Slot"); int slot = item.getInteger("Slot");
// Just double-checking that the saved slot index is within our inventory array bounds // Just double-checking that the saved slot index is within our inventory array bounds
if (slot >= 0 && slot < getSlots()) { if (slot >= 0 && slot < getFieldCount()) {
inventory[slot] = new ItemStack(item); inventory[slot] = new ItemStack(item);
} }
} }
@@ -124,7 +181,7 @@ public class RandomizerBagInventory implements IItemHandler {
// Create a new NBT Tag List to store itemstacks as NBT Tags // Create a new NBT Tag List to store itemstacks as NBT Tags
NBTTagList items = new NBTTagList(); NBTTagList items = new NBTTagList();
for (int i = 0; i < getSlots(); ++i) { for (int i = 0; i < getSizeInventory(); ++i) {
// Only write stacks that contain items // Only write stacks that contain items
if (getStackInSlot(i) != null) { if (getStackInSlot(i) != null) {
// Make a new NBT Tag Compound to write the itemstack and slot index to // Make a new NBT Tag Compound to write the itemstack and slot index to
@@ -141,4 +198,18 @@ public class RandomizerBagInventory implements IItemHandler {
tagcompound.setTag("ItemInventory", items); tagcompound.setTag("ItemInventory", items);
} }
@Override
public String getName() {
return "Testname";
}
@Override
public boolean hasCustomName() {
return false;
}
@Override
public ITextComponent getDisplayName() {
return new TextComponentString("Testname");
}
} }

View File

@@ -1,6 +1,7 @@
package nl.requios.effortlessbuilding.inventory; package nl.requios.effortlessbuilding.inventory;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler; import net.minecraftforge.fml.common.network.IGuiHandler;
import nl.requios.effortlessbuilding.EffortlessBuilding; import nl.requios.effortlessbuilding.EffortlessBuilding;
@@ -14,7 +15,7 @@ public class RandomizerGuiHandler implements IGuiHandler {
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) { if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) {
// Use the player's held item to create the inventory // Use the player's held item to create the inventory
return new RandomizerBagContainer(); return new RandomizerBagContainer(player.inventory, new RandomizerBagInventory(player.getHeldItem(EnumHand.MAIN_HAND)));
} }
return null; return null;
} }
@@ -25,7 +26,7 @@ public class RandomizerGuiHandler implements IGuiHandler {
if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) { if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) {
// We have to cast the new container as our custom class // We have to cast the new container as our custom class
// and pass in currently held item for the inventory // and pass in currently held item for the inventory
return new RandomizerBagGuiContainer(new RandomizerBagContainer()); return new RandomizerBagGuiContainer(player.inventory, new RandomizerBagInventory(player.getHeldItem(EnumHand.MAIN_HAND)));
} }
return null; return null;
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB