Solved Syntax Help

Discussion in 'Spigot Plugin Development' started by LoxleyShadow, Oct 14, 2018.

Thread Status:
Not open for further replies.
  1. Note: Line 33 is my problem.

    Code (Java):
    package loxleyshadow.plugin.event.block;

    import java.util.List;

    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;

    import loxleyshadow.plugin.Plugin;
    import net.md_5.bungee.api.ChatColor;

    public class BlockBreak implements Listener{
     
        private Plugin plugin;
     
        public void Plugin(Plugin pl) {
            plugin = pl;
        }
     
        List<String> blacklistedBlocks = plugin.getConfig().getStringList("Breakable Blocks");
     
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
         
            Block block = event.getBlock();
            Material material = block.getType();
         
            for(String thing : blacklistedBlocks) {
                if(material != Material.thing) {
                    event.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You cannot break this block!");
                }
            }
        }
    }
     

    I am unfamiliar with Java so can anyone give me some pointers on how to get this configuration file integrated with my plugin?
     
  2. Do away with the for loop. Since the string array blacklistedBlocks presumably contains a list of the blocks which you do not want people to break, simply check if blacklistedBlocks.contains(material.toString()), then cancel the event if it does.
     
    • Useful Useful x 1
  3. Thanks. I will try that right now
     
  4. Code (Java):
            if(blacklistedBlocks.contains(material.toString())) {
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + "You cannot break this block!");
            }
    Like this?
     
  5. It's not that difficult take a look at my sample code.
    Code (Java):
            Material material = Material.DIAMOND_BLOCK; //Broken block
            YamlConfiguration configuration = new YamlConfiguration();
            //configuration.load(file); Load your config here
            List<String> rawBlockedItems = (List<String>) configuration.getList("blockedItems");
            List<Material> blockedItems = rawBlockedItems.stream().map(Material::getMaterial).collect(Collectors.toList()); //Converting list elements to materials
            if(blockedItems.contains(material)){
                //Logic for blocked Items
            }
    Your config would look something like this:

    Code (YAML):
    blockedItems:
     - DIAMOND_BLOCK
     - more blocks
     
    • Useful Useful x 1
  6. You should load the config once like you did in your example code. The reason why your code won't compile is because you can't get an enum value with a variable directly.

    Material.variable -> will not work
    Material.valueOf(variable) -> works for all enums
    Material.getMaterial(variable) -> works for materials, recommended method
     
  7. Thank you. I will test it out in my test spigot server.
     
  8. Also, you config file is somewhat confused...
    You are getting 'Breakable Blocks' from the config file, to populate a list of 'Unbreakable Blocks'.
    This seems like a recipe for disaster...
     
  9. Code (Java):
    package loxleyshadow.plugin.event.block;

    import java.util.List;

    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;

    import loxleyshadow.plugin.Plugin;
    import net.md_5.bungee.api.ChatColor;

    public class BlockBreak implements Listener{
       
        private Plugin plugin;
       
        public void Plugin(Plugin pl) {
            plugin = pl;
        }
       
        List<String> blacklistedBlocks = plugin.getConfig().getStringList("Breakable Blocks");
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
           
            Block block = event.getBlock();
            Material material = block.getType();
           
            if(blacklistedBlocks.contains(material.toString())) {
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + "You cannot break this block!");
            }
        }
    }
     

    Will this code work?
     
  10. It worked when there was no config. I just blacklisted Material.GRASS. Now I want to blacklist all the stuff in the config where it is in the format of a list.
     
  11. Tried the above and got this:
    Code (Java):
    [12:21:47 ERROR]: Error occurred while enabling Blacklist v1.12.2 (Is it up to date?)
    java.lang.NullPointerException: null
        at loxleyshadow.plugin.event.block.BlockBreak.<init>(BlockBreak.java:23) ~[?:?]
        at loxleyshadow.plugin.Plugin.registerEvents(Plugin.java:43) ~[?:?]
        at loxleyshadow.plugin.Plugin.onEnable(Plugin.java:22) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot.jar:git-Spigot-dcd1643-e60fc34]
        at java.lang.Thread.run(Thread.java:844) [?:?]
     
  12. What does your config look like?
     
  13. No, because you can't use spaces in your config keys.
    Breakable Blocks -> BreakableBlocks

    The only thing I can imagine is that your path for the config is wrong.
     
    #13 iFighter, Oct 14, 2018
    Last edited: Oct 14, 2018
  14. You sure can.
     
  15. Ty, you are right. Fixed my post.
     
  16. Code (Java):

        private Plugin plugin;
     
        public void Plugin(Plugin pl) {
            plugin = pl;
        }
     
    Did you mean to use a dependency injection for the BlockBreak class? If so, the constructor should be named after your class and your plugin variable should be final.
     
  17. Code (YAML):
    blockedItems:  
        - GRASS      
        - STONE  
     
  18. Code (Java):

    plugin.getConfig().getStringList("Breakable Blocks");
     
    This is obvious isn't it?
    Code (Text):
    blockedItems:  
        - GRASS      
        - STONE
    The path "Breakable Blocks" doesn't exist and therefore #getStringList returns null. A valid path is "blockedItems"
     
    • Agree Agree x 1
  19. Remove: List<String> blacklistedBlocks = plugin.getConfig().getStringList("Breakable Blocks");

    This:
    Code (Java):
    List<String> rawBlockedItems = (List<String>) configuration.getList("blockedItems");
    Should be:
    Code (Java):

    final List<String> rawBlockedItems = configuration.getStringList("blockedItems");
     
     
    • Useful Useful x 1
  20. Screen Shot 2018-10-14 at 1.46.34 PM.png



    This is my code that still throws the same error
     
Thread Status:
Not open for further replies.

Share This Page