Jump to content

Method to attach Capabilities after Entity registration


ben_mkiv

Recommended Posts

Hello,

 

i'm writing a mod which adds a set of different Capabilities for Entities. Actually i'm giving any Entity my capabilities on registration, which creates a lot of unnecessary overhead.

 

Please add some Method to add/remove Capabilities from Entities on the fly. Thanks :)

 

Link to comment
Share on other sites

well its kinda lot of code... actually i'm attaching the capabilities to every EntityLiving/EntityMinecart by catching the attachCapability event.

 

every capability has a wrapper around it which is able to attach/detach the capability. so i dont see any place to optimize here except of adding a wrapper around all capabilities so that i end up with one capability... but even those wrappers already used are annoying me ;)

 

its just that multiplayer server hold tons of entities cached and i would like to keep my overhead at the possible minimum. And Capabilities would be a great Tool for me if i could just add/remove them on the fly to a entity. 

 

@dieSieben07 also while you are reading this thread anyways, thanks for all the questions you answered before on the forums, that helped a lot to get started with modding minecraft :)

 

well thats one of my capabilities, the attaching/detaching is managed within the capability provider

public class authorityCapability implements ICapabilitySerializable<NBTBase> {
    @CapabilityInject(IauthorityCapability.class)
    public static Capability<IauthorityCapability> AUTHORITY = null;

    private authorityProvider instance = null;

    public static void register(){
        CapabilityManager.INSTANCE.register(IauthorityCapability.class, new authorityStorage(), authorityProvider.class);
        MinecraftForge.EVENT_BUS.register(new eventHandler());
        capabilities.register(AUTHORITY);}

    public authorityCapability(Entity e){
        instance = new authorityProvider(e);
    }

    public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
        return capability.equals(AUTHORITY);
    }

    @Nullable
    public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
        if(!capability.equals(AUTHORITY))
            return null;

        return AUTHORITY.<T> cast(instance);
    }

    @Override
    public NBTBase serializeNBT(){
        if(instance == null)
            return new NBTTagCompound();

        return AUTHORITY.getStorage().writeNBT(AUTHORITY, instance, null);
    }

    @Override
    public void deserializeNBT(NBTBase nbt){
        if(instance != null)
            AUTHORITY.getStorage().readNBT(AUTHORITY, instance, null, nbt);
    }

    public static class eventHandler {
        private static final ResourceLocation KEY = new ResourceLocation(OpenEntity.MODID, "authorityCapability");

        @SubscribeEvent
        public void StartTracking(PlayerEvent.StartTracking event){
            if(!event.getTarget().hasCapability(AUTHORITY, null))
                return;

            if(!event.getTarget().getCapability(AUTHORITY, null).isAttached())
                return;

            event.getTarget().getCapability(AUTHORITY, null).syncToClient(event.getEntityPlayer());
        }

        @SubscribeEvent
        public void attachCapability(AttachCapabilitiesEvent<Entity> event) {
            Entity entity = event.getObject();

            if (!giveCapToEntity(entity))
                return;

            if(entity.hasCapability(AUTHORITY, null))
                return;

            //Logger.getLogger(OpenEntity.MODID).info("adding cap INVENTORY " + entity.world.isRemote);

            event.addCapability(KEY, new authorityCapability(entity));
        }

        @SubscribeEvent
        public void onDeath(LivingDeathEvent event) {
            if(event.getEntity().hasCapability(AUTHORITY, null))
                event.getEntity().getCapability(AUTHORITY, null).detach(null);
        }

        public static boolean giveCapToEntity(Entity e){
            if(e instanceof EntityCreature)
                return true;

            if(e instanceof EntityMinecart)
                return true;

            return false;
        }
    }
}

 

Edited by ben_mkiv
Link to comment
Share on other sites

So my problem with this is that my Capability is used to attach a OpenComputers machine to Mobs for example. Which will be used on like 1-2 Mobs for each user, while every other mob has to get the capability handler assigned even when never using it.

 

