Merge pull request #2614 from williewillus/entitycaps

Expose IItemHandler on vanilla entities
This commit is contained in:
LexManos 2016-03-21 19:27:25 -07:00
commit 5cb4fe33bc
9 changed files with 185 additions and 82 deletions

View file

@ -285,3 +285,31 @@
} }
this.func_184602_cy(); this.func_184602_cy();
@@ -2626,4 +2707,27 @@
{
return true;
}
+
+ // FORGE
+ private final net.minecraftforge.items.IItemHandlerModifiable handHandler = new net.minecraftforge.items.ItemStackHandler(field_184630_bs);
+ private final net.minecraftforge.items.IItemHandlerModifiable armorHandler = new net.minecraftforge.items.ItemStackHandler(field_184631_bt);
+ private final net.minecraftforge.items.IItemHandler joinedHandler = new net.minecraftforge.items.wrapper.CombinedInvWrapper(armorHandler, handHandler);
+
+ @Override
+ public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.util.EnumFacing facing)
+ {
+ if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
+ {
+ 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);
+ }
+
+ @Override
+ public boolean hasCapability(net.minecraftforge.common.capabilities.Capability<?> capability, net.minecraft.util.EnumFacing facing)
+ {
+ return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
+ }
}

View file

@ -1,6 +1,14 @@
--- ../src-base/minecraft/net/minecraft/entity/passive/EntityHorse.java --- ../src-base/minecraft/net/minecraft/entity/passive/EntityHorse.java
+++ ../src-work/minecraft/net/minecraft/entity/passive/EntityHorse.java +++ ../src-work/minecraft/net/minecraft/entity/passive/EntityHorse.java
@@ -1200,6 +1200,7 @@ @@ -433,6 +433,7 @@
this.field_110296_bG.func_110134_a(this);
this.func_110232_cE();
+ this.itemHandler = new net.minecraftforge.items.wrapper.InvWrapper(this.field_110296_bG);
}
private void func_110232_cE()
@@ -1200,6 +1201,7 @@
} }
this.field_110277_bt = 0.0F; this.field_110277_bt = 0.0F;
@ -8,3 +16,24 @@
} }
this.field_70138_W = 1.0F; this.field_70138_W = 1.0F;
@@ -1793,4 +1795,20 @@
this.field_188477_b = p_i46589_2_;
}
}
+
+ // FORGE
+ private net.minecraftforge.items.IItemHandler itemHandler = null; // Initialized by initHorseChest above.
+
+ @Override
+ public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.util.EnumFacing facing)
+ {
+ 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 || super.hasCapability(capability, facing);
+ }
}

View file

@ -401,7 +401,7 @@
if (this.func_70608_bn()) if (this.func_70608_bn())
{ {
@@ -2340,6 +2447,136 @@ @@ -2340,6 +2447,160 @@
return (float)this.func_110148_a(SharedMonsterAttributes.field_188792_h).func_111126_e(); return (float)this.func_110148_a(SharedMonsterAttributes.field_188792_h).func_111126_e();
} }
@ -533,6 +533,30 @@
+ public java.util.Collection<ITextComponent> getPrefixes() { return this.prefixes; } + public java.util.Collection<ITextComponent> getPrefixes() { return this.prefixes; }
+ public java.util.Collection<ITextComponent> getSuffixes() { return this.suffixes; } + public java.util.Collection<ITextComponent> 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 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
+ public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.util.EnumFacing facing)
+ {
+ if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
+ {
+ 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);
+ }
+
+ @Override
+ public boolean hasCapability(net.minecraftforge.common.capabilities.Capability<?> capability, net.minecraft.util.EnumFacing facing)
+ {
+ return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
+ }
+
+ /* ======================================== FORGE END =====================================*/ + /* ======================================== FORGE END =====================================*/
+ +
public static enum EnumChatVisibility public static enum EnumChatVisibility

View file

