1.15.2 disabled-worlds settings

Discussion in 'Spigot Plugin Development' started by xstronggg, Jun 11, 2021.

  1. hi everyone,
    How do I add the disabled-worlds property?

    disabled-worlds:
    - 'world_nether'
    - 'world_the_end'


    Code (Java):
          @EventHandler
          public void onSpawn(CreatureSpawnEvent e) {
            String spawner = getConfig().getString("Spawners");
            String spawnegg = getConfig().getString("SpawnEggs");
            if (spawner == "true")
              if (spawnegg == "true") {
                if (e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.SPAWNER && e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.SPAWNER_EGG)
                  e.setCancelled(true);
              } else if (e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.SPAWNER) {
                e.setCancelled(true);
              }
          }
     
  2. This should work.

    Code (Java):
    if (getConfig().getStringList("disabled-worlds").stream().anyMatch(e.getEntity().getLocation().getWorld().getName()))
    {
        // The event happened in disabled world.
        // e.setCancelled(true);
    }
     
  3. thank you for replying but it gives an error
     

    Attached Files:

  4. Code (Java):
    if (getConfig().getStringList("disabled-worlds").stream().anyMatch(w -> e.getEntity().getLocation().getWorld().getName().equals(w)))
    {
        // The event happened in disabled world.
        // e.setCancelled(true);
    }
     
  5. thanks worked :)
     
  6. If you are interested in how it works (which you should), then lookup Java Lambda Expressions and Java Streams
     
  7. "== true"
    *stabs my own eyes*
     
    • Funny Funny x 1
  8. okay I will check it
     
  9. why? It's technically the same as doing if(stuff), just a bit more verbose

    EDIT: Sorry, just saw that it's a String comparison - that's indeed not very good lol
     
  10. Even a bool check with == true is "bad", it does the same yeah but it just doesn’t look right and you take more time to write it which is more inefficient.

    Also I just noticed it’s a string comparaison too I didn’t even see it at first lol
     
  11. I think the point is the quotation marks in op's original code comparing a string using == " ", op will run into issues in the future when comparing strings using ==, the code will still return false even if comparing to "true".

    OP, you should be using
    Code (Java):
    boolean spawner = getConfig().getBoolean("Spawners");
    boolean spawnegg = getConfig().getBoolean("SpawnEggs");
    Your current code is not working as you intend, and those blocks will never be reached. If you want to compare a String to a String you need to use the String#equals() method.

    Also your if statements are nested, so both will have to be true for anything to ever get cancelled.
     
    #11 v_M_v, Jun 12, 2021
    Last edited: Jun 12, 2021
    • Like Like x 2
  12. I'm new to Java, what exactly is the problem?
    what do i need to change?
     
  13. I gave you a small code snippet to give you a head start, try for yourself and come back when you run into the issues I also described. I won't tell you exactly what you need to change because then you won't learn.
     
  14. My only problem right now is that when I set the Spawners value to false it applies to all worlds, what I want is this; The spawners will be true in the world names I type in the disabled-worlds setting in the config.
     
  15. all codes are here

    Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class Main extends JavaPlugin implements Listener {

        @Override
        public void onEnable() {
            System.out.println(ChatColor.LIGHT_PURPLE + "-------------------------");
            System.out.println(ChatColor.LIGHT_PURPLE + "-------------------------");
            System.out.println(ChatColor.YELLOW + "blxSpawners worked!");
            System.out.println(ChatColor.LIGHT_PURPLE + "-------------------------");
            System.out.println(ChatColor.LIGHT_PURPLE+ "-------------------------");
           
            this.getConfig().options().copyDefaults();
            saveDefaultConfig();
           
            Bukkit.getPluginManager().registerEvents(this, this);

        }
       
        @Override
        public void onDisable() {
           
            System.out.println(ChatColor.LIGHT_PURPLE + "-------------------------");
            System.out.println(ChatColor.LIGHT_PURPLE + "-------------------------");
            System.out.println(ChatColor.RED + "blxSpawners does not worked!");
            System.out.println(ChatColor.LIGHT_PURPLE + "-------------------------");
            System.out.println(ChatColor.LIGHT_PURPLE + "-------------------------");
           
        }
       
          @EventHandler
          public void onSpawn(CreatureSpawnEvent e) {
             
                String spawner = getConfig().getString("Spawners");
                String spawnegg = getConfig().getString("SpawnEggs");
             
                if (spawner == "true")
                  if (spawnegg == "true") {
                      if (getConfig().getStringList("disabled-worlds").stream().anyMatch(w -> e.getEntity().getLocation().getWorld().getName().equals(w)))
                      {
                            if (e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.SPAWNER && e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.SPAWNER_EGG)
                                  e.setCancelled(true);
                              } else if (e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.SPAWNER) {
                                e.setCancelled(true);
                              }
                      }
               
    }
         
          public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

              String Spawners = this.getConfig().getString("Spawners");
              String SpawnEggs = this.getConfig().getString("SpawnEggs");
             
              Player player = (Player) sender;
             
              if (cmd.getName().equals("blxspawners")) {
                  if (player.hasPermission("blx.info")) {
                      reloadConfig();
                      player.sendMessage(ChatColor.GRAY + "--------------" + ChatColor.DARK_RED + " blxSpawners " + ChatColor.GRAY + "--------------");
                      player.sendMessage(ChatColor.AQUA + " Spawners: " + ChatColor.GREEN + Spawners);
                      player.sendMessage(ChatColor.AQUA + " SpawnEggs: " + ChatColor.GREEN + SpawnEggs);
                     
                  } else {
                      player.sendMessage(ChatColor.GOLD + "You are not authorized to do this!");
                  }
                 
              }

              return false;
          }
           
        }
     
  16. default config

    Code (YAML):
    #If true mobs will only spawn from Spawners
    #And if false mobs will spawn naturally
    Spawners
    : true

    #If true mobs will be able to spawn from Spawn Eggs
    SpawnEggs
    : true

    #World Settings
    disabled-worlds
    :
    - 'world_nether'
    - 'world_the_end'  
     
  17. You didn’t change your code… take the code that this guy send you and use it. You can’t compare strings with == in java.
     
  18. i will trying now
     
  19.  

    Attached Files:

  20. Remove the == "true"
    Like this:
    if (spawner) {