Bosses plugin

Discussion in 'Plugin Development' started by Limbo, Jun 14, 2022.

  1. Offline

    Limbo

    Hello, I'm trying to create a plugin with bosses on my server, everything is ready, there are no errors, but when I run it, I get the following error:

    at ru.zubastik.mobs.Spawner$SpawnerUtils.init(Spawner.java:115) ~[?:?]
    at ru.zubastik.main.Mobs.onEnable(Mobs.java:27) ~[?:?]
    Caused by: java.lang.NullPointerException: Cannot invoke "[Lru.zubastik.mobs.Cus
    tomTypes;.clone()" because "ru.zubastik.mobs.CustomTypes.$VALUES" is null
    at ru.zubastik.mobs.CustomTypes.values(CustomTypes.java:16) ~[?:?]
    at ru.zubastik.mobs.CustomTypes.<init>(CustomTypes.java:29) ~[?:?]
    at ru.zubastik.mobs.CustomTypes.<clinit>(CustomTypes.java:18) ~[?:?]

    My code:

    Code:
    package ru.zubastik.mobs;
    
    import net.minecraft.server.v1_16_R3.Entity;
    import net.minecraft.server.v1_16_R3.EntityTypes;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import java.lang.reflect.Field;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    enum CustomTypes {
    
        DARKWITCH("PrisonDarkWitch", 66, (Class)CustomWitch.class);
    
        private static Map<Integer, UUID> assoc;
        String name;
        int id;
        Class<? extends Entity> custom;
        static {assoc = new HashMap<>();}
    
        CustomTypes(String name, int id, Class<? extends Entity> custom) {
            this.name = name;
            this.id = id;
            addToMaps(this.custom = custom, name, id);
        }
    
        public static void spawnEntity(CustomTypes entityType, Location location, Spawner spawner) {
            try {
                Entity entity = entityType.getEntityClass().getConstructor(new Class[] { Spawner.class }).newInstance(new Object[] { spawner });
                entity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
                ((CraftWorld)location.getWorld()).getHandle().addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM);
            } catch (InstantiationException|IllegalAccessException|IllegalArgumentException|java.lang.reflect.InvocationTargetException|NoSuchMethodException|SecurityException ex7) {
                ex7.printStackTrace();
            }
        }
    
        public static Object getPrivateField(String fieldName, Class<EntityTypes> clazz, Object object) {
            Object o = null;
            try {
                Field field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                o = field.get(object);
            } catch (NoSuchFieldException|IllegalAccessException ex5) {
                ex5.printStackTrace();
            }
            return o;
        }
    
        static void addToMaps(Class<?> clazz, String name, int id) {
            ((Map<String, Class<?>>)getPrivateField("c", EntityTypes.class, null)).put(name, clazz);
            ((Map<Class<?>, String>)getPrivateField("d", EntityTypes.class, null)).put(clazz, name);
            ((Map<Class<?>, Integer>)getPrivateField("f", EntityTypes.class, null)).put(clazz, Integer.valueOf(id));
        }
    
        public String getName() {
            return this.name;
        }
    
        public Integer getID() {
            return Integer.valueOf(this.id);
        }
    
        public Class<? extends Entity> getEntityClass() {
            return this.custom;
        }
    
        public static void associate(Entity entity, Spawner spawner) {
            assoc.put(Integer.valueOf(entity.hashCode()), spawner.getUUID());
        }
    
        public static Spawner getSpawnerByEntity(Entity entity) {
            return Spawner.spawners.get(assoc.get(Integer.valueOf(entity.hashCode())));
        }
    
    }
    HELP PLEASE I WILL HAVE TO!

    THANKS IN ADVANCE!!!
     
  2. Online

    timtower Administrator Administrator Moderator

    @Limbo Why does this look like a decompiled class?
    And need those other classes as well in that stacktrace.
     
  3. Offline

    Limbo

    Here are the other classes:

    Code:
    package ru.zubastik.mobs;
    
    import net.minecraft.server.v1_16_R3.Entity;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.scheduler.BukkitRunnable;
    import ru.zubastik.main.Mobs;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    public class Spawner {
    
        private static final Mobs plugin = Mobs.getPlugin(Mobs.class);
        public static Map<UUID, Spawner> spawners = new HashMap<>();
        Location location;
        CustomTypes type;
        Entity current;
        long deathTime;
        int interval;
        UUID uuid;
    
        public Spawner(Location location, CustomTypes type, int interval) {
            this.location = location;
            this.current = null;
            this.deathTime = -1L;
            this.uuid = UUID.randomUUID();
            this.type = type;
            this.interval = interval;
            spawners.put(this.uuid, this);
        }
    
        public Spawner(Location location, CustomTypes type) {
            this.location = location;
            this.current = null;
            this.deathTime = -1L;
            this.uuid = UUID.randomUUID();
            this.type = type;
            this.interval = -1;
            spawners.put(this.uuid, this);
        }
    
        public void spawn() {
            try {
                if (this.location != null && this.location.getChunk() != null && this.location.getChunk().isLoaded())
                    CustomTypes.spawnEntity(this.type, this.location.clone().add(0.0D, 1.0D, 0.0D), this);
            } catch (NullPointerException nullPointerException) {}
        }
    
        public void dead() {
            this.current = null;
            this.deathTime = System.currentTimeMillis() / 1000L;
        }
    
        public Location getSpawnLocation() {
            return this.location;
        }
    
        public void update() {
            if (this.current == null) {
                if (System.currentTimeMillis() / 1000L - this.deathTime >= this.interval && this.interval > 0)
                    spawn();
            } else if (this.current.getBukkitEntity().getLocation().distance(this.location) > 64.0D) {
                reset();
            }
        }
    
        public void register(Entity entity) {
            this.current = entity;
        }
    
        public void reset() {
            if (this.current != null) {
                if (this.current.passengers != null)
                    this.current.passengers.remove(this.current.getBukkitEntity());
                this.current.getBukkitEntity().remove();
            }
            this.deathTime = -1L;
            spawn();
        }
    
        public UUID getUUID() {
            return this.uuid;
        }
    
        public Entity getCurrent() {
            return this.current;
        }
    
        public CustomTypes getType() {
            return this.type;
        }
    
        public static class SpawnerUtils {
            public static void init() {
                ConfigurationSection section = plugin.mobs.getConfig().getConfigurationSection("mobs");
                for (String currentSpawn : section.getKeys(false)) {
                    ConfigurationSection path = section.getConfigurationSection(currentSpawn);
                    Location loc = null;
                    double x = 0.0D;
                    double y = 0.0D;
                    double z = 0.0D;
                    try {
                        String[] locFull = path.getString("location").split(" ");
                        World world = Bukkit.getWorld(locFull[0]);
                        x = Double.valueOf(locFull[1]).doubleValue();
                        y = Double.valueOf(locFull[2]).doubleValue();
                        z = Double.valueOf(locFull[3]).doubleValue();
                        loc = new Location(world, x, y, z);
                    } catch (NullPointerException|NumberFormatException ex7) {
                        ex7.printStackTrace();
                    }
                    CustomTypes type = CustomTypes.valueOf(path.getString("type").toUpperCase());
                    int interval = path.getInt("interval");
                    (new Spawner(
                            loc,
                            type,
                            interval))
                            .update();
                }
                Spawner.plugin.log("&aLoaded &6" + Spawner.spawners.size() + " &amob-spawners");
            }
        }
    
        public static class SpawnerUpdater extends BukkitRunnable {
            public void run() {
                for (Spawner cSpawner : Spawner.spawners.values())
                    cSpawner.update();
            }
        }
    
    }
    
    Code:
    package ru.zubastik.mobs;
    
    import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    import com.sk89q.worldguard.protection.managers.RegionManager;
    import com.sk89q.worldguard.protection.regions.ProtectedRegion;
    import net.minecraft.server.v1_16_R3.*;
    import net.minecraft.server.v1_16_R3.EntityTypes;
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.GameMode;
    import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
    import org.bukkit.craftbukkit.v1_16_R3.entity.CraftEntity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityRegainHealthEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    import ru.zubastik.economy.AbstractEconomy;
    import ru.zubastik.items.AbstractItems;
    import ru.zubastik.main.Mobs;
    
    import java.util.*;
    
    public class CustomWitch extends EntityMonster {
    
        private static final Mobs plugin = Mobs.getPlugin(Mobs.class);
        private static final WorldGuardPlugin worldguard = plugin.getWorldGuard();
        int interval;
        IChatBaseComponent name;
        int health;
        double damage;
        double followRange;
        double knobackResistence;
        double speed;
        double money;
        int debuff;
        int toDebuff;
        Spawner spawner;
        CraftEntity bukkitEntity;
        HashMap<String, Integer> attackers;
        int totalDamage;
        int hpDelay;
        Random rnd;
    
        public CustomWitch(Spawner spawner) {
            super(EntityTypes.WITCH, ((CraftWorld)spawner.getSpawnLocation().getWorld()).getHandle());
            this.interval = 60;
            this.name = new ChatMessage("");
            this.health = 800;
            this.damage = 11.0D;
            this.followRange = 32.0D;
            this.knobackResistence = 2.147483647E9D;
            this.speed = 0.00925D;
            this.money = 4500.0D;
            this.debuff = 200;
            this.toDebuff = this.debuff;
            this.hpDelay = 20;
            this.rnd = new Random();
            ((LivingEntity)getBukkitEntity()).setRemoveWhenFarAway(false);
            ((LivingEntity)getBukkitEntity()).addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 2147483647, 1), true);
            ((LivingEntity)getBukkitEntity()).addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2147483647, 2), true);
            getAttributeInstance(GenericAttributes.MAX_HEALTH).setValue(this.health);
            getAttributeInstance(GenericAttributes.FOLLOW_RANGE).setValue(this.followRange);
            getAttributeInstance(GenericAttributes.KNOCKBACK_RESISTANCE).setValue(this.knobackResistence);
            getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(this.speed);
            getAttributeInstance(GenericAttributes.ATTACK_DAMAGE).setValue(this.damage);
            setHealth(this.health);
            this.goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, (float)this.followRange));
            this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 10.0D));
            this.goalSelector.a(7, new PathfinderGoalRandomStroll(this, 1.0D));
            this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, true));
            this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, new Class[0]));
            this.goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, (float)(this.followRange / 2.0D)));
            this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, this.damage, true));
            (this.spawner = spawner).register(this);
            setCustomName(new ChatMessage(this.name.getText().replace("<3", String.format("%.2f", new Object[] { Float.valueOf(getHealth()) }))));
            setCustomNameVisible(true);
            this.canPickUpLoot = false;
            this.persistent = true;
            this.expToDrop = 0;
            this.bukkitEntity = getBukkitEntity();
            this.attackers = new HashMap<>();
            this.totalDamage = 0;
        }
    
        protected CustomWitch(EntityTypes<? extends EntityMonster> var0, World var1) {
            super(var0, var1);
        }
    
        public boolean damageEntity(DamageSource source, float a) {
            if ((this.passengers != null && source.getEntity() == this.passengers) || source == DamageSource.STUCK)
                return false;
            setCustomName(new ChatMessage(this.name.getText().replace("<3", String.format("%.2f", new Object[] { Float.valueOf(getHealth()) }))));
            if (this.passengers != null)
                return (source != DamageSource.projectile((Entity)this, (Entity) this.passengers) && ((Entity) this.passengers).damageEntity(source, a));
            Entity entity = source.getEntity();
            if (entity != null && entity.getBukkitEntity().getType() == EntityType.PLAYER) {
                Player pAttacker = (Player)entity.getBukkitEntity();
                if (!this.attackers.containsKey(pAttacker.getName())) {
                    this.attackers.put(pAttacker.getName(), Integer.valueOf((int)a));
                } else {
                    this.attackers.put(pAttacker.getName(), Integer.valueOf((int)(((Integer)this.attackers.get(pAttacker.getName())).intValue() + a)));
                }
                this.totalDamage += (int)a;
            }
            if ((new Random()).nextFloat() < 0.1D)
                for (org.bukkit.entity.Entity e : getBukkitEntity().getNearbyEntities(15.0D, 5.0D, 15.0D)) {
                    if (e.getType() == EntityType.PLAYER) {
                        LivingEntity ent = (LivingEntity)e;
                        ent.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 500, 0), true);
                        ent.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 150, 255, true));
                        ent.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 200, 255, true, false));
                        ent.getLocation().getWorld().playEffect(ent.getLocation(), Effect.ENDEREYE_LAUNCH, 1);
                        ent.sendMessage("");
                    }
                }
            if ((new Random()).nextFloat() < 0.07D)
                for (org.bukkit.entity.Entity e : getBukkitEntity().getNearbyEntities(15.0D, 10.0D, 15.0D)) {
                    if (e instanceof Player) {
                        Player p = (Player)e;
                        p.sendMessage("");
                        org.bukkit.inventory.ItemStack i1 = p.getInventory().getItem(0);
                        org.bukkit.inventory.ItemStack i2 = p.getInventory().getItem(1);
                        org.bukkit.inventory.ItemStack i3 = p.getInventory().getItem(2);
                        org.bukkit.inventory.ItemStack i4 = p.getInventory().getItem(3);
                        org.bukkit.inventory.ItemStack i5 = p.getInventory().getItem(4);
                        org.bukkit.inventory.ItemStack i6 = p.getInventory().getItem(5);
                        org.bukkit.inventory.ItemStack i7 = p.getInventory().getItem(6);
                        org.bukkit.inventory.ItemStack i8 = p.getInventory().getItem(7);
                        org.bukkit.inventory.ItemStack i9 = p.getInventory().getItem(8);
                        org.bukkit.inventory.ItemStack i10 = p.getInventory().getItem(9);
                        p.getInventory().setItem(0, i5);
                        p.getInventory().setItem(1, i7);
                        p.getInventory().setItem(2, i10);
                        p.getInventory().setItem(3, i8);
                        p.getInventory().setItem(4, i1);
                        p.getInventory().setItem(5, i2);
                        p.getInventory().setItem(6, i3);
                        p.getInventory().setItem(7, i6);
                        p.getInventory().setItem(8, i4);
                        p.getInventory().setItem(9, i9);
                    }
                }
            if ((new Random()).nextFloat() < 0.1D)
                for (org.bukkit.entity.Entity e : getBukkitEntity().getNearbyEntities(15.0D, 5.0D, 15.0D)) {
                    if (e.getType() == EntityType.PLAYER) {
                        LivingEntity ent = (LivingEntity)e;
                        ent.setVelocity((new Vector(0.0D, 1.075D, 0.0D)).multiply(1));
                        ent.getLocation().getWorld().playEffect(ent.getLocation(), Effect.ENDER_SIGNAL, 1);
                        ent.sendMessage("");
                    }
                }
            return super.damageEntity(source, a);
        }
    
        public void die() {
            if (this.spawner != null)
                this.spawner.dead();
            if (this.killer != null) {
                Bukkit.broadcastMessage("");
                        HashMap<String, Double> percents = plugin.calculatePercents(this.attackers, this.totalDamage);
                for (String key : percents.keySet()) {
                    Player pp = Bukkit.getPlayer(key);
                    Double money = Double.valueOf(((Double)percents.get(key)).doubleValue() * this.money);
                    if (money.doubleValue() < 0.0D)
                        money = Double.valueOf(0.0D);
                    if (pp != null) {
                        AbstractEconomy.addBalance(pp, money.doubleValue());
                    }
                    if (Bukkit.getPlayer(key) != null)
                        Bukkit.getPlayer(key).sendMessage("".replace("%money%", String.format("%.2f", new Object[] { money })));
                }
                ItemStack star = AbstractItems.getItem("star");
                star.setAmount((new Random()).nextInt(15) + 10);
                getBukkitEntity().getLocation().getWorld().dropItem(this.bukkitEntity.getLocation(), star);
            }
            super.die();
        }
    
        public String getName() {
            return this.name.getText().replace("<3", String.format("%.2f", new Object[] { Float.valueOf(getHealth()) }));
        }
    
    }
    
     
  4. Offline

    Limbo

    Just adding a name and id to the map does not work for me. I used to make the same plugin for version 1.8, but already on version 1.16 there were problems
     

Share This Page