Custom Entities not overiding normal ones

Discussion in 'Plugin Development' started by Dablakbandit, Aug 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Dablakbandit

    I have created custom classes that override the normal NMS entities with this method

    Code:
    public void registerEntity(String name, int id, Class<?> test) {
            try {
                Class<?> class1 = (Class<?>) NMSUtils.getNMSClass("EntityTypes").getDeclaredMethod("a", int.class).invoke(null, id);
                List<Map<?, ?>> dataMaps = new ArrayList<Map<?, ?>>();
                for (Field f : NMSUtils.getNMSClass("EntityTypes").getDeclaredFields()) {
                    if (f.getType().getSimpleName().equals(Map.class.getSimpleName())) {
                        f.setAccessible(true);
                        dataMaps.add((Map<?, ?>) f.get(null));
                    }
                }
                if (dataMaps.get(2).containsKey(id)) {
                    dataMaps.get(0).remove(name);
                    dataMaps.get(2).remove(id);
                }
                Method method = NMSUtils.getNMSClass("EntityTypes").getDeclaredMethod("a", Class.class, String.class, int.class);
                method.setAccessible(true);
                method.invoke(null, test, name, id);
                Class<?> biomebase = NMSUtils.getNMSClass("BiomeBase");
                Class<?> biomemeta = NMSUtils.getNMSClass("BiomeMeta");
                for (Field f : biomebase.getDeclaredFields()) {
                    if (f.getType().getSimpleName().equals(biomebase.getSimpleName())) {
                        if (f.get(null) != null) {
                            for (Field list : biomebase.getDeclaredFields()) {
                                if (list.getType().getSimpleName().equals(List.class.getSimpleName())) {
                                    list.setAccessible(true);
                                    List<?> metaList = (List<?>) list.get(f.get(null));
                                    for (Object meta : metaList) {
                                        Field clazz = biomemeta.cast(meta).getClass().getDeclaredFields()[0];
                                        if (clazz.get(meta).equals(class1)) {
                                            clazz.set(meta, test);
                                        }
                                    }
                                }
                            }
     
                        }
                    }
                }
     
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    which works as when a new entity spawns its my custom class, but for the already current entities in the world, they are still the old NMS class of a normal entity, which means i cant use my custom methods in the custom class to edit the entity.

    Was wondering if anyone knows how to fully override the already spawned entity's

    Thanks,
    Dablakbandit
     
  2. Dablakbandit get all mobs on the server, loop through them, remove it and spawn it again at the same location. is that what you were searching for?
     
  3. Offline

    Dablakbandit

    This would work yes, but the entities wouldn't have like the same armour/attributes that they had beforehand
     
  4. Dablakbandit write a method which does that
    Code:
    private void changeMobs(LivingEntity mobToChange){
    YourMob m = spawnYourMob(mobToChange);
    m.setMaxHealth(mobToChange.getMaxHealth());
    m.setHealth(mobToChange.getHealth());
    setArmor(mobToChange, m);
    setEffects(mobToChange, m);
    mobToChange.remove();
    }
    spawnYourMob(LivingEntity) -> checks which mob you want to change, gets your custom mob and spawn it at the location of the mob you want to change. it returns the mob you spawned.
    setArmor(LivingEntity, YourMob) -> checks if the mob you want to change is wearing any armor and if so, set the armor of your spawned mob to the same
    setEffects(LivingEntity, YourMob) -> checks if the mob is effected by any potioneffects or other stuff and if so, set the effects of your spawned mob to the same

    That's not that hard and could be easily done i think. would be at least my way to do this. i guess there is no easy way to copy the monster, except you got an constructor, which copies the values for you :)
     
Thread Status:
Not open for further replies.

Share This Page