MultiWorld?

Discussion in 'Plugin Development' started by adde, Jul 9, 2013.

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

    adde

    Hello.
    I am coding a plugin that works when I am using one world in the config file, but whenever I try to add a second world it will just use the second world.
    I tried with + 1 and such, changed from String to int to boolean but none seem to work, here's my code:
    Code:java
    1.  
    2. @EventHandler
    3. public void BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent e){
    4. Player player = e.getPlayer();
    5.  
    6. int num = getConfig().getInt("Count");
    7. if(player.getWorld().getName().equals(getConfig().getString("World" + num))){
    8. if(getConfig().getString("Prevent breaking").equals("true")){
    9. if(player.hasPermission("BuildVsBreak.break.bypass")){
    10.  
    11. }else{
    12. e.setCancelled(true);
    13. player.sendMessage(ChatColor.DARK_RED + "You can't break blocks in this world.");
    14. }
    15. }
    16. }
    17. }
    18.  


    I know that when I change the number in the config file on "Count" from 1 to 2 it will only use World2: blabla.
    So my question is, how do I make it use all worlds?
     
  2. Offline

    LinearLogic

    Are you trying to check if block break prevention is enabled for the player's current world? If so you'll need one boolean per world, rather than a global one... What's in your config.yml?
     
  3. Offline

    adde

    , I just want like if "Count" is set to 2, you would be able to set
    World1: world
    World2: world2
    It should set event to false in both worlds, but it won't work
     
  4. Offline

    LinearLogic

    You'd be able to set it where? Just describe the goal of the plugin as a whole and I'll be better able to help.
     
  5. Offline

    adde

    I guess you should be able to understand by the code I posted but I will try to explain as good as I can.

    I have two events, one BlockBreakEvent and one BlockPlaceEvent. If the Prevent Building: is set to true in the config, it will prevent building in the world(s) that is in the config, but whenever I try to add more worlds by putting my "Count" string to like 2, it will just use World2 instead of both World1 and World2.
    Here's my config, you should be able to tell how it works now.
    Code:
    Prevent building: true
    Prevent breaking: false
    Count: 1
    World1: world
    
    Whenever I put "Count" to 2, and add World2: it will use ONLY World2 string, not both.

    Bump? :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    Chinwe

    You can tidy up your code a bit:
    - Instead of using
    Code:
    if (getConfig().getString("Prevent breaking").equals("true"))
    you can simply do
    Code:
    if (getConfig().getBoolean("preventBreaking")
    as it is a boolean. And I don't believe you can use spaces? (Im not sure though :'()

    - Instead of
    Code:
     if(player.hasPermission("BuildVsBreak.break.bypass")){
     
                }else{
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.DARK_RED + "You can't break blocks in this world.");
                }
    you can use
    Code:
    if(!player.hasPermission("BuildVsBreak.break.bypass"))
    {
      e.setCancelled(true);
      player.sendMessage(ChatColor.DARK_RED + "You can't break blocks in this world.");
    }
    And if you want to prevent building/placing in several worlds, use a list instead, and use
    Code:
    if (getConfig().getStringList("disabledWorlds").contains(player.getWorld().getName())
    {
        // do yo' cancelling
    }
     
    adde likes this.
  7. Offline

    LinearLogic

    I'd simply use a config node called "worlds" that has a string list containing names of worlds to which the build/break prevention applies. Then you can edit the node with:
    Code:java
    1. List<String> worldNames = new ArrayList<String>();
    2. worldNames.add("world");
    3. worldNames.add("world_nether");
    4. // etc.
    5. plugin.getConfig().set("worlds", worldNames);
    6. plugin.saveConfig();

    And retrieve the world names with:
    Code:java
    1. List<String> worldNames = plugin.getConfig().getStringList("worlds");
    2. if (worldNames.contains(player.getWorld().getName())
    3. // Prevent the player from building/breaking as applicable
    4.  
     
    adde likes this.
  8. Offline

    adde

    So the first code should be at onEnable?
     
  9. Offline

    LinearLogic

    It's probably not a good idea to include the first bit at all; just edit the config yourself so your config isn't being reset each time the plugin is reloaded. as for the List<String> worldNames, make that a field in your main class and initialize it in onEnable().
     
Thread Status:
Not open for further replies.

Share This Page