@ -20,6 +20,11 @@ public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, I
stacks = new ItemStack[size]; stacks = new ItemStack[size];
} }
public ItemStackHandler(ItemStack[] stacks)
{
this.stacks = stacks;
}
public void setSize(int size) public void setSize(int size)
{ {
stacks = new ItemStack[size]; stacks = new ItemStack[size];

View file

@ -4,29 +4,14 @@ import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.EntityEquipmentSlot; import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
public class PlayerArmorInvWrapper extends InvWrapper public class PlayerArmorInvWrapper extends RangedWrapper
{ {
public final InventoryPlayer inventoryPlayer; public final InventoryPlayer inventoryPlayer;
public final int offset;
public PlayerArmorInvWrapper(InventoryPlayer inv) public PlayerArmorInvWrapper(InventoryPlayer inv)
{ {
super(inv); super(new InvWrapper(inv), inv.mainInventory.length, inv.mainInventory.length + inv.armorInventory.length);
inventoryPlayer = inv; 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 @Override
@ -44,20 +29,8 @@ public class PlayerArmorInvWrapper extends InvWrapper
// check if it's valid for the armor slot // check if it's valid for the armor slot
if (slot < 4 && stack != null && stack.getItem().isValidArmor(stack, equ, inventoryPlayer.player)) 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; 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);
}
} }

View file

@ -6,6 +6,6 @@ public class PlayerInvWrapper extends CombinedInvWrapper
{ {
public PlayerInvWrapper(InventoryPlayer inv) public PlayerInvWrapper(InventoryPlayer inv)
{ {
super(new PlayerMainInvWrapper(inv), new PlayerArmorInvWrapper(inv)); super(new PlayerMainInvWrapper(inv), new PlayerArmorInvWrapper(inv), new PlayerOffhandInvWrapper(inv));
} }
} }

View file

@ -8,55 +8,19 @@ import net.minecraft.item.ItemStack;
* Exposes the player inventory WITHOUT the armor inventory as IItemHandler. * Exposes the player inventory WITHOUT the armor inventory as IItemHandler.
* Also takes core of inserting/extracting having the same logic as picking up items. * 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 final InventoryPlayer inventoryPlayer;
public PlayerMainInvWrapper(InventoryPlayer inv) public PlayerMainInvWrapper(InventoryPlayer inv)
{ {
super(inv); super(new InvWrapper(inv), 0, inv.mainInventory.length);
inventoryPlayer = inv; inventoryPlayer = inv;
} }
@Override
public int getSlots()
{
return inventoryPlayer.mainInventory.length;
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
// prevent setting of armor inventory
if (slot > getSlots())
{
return;
}
super.setStackInSlot(slot, stack);
}
@Override
public ItemStack getStackInSlot(int slot)
{
// prevent getting of armor inventory
if (slot > getSlots())
{
return null;
}
return super.getStackInSlot(slot);
}
@Override @Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{ {
// prevent inserting into armor inventory
if (slot > getSlots())
{
return stack;
}
ItemStack rest = super.insertItem(slot, stack, simulate); ItemStack rest = super.insertItem(slot, stack, simulate);
if (rest == null || rest.stackSize != stack.stackSize) if (rest == null || rest.stackSize != stack.stackSize)
{ {
@ -75,15 +39,4 @@ public class PlayerMainInvWrapper extends InvWrapper
} }
return rest; return rest;
} }
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
// prevent extraction from armor inventory
if (slot > getSlots())
{
return null;
}
return super.extractItem(slot, amount, simulate);
}
} }

View file

@ -0,0 +1,13 @@
package net.minecraftforge.items.wrapper;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
public class PlayerOffhandInvWrapper extends RangedWrapper
{
public PlayerOffhandInvWrapper(InventoryPlayer inv)
{
super(new InvWrapper(inv), inv.mainInventory.length + inv.armorInventory.length,
inv.mainInventory.length + inv.armorInventory.length + inv.offHandInventory.length);
}
}

View file

@ -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;
}
}