From dabb17f869a07ca2c506deb5848f05f79e774854 Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Mon, 21 Mar 2016 20:31:33 -0500 Subject: [PATCH] Update according to suggestions --- .../entity/EntityLivingBase.java.patch | 15 +--- .../entity/passive/EntityHorse.java.patch | 11 +-- .../entity/player/EntityPlayer.java.patch | 23 ++---- .../items/wrapper/PlayerArmorInvWrapper.java | 33 +------- .../items/wrapper/PlayerMainInvWrapper.java | 50 +----------- .../wrapper/PlayerOffhandInvWrapper.java | 42 +--------- .../items/wrapper/RangedWrapper.java | 78 +++++++++++++++++++ 7 files changed, 100 insertions(+), 152 deletions(-) create mode 100644 src/main/java/net/minecraftforge/items/wrapper/RangedWrapper.java diff --git a/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch b/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch index 4c463a58a..5c4973476 100644 --- a/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch @@ -285,7 +285,7 @@ } this.func_184602_cy(); -@@ -2626,4 +2707,34 @@ +@@ -2626,4 +2707,27 @@ { return true; } @@ -300,16 +300,9 @@ + { + if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + { -+ if (facing == net.minecraft.util.EnumFacing.UP) -+ { -+ return (T) armorHandler; -+ } else if (facing.func_176740_k().func_176722_c()) -+ { -+ return (T) handHandler; -+ } else -+ { -+ return (T) joinedHandler; -+ } ++ if (facing == null) return (T) joinedHandler; ++ else if (facing.func_176740_k().func_176720_b()) return (T) handHandler; ++ else if (facing.func_176740_k().func_176722_c()) return (T) armorHandler; + } + return super.getCapability(capability, facing); + } diff --git a/patches/minecraft/net/minecraft/entity/passive/EntityHorse.java.patch b/patches/minecraft/net/minecraft/entity/passive/EntityHorse.java.patch index 4c14f0c21..5f4ea94e7 100644 --- a/patches/minecraft/net/minecraft/entity/passive/EntityHorse.java.patch +++ b/patches/minecraft/net/minecraft/entity/passive/EntityHorse.java.patch @@ -16,7 +16,7 @@ } this.field_70138_W = 1.0F; -@@ -1793,4 +1795,25 @@ +@@ -1793,4 +1795,20 @@ this.field_188477_b = p_i46589_2_; } } @@ -27,18 +27,13 @@ + @Override + public T getCapability(net.minecraftforge.common.capabilities.Capability capability, net.minecraft.util.EnumFacing facing) + { -+ if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY -+ && itemHandler != null) -+ { -+ return (T) itemHandler; -+ } ++ if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (T) itemHandler; + return super.getCapability(capability, facing); + } + + @Override + public boolean hasCapability(net.minecraftforge.common.capabilities.Capability capability, net.minecraft.util.EnumFacing facing) + { -+ return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && itemHandler != null -+ || super.hasCapability(capability, facing); ++ return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing); + } } diff --git a/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch b/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch index ffdccb23e..f0c090d77 100644 --- a/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch +++ b/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch @@ -401,7 +401,7 @@ if (this.func_70608_bn()) { -@@ -2340,6 +2447,169 @@ +@@ -2340,6 +2447,160 @@ return (float)this.func_110148_a(SharedMonsterAttributes.field_188792_h).func_111126_e(); } @@ -534,8 +534,9 @@ + public java.util.Collection getSuffixes() { return this.suffixes; } + + private final net.minecraftforge.items.IItemHandler playerMainHandler = new net.minecraftforge.items.wrapper.PlayerMainInvWrapper(field_71071_by); -+ private final net.minecraftforge.items.IItemHandler playerArmorHandler = new net.minecraftforge.items.wrapper.PlayerArmorInvWrapper(field_71071_by); -+ private final net.minecraftforge.items.IItemHandler playerOffhandHandler = new net.minecraftforge.items.wrapper.PlayerOffhandInvWrapper(field_71071_by); ++ private final net.minecraftforge.items.IItemHandler playerEquipmentHandler = new net.minecraftforge.items.wrapper.CombinedInvWrapper( ++ new net.minecraftforge.items.wrapper.PlayerArmorInvWrapper(field_71071_by), ++ new net.minecraftforge.items.wrapper.PlayerOffhandInvWrapper(field_71071_by)); + private final net.minecraftforge.items.IItemHandler playerJoinedHandler = new net.minecraftforge.items.wrapper.PlayerInvWrapper(field_71071_by); + + @Override @@ -543,19 +544,9 @@ + { + if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + { -+ if (facing == net.minecraft.util.EnumFacing.UP) -+ { -+ return (T) playerArmorHandler; -+ } else if (facing == net.minecraft.util.EnumFacing.DOWN) -+ { -+ return (T) playerMainHandler; -+ } else if (facing.func_176740_k().func_176722_c()) -+ { -+ return (T) playerOffhandHandler; -+ } else -+ { -+ return (T) playerJoinedHandler; -+ } ++ if (facing == null) return (T) playerJoinedHandler; ++ else if (facing.func_176740_k().func_176720_b()) return (T) playerMainHandler; ++ else if (facing.func_176740_k().func_176722_c()) return (T) playerEquipmentHandler; + } + return super.getCapability(capability, facing); + } diff --git a/src/main/java/net/minecraftforge/items/wrapper/PlayerArmorInvWrapper.java b/src/main/java/net/minecraftforge/items/wrapper/PlayerArmorInvWrapper.java index f8c70dd56..063bec7e1 100644 --- a/src/main/java/net/minecraftforge/items/wrapper/PlayerArmorInvWrapper.java +++ b/src/main/java/net/minecraftforge/items/wrapper/PlayerArmorInvWrapper.java @@ -4,29 +4,14 @@ import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.EntityEquipmentSlot; import net.minecraft.item.ItemStack; -public class PlayerArmorInvWrapper extends InvWrapper +public class PlayerArmorInvWrapper extends RangedWrapper { public final InventoryPlayer inventoryPlayer; - private final int offset; public PlayerArmorInvWrapper(InventoryPlayer inv) { - super(inv); - + super(new InvWrapper(inv), inv.mainInventory.length, inv.mainInventory.length + inv.armorInventory.length); inventoryPlayer = inv; - offset = inventoryPlayer.mainInventory.length; - } - - @Override - public int getSlots() - { - return inventoryPlayer.armorInventory.length; - } - - @Override - public ItemStack getStackInSlot(int slot) - { - return super.getStackInSlot(slot + offset); } @Override @@ -44,20 +29,8 @@ public class PlayerArmorInvWrapper extends InvWrapper // check if it's valid for the armor slot if (slot < 4 && stack != null && stack.getItem().isValidArmor(stack, equ, inventoryPlayer.player)) { - return super.insertItem(slot + offset, stack, simulate); + return super.insertItem(slot, stack, simulate); } return stack; } - - @Override - public void setStackInSlot(int slot, ItemStack stack) - { - super.setStackInSlot(slot + offset, stack); - } - - @Override - public ItemStack extractItem(int slot, int amount, boolean simulate) - { - return super.extractItem(slot + offset, amount, simulate); - } } diff --git a/src/main/java/net/minecraftforge/items/wrapper/PlayerMainInvWrapper.java b/src/main/java/net/minecraftforge/items/wrapper/PlayerMainInvWrapper.java index 289348689..421f65412 100644 --- a/src/main/java/net/minecraftforge/items/wrapper/PlayerMainInvWrapper.java +++ b/src/main/java/net/minecraftforge/items/wrapper/PlayerMainInvWrapper.java @@ -8,54 +8,19 @@ import net.minecraft.item.ItemStack; * Exposes the player inventory WITHOUT the armor inventory as IItemHandler. * Also takes core of inserting/extracting having the same logic as picking up items. */ -public class PlayerMainInvWrapper extends InvWrapper +public class PlayerMainInvWrapper extends RangedWrapper { public final InventoryPlayer inventoryPlayer; public PlayerMainInvWrapper(InventoryPlayer inv) { - super(inv); - + super(new InvWrapper(inv), 0, inv.mainInventory.length); inventoryPlayer = inv; } - @Override - public int getSlots() - { - return inventoryPlayer.mainInventory.length; - } - - @Override - public void setStackInSlot(int slot, ItemStack stack) - { - // prevent setting of armor and offhand - if (slot > getSlots()) - { - return; - } - super.setStackInSlot(slot, stack); - } - - @Override - public ItemStack getStackInSlot(int slot) - { - // prevent getting of armor and offhand - if (slot > getSlots()) - { - return null; - } - return super.getStackInSlot(slot); - } - @Override public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) { - // prevent inserting into armor and offhand - if (slot > getSlots()) - { - return stack; - } - ItemStack rest = super.insertItem(slot, stack, simulate); if (rest == null || rest.stackSize != stack.stackSize) { @@ -74,15 +39,4 @@ public class PlayerMainInvWrapper extends InvWrapper } return rest; } - - @Override - public ItemStack extractItem(int slot, int amount, boolean simulate) - { - // prevent extraction from armor and offhand - if (slot > getSlots()) - { - return null; - } - return super.extractItem(slot, amount, simulate); - } } diff --git a/src/main/java/net/minecraftforge/items/wrapper/PlayerOffhandInvWrapper.java b/src/main/java/net/minecraftforge/items/wrapper/PlayerOffhandInvWrapper.java index 5272a3ec3..0d3af4e8d 100644 --- a/src/main/java/net/minecraftforge/items/wrapper/PlayerOffhandInvWrapper.java +++ b/src/main/java/net/minecraftforge/items/wrapper/PlayerOffhandInvWrapper.java @@ -3,47 +3,11 @@ package net.minecraftforge.items.wrapper; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; -public class PlayerOffhandInvWrapper extends InvWrapper +public class PlayerOffhandInvWrapper extends RangedWrapper { - public final InventoryPlayer inventoryPlayer; - private final int offset; - public PlayerOffhandInvWrapper(InventoryPlayer inv) { - super(inv); - - inventoryPlayer = inv; - offset = inventoryPlayer.mainInventory.length + inventoryPlayer.armorInventory.length; + super(new InvWrapper(inv), inv.mainInventory.length + inv.armorInventory.length, + inv.mainInventory.length + inv.armorInventory.length + inv.offHandInventory.length); } - - @Override - public int getSlots() - { - return inventoryPlayer.offHandInventory.length; - } - - @Override - public ItemStack getStackInSlot(int slot) - { - return super.getStackInSlot(slot + offset); - } - - @Override - public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) - { - return super.insertItem(slot + offset, stack, simulate); - } - - @Override - public void setStackInSlot(int slot, ItemStack stack) - { - super.setStackInSlot(slot + offset, stack); - } - - @Override - public ItemStack extractItem(int slot, int amount, boolean simulate) - { - return super.extractItem(slot + offset, amount, simulate); - } - } diff --git a/src/main/java/net/minecraftforge/items/wrapper/RangedWrapper.java b/src/main/java/net/minecraftforge/items/wrapper/RangedWrapper.java new file mode 100644 index 000000000..00b218eb6 --- /dev/null +++ b/src/main/java/net/minecraftforge/items/wrapper/RangedWrapper.java @@ -0,0 +1,78 @@ +package net.minecraftforge.items.wrapper; + +import com.google.common.base.Preconditions; +import net.minecraft.item.ItemStack; +import net.minecraftforge.items.IItemHandlerModifiable; + +/** + * A wrapper that composes another IItemHandlerModifiable, exposing only a range of the composed slots. + * Shifting of slot indices is handled automatically for you. + */ +public class RangedWrapper implements IItemHandlerModifiable { + + private final IItemHandlerModifiable compose; + private final int minSlot; + private final int maxSlot; + + public RangedWrapper(IItemHandlerModifiable compose, int minSlot, int maxSlotExclusive) + { + Preconditions.checkArgument(maxSlotExclusive > minSlot, "Max slot must be greater than min slot"); + this.compose = compose; + this.minSlot = minSlot; + this.maxSlot = maxSlotExclusive; + } + + @Override + public int getSlots() + { + return maxSlot - minSlot; + } + + @Override + public ItemStack getStackInSlot(int slot) + { + if (checkSlot(slot)) + { + return compose.getStackInSlot(slot + minSlot); + } + + return null; + } + + @Override + public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) + { + if (checkSlot(slot)) + { + return compose.insertItem(slot + minSlot, stack, simulate); + } + + return stack; + } + + @Override + public ItemStack extractItem(int slot, int amount, boolean simulate) + { + if (checkSlot(slot)) + { + return compose.extractItem(slot + minSlot, amount, simulate); + } + + return null; + } + + @Override + public void setStackInSlot(int slot, ItemStack stack) + { + if (checkSlot(slot)) + { + compose.setStackInSlot(slot + minSlot, stack); + } + } + + private boolean checkSlot(int localSlot) + { + return localSlot + minSlot < maxSlot; + } + +}