maybe capabilities are the wrong tool for that, but i dont know how else to attach custom data to a mob

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • " BLACK MAGIC INSTANT DEATH SPELL CASTER IN UGANDA, NETHERLANDS, SPAIN, KENYA, RWANDA POWERFUL WITCHCRAFT REVENGE SPELLS CASTER IN GHANA, BENIN. STRONG LOVE SPELLS CASTER IN MAURITIUS, MALTA. VOODOO DOLL SPELLS IN USA, UK,spell casters spell to make someone sick and die without delay. Germany, Asian, Europe, Philippines, Canada, South Africa, Italy, Peru, India, Iran, Gambia. Sweden, Australia, Nigeria, Spain, Ghana, California, Greece , Voodoo revenge spell casters spell to make someone sick and die without delay. Drmama Bonne +256751735278
    • SPELL CASTER, DEATH SPELL, SPELL CASTER REVIEW, WITCHCRAFT, PSYCHIC, MAGIC FORUM,spell casters spell to make someone sick and die without delay. Germany, Asian, Europe, Philippines, Canada, South Africa, Italy, Peru, India, Iran, Gambia. Sweden, Australia, Nigeria, Spain, Ghana, California, Greece , Voodoo revenge spell casters spell to make someone sick and die without delay. Drmama Bonne +256751735278
    • BLACK MAGIC INSTANT DEATH SPELL CASTER AND POWERFUL+256751735278 BLACK MAGIC INSTANT DEATH SPELL CASTER AND POWERFUL REVENGE SPELLS THAT WORK FAST IN AUSTRALIA, CANADA, UK GERMANY FRANCE DENMARK. +256751735278 Drmama Bonnei , black magic Revenge spells that work overnight or by accident i? Cast these strongest black magic revenge death spells that work fast overnight to kill ex lover, husband, wife girlfriend Enemies overnight without delay. It doesn’t matter whether he or she is in a far location, I guarantee you to have your results you are looking for immediately. Just make sure before you contact me you are committed and you want what you are looking for (Victim Death) because my Revenge spell work fast overnight after casting the spells. Immediately working black magic death spells that work fast will be cast on the person and result is 48hours,24 Hours . How To Cast A Revenge Spell On Someone, Call/ Whats App  Drmama Bonne Revenge Spells That Work Overnight to kill wicked Step-dad/ Step mom Death Revenge Spell on wicked friends, Voodoo Revenge Spells to kill Enemies Black Magic Spells To Harm Someone, Black magic Revenge spells on ex lover Revenge instant Revenge spells on uncle , powerful instant Revenge spells caster, online instant spell that work fast in USA, UK, Kuwait, Germany, Asian, Europe, Philippines, Canada, South Africa, Italy, Peru, India, Iran, Gambia. Sweden, Australia, Nigeria, Spain, Ghana, California, Greece , Voodoo revenge spell casters spell to make someone sick and die without delay. Germany, Asian, Europe, Philippines, Canada, South Africa, Italy, Peru, India, Iran, Gambia. Sweden, Australia, Nigeria, Spain, Ghana, California, Greece , Voodoo revenge spell casters spell to make someone sick and die without delay. Drmama Bonne +256751735278
    • Voodoo Spells Caster /SANGOMA / Death & Revenge Spells  +256751735278 Drmama Bonnei , black magic Revenge spells that work overnight or by accident i? Cast these strongest black magic revenge death spells that work fast overnight to kill ex lover, husband, wife girlfriend Enemies overnight without delay. It doesn’t matter whether he or she is in a far location, I guarantee you to have your results you are looking for immediately. Just make sure before you contact me you are committed and you want what you are looking for (Victim Death) because my Revenge spell work fast overnight after casting the spells. Immediately working black magic death spells that work fast will be cast on the person and result is 48hours,24 Hours . How To Cast A Revenge Spell On Someone, Call/ Whats App  Drmama Bonne Revenge Spells That Work Overnight to kill wicked Step-dad/ Step mom Death Revenge Spell on wicked friends, Voodoo Revenge Spells to kill Enemies Black Magic Spells To Harm Someone, Black magic Revenge spells on ex lover Revenge instant Revenge spells on uncle , powerful instant Revenge spells caster, online instant spell that work fast in USA, UK, Kuwait, Germany, Asian, Europe, Philippines, Canada, South Africa, Italy, Peru, India, Iran, Gambia. Sweden, Australia, Nigeria, Spain, Ghana, California, Greece , Voodoo revenge spell casters spell to make someone sick and die without delay. Germany, Asian, Europe, Philippines, Canada, South Africa, Italy, Peru, India, Iran, Gambia. Sweden, Australia, Nigeria, Spain, Ghana, California, Greece , Voodoo revenge spell casters spell to make someone sick and die without delay. Drmama Bonne +256751735278
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.