1.14.4 Unsafe Enchantments Break BlockBreakEvent

Discussion in 'Spigot Plugin Development' started by DarkKnights22, Jan 13, 2020.

  1. Code (Java):
     @EventHandler(priority = EventPriority.LOWEST)
        public void onBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();

            for (int i = 0; i < PlatinumGenerator.getInstance().data.getGeneratorsAmount(p); i++) {
                if (PlatinumGenerator.getInstance().playerJoinListener.fileConfiguration.get(p.getUniqueId()).getString("generators." + i + ".location.world") == null) break;
                int generatorID = PlatinumGenerator.getInstance().playerJoinListener.fileConfiguration.get(p.getUniqueId()).getInt("generators." + i + ".generator");

                if (e.getBlock().getType() == Material.getMaterial(PlatinumGenerator.getInstance().configFile.getConfig().getString("generators." + generatorID + ".item.material"))) {
                    Location generatorLoc = new Location(Bukkit.getWorld(PlatinumGenerator.getInstance().playerJoinListener.fileConfiguration.get(p.getUniqueId()).getString("generators." + i + ".location.world")),
                            PlatinumGenerator.getInstance().playerJoinListener.fileConfiguration.get(p.getUniqueId()).getInt("generators." + i + ".location.X"),
                            PlatinumGenerator.getInstance().playerJoinListener.fileConfiguration.get(p.getUniqueId()).getInt("generators." + i + ".location.Y"),
                            PlatinumGenerator.getInstance().playerJoinListener.fileConfiguration.get(p.getUniqueId()).getInt("generators." + i + ".location.Z"));

                    if (e.getBlock().getLocation().distance(generatorLoc) <= 1) {
                        if (!p.isSneaking()) {
                            e.setCancelled(true);
                            p.sendMessage(Chat.color("Generator-Destroy-Warning", true));
                            continue;
                        }
                        if (PlatinumGenerator.getInstance().breakCooldown.contains(p.getUniqueId())) {
                            e.setCancelled(true);
                            p.sendMessage(Chat.color("Generator-Break-Cooldown", true));
                            continue;
                        }
                        //> Break Generator
                        //> Give player Generator-Item
                        PlatinumGenerator.getInstance().playerJoinListener.fileConfiguration.get(p.getUniqueId()).set("generators." + i, null);
                        try {
                            PlatinumGenerator.getInstance().playerJoinListener.fileConfiguration.get(p.getUniqueId()).save(PlatinumGenerator.getInstance().playerJoinListener.file.get(p.getUniqueId()));
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                        boolean generatorExists = false;
                        for (int i2 = 0; i2 < PlatinumGenerator.getInstance().data.getGeneratorsAmount(p); i2++)
                            if (PlatinumGenerator.getInstance().playerJoinListener.fileConfiguration.get(p.getUniqueId()).getString("generators." + i2 + ".location.world") != null)
                                generatorExists = true;

                        if (!generatorExists)
                            PlatinumGenerator.getInstance().playerJoinListener.file.get(p.getUniqueId()).delete();

                        PlatinumGenerator.getInstance().schedulerManager.reloadSchedulers(p);
                        PlatinumGenerator.getInstance().breakCooldown.add(p.getUniqueId());
                        PlatinumGenerator.getInstance().addToBreakCooldown(p);

                        e.getBlock().setType(XMaterial.AIR.parseMaterial());

                        String itemTitle = Chat.colorRaw(PlatinumGenerator.getInstance().configFile.getConfig().getString("generators." + generatorID + ".item.name"));
                        Material itemMaterial = XMaterial.valueOf(PlatinumGenerator.getInstance().configFile.getConfig().getString("generators." + generatorID + ".item.material")).parseMaterial();
                        boolean itemGlow = PlatinumGenerator.getInstance().configFile.getConfig().getBoolean("generators." + generatorID + ".item.glow");

                        int blockAmount = PlatinumGenerator.getInstance().configFile.getConfig().getInt("generators." + generatorID + ".block-amount");
                        int generateSpeed = PlatinumGenerator.getInstance().configFile.getConfig().getInt("generators." + generatorID + ".generate-time");

                        List<String> itemLore = new ArrayList<>();
                        for (String key : PlatinumGenerator.getInstance().configFile.getConfig().getStringList("generators." + generatorID + ".item.lore")) {
                            String lore = Chat.colorRaw(key)
                                    .replaceAll("%block_amount%", "" + blockAmount)
                                    .replaceAll("%generate_speed%", "" + generateSpeed);
                            itemLore.add(lore);
                        }

                        ItemStack item = PlatinumGenerator.getInstance().itemCreator.create(itemTitle, itemLore, itemMaterial, itemGlow, 1);

                        e.getBlock().getLocation().getWorld().dropItem(e.getBlock().getLocation(), item);

                        p.sendMessage(Chat.color("Generator-Broken", true));
                    }
                }
            }
        }
    I know this code is horrible, but it's temporary. It works perfectly fine on all Spigot versions except when unsafe enchantments are used to break a generator. Why idea why?
     
  2. The BlockBreakEvent doesn't change at all if you use unsafe enchantments. "Unsafe enchantments" is just a definition saying the enchantment goes over the normal limit, it's really nothing special. There's nothing in your code that even uses the player's item in hand or checks any sort of enchantments. Are you sure it's not something else? I'd probably step through it with breakpoints from the top and see if it's not something else causing an issue.
     
  3. You are right. Have you tried debugging your code when using unsafeenchantments.
     
  4. Hm ok so it seems the problem is present without unsafe enchantments aswell.
    Code (Java):
    File playerdataFile = new File("plugins/PlatinumGenerator/playerdata/" + p.getUniqueId().toString() + ".yml");
            FileConfiguration playerdataFileConfiguration = YamlConfiguration.loadConfiguration(playerdataFile);
            fileConfiguration.put(p.getUniqueId(), playerdataFileConfiguration);
            file.put(p.getUniqueId(), playerdataFile);
    This is what I put into the files maps. I use this throughout the plugin to access the player's data files. The files are loaded when they join. I am able to break generators and get a normal block instead of the generator, but sometimes I get the generator. Any idea as to why that is? I think the data isn't saving correctly.
     
  5. Again have you tried to debug your code and if so what did you find
     
  6. I have, and I didn't find anything.