Created
August 6, 2016 19:37
-
-
Save gigaherz/d4c6370cb331137e82be2ec687cb7f57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gigaherz.packingtape.cap; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.item.ItemStack; | |
import net.minecraftforge.items.ItemStackHandler; | |
/** | |
* Created by gigaherz on 06/08/2016. | |
* | |
* This class is the feature we want to attach to the player. | |
*/ | |
public class InventoryExpansion | |
{ | |
public static final int INVENTORY_SLOTS = 5; | |
final ItemStackHandler slots = new ItemStackHandler(INVENTORY_SLOTS); | |
EntityPlayer player; | |
public InventoryExpansion() | |
{ | |
} | |
public EntityPlayer getPlayer() { return player; } | |
public void setPlayer(EntityPlayer newPlayer) | |
{ | |
this.player = newPlayer; | |
} | |
/* | |
* Internal method used by IStorage in the capability | |
*/ | |
protected ItemStackHandler getSlots() | |
{ | |
return slots; | |
} | |
/* | |
* API method for getting a slot by name | |
*/ | |
public ItemStack getStackInSlot(ExpansionSlots slot) | |
{ | |
return slots.getStackInSlot(slot.ordinal()); | |
} | |
/* | |
* API method for setting a slot by name | |
*/ | |
public void setStackInSlot(ExpansionSlots slot, ItemStack stack) | |
{ | |
// TODO: Remove old attribute modifiers from player | |
slots.setStackInSlot(slot.ordinal(), stack); | |
// TODO: Add new attribute modifiers to player | |
} | |
/* | |
* An arbitrary set of slots, for example purposes | |
*/ | |
public enum ExpansionSlots | |
{ | |
NECK, | |
WRISTS, | |
ANKLES, | |
EARS, | |
FINGERS | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gigaherz.packingtape.cap; | |
import net.minecraft.nbt.NBTBase; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.util.EnumFacing; | |
import net.minecraftforge.common.capabilities.Capability; | |
import net.minecraftforge.common.capabilities.CapabilityInject; | |
import net.minecraftforge.common.capabilities.CapabilityManager; | |
import java.util.concurrent.Callable; | |
/** | |
* Created by gigaherz on 06/08/2016. | |
* | |
* This class is only for structure only, everything important is a nested/anonymous class. | |
*/ | |
public class InventoryExpansionCapability | |
{ | |
/* | |
* This field will contain the forge-allocated Capability class. | |
* This instance will be initialized internally by Forge, upon calling register. | |
*/ | |
@CapabilityInject(InventoryExpansion.class) | |
public static Capability<InventoryExpansion> INSTANCE; | |
/* | |
* This | |
*/ | |
public void register() | |
{ | |
CapabilityManager.INSTANCE.register( | |
// This is theclass the capability works with | |
InventoryExpansion.class, | |
// This is a helper for users to save and load | |
new StorageHelper(), | |
// This is a factory for default instances | |
new DefaultInstanceFactory() | |
); | |
} | |
/* | |
* This class handles saving and loading the data. | |
*/ | |
public static class StorageHelper implements Capability.IStorage<InventoryExpansion> | |
{ | |
@Override | |
public NBTBase writeNBT(Capability<InventoryExpansion> capability, InventoryExpansion instance, EnumFacing side) | |
{ | |
return instance.getSlots().serializeNBT(); | |
} | |
@Override | |
public void readNBT(Capability<InventoryExpansion> capability, InventoryExpansion instance, EnumFacing side, NBTBase nbt) | |
{ | |
instance.getSlots().deserializeNBT((NBTTagCompound)nbt); | |
} | |
} | |
/* | |
* This class handles constructing new instances for this capability | |
*/ | |
public static class DefaultInstanceFactory implements Callable<InventoryExpansion> | |
{ | |
@Override | |
public InventoryExpansion call() throws Exception | |
{ | |
return new InventoryExpansion(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gigaherz.packingtape.cap; | |
import gigaherz.packingtape.ModPackingTape; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.util.EnumFacing; | |
import net.minecraft.util.ResourceLocation; | |
import net.minecraftforge.common.MinecraftForge; | |
import net.minecraftforge.common.capabilities.Capability; | |
import net.minecraftforge.common.capabilities.ICapabilitySerializable; | |
import net.minecraftforge.event.AttachCapabilitiesEvent; | |
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | |
import javax.annotation.Nullable; | |
/** | |
* Created by gigaherz on 06/08/2016. | |
* | |
* This class handles events for the inventory expansion capability | |
*/ | |
public class InventoryExpansionEventHandlers | |
{ | |
public static void register() | |
{ | |
MinecraftForge.EVENT_BUS.register(new InventoryExpansionEventHandlers()); | |
} | |
@SubscribeEvent | |
public void onAttach(AttachCapabilitiesEvent.Entity ev) | |
{ | |
if (!(ev.getEntity() instanceof EntityPlayer)) | |
return; | |
ev.addCapability(ExpansionProvider.KEY, new ExpansionProvider((EntityPlayer)ev.getEntity())); | |
} | |
/* | |
* TODO: Add event handlers for synchronizing data with clients, | |
* dropping/transferring items upon death, etc. | |
*/ | |
/* | |
* This class handles the capability requests on behalf of the player | |
*/ | |
public static class ExpansionProvider implements ICapabilitySerializable<NBTTagCompound> | |
{ | |
/* | |
* Unique key to identify the attached provider from others | |
*/ | |
static final ResourceLocation KEY = new ResourceLocation(ModPackingTape.MODID, "inventoryExpansion"); | |
/* | |
* The instance that we are providing | |
*/ | |
final InventoryExpansion slots = new InventoryExpansion(); | |
public ExpansionProvider(EntityPlayer player) | |
{ | |
slots.setPlayer(player); | |
} | |
@Override | |
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) | |
{ | |
if (capability == InventoryExpansionCapability.INSTANCE) | |
return true; | |
return false; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) | |
{ | |
if (capability == InventoryExpansionCapability.INSTANCE) | |
return (T)slots; | |
return null; | |
} | |
@Override | |
public NBTTagCompound serializeNBT() | |
{ | |
return (NBTTagCompound) InventoryExpansionCapability.INSTANCE.writeNBT(slots, null); | |
} | |
@Override | |
public void deserializeNBT(NBTTagCompound nbt) | |
{ | |
InventoryExpansionCapability.INSTANCE.readNBT(slots, null, nbt); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment