Posted May 10, 20169 yr I have to create a new variable for the player class. It's an integer that goes up by one every tick and goes back to 0 when we right click. In forge 1.8 I guess I would use IExtendedProperties but it's not there anymore and it's replaced by capabilities. Can someone tell me how can I do it, and if I have to use capabilities, make an example of how to do it because I saw some examples of this new thing but they were too complicated
May 11, 20169 yr Author So I came up with this code, it seems to work but I'd like to ask you if everything is correct. IOffHandAttack.class public interface IOffHandAttack { public int getTicksSinceLastSwing2(); public void setTicksSinceLastSwing2(int amount); public void tick(); } EventHandler.class public class EventHandler { @CapabilityInject(IOffHandAttack.class) private static final Capability<IOffHandAttack> OFFHAND_CAP = null; @SubscribeEvent public void onAttack(AttackEntityEvent event) { final IOffHandAttack atk = event.getEntityPlayer().getCapability(OFFHAND_CAP, null); atk.setTicksSinceLastSwing2(0); } @SubscribeEvent public void onLivingUpdate(LivingUpdateEvent event) { if(event.getEntityLiving() instanceof EntityPlayer) { final IOffHandAttack atk = event.getEntityLiving().getCapability(OFFHAND_CAP, null); atk.tick(); } } @SubscribeEvent public void onEntityConstruct(AttachCapabilitiesEvent.Entity evt) { evt.addCapability(new ResourceLocation(Reference.MOD_ID, "IOffHandAttack"), new ICapabilitySerializable<NBTPrimitive>() { IOffHandAttack inst = OFFHAND_CAP.getDefaultInstance(); @Override public boolean hasCapability(Capability<?> capability, EnumFacing facing) { return capability == OFFHAND_CAP; } @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { return capability == OFFHAND_CAP ? OFFHAND_CAP.<T>cast(inst) : null; } @Override public NBTPrimitive serializeNBT() { return (NBTPrimitive)OFFHAND_CAP.getStorage().writeNBT(OFFHAND_CAP, inst, null); } @Override public void deserializeNBT(NBTPrimitive nbt) { OFFHAND_CAP.getStorage().readNBT(OFFHAND_CAP, inst, null, nbt); } }); } } Storage.class public class Storage implements IStorage<IOffHandAttack> { @Override public NBTBase writeNBT(Capability<IOffHandAttack> capability, IOffHandAttack instance, EnumFacing side) { return new NBTTagInt(instance.getTicksSinceLastSwing2()); } @Override public void readNBT(Capability<IOffHandAttack> capability, IOffHandAttack instance, EnumFacing side, NBTBase nbt) { instance.setTicksSinceLastSwing2(((NBTPrimitive)nbt).getInt()); } } DefaultImpl.class public class DefaultImpl implements IOffHandAttack { private int ticksSinceLastSwing2; @Override public int getTicksSinceLastSwing2() { return ticksSinceLastSwing2; } @Override public void setTicksSinceLastSwing2(int amount) { this.ticksSinceLastSwing2 = amount; } @Override public void tick() { this.ticksSinceLastSwing2++; } } CommonProxy.class public class CommonProxy { public void preInit(FMLPreInitializationEvent e) { CapabilityManager.INSTANCE.register(IOffHandAttack.class, new Storage(), DefaultImpl.class); } }
May 11, 20169 yr Looks fine to me (You could rename AttachCaps method, since it is not exacly EntityConstructing event) 1.7.10 is no longer supported by forge, you are on your own.
May 11, 20169 yr Author Ok, I found that pretty strange that setting the value will also change the server side value without using packets. Also, how would you explain how this method works? @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { return capability == OFFHAND_CAP ? OFFHAND_CAP.<T>cast(inst) : null; } I know the 1 line "if-else" thing but I don't understand the whole idea of this method
May 11, 20169 yr Ok, I found that pretty strange that setting the value will also change the server side value without using packets. This is impossible. You must be calling setter method from server. Also, how would you explain how this method works? @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { return capability == OFFHAND_CAP ? OFFHAND_CAP.<T>cast(inst) : null; } Method itself - it is used to return Capability attached to given thing (ItemStack, Entity, Tile, etc.) when you ask for it. Basically - you ask for "OFFHAND_CAP", it will give you instance of it assigned to thing you ask capability for. Pretty much whole point of CapabilityProvider class, which apparently is NOT even needed (I read it somewhere). Just look at callbacks bruh 1.7.10 is no longer supported by forge, you are on your own.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.