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 @EventHandler public void BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent e){ Player player = e.getPlayer(); int num = getConfig().getInt("Count"); if(player.getWorld().getName().equals(getConfig().getString("World" + num))){ if(getConfig().getString("Prevent breaking").equals("true")){ if(player.hasPermission("BuildVsBreak.break.bypass")){ }else{ e.setCancelled(true); player.sendMessage(ChatColor.DARK_RED + "You can't break blocks in this world."); } } } } 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?
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?
, 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
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.
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.
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 }
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 List<String> worldNames = new ArrayList<String>();worldNames.add("world");worldNames.add("world_nether");// etc.plugin.getConfig().set("worlds", worldNames);plugin.saveConfig(); And retrieve the world names with: Code:java List<String> worldNames = plugin.getConfig().getStringList("worlds");if (worldNames.contains(player.getWorld().getName()) // Prevent the player from building/breaking as applicable
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().