Null error

Discussion in 'Plugin Development' started by vicen621, Sep 18, 2020.

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

    vicen621

    Hi!, I hope someone can help me, Im making a custom Mob plugin that function with days in game, when i summon a skeleton give this error on the console:

    Code:
    [ERROR] Could not pass event CreatureSpawnEvent to DeathNightmares v1.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    atorg.bukkit.craftbukkit.v1_16_R2.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:637) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    atorg.bukkit.craftbukkit.v1_16_R2.event.CraftEventFactory.doEntityAddEventCalling(CraftEventFactory.java:574) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.WorldServer.addEntity0(WorldServer.java:911) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.WorldServer.addEntity(WorldServer.java:841) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.WorldAccess.lambda$0(WorldAccess.java:13) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:419) ~[?:1.8.0_212]
    at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742) ~[?:1.8.0_212]
    at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) ~[?:1.8.0_212]
    at net.minecraft.server.v1_16_R2.WorldAccess.addAllEntities(WorldAccess.java:13) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.SpawnerCreature.a(SpawnerCreature.java:197) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.SpawnerCreature.a(SpawnerCreature.java:137) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.SpawnerCreature.a(SpawnerCreature.java:121) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.ChunkProviderServer.lambda$9(ChunkProviderServer.java:394) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a
    at java.util.ArrayList.forEach(ArrayList.java:1257) ~[?:1.8.0_212]
    at net.minecraft.server.v1_16_R2.ChunkProviderServer.tickChunks(ChunkProviderServer.java:378) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.ChunkProviderServer.tick(ChunkProviderServer.java:346) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.WorldServer.doTick(WorldServer.java:329) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.MinecraftServer.b(MinecraftServer.java:1104) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.DedicatedServer.b(DedicatedServer.java:352) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.MinecraftServer.a(MinecraftServer.java:1007) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:846) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_212]
    Caused by: java.lang.NullPointerException
    at com.permadeathcoredeathtrain.Mobs.Skeletons.b(Skeletons.java:89) ~[?:?]
    at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot-1.16.2.jar:git-Spigot-b5a13e6-0d8350a]
    ... 26 more
    And here the Skeletons class and the Main Class

    Skeletons:

    Code:
    package com.permadeathcoredeathtrain.Mobs;
    
    import com.permadeathcoredeathtrain.Main;
    import com.permadeathcoredeathtrain.Utils.d;
    import org.bukkit.*;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.*;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.LeatherArmorMeta;
    import java.util.concurrent.ThreadLocalRandom;
    
    import static org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.CUSTOM;
    
    public class Skeletons implements Listener{
        private Main plugin;
    
        public Skeletons(Main main) {
            this.plugin = main;
        }
    
        @EventHandler
        private void b(CreatureSpawnEvent e) {
            Entity Mob;
            ItemStack Helmet;
            ItemStack Chestplate;
            ItemStack Leggings;
            ItemStack Boots;
            ItemStack Weapon;
            LivingEntity Entity;
            ItemMeta WeaponMeta;
    
            if (this.plugin.getDays() >= 10L && e.getSpawnReason() != CUSTOM && (e.getEntity().getType() == EntityType.STRAY || e.getEntity().getType() == EntityType.SKELETON)) {
                int i = ThreadLocalRandom.current().nextInt(4);
                if (i == 0) {
                    Location loc = e.getLocation();
                    Mob = Main.instance.f().a("Skeleton", EntityType.SKELETON, loc, CUSTOM);
                    Mob.setCustomName(d.chat("&6Esqueleto Asesino"));
                    Mob.setCustomNameVisible(true);
                    Entity = (LivingEntity) Mob;
                    Helmet = new ItemStack(Material.DIAMOND_HELMET);
                    Chestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
                    Leggings = new ItemStack(Material.DIAMOND_LEGGINGS);
                    Boots = new ItemStack(Material.DIAMOND_BOOTS);
                    Weapon = new ItemStack(Material.BOW, 1);
                    WeaponMeta = Weapon.getItemMeta();
                    WeaponMeta.addEnchant(Enchantment.ARROW_DAMAGE, 3, true);
                    Weapon.setItemMeta(WeaponMeta);
                    Entity.getEquipment().setHelmet(Helmet);
                    Entity.getEquipment().setChestplate(Chestplate);
                    Entity.getEquipment().setLeggings(Leggings);
                    Entity.getEquipment().setBoots(Boots);
                    Entity.getEquipment().setItemInMainHand(Weapon);
                    ((Skeleton) Mob).getEquipment().setHelmetDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setChestplateDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setLeggingsDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setBootsDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setItemInMainHandDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setItemInOffHandDropChance(0.0F);
                    e.getEntity().remove();
                }
    
                if (i == 1) {
                    Location loc = e.getLocation();
                    Mob = Main.instance.f().a("Skeleton", EntityType.SKELETON, loc, CUSTOM);
                    Mob.setCustomName(d.chat("&6Esqueleto Cosoplayer"));
                    Mob.setCustomNameVisible(true);
                    Entity = (LivingEntity) Mob;
                    Helmet = new ItemStack(Material.IRON_HELMET);
                    Chestplate = new ItemStack(Material.IRON_CHESTPLATE);
                    Leggings = new ItemStack(Material.IRON_LEGGINGS);
                    Boots = new ItemStack(Material.IRON_BOOTS);
                    Weapon = new ItemStack(Material.DIAMOND_AXE, 1);
                    WeaponMeta = Weapon.getItemMeta();
                    WeaponMeta.addEnchant(Enchantment.FIRE_ASPECT, 2, true);
                    Weapon.setItemMeta(WeaponMeta);
                    Entity.getEquipment().setHelmet(Helmet);
                    Entity.getEquipment().setChestplate(Chestplate);
                    Entity.getEquipment().setLeggings(Leggings);
                    Entity.getEquipment().setBoots(Boots);
                    Entity.getEquipment().setItemInMainHand(Weapon);
                    ((Skeleton) Mob).getEquipment().setHelmetDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setChestplateDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setLeggingsDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setBootsDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setItemInMainHandDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setItemInOffHandDropChance(0.0F);
                    e.getEntity().remove();
                }
    
                if (i == 2) {
                    Mob = Main.instance.f().a("Skeleton", EntityType.SKELETON, e.getLocation(), CUSTOM);
                    Entity = (LivingEntity)Mob;
                    Helmet = new ItemStack(Material.GOLDEN_HELMET);
                    Chestplate = new ItemStack(Material.GOLDEN_CHESTPLATE);
                    Leggings = new ItemStack(Material.GOLDEN_LEGGINGS);
                    Boots = new ItemStack(Material.GOLDEN_BOOTS);
                    Weapon = new ItemStack(Material.BOW, 1);
                    WeaponMeta = Weapon.getItemMeta();
                    WeaponMeta.addEnchant(Enchantment.ARROW_KNOCKBACK, 10, true);
                    Weapon.setItemMeta(WeaponMeta);
                    Entity.getEquipment().setHelmet(Helmet);
                    Entity.getEquipment().setChestplate(Chestplate);
                    Entity.getEquipment().setLeggings(Leggings);
                    Entity.getEquipment().setBoots(Boots);
                    Entity.getEquipment().setItemInMainHand(Weapon);
                    ((Skeleton) Mob).getEquipment().setHelmetDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setChestplateDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setLeggingsDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setBootsDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setItemInMainHandDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setItemInOffHandDropChance(0.0F);
                    e.getEntity().remove();
                }
    
                if (i == 3) {
                    Location loc = e.getLocation();
                    Mob = Main.instance.f().a("Skeleton", EntityType.SKELETON, loc, CUSTOM);
                    Mob.setCustomName(d.chat("&6Esqueleto Peronista"));
                    Mob.setCustomNameVisible(true);
                    Entity = (LivingEntity) Mob;
                    Helmet = new ItemStack(Material.LEATHER_HELMET);
                    Chestplate = new ItemStack(Material.LEATHER_CHESTPLATE);
                    Leggings = new ItemStack(Material.LEATHER_LEGGINGS);
                    Boots = new ItemStack(Material.LEATHER_BOOTS);
                    Weapon = new ItemStack(Material.BOW, 1);
                    WeaponMeta = Weapon.getItemMeta();
                    WeaponMeta.addEnchant(Enchantment.ARROW_DAMAGE, 7, true);
                    Weapon.setItemMeta(WeaponMeta);
                    LeatherArmorMeta HelmetMeta = (LeatherArmorMeta) Helmet.getItemMeta();
                    LeatherArmorMeta ChestplateMeta = (LeatherArmorMeta) Chestplate.getItemMeta();
                    LeatherArmorMeta LeggingsMeta = (LeatherArmorMeta) Leggings.getItemMeta();
                    LeatherArmorMeta BootsMeta = (LeatherArmorMeta) Boots.getItemMeta();
                    HelmetMeta.setColor(Color.RED);
                    ChestplateMeta.setColor(Color.RED);
                    LeggingsMeta.setColor(Color.RED);
                    BootsMeta.setColor(Color.RED);
                    Helmet.setItemMeta(HelmetMeta);
                    Chestplate.setItemMeta(ChestplateMeta);
                    Leggings.setItemMeta(LeggingsMeta);
                    Boots.setItemMeta(BootsMeta);
                    Entity.getEquipment().setHelmet(Helmet);
                    Entity.getEquipment().setChestplate(Chestplate);
                    Entity.getEquipment().setLeggings(Leggings);
                    Entity.getEquipment().setBoots(Boots);
                    Entity.getEquipment().setItemInMainHand(Weapon);
                    ((Skeleton) Mob).getEquipment().setHelmetDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setChestplateDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setLeggingsDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setBootsDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setItemInMainHandDropChance(0.0F);
                    ((Skeleton) Mob).getEquipment().setItemInOffHandDropChance(0.0F);
                    e.getEntity().remove();
                }
            }
        }
    }
    
    Main:

    Code:
    package com.permadeathcoredeathtrain;
    
    import com.permadeathcoredeathtrain.Mobs.Phantoms;
    import com.permadeathcoredeathtrain.Mobs.Skeletons;
    import com.permadeathcoredeathtrain.Mobs.Spiders;
    import com.permadeathcoredeathtrain.Mobs.Zombies;
    import com.permadeathcoredeathtrain.Utils.ShortCuts;
    import net.md_5.bungee.api.ChatMessageType;
    import net.md_5.bungee.api.chat.TextComponent;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Statistic;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import java.time.LocalDate;
    import java.time.LocalTime;
    import java.time.temporal.ChronoUnit;
    import java.util.Iterator;
    import java.util.Objects;
    
    public final class Main extends JavaPlugin implements Listener {
        String fecha;
        private ShortCuts l;
        private Eventos eventos;
        public static Main instance;
        public LocalDate fechaInicio;
        public LocalDate fechaActual = LocalDate.now();
        private long getDays;
        public String tag;
        public long stormTicks;
        public long stormHours;
        public World world;
        public World endWorld;
        public static int dias;
        private long u;
        public World h = Bukkit.getWorld("world");
    
        public Main() {
            this.tag = ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "DEATH" + ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "NIGHTMARES" + ChatColor.GRAY + " >> " + ChatColor.RESET;
            this.world = Bukkit.getWorld("world");
            this.endWorld = Bukkit.getWorld("world_the_end");
        }
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new Spiders(this),this);
            getServer().getPluginManager().registerEvents(new Zombies(this),this);
            getServer().getPluginManager().registerEvents(new Phantoms(this),this);
            getServer().getPluginManager().registerEvents(new Skeletons(this),this);
            this.saveDefaultConfig();
            if (this.getConfig().getString("Fecha").isEmpty()) {
                LocalDate act = LocalDate.now();
                this.fechaActual = act.minusDays(1L);
                int month = this.fechaActual.getMonthValue();
                int day = this.fechaActual.getDayOfMonth();
                String s = "";
                if (month < 10) {
                    s = this.fechaActual.getYear() + "-0" + month + "-";
                } else {
                    s = this.fechaActual.getYear() + "-" + month + "-";
                }
    
                if (day < 10) {
                    s = s + "0" + day;
                } else {
                    s = s + day;
                }
    
                this.getConfig().set("Fecha", s);
                this.saveConfig();
                this.reloadConfig();
            }
    
            this.fecha = this.getConfig().getString("Fecha");
            this.fechaInicio = LocalDate.parse(this.getConfig().getString("Fecha"));
            this.getDays = this.fechaInicio.until(this.fechaActual, ChronoUnit.DAYS);
            System.out.println("\u001B[35m[DeathNightmares] - Estado: \u001b[36mEL PLUGIN SE HA CARGADO CORRECTAMENTE!\u001b[0m");
            System.out.println("\u001B[35m[DeathNightmares] - Version: \u001b[36m1.0\u001b[0m");
            System.out.println("\u001B[35m[DeathNightmares] - Developed by: \u001b[36mVicen621\u001b[0m");
            if (this.getDays == 0L) {
                System.out.println("DIA 1");
                this.getDays = 1L;
            }
    
            if (this.getDays < 1L) {
                System.out.println("[ERROR] 'Fecha' en el archivo config.yml tiene un valor negativo o incorrecto!");
                System.out.println("[ERROR] La fecha introducida genera un numero negativo!");
            }
    
            this.getServer().getPluginManager().registerEvents(this, this);
            instance = this;
            this.eventos = new Eventos(instance);
            this.getServer().getPluginManager().registerEvents(this.eventos, instance);
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    long segundosbrutos = (long) (Main.this.world.getWeatherDuration() / 20);
                    long hours = segundosbrutos % 86400L / 3600L;
                    long minutes = segundosbrutos % 3600L / 60L;
                    long seconds = segundosbrutos % 60L;
                    long days = segundosbrutos / 86400L;
                    String time = String.format("%02d:%02d:%02d:%02d", days, hours, minutes, seconds);
                    Main.this.getDays = Main.this.fechaInicio.until(Main.this.fechaActual, ChronoUnit.DAYS);
                    Main.this.fechaActual = LocalDate.now();
                    Main.this.stormTicks = Main.this.getDays * 3600L;
                    Main.this.stormHours = Main.this.stormTicks / 60L / 60L;
                    Main.this.fechaInicio = LocalDate.parse(Main.this.getConfig().getString("Fecha"));
                    if (Main.this.world.hasStorm()) {
                        String Message = Main.instance.getConfig().getString("Server-Messages.ActionBarMessage");
                        Bukkit.getOnlinePlayers().forEach((player) -> {
                            player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', Message.replace("%tiempo%", time))));
                        });
                    }
    
                }
            }, 0L, 20L);
            if (Bukkit.getWorld((String) Objects.requireNonNull(instance.getConfig().getString("Worlds.MainWorld"))) == null) {
                System.out.println("[ERROR] Error al cargar el mundo principal, esto hará que los Death Train no se presenten.");
                System.out.println("[ERROR] Tan solo ve a config.yml y establece el mundo principal en la opción: MainWorld");
                this.world = (World) Bukkit.getWorlds().get(0);
                System.out.println("[INFO] El plugin utilizará el mundo " + this.world.getName() + " como mundo principal.");
                System.out.println("[INFO] Si deseas utilizar otro mundo, configura en el archivo config.yml.");
            } else {
                this.world = Bukkit.getWorld((String) Objects.requireNonNull(instance.getConfig().getString("Worlds.MainWorld")));
            }
    
            if (Bukkit.getWorld((String) Objects.requireNonNull(instance.getConfig().getString("Worlds.EndWorld"))) == null) {
                System.out.println("[ERROR] Error al cargar el mundo del end, esto hará que el end no funcione como debe.");
                System.out.println("[ERROR] Tan solo ve a config.yml y establece el mundo del end en la opción: EndWorld");
                Iterator<?> var5 = Bukkit.getWorlds().iterator();
    
                while (var5.hasNext()) {
                    World w = (World) var5.next();
                    if (w.getName().endsWith("the_end")) {
                        this.endWorld = this.world;
                        System.out.println("[INFO] El plugin utilizará el mundo " + w.getName() + " como mundo del End.");
                    }
                }
    
                if (this.endWorld == null) {
                    System.out.println("[ERROR] El plugin no pudo encontrar un mundo para el End, esto generará errores.");
                }
            } else {
                this.world = Bukkit.getWorld((String) Objects.requireNonNull(instance.getConfig().getString("Worlds.MainWorld")));
            }
            getServer().getPluginManager().registerEvents(new Spiders(this),this);
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = (Player) sender;
            if (command.getName().equalsIgnoreCase("dn") && sender instanceof Player) {
                if (args.length > 0) {
                    if (args[0].equalsIgnoreCase("awake")) {
                        int timeAwake = player.getStatistic(Statistic.TIME_SINCE_REST) / 20;
                        player.sendMessage(this.tag + ChatColor.DARK_PURPLE + "Tiempo despierto: " + ChatColor.GRAY + timeAwake + "s");
                    } else if (args[0].equalsIgnoreCase("duracion")) {
                        boolean weather = this.world.hasStorm();
                        int stormDurationTick = this.world.getWeatherDuration();
                        int stormTicksToSeconds = stormDurationTick / 20;
                        int seconds = stormTicksToSeconds;
                        if (weather) {
                            LocalTime timeOfDay;
                            String time;
                            if (stormTicksToSeconds < 86400) {
                                timeOfDay = LocalTime.ofSecondOfDay((long) stormTicksToSeconds);
                                time = timeOfDay.toString();
                                player.sendMessage(this.tag + ChatColor.DARK_PURPLE + "Quedan " + ChatColor.GRAY + time);
                            } else {
                                for (dias = 0; seconds > 86400; ++dias) {
                                    seconds -= 86400;
                                }
    
                                timeOfDay = LocalTime.ofSecondOfDay((long) seconds);
                                time = timeOfDay.toString();
                                player.sendMessage(this.tag + ChatColor.DARK_PURPLE + "Quedan " + ChatColor.GRAY + dias + "d " + time);
                            }
                        } else {
                            player.sendMessage(this.tag + ChatColor.DARK_PURPLE + "¡No hay ninguna tormenta en marcha!");
                        }
                    } else if (args[0].equalsIgnoreCase("reload")) {
                        if (player.hasPermission("permadeathcore.reload")) {
                            this.reloadConfig();
                            player.sendMessage(format("&aSe ha recargado el archivo de configuración."));
                            this.fecha = this.getConfig().getString("Fecha");
                            this.fechaInicio = LocalDate.parse(this.getConfig().getString("Fecha"));
                            this.fechaActual = LocalDate.now();
                            this.getDays = this.fechaInicio.until(this.fechaActual, ChronoUnit.DAYS);
                            player.sendMessage(format("&dSe han actualizado los días a &7" + this.getDays));
                            player.sendMessage(format("&5&lSolo los días y mensajes se actualizan, los mundos no, para ello debes reiniciar el servidor"));
                            this.eventos.loadTicks();
                        } else {
                            player.sendMessage(format("&cNo tienes permiso para utilizar este comando."));
                        }
                    } else if (args[0].equalsIgnoreCase("dias")) {
                        if (this.getDays < 1L) {
                            player.sendMessage(this.tag + ChatColor.DARK_PURPLE + "[ERROR!] Se ha producido un error al cargar el dia, config.yml mal configurado.");
                        } else {
                            player.sendMessage(this.tag + ChatColor.DARK_PURPLE + "Estamos en el dia: " + ChatColor.GRAY + this.getDays);
                        }
                    }
                } else {
                    sender.sendMessage(this.tag + ChatColor.LIGHT_PURPLE + "Comandos disponibles:");
                    sender.sendMessage(ChatColor.DARK_PURPLE + "/dn awake " + ChatColor.GRAY + ChatColor.ITALIC + "(Muestra el tiempo despierto)");
                    sender.sendMessage(ChatColor.DARK_PURPLE + "/dn duracion " + ChatColor.GRAY + ChatColor.ITALIC + "(Muestra la duracion de la tormenta)");
                    sender.sendMessage(ChatColor.DARK_PURPLE + "/dn dias " + ChatColor.GRAY + ChatColor.ITALIC + "(Muestra el dia en el que esta el plugin)");
                    sender.sendMessage(ChatColor.DARK_PURPLE + "/dn reload " + ChatColor.GRAY + ChatColor.ITALIC + "(Recarga el archivo config.yml)");
                }
            }
    
            return false;
        }
    
        public static String format(String texto) {
            return ChatColor.translateAlternateColorCodes('&', texto);
        }
    
        public long getDays() {
            return this.getDays;
        }
    
        public ShortCuts f() {
            return this.l;
        }
    
    
    }
    This is the shrotcut Interface:


    Code:
    package com.permadeathcoredeathtrain.Utils;import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    
    public interface ShortCuts {
        EntityType a(Object paramObject);
    
        Class a();
    
        Class a(String paramString);
    
        Object a(EntityType paramEntityType);
    
        Object b(String paramString);
    
        void a(Player paramPlayer, String paramString);
    
        void a(Player paramPlayer, String paramString1, String paramString2, int paramInt1, int paramInt2, int paramInt3);
    
        Object a(World paramWorld);
    
        Entity a(String paramString, EntityType paramEntityType, Location paramLocation, CreatureSpawnEvent.SpawnReason paramSpawnReason);
    
        Entity a(Location paramLocation);
    
        Entity a(EntityType paramEntityType, Location paramLocation, CreatureSpawnEvent.SpawnReason paramSpawnReason);
    
        Entity b(String paramString, EntityType paramEntityType, Location paramLocation, CreatureSpawnEvent.SpawnReason paramSpawnReason);
    
        Entity a(Location paramLocation, CreatureSpawnEvent.SpawnReason paramSpawnReason, boolean paramBoolean);
    
        Entity b(Location paramLocation, CreatureSpawnEvent.SpawnReason paramSpawnReason, boolean paramBoolean);
    
        void b();
    }
     
  2. Offline

    KarimAKL

    @vicen621
    1. You shouldn't obfuscate your own source, a program should do that for you, and not on the source.
    2. I don't think the stacktrace and the source code are of the same version, as the line seems like it shouldn't cause a NullPointerException.
     
  3. Offline

    Strahan

    Agreed. I'd also add that this is pretty inefficient. Any time you find yourself essentially copy/pasting code and only changing a few pieces, it's a sign you need to stop and reevaluate your approach. In this case, a single method to generate the entity would be far better, then you just pass which type you need to the method. Also ideally you'd pull the type specific stuff from config, not hard coding. Hard coding should be avoided wherever possible. It's much better to use a config, so when you decide one day that you either want to tweak the loadout of one type or want to add/remove a type, you don't have to fire up your IDE and compile a new version.
     
  4. Offline

    vicen621

    The problem is that I can't spur a skeleton because the event detects you, is there any way you can spwn a skeleton with custom spawnreason, or that can't be detected by the event?

    What do you mean with obfuscate your own source? And how can I update the StackTrace?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 19, 2020
  5. Offline

    Strahan

    I'm not sure what you mean with the event. The idea here is that when a skel spawns, you replace it with a random loadout right? If I were doing it, I'd do something akin to:
    Code:
    package com.sylvcraft;
    
    import java.util.concurrent.ThreadLocalRandom;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Skeletons implements Listener {
      private Test16 plugin;
    
      public Skeletons(Test16 main) {
        this.plugin = main;
      }
    
      @EventHandler
      private void b(CreatureSpawnEvent e) {
        if (this.plugin.getDays() < 10L) return;
        if (e.getSpawnReason() == SpawnReason.CUSTOM) return;
        if (e.getEntity().getType() != EntityType.STRAY && e.getEntity().getType() != EntityType.SKELETON) return;
    
        spawnSkeleton(e.getEntity(), e.getLocation(), ThreadLocalRandom.current().nextInt(4));
        e.getEntity().remove();
      }
    
      private void spawnSkeleton(Entity skeleton, Location loc, int skelType) {
        String customName = plugin.getConfig().getString("skeletons." + skelType + ".customName");
    
        LivingEntity skelEntity = (LivingEntity) loc.getWorld().spawnEntity(loc, EntityType.SKELETON);
        if (customName != null) {
          skelEntity.setCustomName(d.chat(customName));
          skelEntity.setCustomNameVisible(true);
        }
        ItemStack helmet = getItem("helmet", skelType);
        if (helmet != null) {
          skelEntity.getEquipment().setHelmet(helmet);
          skelEntity.getEquipment().setHelmetDropChance(0.0F);
        }
    
        ItemStack chestPlate = getItem("chestplate", skelType);
        if (chestPlate != null) {
          skelEntity.getEquipment().setChestplate(chestPlate);
          skelEntity.getEquipment().setChestplateDropChance(0.0F);
        }
    
        ItemStack leggings = getItem("leggings", skelType);
        if (leggings != null) {
          skelEntity.getEquipment().setLeggings(leggings);
          skelEntity.getEquipment().setLeggingsDropChance(0.0F);
        }
    
        ItemStack boots = getItem("boots", skelType);
        if (boots != null) {
          skelEntity.getEquipment().setBoots(boots);
          skelEntity.getEquipment().setBootsDropChance(0.0F);
        }
    
        ItemStack weapon = getItem("weapon", skelType);
        if (weapon != null) {
          ItemMeta weaponMeta = weapon.getItemMeta();
          weaponMeta.addEnchant(Enchantment.ARROW_DAMAGE, 3, true);
          weapon.setItemMeta(weaponMeta);
          skelEntity.getEquipment().setItemInMainHand(weapon);
          skelEntity.getEquipment().setItemInMainHandDropChance(0.0F);
          skelEntity.getEquipment().setItemInOffHandDropChance(0.0F);
        }
      }
      private ItemStack getItem(String itemType, int skelType) {
        String materialName = plugin.getConfig().getString("skeletons." + skelType + ".items." + itemType);
        if (materialName == null) return null;
    
        try {
          Material itemMaterial = Material.matchMaterial(materialName);
          if (itemMaterial == null) return null;
      
          return new ItemStack(itemMaterial);
        } catch (IllegalArgumentException ex) {
          return null;
        }
      }
    }
    As to obfuscation, he means the stuff you do with methods named as single letters; that's how Minecraft obfuscates. But that isn't necessary. An obfuscation program will do that for you. Now if you were doing it just to save yourself time, disregard. Though if you are interested in pursuing programming as a career, break that habit now because if you try that in a production codebase, your future boss will castrate you lol
     
  6. Offline

    vicen621

    The Config look like this?
    Code:
    skeletons:
      0:
        customName: '&6Test'
        helmet: 'DIAMOND_HELMET'
        chestplate: 'DIAMOND_CHESTPLATE'
        leggings: 'DIAMOND_LEGGINGS'
        boots: 'DIAMOND_BOOTS'
        weapon: 'BOW'
    
    And if I want to make a file only for the skeletons because the Config.yml is alredy ocuped with other stuff, how can I make it? I also want to put different enchantments to skeleton weapons, can this be in the configuration too?
     
    Last edited: Sep 19, 2020
Thread Status:
Not open for further replies.

Share This Page