Fix global protection persistence; resolves #471

This commit is contained in:
Daniel Saukel 2018-10-12 20:27:12 +02:00
parent 80e9888174
commit 684b6df0f7
6 changed files with 72 additions and 122 deletions

View File

@ -203,7 +203,6 @@ public class DungeonsXL extends DREPlugin {
public void loadConfig() {
messageConfig = new MessageConfig(DMessage.class, new File(LANGUAGES, "english.yml"));
globalData = new GlobalData(this, new File(getDataFolder(), "data.yml"));
mainConfig = new MainConfig(this, new File(getDataFolder(), "config.yml"));
messageConfig = new MessageConfig(DMessage.class, new File(LANGUAGES, mainConfig.getLanguage() + ".yml"));
}
@ -234,6 +233,8 @@ public class DungeonsXL extends DREPlugin {
dWorlds.init(MAPS);
dungeons.init(DUNGEONS);
Bukkit.getPluginManager().registerEvents(new GlobalProtectionListener(this), this);
globalData = new GlobalData(this, new File(getDataFolder(), "data.yml"));
globalData.load();
dMobProviders.init();
dPlayers.init();
initAnnouncerCache(ANNOUNCERS);
@ -250,7 +251,6 @@ public class DungeonsXL extends DREPlugin {
}
public void loadData() {
protections.loadAll();
dPlayers.loadAll();
dWorlds.check();
}

View File

@ -35,6 +35,7 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
@ -73,6 +74,21 @@ public class DPortal extends GlobalProtection {
this.active = active;
}
public DPortal(DungeonsXL plugin, World world, int id, ConfigurationSection config) {
super(plugin, world, id);
block1 = world.getBlockAt(config.getInt("loc1.x"), config.getInt("loc1.y"), config.getInt("loc1.z"));
block2 = world.getBlockAt(config.getInt("loc2.x"), config.getInt("loc2.y"), config.getInt("loc2.z"));
material = plugin.getCaliburn().getExItem(config.getString("material"));
if (material == null) {
material = VanillaItem.NETHER_PORTAL;
}
String axis = config.getString("axis");
this.axis = (byte) (axis != null && axis.equalsIgnoreCase("z") ? 2 : 1);
active = true;
create(null);
}
/**
* @return the block1
*/

View File

@ -17,8 +17,11 @@
package de.erethon.dungeonsxl.global;
import de.erethon.commons.config.DREConfig;
import de.erethon.commons.misc.NumberUtil;
import de.erethon.dungeonsxl.DungeonsXL;
import java.io.File;
import java.util.Map.Entry;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
@ -42,7 +45,6 @@ public class GlobalData extends DREConfig {
if (initialize) {
initialize();
}
load();
}
@Override
@ -55,21 +57,27 @@ public class GlobalData extends DREConfig {
for (World world : Bukkit.getWorlds()) {
ConfigurationSection gameSigns = config.getConfigurationSection("protections.gameSigns." + world.getName());
ConfigurationSection groupSigns = config.getConfigurationSection("protections.groupSigns." + world.getName());
if (gameSigns == null && groupSigns == null) {
continue;
}
int i = 0;
while (true) {
String key = String.valueOf("i");
if (gameSigns != null && gameSigns.contains(key)) {
new GameSign(plugin, world, i, gameSigns.getConfigurationSection(key));
ConfigurationSection leaveSigns = config.getConfigurationSection("protections.leaveSigns." + world.getName());
ConfigurationSection portals = config.getConfigurationSection("protections.portals." + world.getName());
Random random = new Random();
if (gameSigns != null) {
for (Entry<String, Object> entry : gameSigns.getValues(false).entrySet()) {
new GameSign(plugin, world, NumberUtil.parseInt(entry.getKey(), random.nextInt()), gameSigns.getConfigurationSection(entry.getKey()));
}
if (groupSigns != null && groupSigns.contains(key)) {
new GroupSign(plugin, world, i, groupSigns.getConfigurationSection(key));
} else if ((gameSigns == null || !gameSigns.contains(key)) && (groupSigns == null || !groupSigns.contains(key))) {
break;
}
if (groupSigns != null) {
for (Entry<String, Object> entry : groupSigns.getValues(false).entrySet()) {
new GroupSign(plugin, world, NumberUtil.parseInt(entry.getKey(), random.nextInt()), groupSigns.getConfigurationSection(entry.getKey()));
}
}
if (leaveSigns != null) {
for (Entry<String, Object> entry : leaveSigns.getValues(false).entrySet()) {
new LeaveSign(plugin, world, NumberUtil.parseInt(entry.getKey(), random.nextInt()), leaveSigns.getConfigurationSection(entry.getKey()));
}
}
if (portals != null) {
for (Entry<String, Object> entry : portals.getValues(false).entrySet()) {
new DPortal(plugin, world, NumberUtil.parseInt(entry.getKey(), random.nextInt()), portals.getConfigurationSection(entry.getKey()));
}
}
}

View File

@ -16,18 +16,14 @@
*/
package de.erethon.dungeonsxl.global;
import de.erethon.caliburn.item.ExItem;
import de.erethon.caliburn.item.VanillaItem;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.player.DGroup;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@ -168,92 +164,4 @@ public class GlobalProtectionCache {
}
}
/* SUBJECT TO CHANGE */
@Deprecated
public void loadAll() {
FileConfiguration data = plugin.getGlobalData().getConfig();
for (World world : Bukkit.getWorlds()) {
// GameSigns
if (data.contains("protections.gameSigns." + world.getName())) {
int id = 0;
String preString;
do {
id++;
preString = "protections.gameSigns." + world.getName() + "." + id + ".";
if (data.contains(preString)) {
String mapName = data.getString(preString + ".dungeon");
int maxGroupsPerGame = data.getInt(preString + ".maxGroupsPerGame");
int startIfElementsAtLeast = data.getInt(preString + ".startIfElementsAtLeast");
Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z"));
new GameSign(plugin, id, startSign, mapName, maxGroupsPerGame, startIfElementsAtLeast);
}
} while (data.contains(preString));
}
// GroupSigns
if (data.contains("protections.groupSigns." + world.getName())) {
int id = 0;
String preString;
do {
id++;
preString = "protections.groupSigns." + world.getName() + "." + id + ".";
if (data.contains(preString)) {
String mapName = data.getString(preString + ".dungeon");
String groupName = data.getString(preString + ".groupName");
int maxPlayersPerGroup = data.getInt(preString + ".maxPlayersPerGroup");
int startIfElementsAtLeast = data.getInt(preString + ".startIfElementsAtLeast");
Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z"));
new GroupSign(plugin, id, startSign, mapName, maxPlayersPerGroup, startIfElementsAtLeast, groupName);
}
} while (data.contains(preString));
}
if (data.contains("protections.leaveSigns." + world.getName())) {
int id = 0;
String preString;
do {
id++;
preString = "protections.leaveSigns." + world.getName() + "." + id + ".";
if (data.contains(preString)) {
Block block = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z"));
if (block.getState() instanceof Sign) {
Sign sign = (Sign) block.getState();
new LeaveSign(plugin, id, sign);
}
}
} while (data.contains(preString));
}
// DPortals
if (data.contains("protections.portals." + world.getName())) {
int id = 0;
String preString;
do {
id++;
preString = "protections.portals." + world.getName() + "." + id + ".";
if (data.contains(preString)) {
Block block1 = world.getBlockAt(data.getInt(preString + "loc1.x"), data.getInt(preString + "loc1.y"), data.getInt(preString + "loc1.z"));
Block block2 = world.getBlockAt(data.getInt(preString + "loc2.x"), data.getInt(preString + "loc2.y"), data.getInt(preString + "loc2.z"));
ExItem material = plugin.getCaliburn().getExItem(data.getString(preString + "material"));
String axis = data.getString(preString + "axis");
DPortal dPortal = new DPortal(plugin, id, block1, block2, material != null ? material : VanillaItem.NETHER_PORTAL, (byte) (axis != null && axis.equals("z") ? 2 : 1), true);
dPortal.create(null);
}
} while (data.contains(preString));
}
}
}
}

View File

@ -76,8 +76,6 @@ public class JoinSign extends GlobalProtection {
}
}
verticalSigns = (int) Math.ceil((float) (1 + maxElements) / 4);
// LEGACY
if (config.contains("maxElements")) {
maxElements = config.getInt("maxElements");
@ -87,6 +85,8 @@ public class JoinSign extends GlobalProtection {
maxElements = config.getInt("maxPlayersPerGroup");
}
verticalSigns = (int) Math.ceil((float) (1 + maxElements) / 4);
if (startIfElementsAtLeast > 0 && startIfElementsAtLeast <= maxElements) {
startIfElementsAtLeast = config.getInt("startIfElementsAtLeast");
}
@ -138,7 +138,7 @@ public class JoinSign extends GlobalProtection {
* @param amount the amount to set
*/
public void setStartIfElementsAtLeastAmount(int amount) {
if ((amount > 0 || amount == -1) && amount < maxElements) {
if ((amount > 0 || amount == -1) && amount <= maxElements) {
startIfElementsAtLeast = amount;
} else {
throw new IllegalArgumentException("startIfElementsAtLeastAmount is < 0 or < maxElements");
@ -201,7 +201,9 @@ public class JoinSign extends GlobalProtection {
config.set(preString + ".dungeon", dungeon.getName());
}
config.set(preString + ".maxElements", maxElements);
config.set(preString + ".startIfElementsAtLeast", startIfElementsAtLeast);
if (startIfElementsAtLeast != -1) {
config.set(preString + ".startIfElementsAtLeast", startIfElementsAtLeast);
}
}
}

View File

@ -26,8 +26,11 @@ import de.erethon.dungeonsxl.util.LWCUtil;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
@ -40,26 +43,35 @@ public class LeaveSign extends GlobalProtection {
public static final String LEAVE_SIGN_TAG = "Leave";
private Sign sign;
private Block sign;
private Set<Block> blocks;
public LeaveSign(DungeonsXL plugin, int id, Sign sign) {
super(plugin, sign.getWorld(), id);
this.sign = sign;
this.sign = sign.getBlock();
setText();
LWCUtil.removeProtection(sign.getBlock());
}
public LeaveSign(DungeonsXL plugin, World world, int id, ConfigurationSection config) {
super(plugin, world, id);
sign = world.getBlockAt(config.getInt("x"), config.getInt("y"), config.getInt("z"));
setText();
LWCUtil.removeProtection(sign);
}
/* Getters and setters */
@Override
public Set<Block> getBlocks() {
if (blocks == null) {
blocks = new HashSet<>();
blocks.add(sign.getBlock());
blocks.add(BlockUtil.getAttachedBlock(sign.getBlock()));
blocks.add(sign);
blocks.add(BlockUtil.getAttachedBlock(sign));
}
return blocks;
@ -67,11 +79,15 @@ public class LeaveSign extends GlobalProtection {
/* Actions */
public void setText() {
sign.setLine(0, ChatColor.BLUE + "############");
sign.setLine(1, DMessage.SIGN_LEAVE.getMessage());
sign.setLine(2, "");
sign.setLine(3, ChatColor.BLUE + "############");
sign.update();
BlockState state = sign.getState();
if (state instanceof Sign) {
Sign sign = (Sign) state;
sign.setLine(0, ChatColor.BLUE + "############");
sign.setLine(1, DMessage.SIGN_LEAVE.getMessage());
sign.setLine(2, "");
sign.setLine(3, ChatColor.BLUE + "############");
sign.update();
}
}
public void onPlayerInteract(Player player) {