eventhandler placeblock

Discussion in 'Spigot Plugin Development' started by UnlimitedNinjas, Jun 10, 2018.

  1. I want to make it so theres a list in the config of block id and when a player places a block if that block is in the list it will block it.
    heres what im working with:
    Code (Text):
        @SuppressWarnings({ "deprecation", "unlikely-arg-type" })
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            if (getConfig().getStringList("restrictedblocks").contains(e.getBlock().getType().getId())) {
                    e.setCancelled(true);
                    e.getPlayer().kickPlayer(ChatColor.DARK_RED + "That block is not allowed on the server!");
                    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                        if (p.hasPermission("sp.notify")) {
                            p.sendMessage(ChatColor.RED + e.getPlayer().getName() + " tried to place " + e.getBlock().getType().getData());
                        }
                    }
                }
            }
        }
     
  2. First off I would store your blocks in memory instead of reading from config every time this event is called (Optimization)

    Second, I would also use e.getBlock().getType(). This would let you use the name of the block (from https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html) instead of a 'magic' deprecated number.

    Also make sure this listener is registered
     
  3. do not use the id system it is deprecated.
    use block types
     
  4. I thought the plugin config is stored in memory?
     
  5. Optic_Fusion1

    Resource Staff

    knowing md_5 and his love for backwards compatibility the id system probably won't go anywhere though :p
     
  6. yeah but spigot 1.13 is coming and I heard item id will be removed when you compile on 1.13 API but if you compile on 1.12 down item id is still usable
     
  7. Now that you say that I believe it is. (At least would make sense).

    As for cleaner code, it would be better to load the list from config into an array called 'restrictedblocks' instead of getConfig().getStringList("restrictedblocks")
     
  8. Optic_Fusion1

    Resource Staff

    *gasp* something md_5 can't make backwards compatible in some way, shape or form :O
     
  9. Can't find in documentation or forums if the config is stored in memory. So I'd still load the list onEnable() to be safe. At worst the performance is equivalent if not better if the config isn't stored in memory.
     
  10. yeah no wrappers and stuff but be sure to clean the array when the plugin reload as to avoid memory leaks
    its not that he cant haha but its for the better
     
  11. well a proof of this is when you modify the values of the config then shut down the server the config.yml stays the same so yeah plugin config is loaded into memory

    until you do saveConfig()
     
  12. I still dont see how any of this would fix it?
     
  13. Well state your problem as we can see your code seems fine what the others commented is how to improve your code.
    Question What is the problem bothering you
     
  14. It doesnt work, i need to check the block if its in the config and if it is cancel the event.
     
  15. how does it not work does it output an error in the console? does the plugin not load?
     
  16. Nothing shows up..
     
  17. You can all argue about whether to use item IDs or not but ignore the obvious things lmao

    @OP, don't ignore warnings from your IDE, they are there to help you. You're checking if a list of STRINGS contains a BYTE. See the problem? To fix this, wrapping the byte with String.valueOf should work (although not sure about the implementation of List#contains from config, it likely calls Object#equals)

    Hope this helps you out

    Sent from my SM-G903F using Tapatalk
     
  18. this is what i was also thinking and was testing it now in eclipse
     
  19. i dont really know how to do this..
     
  20. Also if a method is deprecated means theres just a better way to do it, and might be removed in next update?
    just like setBanned(true), which i dont know how to do now.