[1.11.2] invisible custom entity

Discussion in 'Spigot Plugin Development' started by Niels_Lucas, Jun 5, 2017.

  1. Hey everyone, I am new to java and started working on minecraft plugins to learn some java. Every was going fine till I try to make a custom enity. A custom villager to be specific.

    To be clear: I can spawn the villager, I just can't see it. I can hear it, zombies will attack the villager I just CANT SEE IT.

    Am I missing something? Am I just stupit? I am having this bugg for couple of days now and I really want this to be fixed for me.

    for my code:

    Code (Text):
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class customMob extends JavaPlugin implements Listener {
        public Commands commands = new Commands();
       
        public void onEnable() {
            getCommand(commands.cmd1).setExecutor(commands);
            CustomEntityRegistry.registerCustomEntity(120, "villager", customVillager.class);
        }
       
        public void onDisable() {
           
        }
    }
    Command(s):

    Code (Text):
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_11_R1.CraftWorld;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;

    import net.md_5.bungee.api.ChatColor;
    import net.minecraft.server.v1_11_R1.CommandExecute;
    import net.minecraft.server.v1_11_R1.EntityVillager;
    import net.minecraft.server.v1_11_R1.WorldServer;

    public class Commands extends CommandExecute implements Listener, CommandExecutor{

        public String cmd1 = "mob";
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if(sender instanceof Player) {
                Player player = (Player)sender;
                Location loc = player.getLocation();
                WorldServer world = ((CraftWorld)player.getWorld()).getHandle();
               
                if(cmd.getName().equalsIgnoreCase(cmd1)) {
                     EntityVillager villager = new customVillager(world);
                     villager.setCustomName(ChatColor.RED + args[0]);
                     villager.setCustomNameVisible(true);
                     
                     villager.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
                     
                     world.addEntity(villager);
                }
            }
            else
            {
                sender.sendMessage(ChatColor.RED + "Only players can use this command!");
            }
            return false;
        }

    }
    Custom Villager:
    Code (Text):
    import net.minecraft.server.v1_11_R1.EntityVillager;
    import net.minecraft.server.v1_11_R1.WorldServer;

    public class customVillager extends EntityVillager {

        public customVillager(WorldServer world) {
            super(world);
        }

    }
    costum entity registation I got from: https://www.spigotmc.org/threads/1-...override-existing-entities-and-to-not.194459/

    Code (Text):
    import com.google.common.collect.BiMap;
    import com.google.common.collect.HashBiMap;
    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;
    import java.util.HashMap;
    import java.util.Map;
    import net.minecraft.server.v1_11_R1.RegistryMaterials;
    import net.minecraft.server.v1_11_R1.Entity;
    import net.minecraft.server.v1_11_R1.EntityTypes;
    import net.minecraft.server.v1_11_R1.MinecraftKey;

    public class CustomEntityRegistry extends RegistryMaterials {

        private static CustomEntityRegistry instance = null;

        private final BiMap<MinecraftKey, Class<? extends Entity>> customEntities = HashBiMap.create();
        private final BiMap<Class<? extends Entity>, MinecraftKey> customEntityClasses = this.customEntities.inverse();
        private final Map<Class<? extends Entity>, Integer> customEntityIds = new HashMap<>();

        private final RegistryMaterials wrapped;

        private CustomEntityRegistry(RegistryMaterials original) {
            this.wrapped = original;
        }

        public static CustomEntityRegistry getInstance() {
            if (instance != null) {
                return instance;
            }

            instance = new CustomEntityRegistry(EntityTypes.b);

            try {
                //TODO: Update name on version change (RegistryMaterials)
                Field registryMaterialsField = EntityTypes.class.getDeclaredField("b");
                registryMaterialsField.setAccessible(true);

                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(registryMaterialsField, registryMaterialsField.getModifiers() & ~Modifier.FINAL);

                registryMaterialsField.set(null, instance);
            } catch (Exception e) {
                instance = null;

                throw new RuntimeException("Unable to override the old entity RegistryMaterials", e);
            }

            return instance;
        }

        public static void registerCustomEntity(int entityId, String entityName, Class<? extends Entity> entityClass) {
            getInstance().putCustomEntity(entityId, entityName, entityClass);
        }

        public void putCustomEntity(int entityId, String entityName, Class<? extends Entity> entityClass) {
            MinecraftKey minecraftKey = new MinecraftKey(entityName);

            this.customEntities.put(minecraftKey, entityClass);
            this.customEntityIds.put(entityClass, entityId);
        }

        @Override
        public Class<? extends Entity> get(Object key) {
            if (this.customEntities.containsKey(key)) {
                return this.customEntities.get(key);
            }

            return (Class<? extends Entity>) wrapped.get(key);
        }

        @Override
        public int a(Object key) { //TODO: Update name on version change (getId)
            if (this.customEntityIds.containsKey(key)) {
                return this.customEntityIds.get(key);
            }

            return this.wrapped.a(key);
        }

        @Override
        public MinecraftKey b(Object value) { //TODO: Update name on version change (getKey)
            if (this.customEntityClasses.containsKey(value)) {
                return this.customEntityClasses.get(value);
            }

            return (MinecraftKey) wrapped.b(value);
        }
    }
     
  2. I don't know, but this is how I make my custom entities
    Code (Text):
    Villager v = (Villager) Bukkit.getWorld("world").spawnEntity(loc, EntityType.VILLAGER);
    v.setCustomName("whatever");
     
  3. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    That's not a custom entity, that's just spawning a villager. OP Wants to modify the default entity behavior, thus a custom entity.

    EDIT: I don't use NMS much, least of all custom entities, but maybe this might help: https://bukkit.org/threads/nms-custom-entity-is-like-invisible.322708/
     
  4. Oh haha, sorry. That's how I create my entities. I also never really worked with NMS.