From 768064e6e9024ef3930907a9415b897b1186e672 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Sun, 27 Mar 2016 21:28:55 +0200 Subject: [PATCH 01/11] Global protection and player handler rewrite These two important rewrites are tied together, so I did a lot of the player handler rewrite in this commit, even though it mainly tackles the protection rewrite. * Rewrote global protections * Deprecated methods which are subject to change * Protection system is now waaay more dynamic and provides an API * Began to rewrite player handling * Still uses DPlayer in many cases where e.g. DEditPlayer should be used! * Added class to represent the data file --- pom.xml | 2 +- .../github/dre2n/dungeonsxl/DungeonsXL.java | 153 +++----- .../dungeonsxl/command/BreakCommand.java | 13 +- .../dungeonsxl/command/ChatSpyCommand.java | 8 +- .../dungeonsxl/command/GroupCommand.java | 4 +- .../dungeonsxl/command/LeaveCommand.java | 13 +- .../dre2n/dungeonsxl/command/MainCommand.java | 2 +- .../dre2n/dungeonsxl/command/PlayCommand.java | 2 +- .../dungeonsxl/command/PortalCommand.java | 31 +- .../dungeonsxl/command/ReloadCommand.java | 3 +- .../dre2n/dungeonsxl/config/DataConfig.java | 48 +++ .../event/dgroup/DGroupDisbandEvent.java | 1 + .../dplayer/DPlayerLeaveDGroupEvent.java | 65 ++++ .../dre2n/dungeonsxl/global/DPortal.java | 155 ++++---- .../dre2n/dungeonsxl/global/GameSign.java | 339 ++++++++++-------- .../dungeonsxl/global/GlobalProtection.java | 91 +++++ .../dungeonsxl/global/GlobalProtections.java | 236 ++++++++++++ .../dre2n/dungeonsxl/global/GroupSign.java | 320 +++++++++-------- .../dre2n/dungeonsxl/global/LeaveSign.java | 134 +++---- .../dungeonsxl/listener/BlockListener.java | 87 ++--- .../dungeonsxl/listener/EntityListener.java | 24 +- .../dungeonsxl/listener/PlayerListener.java | 48 ++- .../dre2n/dungeonsxl/player/DEditPlayer.java | 30 ++ .../dungeonsxl/player/DGlobalPlayer.java | 101 ++++++ .../dre2n/dungeonsxl/player/DGroup.java | 17 +- .../dre2n/dungeonsxl/player/DPlayer.java | 58 ++- .../dre2n/dungeonsxl/player/DPlayers.java | 132 +++++++ .../dre2n/dungeonsxl/player/DSavePlayer.java | 36 +- .../github/dre2n/dungeonsxl/sign/EndSign.java | 2 +- .../dre2n/dungeonsxl/sign/FloorSign.java | 2 +- .../dre2n/dungeonsxl/sign/LeaveSign.java | 2 +- .../dre2n/dungeonsxl/sign/ReadySign.java | 2 +- .../dre2n/dungeonsxl/task/LazyUpdateTask.java | 2 +- .../dre2n/dungeonsxl/task/UpdateTask.java | 2 +- 34 files changed, 1384 insertions(+), 781 deletions(-) create mode 100644 src/main/java/io/github/dre2n/dungeonsxl/config/DataConfig.java create mode 100644 src/main/java/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerLeaveDGroupEvent.java create mode 100644 src/main/java/io/github/dre2n/dungeonsxl/global/GlobalProtection.java create mode 100644 src/main/java/io/github/dre2n/dungeonsxl/global/GlobalProtections.java create mode 100644 src/main/java/io/github/dre2n/dungeonsxl/player/DEditPlayer.java create mode 100644 src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java create mode 100644 src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java diff --git a/pom.xml b/pom.xml index 07a24c47..b3e4a0ec 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ io.github.dre2n commons - 0.1 + 0.2 io.github.dre2n diff --git a/src/main/java/io/github/dre2n/dungeonsxl/DungeonsXL.java b/src/main/java/io/github/dre2n/dungeonsxl/DungeonsXL.java index fa37a499..ef781a8f 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/DungeonsXL.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/DungeonsXL.java @@ -22,6 +22,7 @@ import io.github.dre2n.commons.javaplugin.BRPlugin; import io.github.dre2n.commons.javaplugin.BRPluginSettings; import io.github.dre2n.commons.util.FileUtil; import io.github.dre2n.dungeonsxl.command.*; +import io.github.dre2n.dungeonsxl.config.DataConfig; import io.github.dre2n.dungeonsxl.config.MainConfig; import io.github.dre2n.dungeonsxl.config.MessageConfig; import io.github.dre2n.dungeonsxl.config.WorldConfig; @@ -31,10 +32,7 @@ import io.github.dre2n.dungeonsxl.dungeon.EditWorld; import io.github.dre2n.dungeonsxl.game.Game; import io.github.dre2n.dungeonsxl.game.GameTypes; import io.github.dre2n.dungeonsxl.game.GameWorld; -import io.github.dre2n.dungeonsxl.global.DPortal; -import io.github.dre2n.dungeonsxl.global.GameSign; -import io.github.dre2n.dungeonsxl.global.GroupSign; -import io.github.dre2n.dungeonsxl.global.LeaveSign; +import io.github.dre2n.dungeonsxl.global.GlobalProtections; import io.github.dre2n.dungeonsxl.listener.BlockListener; import io.github.dre2n.dungeonsxl.listener.EntityListener; import io.github.dre2n.dungeonsxl.listener.HangingListener; @@ -42,6 +40,7 @@ import io.github.dre2n.dungeonsxl.listener.PlayerListener; import io.github.dre2n.dungeonsxl.listener.WorldListener; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.player.DPlayer; +import io.github.dre2n.dungeonsxl.player.DPlayers; import io.github.dre2n.dungeonsxl.player.DSavePlayer; import io.github.dre2n.dungeonsxl.requirement.RequirementTypes; import io.github.dre2n.dungeonsxl.reward.RewardTypes; @@ -51,11 +50,7 @@ import io.github.dre2n.dungeonsxl.task.UpdateTask; import io.github.dre2n.dungeonsxl.task.WorldUnloadTask; import io.github.dre2n.dungeonsxl.trigger.Triggers; import java.io.File; -import java.io.IOException; import java.util.concurrent.CopyOnWriteArrayList; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.bukkit.scheduler.BukkitTask; @@ -68,6 +63,7 @@ public class DungeonsXL extends BRPlugin { public static final String[] EXCLUDED_FILES = {"config.yml", "uid.dat", "DXLData.data"}; + private DataConfig dataConfig; private MainConfig mainConfig; private MessageConfig messageConfig; @@ -78,23 +74,18 @@ public class DungeonsXL extends BRPlugin { private RewardTypes rewardTypes; private Triggers triggers; private Dungeons dungeons; + private GlobalProtections protections; + private DPlayers dPlayers; private BukkitTask worldUnloadTask; private BukkitTask lazyUpdateTask; private BukkitTask updateTask; - private CopyOnWriteArrayList inBreakMode = new CopyOnWriteArrayList<>(); - private CopyOnWriteArrayList chatSpyers = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList dLootInventories = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList editWorlds = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList gameWorlds = new CopyOnWriteArrayList<>(); - private CopyOnWriteArrayList gameSigns = new CopyOnWriteArrayList<>(); - private CopyOnWriteArrayList groupSigns = new CopyOnWriteArrayList<>(); - private CopyOnWriteArrayList leaveSigns = new CopyOnWriteArrayList<>(); - private CopyOnWriteArrayList dPortals = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList games = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList dGroups = new CopyOnWriteArrayList<>(); - private CopyOnWriteArrayList dPlayers = new CopyOnWriteArrayList<>(); public DungeonsXL() { /* @@ -124,6 +115,7 @@ public class DungeonsXL extends BRPlugin { // Load Language loadMessageConfig(new File(getDataFolder(), "languages/en.yml")); // Load Config + loadDataConfig(new File(getDataFolder(), "data.yml")); loadMainConfig(new File(getDataFolder(), "config.yml")); // Load Language 2 loadMessageConfig(new File(getDataFolder(), "languages/" + mainConfig.getLanguage() + ".yml")); @@ -134,6 +126,8 @@ public class DungeonsXL extends BRPlugin { loadTriggers(); loadDSigns(); loadDungeons(); + loadGlobalProtections(); + loadDPlayers(); manager.registerEvents(new EntityListener(), this); manager.registerEvents(new PlayerListener(), this); @@ -157,18 +151,13 @@ public class DungeonsXL extends BRPlugin { messageConfig.save(); // DPlayer leaves World - for (DPlayer dPlayer : dPlayers) { + for (DPlayer dPlayer : dPlayers.getDPlayers()) { dPlayer.leave(); } // Delete all Data - chatSpyers.clear(); dLootInventories.clear(); - groupSigns.clear(); - leaveSigns.clear(); - dPortals.clear(); dGroups.clear(); - dPlayers.clear(); // Delete Worlds GameWorld.deleteAll(); @@ -205,36 +194,19 @@ public class DungeonsXL extends BRPlugin { } } - // Save and Load + // Save and load public void saveData() { - File file = new File(getDataFolder(), "data.yml"); - FileConfiguration configFile = new YamlConfiguration(); - - DPortal.save(configFile); - GroupSign.save(configFile); - LeaveSign.save(configFile); - - try { - configFile.save(file); - - } catch (IOException exception) { - exception.printStackTrace(); + protections.saveAll(); + DSavePlayer.save(); + for (EditWorld editWorld : editWorlds) { + editWorld.save(); } } public void loadAll() { - // Load world data - File file = new File(getDataFolder(), "data.yml"); - FileConfiguration configFile = YamlConfiguration.loadConfiguration(file); - - DPortal.load(configFile); - GroupSign.load(configFile); - LeaveSign.load(configFile); - - // Load saved players + protections.loadAll(); + dPlayers.loadAll(); DSavePlayer.load(); - - // Check Worlds checkWorlds(); } @@ -267,6 +239,20 @@ public class DungeonsXL extends BRPlugin { return instance; } + /** + * @return the loaded instance of DataConfig + */ + public DataConfig getDataConfig() { + return dataConfig; + } + + /** + * load / reload a new instance of MainConfig + */ + public void loadDataConfig(File file) { + dataConfig = new DataConfig(file); + } + /** * @return the loaded instance of MainConfig */ @@ -421,6 +407,34 @@ public class DungeonsXL extends BRPlugin { dungeons = new Dungeons(); } + /** + * @return the loaded instance of GlobalProtections + */ + public GlobalProtections getGlobalProtections() { + return protections; + } + + /** + * load / reload a new instance of GlobalProtections + */ + public void loadGlobalProtections() { + protections = new GlobalProtections(); + } + + /** + * @return the loaded instance of DPlayers + */ + public DPlayers getDPlayers() { + return dPlayers; + } + + /** + * load / reload a new instance of DPlayers + */ + public void loadDPlayers() { + dPlayers = new DPlayers(); + } + /** * @return the worldUnloadTask */ @@ -463,20 +477,6 @@ public class DungeonsXL extends BRPlugin { updateTask = new UpdateTask().runTaskTimer(this, 0L, period); } - /** - * @return the inBreakMode - */ - public CopyOnWriteArrayList getInBreakMode() { - return inBreakMode; - } - - /** - * @return the chatSpyers - */ - public CopyOnWriteArrayList getChatSpyers() { - return chatSpyers; - } - /** * @return the dLootInventories */ @@ -505,34 +505,6 @@ public class DungeonsXL extends BRPlugin { return gameWorlds; } - /** - * @return the gameSigns - */ - public CopyOnWriteArrayList getGameSigns() { - return gameSigns; - } - - /** - * @return the groupSigns - */ - public CopyOnWriteArrayList getGroupSigns() { - return groupSigns; - } - - /** - * @return the dPortals - */ - public CopyOnWriteArrayList getDPortals() { - return dPortals; - } - - /** - * @return the leaveSigns - */ - public CopyOnWriteArrayList getLeaveSigns() { - return leaveSigns; - } - /** * @return the games */ @@ -547,11 +519,4 @@ public class DungeonsXL extends BRPlugin { return dGroups; } - /** - * @return the dPlayers - */ - public CopyOnWriteArrayList getDPlayers() { - return dPlayers; - } - } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/BreakCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/BreakCommand.java index ddbf736e..5cafce7d 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/BreakCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/BreakCommand.java @@ -21,6 +21,7 @@ import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.MessageConfig; import io.github.dre2n.dungeonsxl.config.MessageConfig.Messages; +import io.github.dre2n.dungeonsxl.player.DGlobalPlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -44,13 +45,15 @@ public class BreakCommand extends BRCommand { @Override public void onExecute(String[] args, CommandSender sender) { Player player = (Player) sender; - if (!plugin.getInBreakMode().contains(player)) { - plugin.getInBreakMode().add(player); - MessageUtil.sendMessage(sender, messageConfig.getMessage(Messages.CMD_BREAK_BREAK_MODE)); + DGlobalPlayer dGlobalPlayer = plugin.getDPlayers().getByPlayer(player); + + if (dGlobalPlayer.isInBreakMode()) { + dGlobalPlayer.setInBreakMode(false); + MessageUtil.sendMessage(sender, messageConfig.getMessage(Messages.CMD_BREAK_PROTECTED_MODE)); } else { - plugin.getInBreakMode().remove(player); - MessageUtil.sendMessage(sender, messageConfig.getMessage(Messages.CMD_BREAK_PROTECTED_MODE)); + dGlobalPlayer.setInBreakMode(true); + MessageUtil.sendMessage(sender, messageConfig.getMessage(Messages.CMD_BREAK_BREAK_MODE)); } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/ChatSpyCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/ChatSpyCommand.java index 9c2445aa..35370af3 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/ChatSpyCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/ChatSpyCommand.java @@ -21,6 +21,7 @@ import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.MessageConfig; import io.github.dre2n.dungeonsxl.config.MessageConfig.Messages; +import io.github.dre2n.dungeonsxl.player.DGlobalPlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -44,13 +45,14 @@ public class ChatSpyCommand extends BRCommand { @Override public void onExecute(String[] args, CommandSender sender) { Player player = (Player) sender; + DGlobalPlayer dGlobalPlayer = plugin.getDPlayers().getByPlayer(player); - if (plugin.getChatSpyers().contains(player)) { - plugin.getChatSpyers().remove(player); + if (dGlobalPlayer.isInChatSpyMode()) { + dGlobalPlayer.setInChatSpyMode(false); MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.CMD_CHATSPY_STOPPED)); } else { - plugin.getChatSpyers().add(player); + dGlobalPlayer.setInChatSpyMode(true); MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.CMD_CHATSPY_START)); } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/GroupCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/GroupCommand.java index c8e0fdff..438f626c 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/GroupCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/GroupCommand.java @@ -114,7 +114,7 @@ public class GroupCommand extends BRCommand { DGroupCreateEvent event = new DGroupCreateEvent(dGroup, player, DGroupCreateEvent.Cause.COMMAND); if (event.isCancelled()) { - plugin.getDGroups().remove(dGroup); + dGroup.delete(); dGroup = null; } else { @@ -131,7 +131,7 @@ public class GroupCommand extends BRCommand { DGroupDisbandEvent event = new DGroupDisbandEvent(dGroup, player, DGroupDisbandEvent.Cause.COMMAND); if (!event.isCancelled()) { - plugin.getDGroups().remove(dGroup); + dGroup.delete(); MessageUtil.sendMessage(sender, messageConfig.getMessage(Messages.GROUP_DISBANDED, sender.getName(), dGroup.getName())); dGroup = null; } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/LeaveCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/LeaveCommand.java index b8e7e336..639d1139 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/LeaveCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/LeaveCommand.java @@ -22,6 +22,7 @@ import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.MessageConfig; import io.github.dre2n.dungeonsxl.config.MessageConfig.Messages; import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerEscapeEvent; +import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerLeaveDGroupEvent; import io.github.dre2n.dungeonsxl.game.GameWorld; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.player.DPlayer; @@ -58,9 +59,17 @@ public class LeaveCommand extends BRCommand { } if (dPlayer != null) { - DPlayerEscapeEvent event = new DPlayerEscapeEvent(dPlayer); + DGroup dGroup = DGroup.getByPlayer(player); - if (event.isCancelled()) { + if (dGroup == null) { + MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.ERROR_NOT_IN_GROUP)); + return; + } + + DPlayerEscapeEvent dPlayerEscapeEvent = new DPlayerEscapeEvent(dPlayer); + DPlayerLeaveDGroupEvent dPlayerLeaveDGroupEvent = new DPlayerLeaveDGroupEvent(dPlayer, dGroup); + + if (dPlayerEscapeEvent.isCancelled() || dPlayerLeaveDGroupEvent.isCancelled()) { return; } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/MainCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/MainCommand.java index 5f374a81..31767c29 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/MainCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/MainCommand.java @@ -52,7 +52,7 @@ public class MainCommand extends BRCommand { int maps = new File(plugin.getDataFolder() + "/maps").listFiles().length; int dungeons = new File(plugin.getDataFolder() + "/dungeons").listFiles().length; int loaded = plugin.getEditWorlds().size() + plugin.getGameWorlds().size(); - int players = plugin.getDPlayers().size(); + int players = plugin.getDPlayers().getDPlayers().size(); Internals internals = CompatibilityHandler.getInstance().getInternals(); String vault = ""; if (plugins.getPlugin("Vault") != null) { diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/PlayCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/PlayCommand.java index f611070f..9a660346 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/PlayCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/PlayCommand.java @@ -160,7 +160,7 @@ public class PlayCommand extends BRCommand { if (dGroup.getGameWorld() == null) { MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.ERROR_NOT_SAVED, DGroup.getByPlayer(player).getMapName())); - dGroup.remove(); + dGroup.delete(); return; } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/PortalCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/PortalCommand.java index b523c8fc..35d7a155 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/PortalCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/PortalCommand.java @@ -22,6 +22,7 @@ import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.MessageConfig; import io.github.dre2n.dungeonsxl.config.MessageConfig.Messages; import io.github.dre2n.dungeonsxl.global.DPortal; +import io.github.dre2n.dungeonsxl.player.DGlobalPlayer; import io.github.dre2n.dungeonsxl.player.DPlayer; import org.bukkit.Material; import org.bukkit.command.CommandSender; @@ -32,10 +33,10 @@ import org.bukkit.inventory.ItemStack; * @author Frank Baumann, Daniel Saukel */ public class PortalCommand extends BRCommand { - + protected static DungeonsXL plugin = DungeonsXL.getInstance(); protected static MessageConfig messageConfig = plugin.getMessageConfig(); - + public PortalCommand() { setCommand("portal"); setMinArgs(0); @@ -44,29 +45,31 @@ public class PortalCommand extends BRCommand { setPermission("dxl.portal"); setPlayerCommand(true); } - + @Override public void onExecute(String[] args, CommandSender sender) { Player player = (Player) sender; - DPlayer dPlayer = DPlayer.getByPlayer(player); - - if (dPlayer != null) { + DGlobalPlayer dGlobalPlayer = plugin.getDPlayers().getByPlayer(player); + + if (dGlobalPlayer instanceof DPlayer) { MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.ERROR_LEAVE_DUNGEON)); + return; } - - DPortal dPortal = DPortal.getByPlayer(player); - + + DPortal dPortal = dGlobalPlayer.getPortal(); + if (dPortal == null) { - dPortal = new DPortal(false); - dPortal.setPlayer(player); + dPortal = new DPortal(plugin.getGlobalProtections().generateId(DPortal.class, player.getWorld()), player.getWorld(), false); + dGlobalPlayer.setCreatingPortal(dPortal); dPortal.setWorld(player.getWorld()); player.getInventory().setItemInHand(new ItemStack(Material.WOOD_SWORD)); MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.PLAYER_PORTAL_INTRODUCTION)); - + } else { - plugin.getDPortals().remove(dPortal); + dPortal.delete(); + dGlobalPlayer.setCreatingPortal(null); MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.PLAYER_PORTAL_ABORT)); } } - + } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/ReloadCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/ReloadCommand.java index 185204b2..113f234e 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/ReloadCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/ReloadCommand.java @@ -53,7 +53,7 @@ public class ReloadCommand extends BRCommand { int maps = new File(plugin.getDataFolder() + "/maps").listFiles().length; int dungeons = new File(plugin.getDataFolder() + "/dungeons").listFiles().length; int loaded = plugin.getEditWorlds().size() + plugin.getGameWorlds().size(); - int players = plugin.getDPlayers().size(); + int players = plugin.getDPlayers().getDPlayers().size(); Internals internals = CompatibilityHandler.getInstance().getInternals(); String vault = ""; if (plugins.getPlugin("Vault") != null) { @@ -72,6 +72,7 @@ public class ReloadCommand extends BRCommand { // Load Language plugin.loadMessageConfig(new File(plugin.getDataFolder(), "languages/en.yml")); // Load Config + plugin.loadDataConfig(new File(plugin.getDataFolder(), "data.yml")); plugin.loadMainConfig(new File(plugin.getDataFolder(), "config.yml")); // Load Language 2 plugin.loadMessageConfig(new File(plugin.getDataFolder(), "languages/" + plugin.getMainConfig().getLanguage() + ".yml")); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/config/DataConfig.java b/src/main/java/io/github/dre2n/dungeonsxl/config/DataConfig.java new file mode 100644 index 00000000..7146d9b8 --- /dev/null +++ b/src/main/java/io/github/dre2n/dungeonsxl/config/DataConfig.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2012-2016 Frank Baumann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.dre2n.dungeonsxl.config; + +import io.github.dre2n.commons.config.BRConfig; +import java.io.File; + +/** + * @author Daniel Saukel + */ +public class DataConfig extends BRConfig { + + public static final int CONFIG_VERSION = 1; + + public DataConfig(File file) { + super(file, CONFIG_VERSION); + + if (initialize) { + initialize(); + } + load(); + } + + @Override + public void initialize() { + save(); + } + + @Override + public void load() { + // Load? + } + +} diff --git a/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupDisbandEvent.java b/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupDisbandEvent.java index 9891bf83..d4095dda 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupDisbandEvent.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupDisbandEvent.java @@ -30,6 +30,7 @@ public class DGroupDisbandEvent extends DGroupEvent implements Cancellable { COMMAND, DUNGEON_FINISHED, + GROUP_IS_EMPTY, CUSTOM } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerLeaveDGroupEvent.java b/src/main/java/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerLeaveDGroupEvent.java new file mode 100644 index 00000000..5d8d77c0 --- /dev/null +++ b/src/main/java/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerLeaveDGroupEvent.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2016 Daniel Saukel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.dre2n.dungeonsxl.event.dplayer; + +import io.github.dre2n.dungeonsxl.player.DGroup; +import io.github.dre2n.dungeonsxl.player.DPlayer; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +/** + * @author Daniel Saukel + */ +public class DPlayerLeaveDGroupEvent extends DPlayerEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + + private DGroup dGroup; + + public DPlayerLeaveDGroupEvent(DPlayer dPlayer, DGroup dGroup) { + super(dPlayer); + this.dGroup = dGroup; + } + + /** + * @return the dGroup + */ + public DGroup getDGroup() { + return dGroup; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + +} diff --git a/src/main/java/io/github/dre2n/dungeonsxl/global/DPortal.java b/src/main/java/io/github/dre2n/dungeonsxl/global/DPortal.java index daa7a02c..fef53183 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/global/DPortal.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/global/DPortal.java @@ -16,12 +16,14 @@ */ package io.github.dre2n.dungeonsxl.global; +import io.github.dre2n.commons.util.BlockUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.MessageConfig.Messages; import io.github.dre2n.dungeonsxl.game.GameWorld; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.player.DPlayer; +import java.util.Set; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -32,34 +34,27 @@ import org.bukkit.entity.Player; /** * @author Frank Baumann, Daniel Saukel */ -public class DPortal { +public class DPortal extends GlobalProtection { protected static DungeonsXL plugin = DungeonsXL.getInstance(); + protected static GlobalProtections protections = plugin.getGlobalProtections(); - private World world; private Block block1; private Block block2; private boolean active; - private Player player; + private Set blocks; - public DPortal(boolean active) { - plugin.getDPortals().add(this); + public DPortal(int id, World world, boolean active) { + super(world, id); this.active = active; } - /** - * @return the world - */ - public World getWorld() { - return world; - } + public DPortal(int id, Block block1, Block block2, boolean active) { + super(block1.getWorld(), id); - /** - * @param world - * the world to set - */ - public void setWorld(World world) { - this.world = world; + this.block1 = block1; + this.block2 = block2; + this.active = active; } /** @@ -108,25 +103,11 @@ public class DPortal { } /** - * @return the player + * Create a new DPortal */ - public Player getPlayer() { - return player; - } - - /** - * @param player - * the player to set - */ - public void setPlayer(Player player) { - this.player = player; - } - public void create() { - player = null; - if (block1 == null || block2 == null) { - plugin.getDPortals().remove(this); + delete(); return; } @@ -158,12 +139,12 @@ public class DPortal { int zz = z1; do { - Material type = world.getBlockAt(xx, yy, zz).getType(); + Material type = getWorld().getBlockAt(xx, yy, zz).getType(); if (type == Material.AIR || type == Material.WATER || type == Material.STATIONARY_WATER || type == Material.LAVA || type == Material.STATIONARY_LAVA || type == Material.SAPLING || type == Material.WEB || type == Material.LONG_GRASS || type == Material.DEAD_BUSH || type == Material.PISTON_EXTENSION || type == Material.YELLOW_FLOWER || type == Material.RED_ROSE || type == Material.BROWN_MUSHROOM || type == Material.RED_MUSHROOM || type == Material.TORCH || type == Material.FIRE || type == Material.CROPS || type == Material.REDSTONE_WIRE || type == Material.REDSTONE_TORCH_OFF || type == Material.SNOW || type == Material.REDSTONE_TORCH_ON) { - world.getBlockAt(xx, yy, zz).setType(Material.PORTAL); + getWorld().getBlockAt(xx, yy, zz).setType(Material.PORTAL); } zz = zz + zcount; @@ -176,6 +157,10 @@ public class DPortal { } while (xx != x2 + xcount); } + /** + * @param player + * the player to teleport into his dungeon + */ public void teleport(Player player) { DGroup dgroup = DGroup.getByPlayer(player); @@ -201,8 +186,35 @@ public class DPortal { } } + @Override + public Set getBlocks() { + if (blocks == null) { + blocks = BlockUtil.getBlocksBetween(block1, block2); + } + + return blocks; + } + + @Override + public void save(FileConfiguration configFile) { + if (!active) { + return; + } + + String preString = "portal." + getWorld().getName() + "." + getId(); + // Location1 + configFile.set(preString + ".loc1.x", block1.getX()); + configFile.set(preString + ".loc1.y", block1.getY()); + configFile.set(preString + ".loc1.z", block1.getZ()); + // Location1 + configFile.set(preString + ".loc2.x", block2.getX()); + configFile.set(preString + ".loc2.y", block2.getY()); + configFile.set(preString + ".loc2.z", block2.getZ()); + } + + @Override public void delete() { - plugin.getDPortals().remove(this); + protections.removeProtection(this); int x1 = block1.getX(), y1 = block1.getY(), z1 = block1.getZ(); int x2 = block2.getX(), y2 = block2.getY(), z2 = block2.getZ(); @@ -232,10 +244,10 @@ public class DPortal { do { int zz = z1; do { - Material type = world.getBlockAt(xx, yy, zz).getType(); + Material type = getWorld().getBlockAt(xx, yy, zz).getType(); if (type == Material.PORTAL) { - world.getBlockAt(xx, yy, zz).setType(Material.AIR); + getWorld().getBlockAt(xx, yy, zz).setType(Material.AIR); } zz = zz + zcount; @@ -249,12 +261,21 @@ public class DPortal { } /* Statics */ + /** + * @param location + * a location covered by the returned portal + */ public static DPortal getByLocation(Location location) { return getByBlock(location.getBlock()); } + /** + * @param block + * a block covered by the returned portal + */ public static DPortal getByBlock(Block block) { - for (DPortal portal : plugin.getDPortals()) { + for (GlobalProtection protection : protections.getProtections(DPortal.class)) { + DPortal portal = (DPortal) protection; int x1 = portal.block1.getX(), y1 = portal.block1.getY(), z1 = portal.block1.getZ(); int x2 = portal.block2.getX(), y2 = portal.block2.getY(), z2 = portal.block2.getZ(); int x3 = block.getX(), y3 = block.getY(), z3 = block.getZ(); @@ -291,60 +312,4 @@ public class DPortal { return null; } - public static DPortal getByPlayer(Player player) { - for (DPortal portal : plugin.getDPortals()) { - if (portal.player == player) { - return portal; - } - } - - return null; - } - - // Save and Load - public static void save(FileConfiguration configFile) { - int id = 0; - for (DPortal dPortal : plugin.getDPortals()) { - id++; - - if (!dPortal.active) { - continue; - } - - String preString = "portal." + dPortal.world.getName() + "." + id; - // Location1 - configFile.set(preString + ".loc1.x", dPortal.block1.getX()); - configFile.set(preString + ".loc1.y", dPortal.block1.getY()); - configFile.set(preString + ".loc1.z", dPortal.block1.getZ()); - // Location1 - configFile.set(preString + ".loc2.x", dPortal.block2.getX()); - configFile.set(preString + ".loc2.y", dPortal.block2.getY()); - configFile.set(preString + ".loc2.z", dPortal.block2.getZ()); - } - } - - public static void load(FileConfiguration configFile) { - for (World world : plugin.getServer().getWorlds()) { - if (!configFile.contains("portal." + world.getName())) { - return; - } - - int id = 0; - String preString; - do { - id++; - preString = "portal." + world.getName() + "." + id + "."; - - if (configFile.contains(preString)) { - DPortal dPortal = new DPortal(true); - dPortal.world = world; - dPortal.block1 = world.getBlockAt(configFile.getInt(preString + "loc1.x"), configFile.getInt(preString + "loc1.y"), configFile.getInt(preString + "loc1.z")); - dPortal.block2 = world.getBlockAt(configFile.getInt(preString + "loc2.x"), configFile.getInt(preString + "loc2.y"), configFile.getInt(preString + "loc2.z")); - dPortal.create(); - } - - } while (configFile.contains(preString)); - } - } - } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/global/GameSign.java b/src/main/java/io/github/dre2n/dungeonsxl/global/GameSign.java index 125e29e1..3d88c990 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/global/GameSign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/global/GameSign.java @@ -16,6 +16,7 @@ */ package io.github.dre2n.dungeonsxl.global; +import io.github.dre2n.commons.util.BlockUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.MessageConfig.Messages; @@ -25,6 +26,8 @@ import io.github.dre2n.dungeonsxl.game.Game; import io.github.dre2n.dungeonsxl.game.GameWorld; import io.github.dre2n.dungeonsxl.player.DGroup; import java.io.File; +import java.util.HashSet; +import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -37,15 +40,16 @@ import org.bukkit.entity.Player; /** * @author Frank Baumann, Milan Albrecht, Daniel Saukel */ -public class GameSign { +public class GameSign extends GlobalProtection { protected static DungeonsXL plugin = DungeonsXL.getInstance(); + protected static GlobalProtections protections = plugin.getGlobalProtections(); // Sign Labels public static final String IS_PLAYING = ChatColor.DARK_RED + "Is Playing"; public static final String FULL = ChatColor.DARK_RED + "Full"; - public static final String JOIN_GROUP = ChatColor.DARK_GREEN + "Join Game"; - public static final String NEW_GROUP = ChatColor.DARK_GREEN + "New Game"; + public static final String JOIN_GAME = ChatColor.DARK_GREEN + "Join Game"; + public static final String NEW_GAME = ChatColor.DARK_GREEN + "New Game"; // Variables private Game[] games; @@ -56,9 +60,10 @@ public class GameSign { private Block startSign; private int directionX = 0, directionZ = 0; private int verticalSigns; + private Set blocks; - public GameSign(Block startSign, String identifier, int maxGames, int maxGroupsPerGame, boolean multiFloor) { - plugin.getGameSigns().add(this); + public GameSign(int id, Block startSign, String identifier, int maxGames, int maxGroupsPerGame, boolean multiFloor) { + super(startSign.getWorld(), id); this.startSign = startSign; games = new Game[maxGames]; @@ -84,6 +89,36 @@ public class GameSign { update(); } + /** + * @return the games + */ + public Game[] getGames() { + return games; + } + + /** + * @param games + * the games to set + */ + public void setGames(Game[] games) { + this.games = games; + } + + /** + * @return the multiFloor + */ + public boolean isMultiFloor() { + return multiFloor; + } + + /** + * @param multiFloor + * the multiFloor to set + */ + public void setMultiFloor(boolean multiFloor) { + this.multiFloor = multiFloor; + } + /** * @return the dungeonName */ @@ -115,24 +150,23 @@ public class GameSign { } /** - * @return the multiFloor + * @return the maximum player count per group */ - public boolean isMultiFloor() { - return multiFloor; + public int getMaxGroupsPerGame() { + return maxGroupsPerGame; } /** - * @param multiFloor - * the multiFloor to set + * @param maxGroupsPerGame + * the maximum player count per group to set */ - public void setMultiFloor(boolean multiFloor) { - this.multiFloor = multiFloor; - } - - public void delete() { - plugin.getGameSigns().remove(this); + public void setMaxGroupsPerGame(int maxGroupsPerGame) { + this.maxGroupsPerGame = maxGroupsPerGame; } + /** + * Update this game sign to show the game(s) correctly. + */ public void update() { int i = 0; for (Game game : games) { @@ -169,7 +203,7 @@ public class GameSign { sign.setLine(0, FULL); } else { - sign.setLine(0, JOIN_GROUP); + sign.setLine(0, JOIN_GAME); } int j = 1; @@ -190,7 +224,7 @@ public class GameSign { } } else { - sign.setLine(0, NEW_GROUP); + sign.setLine(0, NEW_GAME); } sign.update(); @@ -199,7 +233,121 @@ public class GameSign { } } - // Static + @Override + public Set getBlocks() { + if (blocks == null) { + blocks = new HashSet<>(); + + int i = games.length; + do { + i--; + + blocks.add(startSign.getRelative(i * directionX, 0, i * directionZ)); + + } while (i >= 0); + + HashSet toAdd = new HashSet<>(); + for (Block block : blocks) { + i = verticalSigns; + do { + i--; + + Block beneath = block.getLocation().add(0, -1 * i, 0).getBlock(); + toAdd.add(beneath); + toAdd.add(BlockUtil.getAttachedBlock(beneath)); + + } while (i >= 0); + } + blocks.addAll(toAdd); + } + + return blocks; + } + + @Override + public void save(FileConfiguration config) { + String preString = "gamesign." + getWorld().getName() + "." + getId(); + + config.set(preString + ".x", startSign.getX()); + config.set(preString + ".y", startSign.getY()); + config.set(preString + ".z", startSign.getZ()); + + if (isMultiFloor()) { + config.set(preString + ".dungeon", dungeonName); + + } else { + config.set(preString + ".dungeon", mapName); + } + + config.set(preString + ".maxGames", games.length); + config.set(preString + ".maxGroupsPerGame", maxGroupsPerGame); + config.set(preString + ".multiFloor", isMultiFloor()); + } + + /* Statics */ + /** + * @param block + * a block which is protected by the returned GameSign + */ + public static GameSign getByBlock(Block block) { + if (!(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) { + return null; + } + + int x = block.getX(), y = block.getY(), z = block.getZ(); + for (GlobalProtection protection : protections.getProtections(GameSign.class)) { + GameSign gameSign = (GameSign) protection; + + int sx1 = gameSign.startSign.getX(), sy1 = gameSign.startSign.getY(), sz1 = gameSign.startSign.getZ(); + int sx2 = sx1 + (gameSign.games.length - 1) * gameSign.directionX; + int sy2 = sy1 - gameSign.verticalSigns + 1; + int sz2 = sz1 + (gameSign.games.length - 1) * gameSign.directionZ; + + if (sx1 > sx2) { + if (x < sx2 || x > sx1) { + continue; + } + + } else if (sx1 < sx2) { + if (x > sx2 || x < sx1) { + continue; + } + + } else if (x != sx1) { + continue; + } + + if (sy1 > sy2) { + if (y < sy2 || y > sy1) { + continue; + } + + } else if (y != sy1) { + continue; + } + + if (sz1 > sz2) { + if (z < sz2 || z > sz1) { + continue; + } + + } else if (sz1 < sz2) { + if (z > sz2 || z < sz1) { + continue; + } + + } else if (z != sz1) { + continue; + } + + return gameSign; + } + + return null; + } + + /* SUBJECT TO CHANGE*/ + @Deprecated public static GameSign tryToCreate(Block startSign, String mapName, int maxGames, int maxGroupsPerGame, boolean multiFloor) { World world = startSign.getWorld(); int direction = startSign.getData(); @@ -296,94 +444,15 @@ public class GameSign { block.setTypeIdAndData(68, startSign.getData(), true); } - GameSign sign = new GameSign(startSign, mapName, maxGames, maxGroupsPerGame, multiFloor); + GameSign sign = new GameSign(protections.generateId(GameSign.class, world), startSign, mapName, maxGames, maxGroupsPerGame, multiFloor); return sign; } - public static boolean isRelativeSign(Block block, int x, int z) { - GameSign gameSign = getSign(block.getRelative(x, 0, z)); - if (gameSign == null) { - return false; - } - - if (x == -1 && gameSign.startSign.getData() == 4) { - return true; - } - - if (x == 1 && gameSign.startSign.getData() == 5) { - return true; - } - - if (z == -1 && gameSign.startSign.getData() == 2) { - return true; - } - - if (z == 1 && gameSign.startSign.getData() == 3) { - return true; - } - - return false; - } - - public static GameSign getSign(Block block) { - if (!(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) { - return null; - } - - int x = block.getX(), y = block.getY(), z = block.getZ(); - for (GameSign gameSign : plugin.getGameSigns()) { - int sx1 = gameSign.startSign.getX(), sy1 = gameSign.startSign.getY(), sz1 = gameSign.startSign.getZ(); - int sx2 = sx1 + (gameSign.games.length - 1) * gameSign.directionX; - int sy2 = sy1 - gameSign.verticalSigns + 1; - int sz2 = sz1 + (gameSign.games.length - 1) * gameSign.directionZ; - - if (sx1 > sx2) { - if (x < sx2 || x > sx1) { - continue; - } - - } else if (sx1 < sx2) { - if (x > sx2 || x < sx1) { - continue; - } - - } else if (x != sx1) { - continue; - } - - if (sy1 > sy2) { - if (y < sy2 || y > sy1) { - continue; - } - - } else if (y != sy1) { - continue; - } - - if (sz1 > sz2) { - if (z < sz2 || z > sz1) { - continue; - } - - } else if (sz1 < sz2) { - if (z > sz2 || z < sz1) { - continue; - } - - } else if (z != sz1) { - continue; - } - - return gameSign; - } - - return null; - } - + @Deprecated public static boolean playerInteract(Block block, Player player) { int x = block.getX(), y = block.getY(), z = block.getZ(); - GameSign gameSign = getSign(block); + GameSign gameSign = getByBlock(block); if (gameSign == null) { return false; @@ -391,6 +460,11 @@ public class GameSign { DGroup dGroup = DGroup.getByPlayer(player); + if (dGroup == null) { + MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.ERROR_JOIN_GROUP)); + return true; + } + if (!dGroup.getCaptain().equals(player)) { MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.ERROR_NOT_CAPTAIN)); return true; @@ -436,11 +510,11 @@ public class GameSign { Sign topSign = (Sign) topBlock.getState(); - if (topSign.getLine(0).equals(NEW_GROUP)) { + if (topSign.getLine(0).equals(NEW_GAME)) { gameSign.games[column] = new Game(dGroup); gameSign.update(); - } else if (topSign.getLine(0).equals(JOIN_GROUP)) { + } else if (topSign.getLine(0).equals(JOIN_GAME)) { gameSign.games[column].addDGroup(dGroup); gameSign.update(); } @@ -448,9 +522,11 @@ public class GameSign { return true; } + @Deprecated public static void updatePerGame(Game gameSearch) { + for (GlobalProtection protection : protections.getProtections(GameSign.class)) { + GameSign gameSign = (GameSign) protection; - for (GameSign gameSign : plugin.getGameSigns()) { int i = 0; for (Game game : gameSign.games) { if (game == null) { @@ -469,6 +545,7 @@ public class GameSign { } } + @Deprecated public static int[] getDirection(byte data) { int[] direction = new int[2]; @@ -492,58 +569,4 @@ public class GameSign { return direction; } - // Save and Load - public static void save(FileConfiguration configFile) { - int id = 0; - - for (GameSign gameSign : plugin.getGameSigns()) { - id++; - String preString = "gamesign." + gameSign.startSign.getWorld().getName() + "." + id; - - // Location - configFile.set(preString + ".x", gameSign.startSign.getX()); - configFile.set(preString + ".y", gameSign.startSign.getY()); - configFile.set(preString + ".z", gameSign.startSign.getZ()); - - // Etc. - if (gameSign.isMultiFloor()) { - configFile.set(preString + ".dungeon", gameSign.dungeonName); - - } else { - configFile.set(preString + ".dungeon", gameSign.mapName); - } - - configFile.set(preString + ".maxGames", gameSign.games.length); - configFile.set(preString + ".maxGroupsPerGame", gameSign.maxGroupsPerGame); - configFile.set(preString + ".multiFloor", gameSign.isMultiFloor()); - } - } - - public static void load(FileConfiguration configFile) { - for (World world : plugin.getServer().getWorlds()) { - if (!configFile.contains("gamesign." + world.getName())) { - continue; - } - - int id = 0; - String preString; - do { - id++; - preString = "gamesign." + world.getName() + "." + id + "."; - if (configFile.contains(preString)) { - String mapName = configFile.getString(preString + ".dungeon"); - int maxGames = configFile.getInt(preString + ".maxGames"); - int maxGroupsPerGame = configFile.getInt(preString + ".maxGroupsPerGame"); - boolean multiFloor = false; - if (configFile.contains(preString + ".multiFloor")) { - multiFloor = configFile.getBoolean(preString + ".multiFloor"); - } - Block startSign = world.getBlockAt(configFile.getInt(preString + ".x"), configFile.getInt(preString + ".y"), configFile.getInt(preString + ".z")); - - new GameSign(startSign, mapName, maxGames, maxGroupsPerGame, multiFloor); - } - } while (configFile.contains(preString)); - } - } - } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/global/GlobalProtection.java b/src/main/java/io/github/dre2n/dungeonsxl/global/GlobalProtection.java new file mode 100644 index 00000000..f3825131 --- /dev/null +++ b/src/main/java/io/github/dre2n/dungeonsxl/global/GlobalProtection.java @@ -0,0 +1,91 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package io.github.dre2n.dungeonsxl.global; + +import io.github.dre2n.dungeonsxl.DungeonsXL; +import java.io.File; +import java.util.Collection; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +/** + * @author Daniel Saukel + */ +public abstract class GlobalProtection { + + protected static DungeonsXL plugin = DungeonsXL.getInstance(); + protected static FileConfiguration config = plugin.getDataConfig().getConfig(); + protected static GlobalProtections protections = plugin.getGlobalProtections(); + + private World world; + private int id; + + protected GlobalProtection(World world, int id) { + this.world = world; + this.id = id; + + protections.addProtection(this); + } + + /** + * @return the world + */ + public World getWorld() { + return world; + } + + /** + * @param world + * the world to set + */ + public void setWorld(World world) { + this.world = world; + } + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * Delete this protection. + */ + public void delete() { + protections.removeProtection(this); + } + + /* Abstracts */ + /** + * Save the data to the default file + */ + public void save() { + save(config); + } + + /** + * @param file + * the file to save the protection to + */ + public void save(File file) { + save(YamlConfiguration.loadConfiguration(file)); + } + + /** + * @param config + * the config to save the protection to + */ + public abstract void save(FileConfiguration config); + + /** + * @return a collection of all blocks covered by this protection + */ + public abstract Collection getBlocks(); + +} diff --git a/src/main/java/io/github/dre2n/dungeonsxl/global/GlobalProtections.java b/src/main/java/io/github/dre2n/dungeonsxl/global/GlobalProtections.java new file mode 100644 index 00000000..f463bf8c --- /dev/null +++ b/src/main/java/io/github/dre2n/dungeonsxl/global/GlobalProtections.java @@ -0,0 +1,236 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package io.github.dre2n.dungeonsxl.global; + +import io.github.dre2n.dungeonsxl.DungeonsXL; +import java.io.File; +import java.util.HashSet; +import java.util.Set; +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; + +/** + * @author Daniel Saukel + */ +public class GlobalProtections { + + protected static DungeonsXL plugin = DungeonsXL.getInstance(); + + private Set protections = new HashSet<>(); + + /** + * @return the protection which covers this location + */ + public GlobalProtection getByLocation(Location location) { + return getByBlock(location.getBlock()); + } + + /** + * @return the protection which covers this block + */ + public GlobalProtection getByBlock(Block block) { + for (GlobalProtection protection : protections) { + if (protection.getBlocks().contains(block)) { + return protection; + } + } + + return null; + } + + /** + * @return the protections + */ + public Set getProtections() { + return protections; + } + + /** + * @param type + * All protections which are an instance of it will be returned. + */ + public Set getProtections(Class type) { + Set protectionsOfType = new HashSet<>(); + for (GlobalProtection protection : protections) { + if (protection.getClass() == type) { + protectionsOfType.add(protection); + } + } + return protectionsOfType; + } + + /** + * @param protection + * the protection type to add + */ + public void addProtection(GlobalProtection protection) { + protections.add(protection); + } + + /** + * @param protection + * the protection to remove + */ + public void removeProtection(GlobalProtection protection) { + protections.remove(protection); + } + + /** + * Save all protections to the default file + */ + public void saveAll() { + saveAll(plugin.getDataConfig().getConfig()); + } + + /** + * @param file + * the file to save all protections to + */ + public void saveAll(File file) { + saveAll(YamlConfiguration.loadConfiguration(file)); + } + + /** + * @param config + * the config to save all protections to + */ + public void saveAll(FileConfiguration config) { + for (GlobalProtection protection : protections) { + protection.save(config); + } + + plugin.getDataConfig().save(); + } + + /** + * @param type + * Each type is stored seperately. + * @param world + * Each world has its own IDs. + * @return an unused ID number for a new protection + */ + public int generateId(Class type, World world) { + int id = 1; + for (GlobalProtection protection : protections) { + if (protection.getClass() == type) { + id++; + } + } + return id; + } + + /** + * @param block + * the block to check + */ + public boolean isProtectedBlock(Block block) { + for (GlobalProtection protection : protections) { + if (protection.getBlocks().contains(block)) { + return true; + } + } + + return false; + } + + /* SUBJECT TO CHANGE */ + @Deprecated + public void loadAll() { + FileConfiguration data = plugin.getDataConfig().getConfig(); + + for (World world : plugin.getServer().getWorlds()) { + // GameSigns + if (data.contains("gamesign." + world.getName())) { + int id = 0; + String preString; + + do { + id++; + preString = "gamesign." + world.getName() + "." + id + "."; + if (data.contains(preString)) { + String mapName = data.getString(preString + ".dungeon"); + int maxGames = data.getInt(preString + ".maxGames"); + int maxGroupsPerGame = data.getInt(preString + ".maxGroupsPerGame"); + boolean multiFloor = false; + if (data.contains(preString + ".multiFloor")) { + multiFloor = data.getBoolean(preString + ".multiFloor"); + } + Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z")); + + new GameSign(id, startSign, mapName, maxGames, maxGroupsPerGame, multiFloor); + } + + } while (data.contains(preString)); + } + + // GroupSigns + if (data.contains("groupsign." + world.getName())) { + int id = 0; + String preString; + + do { + id++; + preString = "groupsign." + world.getName() + "." + id + "."; + if (data.contains(preString)) { + String mapName = data.getString(preString + ".dungeon"); + int maxGroups = data.getInt(preString + ".maxGroups"); + int maxPlayersPerGroup = data.getInt(preString + ".maxPlayersPerGroup"); + boolean multiFloor = false; + if (data.contains(preString + ".multiFloor")) { + multiFloor = data.getBoolean(preString + ".multiFloor"); + } + Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z")); + + new GroupSign(id, startSign, mapName, maxGroups, maxPlayersPerGroup, multiFloor); + } + } while (data.contains(preString)); + } + + if (data.contains("leavesign." + world.getName())) { + + int id = 0; + String preString; + + do { + id++; + preString = "leavesign." + 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(id, sign); + } + } + + } while (data.contains(preString)); + } + + // DPortals + if (data.contains("portal." + world.getName())) { + int id = 0; + String preString; + + do { + id++; + preString = "portal." + 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")); + DPortal dPortal = new DPortal(id, block1, block2, true); + dPortal.create(); + } + + } while (data.contains(preString)); + } + } + } + +} diff --git a/src/main/java/io/github/dre2n/dungeonsxl/global/GroupSign.java b/src/main/java/io/github/dre2n/dungeonsxl/global/GroupSign.java index cb98a793..dbb7054a 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/global/GroupSign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/global/GroupSign.java @@ -16,6 +16,7 @@ */ package io.github.dre2n.dungeonsxl.global; +import io.github.dre2n.commons.util.BlockUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.MessageConfig.Messages; @@ -24,6 +25,8 @@ import io.github.dre2n.dungeonsxl.dungeon.Dungeon; import io.github.dre2n.dungeonsxl.game.GameWorld; import io.github.dre2n.dungeonsxl.player.DGroup; import java.io.File; +import java.util.HashSet; +import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -36,9 +39,10 @@ import org.bukkit.entity.Player; /** * @author Frank Baumann, Milan Albrecht, Daniel Saukel */ -public class GroupSign { +public class GroupSign extends GlobalProtection { protected static DungeonsXL plugin = DungeonsXL.getInstance(); + protected static GlobalProtections protections = plugin.getGlobalProtections(); // Sign Labels public static final String IS_PLAYING = ChatColor.DARK_RED + "Is Playing"; @@ -55,9 +59,10 @@ public class GroupSign { private Block startSign; private int directionX = 0, directionZ = 0; private int verticalSigns; + private Set blocks; - public GroupSign(Block startSign, String identifier, int maxGroups, int maxPlayersPerGroup, boolean multiFloor) { - plugin.getGroupSigns().add(this); + public GroupSign(int id, Block startSign, String identifier, int maxGroups, int maxPlayersPerGroup, boolean multiFloor) { + super(startSign.getWorld(), id); this.startSign = startSign; dGroups = new DGroup[maxGroups]; @@ -83,6 +88,36 @@ public class GroupSign { update(); } + /** + * @return the dGroups + */ + public DGroup[] getDGroups() { + return dGroups; + } + + /** + * @param dGroups + * the dGroups to set + */ + public void setDGroups(DGroup[] dGroups) { + this.dGroups = dGroups; + } + + /** + * @return the multiFloor + */ + public boolean isMultiFloor() { + return multiFloor; + } + + /** + * @param multiFloor + * the multiFloor to set + */ + public void setMultiFloor(boolean multiFloor) { + this.multiFloor = multiFloor; + } + /** * @return the dungeonName */ @@ -114,24 +149,23 @@ public class GroupSign { } /** - * @return the multiFloor + * @return the maximum player count per group */ - public boolean isMultiFloor() { - return multiFloor; + public int getMaxPlayersPerGroup() { + return maxPlayersPerGroup; } /** - * @param multiFloor - * the multiFloor to set + * @param maxPlayersPerGroup + * the maximum player count per group to set */ - public void setMultiFloor(boolean multiFloor) { - this.multiFloor = multiFloor; - } - - public void delete() { - plugin.getGroupSigns().remove(this); + public void setMaxPlayerPerGroup(int maxPlayersPerGroup) { + this.maxPlayersPerGroup = maxPlayersPerGroup; } + /** + * Update this group sign to show the group(s) correctly. + */ public void update() { int i = 0; for (DGroup dGroup : dGroups) { @@ -198,7 +232,121 @@ public class GroupSign { } } + @Override + public Set getBlocks() { + if (blocks == null) { + blocks = new HashSet<>(); + + int i = dGroups.length; + do { + i--; + + blocks.add(startSign.getRelative(i * directionX, 0, i * directionZ)); + + } while (i >= 0); + + HashSet toAdd = new HashSet<>(); + for (Block block : blocks) { + i = verticalSigns; + do { + i--; + + Block beneath = block.getLocation().add(0, -1 * i, 0).getBlock(); + toAdd.add(beneath); + toAdd.add(BlockUtil.getAttachedBlock(beneath)); + + } while (i >= 0); + } + blocks.addAll(toAdd); + } + + return blocks; + } + + @Override + public void save(FileConfiguration config) { + String preString = "groupsign." + getWorld().getName() + "." + getId(); + + config.set(preString + ".x", startSign.getX()); + config.set(preString + ".y", startSign.getY()); + config.set(preString + ".z", startSign.getZ()); + + if (isMultiFloor()) { + config.set(preString + ".dungeon", dungeonName); + + } else { + config.set(preString + ".dungeon", mapName); + } + + config.set(preString + ".maxGroups", dGroups.length); + config.set(preString + ".maxPlayersPerGroup", maxPlayersPerGroup); + config.set(preString + ".multiFloor", isMultiFloor()); + } + /* Statics */ + /** + * @param block + * a block which is protected by the returned GroupSign + */ + public static GroupSign getByBlock(Block block) { + if (!(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) { + return null; + } + + int x = block.getX(), y = block.getY(), z = block.getZ(); + for (GlobalProtection protection : protections.getProtections(GroupSign.class)) { + GroupSign groupSign = (GroupSign) protection; + + int sx1 = groupSign.startSign.getX(), sy1 = groupSign.startSign.getY(), sz1 = groupSign.startSign.getZ(); + int sx2 = sx1 + (groupSign.dGroups.length - 1) * groupSign.directionX; + int sy2 = sy1 - groupSign.verticalSigns + 1; + int sz2 = sz1 + (groupSign.dGroups.length - 1) * groupSign.directionZ; + + if (sx1 > sx2) { + if (x < sx2 || x > sx1) { + continue; + } + + } else if (sx1 < sx2) { + if (x > sx2 || x < sx1) { + continue; + } + + } else if (x != sx1) { + continue; + } + + if (sy1 > sy2) { + if (y < sy2 || y > sy1) { + continue; + } + + } else if (y != sy1) { + continue; + } + + if (sz1 > sz2) { + if (z < sz2 || z > sz1) { + continue; + } + + } else if (sz1 < sz2) { + if (z > sz2 || z < sz1) { + continue; + } + + } else if (z != sz1) { + continue; + } + + return groupSign; + } + + return null; + } + + /* SUBJECT TO CHANGE */ + @Deprecated public static GroupSign tryToCreate(Block startSign, String mapName, int maxGroups, int maxPlayersPerGroup, boolean multiFloor) { World world = startSign.getWorld(); int direction = startSign.getData(); @@ -295,94 +443,15 @@ public class GroupSign { block.setTypeIdAndData(68, startSign.getData(), true); } - GroupSign sign = new GroupSign(startSign, mapName, maxGroups, maxPlayersPerGroup, multiFloor); + GroupSign sign = new GroupSign(protections.generateId(GroupSign.class, world), startSign, mapName, maxGroups, maxPlayersPerGroup, multiFloor); return sign; } - public static boolean isRelativeSign(Block block, int x, int z) { - GroupSign groupSign = getSign(block.getRelative(x, 0, z)); - if (groupSign == null) { - return false; - } - - if (x == -1 && groupSign.startSign.getData() == 4) { - return true; - } - - if (x == 1 && groupSign.startSign.getData() == 5) { - return true; - } - - if (z == -1 && groupSign.startSign.getData() == 2) { - return true; - } - - if (z == 1 && groupSign.startSign.getData() == 3) { - return true; - } - - return false; - } - - public static GroupSign getSign(Block block) { - if (!(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) { - return null; - } - - int x = block.getX(), y = block.getY(), z = block.getZ(); - for (GroupSign groupSign : plugin.getGroupSigns()) { - int sx1 = groupSign.startSign.getX(), sy1 = groupSign.startSign.getY(), sz1 = groupSign.startSign.getZ(); - int sx2 = sx1 + (groupSign.dGroups.length - 1) * groupSign.directionX; - int sy2 = sy1 - groupSign.verticalSigns + 1; - int sz2 = sz1 + (groupSign.dGroups.length - 1) * groupSign.directionZ; - - if (sx1 > sx2) { - if (x < sx2 || x > sx1) { - continue; - } - - } else if (sx1 < sx2) { - if (x > sx2 || x < sx1) { - continue; - } - - } else if (x != sx1) { - continue; - } - - if (sy1 > sy2) { - if (y < sy2 || y > sy1) { - continue; - } - - } else if (y != sy1) { - continue; - } - - if (sz1 > sz2) { - if (z < sz2 || z > sz1) { - continue; - } - - } else if (sz1 < sz2) { - if (z > sz2 || z < sz1) { - continue; - } - - } else if (z != sz1) { - continue; - } - - return groupSign; - } - - return null; - } - + @Deprecated public static boolean playerInteract(Block block, Player player) { int x = block.getX(), y = block.getY(), z = block.getZ(); - GroupSign groupSign = getSign(block); + GroupSign groupSign = getByBlock(block); if (groupSign == null) { return false; @@ -445,9 +514,11 @@ public class GroupSign { return true; } + @Deprecated public static void updatePerGroup(DGroup dGroupSearch) { + for (GlobalProtection protection : protections.getProtections(GroupSign.class)) { + GroupSign groupSign = (GroupSign) protection; - for (GroupSign groupSign : plugin.getGroupSigns()) { int i = 0; for (DGroup dGroup : groupSign.dGroups) { if (dGroup == null) { @@ -466,6 +537,7 @@ public class GroupSign { } } + @Deprecated public static int[] getDirection(byte data) { int[] direction = new int[2]; @@ -489,58 +561,4 @@ public class GroupSign { return direction; } - // Save and Load - public static void save(FileConfiguration configFile) { - int id = 0; - - for (GroupSign groupSign : plugin.getGroupSigns()) { - id++; - String preString = "groupsign." + groupSign.startSign.getWorld().getName() + "." + id; - - // Location - configFile.set(preString + ".x", groupSign.startSign.getX()); - configFile.set(preString + ".y", groupSign.startSign.getY()); - configFile.set(preString + ".z", groupSign.startSign.getZ()); - - // Etc. - if (groupSign.isMultiFloor()) { - configFile.set(preString + ".dungeon", groupSign.dungeonName); - - } else { - configFile.set(preString + ".dungeon", groupSign.mapName); - } - - configFile.set(preString + ".maxGroups", groupSign.dGroups.length); - configFile.set(preString + ".maxPlayersPerGroup", groupSign.maxPlayersPerGroup); - configFile.set(preString + ".multiFloor", groupSign.isMultiFloor()); - } - } - - public static void load(FileConfiguration configFile) { - for (World world : plugin.getServer().getWorlds()) { - if (!configFile.contains("groupsign." + world.getName())) { - continue; - } - - int id = 0; - String preString; - do { - id++; - preString = "groupsign." + world.getName() + "." + id + "."; - if (configFile.contains(preString)) { - String mapName = configFile.getString(preString + ".dungeon"); - int maxGroups = configFile.getInt(preString + ".maxGroups"); - int maxPlayersPerGroup = configFile.getInt(preString + ".maxPlayersPerGroup"); - boolean multiFloor = false; - if (configFile.contains(preString + ".multiFloor")) { - multiFloor = configFile.getBoolean(preString + ".multiFloor"); - } - Block startSign = world.getBlockAt(configFile.getInt(preString + ".x"), configFile.getInt(preString + ".y"), configFile.getInt(preString + ".z")); - - new GroupSign(startSign, mapName, maxGroups, maxPlayersPerGroup, multiFloor); - } - } while (configFile.contains(preString)); - } - } - } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/global/LeaveSign.java b/src/main/java/io/github/dre2n/dungeonsxl/global/LeaveSign.java index db08277a..f1a641ac 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/global/LeaveSign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/global/LeaveSign.java @@ -16,14 +16,15 @@ */ package io.github.dre2n.dungeonsxl.global; +import io.github.dre2n.commons.util.BlockUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.MessageConfig.Messages; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.player.DPlayer; +import java.util.HashSet; +import java.util.Set; import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.configuration.file.FileConfiguration; @@ -32,14 +33,15 @@ import org.bukkit.entity.Player; /** * @author Frank Baumann */ -public class LeaveSign { +public class LeaveSign extends GlobalProtection { protected static DungeonsXL plugin = DungeonsXL.getInstance(); private Sign sign; + private Set blocks; - public LeaveSign(Sign sign) { - plugin.getLeaveSigns().add(this); + public LeaveSign(int id, Sign sign) { + super(sign.getWorld(), id); this.sign = sign; setText(); @@ -53,13 +55,48 @@ public class LeaveSign { sign.update(); } - public void delete() { - plugin.getLeaveSigns().remove(this); + @Override + public Set getBlocks() { + if (blocks == null) { + blocks = new HashSet<>(); + + blocks.add(sign.getBlock()); + blocks.add(BlockUtil.getAttachedBlock(sign.getBlock())); + } + + return blocks; } + @Override + public void save(FileConfiguration config) { + String preString = "leavesign." + sign.getWorld().getName() + "." + getId(); + config.set(preString + ".x", sign.getX()); + config.set(preString + ".y", sign.getY()); + config.set(preString + ".z", sign.getZ()); + } + + /* Statics */ + /** + * @param block + * a block which is protected by the returned LeaveSign + */ + public static LeaveSign getByBlock(Block block) { + for (GlobalProtection protection : plugin.getGlobalProtections().getProtections(LeaveSign.class)) { + LeaveSign leaveSign = (LeaveSign) protection; + + if (leaveSign.getBlocks().contains(block)) { + return leaveSign; + } + } + + return null; + } + + /* SUBJECT TO CHANGE */ + @Deprecated public static boolean playerInteract(Block block, Player player) { - LeaveSign leaveSign = getSign(block); + LeaveSign leaveSign = getByBlock(block); if (leaveSign == null) { return false; @@ -75,7 +112,7 @@ public class LeaveSign { DGroup dgroup = DGroup.getByPlayer(player); if (dgroup != null) { dgroup.removePlayer(player); - MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.PLAYER_LEAVE_GROUP));// ChatColor.YELLOW+"Du hast deine Gruppe erfolgreich verlassen!"); + MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.PLAYER_LEAVE_GROUP)); return true; } } @@ -83,83 +120,4 @@ public class LeaveSign { return false; } - public static boolean isRelativeSign(Block block, int x, int z) { - LeaveSign leaveSign = getSign(block.getRelative(x, 0, z)); - if (leaveSign == null) { - return false; - } - - if (x == -1 && leaveSign.sign.getData().getData() == 4) { - return true; - } - - if (x == 1 && leaveSign.sign.getData().getData() == 5) { - return true; - } - - if (z == -1 && leaveSign.sign.getData().getData() == 2) { - return true; - } - - if (z == 1 && leaveSign.sign.getData().getData() == 3) { - return true; - } - - return false; - } - - public static LeaveSign getSign(Block block) { - if (!(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) { - return null; - } - - for (LeaveSign leavesign : plugin.getLeaveSigns()) { - if (block.getWorld() != leavesign.sign.getWorld()) { - continue; - } - - if (block.getLocation().distance(leavesign.sign.getBlock().getLocation()) < 1) { - return leavesign; - } - } - - return null; - } - - // Save and Load - public static void save(FileConfiguration configFile) { - int id = 0; - for (LeaveSign leaveSign : plugin.getLeaveSigns()) { - id++; - String preString = "leavesign." + leaveSign.sign.getWorld().getName() + "." + id; - configFile.set(preString + ".x", leaveSign.sign.getX()); - configFile.set(preString + ".y", leaveSign.sign.getY()); - configFile.set(preString + ".z", leaveSign.sign.getZ()); - } - } - - public static void load(FileConfiguration configFile) { - for (World world : plugin.getServer().getWorlds()) { - if (!configFile.contains("leavesign." + world.getName())) { - continue; - } - - int id = 0; - String preString; - - do { - id++; - preString = "leavesign." + world.getName() + "." + id + "."; - if (configFile.contains(preString)) { - Block block = world.getBlockAt(configFile.getInt(preString + ".x"), configFile.getInt(preString + ".y"), configFile.getInt(preString + ".z")); - if (block.getState() instanceof Sign) { - Sign sign = (Sign) block.getState(); - new LeaveSign(sign); - } - } - - } while (configFile.contains(preString)); - } - } - } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/listener/BlockListener.java b/src/main/java/io/github/dre2n/dungeonsxl/listener/BlockListener.java index d1038208..920260d5 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/listener/BlockListener.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/listener/BlockListener.java @@ -26,8 +26,12 @@ import io.github.dre2n.dungeonsxl.game.GameType; import io.github.dre2n.dungeonsxl.game.GameTypeDefault; import io.github.dre2n.dungeonsxl.game.GameWorld; import io.github.dre2n.dungeonsxl.global.DPortal; +import io.github.dre2n.dungeonsxl.global.GameSign; +import io.github.dre2n.dungeonsxl.global.GlobalProtection; import io.github.dre2n.dungeonsxl.global.GroupSign; import io.github.dre2n.dungeonsxl.global.LeaveSign; +import io.github.dre2n.dungeonsxl.player.DGlobalPlayer; +import io.github.dre2n.dungeonsxl.player.DPlayers; import io.github.dre2n.dungeonsxl.sign.DSign; import io.github.dre2n.dungeonsxl.task.RedstoneEventTask; import org.bukkit.Location; @@ -51,6 +55,7 @@ import org.bukkit.event.block.SignChangeEvent; public class BlockListener implements Listener { protected static DungeonsXL plugin = DungeonsXL.getInstance(); + protected static DPlayers dPlayers = plugin.getDPlayers(); @EventHandler(priority = EventPriority.HIGH) public void onPhysics(BlockPhysicsEvent event) { @@ -67,33 +72,15 @@ public class BlockListener implements Listener { public void onBreak(BlockBreakEvent event) { Block block = event.getBlock(); Player player = event.getPlayer(); + DGlobalPlayer dGlobalPlayer = dPlayers.getByPlayer(player); - // Deny DPortal destroying - if (block.getType() == Material.PORTAL) { - DPortal dPortal = DPortal.getByBlock(event.getBlock()); - if (dPortal != null) { - if (plugin.getInBreakMode().contains(player)) { - dPortal.delete(); - MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.PLAYER_PROTECTED_BLOCK_DELETED)); - MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.CMD_BREAK_PROTECTED_MODE)); - plugin.getInBreakMode().remove(player); - - } else { - event.setCancelled(true); - } - - return; - } - } - - // Delete GroupSign - GroupSign groupSign = GroupSign.getSign(block); - if (groupSign != null) { - if (plugin.getInBreakMode().contains(player)) { - groupSign.delete(); + GlobalProtection protection = plugin.getGlobalProtections().getByBlock(event.getBlock()); + if (protection != null) { + if (dGlobalPlayer.isInBreakMode()) { + protection.delete(); MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.PLAYER_PROTECTED_BLOCK_DELETED)); MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.CMD_BREAK_PROTECTED_MODE)); - plugin.getInBreakMode().remove(player); + dGlobalPlayer.setInBreakMode(false); } else { event.setCancelled(true); @@ -102,34 +89,6 @@ public class BlockListener implements Listener { return; } - // Deny DGSignblocks destroying - if (GroupSign.isRelativeSign(block, 1, 0) || GroupSign.isRelativeSign(block, -1, 0) || GroupSign.isRelativeSign(block, 0, 1) || GroupSign.isRelativeSign(block, 0, -1)) { - event.setCancelled(true); - return; - } - - // Delete LeaveSign - LeaveSign leaveSign = LeaveSign.getSign(block); - if (leaveSign != null) { - if (plugin.getInBreakMode().contains(player)) { - leaveSign.delete(); - MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.PLAYER_PROTECTED_BLOCK_DELETED)); - MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(Messages.CMD_BREAK_PROTECTED_MODE)); - plugin.getInBreakMode().remove(player); - - } else { - event.setCancelled(true); - } - - return; - } - - // Deny LeaveSignblocks destroying - if (LeaveSign.isRelativeSign(block, 1, 0) || LeaveSign.isRelativeSign(block, -1, 0) || LeaveSign.isRelativeSign(block, 0, 1) || LeaveSign.isRelativeSign(block, 0, -1)) { - event.setCancelled(true); - return; - } - // Editworld Signs EditWorld editWorld = EditWorld.getByWorld(block.getWorld()); if (editWorld != null) { @@ -155,7 +114,6 @@ public class BlockListener implements Listener { } } } - } @EventHandler(priority = EventPriority.HIGH) @@ -184,6 +142,7 @@ public class BlockListener implements Listener { } } } + event.setCancelled(true); } @@ -204,13 +163,13 @@ public class BlockListener implements Listener { return; } - if (lines[1].equalsIgnoreCase("Group")) { + if (lines[1].equalsIgnoreCase("Game") || lines[1].equalsIgnoreCase("Group")) { String dungeonName = lines[2]; String[] data = lines[3].split("\\,"); if (data.length >= 2 && data.length <= 3) { - int maxGroups = NumberUtil.parseInt(data[0]); - int maxPlayersPerGroup = NumberUtil.parseInt(data[1]); + int maxObjects = NumberUtil.parseInt(data[0]); + int maxMembersPerObject = NumberUtil.parseInt(data[1]); boolean multiFloor = false; if (data.length == 3) { if (data[2].equals("+")) { @@ -218,9 +177,16 @@ public class BlockListener implements Listener { } } - if (maxGroups > 0 && maxPlayersPerGroup > 0) { - if (GroupSign.tryToCreate(event.getBlock(), dungeonName, maxGroups, maxPlayersPerGroup, multiFloor) != null) { - event.setCancelled(true); + if (maxObjects > 0 && maxMembersPerObject > 0) { + if (lines[1].equalsIgnoreCase("Game")) { + if (GameSign.tryToCreate(event.getBlock(), dungeonName, maxObjects, maxMembersPerObject, multiFloor) != null) { + event.setCancelled(true); + } + + } else if (lines[1].equalsIgnoreCase("Group")) { + if (GroupSign.tryToCreate(event.getBlock(), dungeonName, maxObjects, maxMembersPerObject, multiFloor) != null) { + event.setCancelled(true); + } } } } @@ -228,7 +194,7 @@ public class BlockListener implements Listener { } else if (lines[1].equalsIgnoreCase("Leave")) { if (block.getState() instanceof Sign) { Sign sign = (Sign) block.getState(); - new LeaveSign(sign); + new LeaveSign(plugin.getGlobalProtections().generateId(LeaveSign.class, sign.getWorld()), sign); } event.setCancelled(true); @@ -283,7 +249,6 @@ public class BlockListener implements Listener { if (editWorld != null) { event.setCancelled(true); } - } @EventHandler(priority = EventPriority.NORMAL) diff --git a/src/main/java/io/github/dre2n/dungeonsxl/listener/EntityListener.java b/src/main/java/io/github/dre2n/dungeonsxl/listener/EntityListener.java index fe100229..2a2e0109 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/listener/EntityListener.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/listener/EntityListener.java @@ -16,14 +16,13 @@ */ package io.github.dre2n.dungeonsxl.listener; +import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.WorldConfig; import io.github.dre2n.dungeonsxl.dungeon.EditWorld; import io.github.dre2n.dungeonsxl.game.Game; import io.github.dre2n.dungeonsxl.game.GameType; import io.github.dre2n.dungeonsxl.game.GameTypeDefault; import io.github.dre2n.dungeonsxl.game.GameWorld; -import io.github.dre2n.dungeonsxl.global.DPortal; -import io.github.dre2n.dungeonsxl.global.GroupSign; import io.github.dre2n.dungeonsxl.mob.DMob; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.player.DPlayer; @@ -246,7 +245,7 @@ public class EntityListener implements Listener { if (gameWorld != null) { if (event.getEntity() instanceof LivingEntity) { - // Disable Creeper explosions in gameditWorlds + // Disable Creeper explosions in gameWorlds event.setCancelled(true); return; @@ -254,26 +253,13 @@ public class EntityListener implements Listener { // Disable drops from TNT event.setYield(0); } - } - // Prevent Portal and Sign Destroying + // Prevent GlobalProtection destroying List blocklist = event.blockList(); for (Block block : blocklist) { - // Portals - if (block.getType() == Material.PORTAL) { - if (DPortal.getByBlock(block) != null) { - event.setCancelled(true); - return; - } - } - - // Signs - if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) { - if (GroupSign.getSign(block) != null) { - event.setCancelled(true); - return; - } + if (DungeonsXL.getInstance().getGlobalProtections().isProtectedBlock(block)) { + event.setCancelled(true); } } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java b/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java index cba335b2..1140bc19 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java @@ -30,10 +30,13 @@ import io.github.dre2n.dungeonsxl.game.GameChest; import io.github.dre2n.dungeonsxl.game.GameWorld; import io.github.dre2n.dungeonsxl.global.DPortal; import io.github.dre2n.dungeonsxl.global.GameSign; +import io.github.dre2n.dungeonsxl.global.GlobalProtection; import io.github.dre2n.dungeonsxl.global.GroupSign; import io.github.dre2n.dungeonsxl.global.LeaveSign; +import io.github.dre2n.dungeonsxl.player.DGlobalPlayer; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.player.DPlayer; +import io.github.dre2n.dungeonsxl.player.DPlayers; import io.github.dre2n.dungeonsxl.task.RespawnTask; import io.github.dre2n.dungeonsxl.trigger.InteractTrigger; import io.github.dre2n.dungeonsxl.trigger.UseItemTrigger; @@ -71,6 +74,7 @@ public class PlayerListener implements Listener { protected static DungeonsXL plugin = DungeonsXL.getInstance(); protected static MessageConfig messageConfig = plugin.getMessageConfig(); + protected static DPlayers dPlayers = plugin.getDPlayers(); @EventHandler(priority = EventPriority.HIGH) public void onDeath(PlayerDeathEvent event) { @@ -138,9 +142,10 @@ public class PlayerListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onInteract(PlayerInteractEvent event) { Player player = event.getPlayer(); + DGlobalPlayer dGlobalPlayer = dPlayers.getByPlayer(player); Block clickedBlock = event.getClickedBlock(); - if (plugin.getInBreakMode().contains(player)) { + if (dGlobalPlayer.isInBreakMode()) { return; } @@ -179,22 +184,26 @@ public class PlayerListener implements Listener { // Check Portals if (event.getItem() != null) { ItemStack item = event.getItem(); - if (item.getType() == Material.WOOD_SWORD) { - if (clickedBlock != null) { - for (DPortal dportal : plugin.getDPortals()) { - if (!dportal.isActive()) { - if (dportal.getPlayer() == player) { - if (dportal.getBlock1() == null) { - dportal.setBlock1(event.getClickedBlock()); - MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.PLAYER_PORTAL_PROGRESS)); - } else if (dportal.getBlock2() == null) { - dportal.setBlock2(event.getClickedBlock()); - dportal.setActive(true); - dportal.create(); - MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.PLAYER_PORTAL_CREATED)); + if (dGlobalPlayer.isCreatingPortal()) { + if (item.getType() == Material.WOOD_SWORD) { + if (clickedBlock != null) { + for (GlobalProtection protection : plugin.getGlobalProtections().getProtections(DPortal.class)) { + DPortal dPortal = (DPortal) protection; + if (!dPortal.isActive()) { + if (dPortal == dGlobalPlayer.getPortal()) { + if (dPortal.getBlock1() == null) { + dPortal.setBlock1(event.getClickedBlock()); + MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.PLAYER_PORTAL_PROGRESS)); + + } else if (dPortal.getBlock2() == null) { + dPortal.setBlock2(event.getClickedBlock()); + dPortal.setActive(true); + dPortal.create(); + MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.PLAYER_PORTAL_CREATED)); + } + event.setCancelled(true); } - event.setCancelled(true); } } } @@ -397,11 +406,11 @@ public class PlayerListener implements Listener { public void onPortal(PlayerPortalEvent event) { Player player = event.getPlayer(); Location location = event.getFrom(); - DPortal dportal = DPortal.getByLocation(location); + DPortal dPortal = DPortal.getByLocation(location); - if (dportal != null) { + if (dPortal != null) { event.setCancelled(true); - dportal.teleport(player); + dPortal.teleport(player); } } @@ -443,6 +452,7 @@ public class PlayerListener implements Listener { DPlayer dPlayer = DPlayer.getByPlayer(player); if (dPlayer == null) { + dPlayers.removePlayer(dPlayer); return; } @@ -473,6 +483,8 @@ public class PlayerListener implements Listener { public void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); + new DGlobalPlayer(player); + // Check dPlayers DPlayer dPlayer = DPlayer.getByName(player.getName()); if (dPlayer != null) { diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DEditPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DEditPlayer.java new file mode 100644 index 00000000..1f87880a --- /dev/null +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DEditPlayer.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2016 Daniel Saukel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.dre2n.dungeonsxl.player; + +/** + * Represents a player in an EditWorld. + * + * @author Daniel Saukel + */ +public class DEditPlayer extends DGlobalPlayer { + + public DEditPlayer(DGlobalPlayer player) { + super(player.getPlayer()); + } + +} diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java new file mode 100644 index 00000000..c43ab87c --- /dev/null +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2016 Daniel Saukel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.dre2n.dungeonsxl.player; + +import io.github.dre2n.dungeonsxl.DungeonsXL; +import io.github.dre2n.dungeonsxl.global.DPortal; +import org.bukkit.entity.Player; + +/** + * Represents a player in the non-DXL worlds of the server. + * + * @author Daniel Saukel + */ +public class DGlobalPlayer { + + protected Player player; + + private boolean breakMode; + private boolean chatSpyMode; + private DPortal creatingPortal; + + public DGlobalPlayer(Player player) { + this.player = player; + + DungeonsXL.getInstance().getDPlayers().addPlayer(this); + } + + /** + * @return the Bukkit player + */ + public Player getPlayer() { + return player; + } + + /** + * @return if the player is in break mode + */ + public boolean isInBreakMode() { + return breakMode; + } + + /** + * @param breakMode + * sets if the player is in break mode + */ + public void setInBreakMode(boolean breakMode) { + this.breakMode = breakMode; + } + + /** + * @return if the player spies the DXL chat channels + */ + public boolean isInChatSpyMode() { + return chatSpyMode; + } + + /** + * @param chatSpyMode + * sets if the player is in chat spy mode + */ + public void setInChatSpyMode(boolean chatSpyMode) { + this.chatSpyMode = chatSpyMode; + } + + /** + * @return if the player is creating a DPortal + */ + public boolean isCreatingPortal() { + return creatingPortal != null; + } + + /** + * @return the portal the player is creating + */ + public DPortal getPortal() { + return creatingPortal; + } + + /** + * @param dPortal + * the portal to create + */ + public void setCreatingPortal(DPortal dPortal) { + creatingPortal = dPortal; + } + +} diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java index 84ea2899..72bb575d 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java @@ -23,6 +23,7 @@ import io.github.dre2n.dungeonsxl.config.MessageConfig; import io.github.dre2n.dungeonsxl.config.MessageConfig.Messages; import io.github.dre2n.dungeonsxl.config.WorldConfig; import io.github.dre2n.dungeonsxl.dungeon.Dungeon; +import io.github.dre2n.dungeonsxl.event.dgroup.DGroupDisbandEvent; import io.github.dre2n.dungeonsxl.event.dgroup.DGroupStartFloorEvent; import io.github.dre2n.dungeonsxl.event.requirement.RequirementDemandEvent; import io.github.dre2n.dungeonsxl.event.reward.RewardAdditionEvent; @@ -30,6 +31,7 @@ import io.github.dre2n.dungeonsxl.game.Game; import io.github.dre2n.dungeonsxl.game.GameType; import io.github.dre2n.dungeonsxl.game.GameTypeDefault; import io.github.dre2n.dungeonsxl.game.GameWorld; +import io.github.dre2n.dungeonsxl.global.GameSign; import io.github.dre2n.dungeonsxl.global.GroupSign; import io.github.dre2n.dungeonsxl.requirement.Requirement; import io.github.dre2n.dungeonsxl.reward.Reward; @@ -195,7 +197,11 @@ public class DGroup { // Check group if (isEmpty()) { - remove(); + DGroupDisbandEvent event = new DGroupDisbandEvent(this, player, DGroupDisbandEvent.Cause.GROUP_IS_EMPTY); + + if (!event.isCancelled()) { + delete(); + } } } @@ -451,8 +457,15 @@ public class DGroup { return players.isEmpty(); } - public void remove() { + /** + * Remove the group from the List + */ + public void delete() { plugin.getDGroups().remove(this); + if (Game.getByDGroup(this) != null){ + Game.getByDGroup(this).removeDGroup(this); + } + GameSign.updatePerGame(Game.getByDGroup(this)); GroupSign.updatePerGroup(this); } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java index 4c3e8074..6cec8599 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java @@ -61,15 +61,16 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; /** + * Represents a player in a GameWorld. + * * @author Frank Baumann, Tobias Schmitz, Milan Albrecht, Daniel Saukel */ -public class DPlayer { +public class DPlayer extends DGlobalPlayer { protected static DungeonsXL plugin = DungeonsXL.getInstance(); protected static MessageConfig messageConfig = plugin.getMessageConfig(); // Variables - private Player player; private World world; private DSavePlayer savePlayer; @@ -94,14 +95,13 @@ public class DPlayer { private int lives; public DPlayer(Player player, World world, Location teleport, boolean editing) { - plugin.getDPlayers().add(this); + super(player); - this.setPlayer(player); this.world = world; double health = ((Damageable) player).getHealth(); - if (CompatibilityHandler.getInstance().getVersion() != Version.MC1_9) { + if (!Version.andHigher(Version.MC1_9).contains(CompatibilityHandler.getInstance().getVersion())) { savePlayer = new DSavePlayer(player.getName(), player.getUniqueId(), player.getLocation(), player.getInventory().getContents(), player.getInventory().getArmorContents(), null, player.getLevel(), player.getTotalExperience(), (int) health, player.getFoodLevel(), player.getFireTicks(), player.getGameMode(), player.getActivePotionEffects()); @@ -143,14 +143,7 @@ public class DPlayer { } } - // Getters and setters - /** - * @return the player - */ - public Player getPlayer() { - return player; - } - + /* Getters and setters */ /** * @param player * the player to set @@ -193,7 +186,7 @@ public class DPlayer { * @return if the player is in test mode */ public boolean isInTestMode() { - DGroup dGroup = DGroup.getByPlayer(player); + DGroup dGroup = DGroup.getByPlayer(getPlayer()); if (dGroup == null) { return false; } @@ -216,17 +209,12 @@ public class DPlayer { return false; } - /** - * @return the editing - */ + @Deprecated public boolean isEditing() { return editing; } - /** - * @param editing - * the editing to set - */ + @Deprecated public void setEditing(boolean editing) { this.editing = editing; } @@ -543,7 +531,7 @@ public class DPlayer { if (finished) { if (gameWorld.getGame() != null) { for (Reward reward : gameWorld.getConfig().getRewards()) { - reward.giveTo(player); + reward.giveTo(getPlayer()); } } @@ -607,7 +595,7 @@ public class DPlayer { } while (groupPlayer == null); } - if (dGroup.getCaptain().equals(player) && dGroup.getPlayers().size() > 0) { + if (dGroup.getCaptain().equals(getPlayer()) && dGroup.getPlayers().size() > 0) { // Captain here! Player newCaptain = dGroup.getPlayers().get(0); dGroup.setCaptain(newCaptain); @@ -808,18 +796,22 @@ public class DPlayer { if (editing) { EditWorld editWorld = EditWorld.getByWorld(world); editWorld.sendMessage(message); - for (Player player : plugin.getChatSpyers()) { - if (!editWorld.getWorld().getPlayers().contains(player)) { - MessageUtil.sendMessage(player, ChatColor.GREEN + "[Chatspy] " + ChatColor.WHITE + message); + for (DGlobalPlayer player : plugin.getDPlayers().getPlayers()) { + if (player.isInChatSpyMode()) { + if (!editWorld.getWorld().getPlayers().contains(player.getPlayer())) { + MessageUtil.sendMessage(player.getPlayer(), ChatColor.GREEN + "[Chatspy] " + ChatColor.WHITE + message); + } } } } else { GameWorld gameWorld = GameWorld.getByWorld(world); gameWorld.sendMessage(message); - for (Player player : plugin.getChatSpyers()) { - if (!gameWorld.getWorld().getPlayers().contains(player)) { - MessageUtil.sendMessage(player, ChatColor.GREEN + "[Chatspy] " + ChatColor.WHITE + message); + for (DGlobalPlayer player : plugin.getDPlayers().getPlayers()) { + if (player.isInChatSpyMode()) { + if (!gameWorld.getWorld().getPlayers().contains(player.getPlayer())) { + MessageUtil.sendMessage(player.getPlayer(), ChatColor.GREEN + "[Chatspy] " + ChatColor.WHITE + message); + } } } } @@ -965,11 +957,11 @@ public class DPlayer { /* Statics */ public static void remove(DPlayer player) { - plugin.getDPlayers().remove(player); + plugin.getDPlayers().removePlayer(player); } public static DPlayer getByPlayer(Player player) { - for (DPlayer dPlayer : plugin.getDPlayers()) { + for (DPlayer dPlayer : plugin.getDPlayers().getDPlayers()) { if (dPlayer.getPlayer().equals(player)) { return dPlayer; } @@ -978,7 +970,7 @@ public class DPlayer { } public static DPlayer getByName(String name) { - for (DPlayer dPlayer : plugin.getDPlayers()) { + for (DPlayer dPlayer : plugin.getDPlayers().getDPlayers()) { if (dPlayer.getPlayer().getName().equalsIgnoreCase(name)) { return dPlayer; } @@ -989,7 +981,7 @@ public class DPlayer { public static CopyOnWriteArrayList getByWorld(World world) { CopyOnWriteArrayList dPlayers = new CopyOnWriteArrayList<>(); - for (DPlayer dPlayer : plugin.getDPlayers()) { + for (DPlayer dPlayer : plugin.getDPlayers().getDPlayers()) { if (dPlayer.world == world) { dPlayers.add(dPlayer); } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java new file mode 100644 index 00000000..a23a1b9b --- /dev/null +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2016 Daniel Saukel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.dre2n.dungeonsxl.player; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +/** + * @author Daniel Saukel + */ +public class DPlayers { + + private List dGlobalPlayers = new CopyOnWriteArrayList<>(); + private List dSavePlayers = new CopyOnWriteArrayList<>(); + + /** + * @return the DGlobalPlayer which represents the player + */ + public DGlobalPlayer getByPlayer(Player player) { + for (DGlobalPlayer dGlobalPlayer : dGlobalPlayers) { + if (dGlobalPlayer.getPlayer() == player) { + return dGlobalPlayer; + } + } + + return null; + } + + /** + * @return the dGlobalPlayers + */ + public List getPlayers() { + return dGlobalPlayers; + } + + /** + * @return the dGlobalPlayers that are an instance of DPlayer + */ + public List getDPlayers() { + List dPlayers = new CopyOnWriteArrayList<>(); + for (DGlobalPlayer player : dGlobalPlayers) { + if (player instanceof DPlayer) { + dPlayers.add((DPlayer) player); + } + } + return dPlayers; + } + + /** + * @return the dGlobalPlayers that are an instance of DEditPlayer + */ + public List getDEditPlayers() { + List dEditPlayers = new CopyOnWriteArrayList<>(); + for (DGlobalPlayer player : dGlobalPlayers) { + if (player instanceof DEditPlayer) { + dEditPlayers.add((DEditPlayer) player); + } + } + return dEditPlayers; + } + + /** + * @param player + * an instance of DGlobalPlayer to add + */ + public void addPlayer(DGlobalPlayer player) { + for (DGlobalPlayer dGlobalPlayer : dGlobalPlayers) { + if (dGlobalPlayer.getPlayer().equals(player.getPlayer())) { + dGlobalPlayers.remove(dGlobalPlayer); + } + } + + dGlobalPlayers.add(player); + } + + /** + * @param player + * an instance of DGlobalPlayer to remove + */ + public void removePlayer(DGlobalPlayer player) { + dGlobalPlayers.remove(player); + } + + /** + * @return the dSavePlayers + */ + public List getDSavePlayers() { + return dSavePlayers; + } + + /** + * @param dSavePlayer + * the dSavePlayer to add + */ + public void addDSavePlayer(DSavePlayer dSavePlayer) { + dSavePlayers.add(dSavePlayer); + } + + /** + * @param dSavePlayer + * the dSavePlayer to remove + */ + public void removeDSavePlayer(DSavePlayer dSavePlayer) { + dSavePlayers.remove(dSavePlayer); + } + + /** + * Load all players + */ + public void loadAll() { + for (Player player : Bukkit.getOnlinePlayers()) { + new DGlobalPlayer(player); + } + } + +} diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DSavePlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DSavePlayer.java index 096f28f1..6cbeeb94 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DSavePlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DSavePlayer.java @@ -27,7 +27,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.UUID; -import java.util.concurrent.CopyOnWriteArrayList; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.World; @@ -38,13 +37,14 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; /** + * Represents a player in a GameWorld who went offline. + * * @author Frank Baumann, Tobias Schmitz, Milan Albrecht, Daniel Saukel */ public class DSavePlayer { protected static DungeonsXL plugin = DungeonsXL.getInstance(); - - private static CopyOnWriteArrayList savePlayers = new CopyOnWriteArrayList<>(); + protected static DPlayers dPlayers = plugin.getDPlayers(); // Variables private String playerName; @@ -64,8 +64,6 @@ public class DSavePlayer { public DSavePlayer(String playerName, UUID uuid, Location oldLocation, ArrayList oldInventory, ArrayList oldArmor, ItemStack oldOffHand, int oldLvl, int oldExp, int oldHealth, int oldFoodLevel, int oldFireTicks, GameMode oldGameMode, Collection oldPotionEffects) { - savePlayers.add(this); - this.playerName = playerName; this.uuid = uuid.toString(); @@ -82,28 +80,12 @@ public class DSavePlayer { this.oldPotionEffects = oldPotionEffects; save(); + dPlayers.addDSavePlayer(this); } public DSavePlayer(String playerName, UUID uuid, Location oldLocation, ItemStack[] oldInventory, ItemStack[] oldArmor, ItemStack oldOffHand, int oldLvl, int oldExp, int oldHealth, int oldFoodLevel, int oldFireTicks, GameMode oldGameMode, Collection oldPotionEffects) { - savePlayers.add(this); - - this.playerName = playerName; - this.uuid = uuid.toString(); - - this.oldLocation = oldLocation; - this.oldInventory = new ArrayList<>(Arrays.asList(oldInventory)); - this.oldArmor = new ArrayList<>(Arrays.asList(oldArmor)); - this.oldOffHand = oldOffHand; - this.oldExp = oldExp; - this.oldHealth = oldHealth; - this.oldFoodLevel = oldFoodLevel; - this.oldGameMode = oldGameMode; - this.oldLvl = oldLvl; - this.oldFireTicks = oldFireTicks; - this.oldPotionEffects = oldPotionEffects; - - save(); + this(playerName, uuid, oldLocation, new ArrayList<>(Arrays.asList(oldInventory)), new ArrayList<>(Arrays.asList(oldArmor)), oldOffHand, oldLvl, oldExp, oldHealth, oldFoodLevel, oldFireTicks, oldGameMode, oldPotionEffects); } public void reset(boolean keepInventory) { @@ -124,7 +106,7 @@ public class DSavePlayer { } player.getInventory().setContents(oldInventory.toArray(new ItemStack[36])); player.getInventory().setArmorContents(oldArmor.toArray(new ItemStack[4])); - if (CompatibilityHandler.getInstance().getVersion() == Version.MC1_9) { + if (Version.andHigher(Version.MC1_9).contains(CompatibilityHandler.getInstance().getVersion())) { player.getInventory().setItemInOffHand(oldOffHand); } player.setTotalExperience(oldExp); @@ -153,15 +135,16 @@ public class DSavePlayer { plugin.getLogger().info("Corrupted playerdata detected and removed!"); } - savePlayers.remove(this); save(); + dPlayers.removeDSavePlayer(this); } /* Statics */ + @Deprecated public static void save() { FileConfiguration configFile = new YamlConfiguration(); - for (DSavePlayer savePlayer : savePlayers) { + for (DSavePlayer savePlayer : dPlayers.getDSavePlayers()) { configFile.set(savePlayer.playerName + ".uuid", savePlayer.uuid); configFile.set(savePlayer.playerName + ".oldGameMode", savePlayer.oldGameMode.toString()); configFile.set(savePlayer.playerName + ".oldFireTicks", savePlayer.oldFireTicks); @@ -188,6 +171,7 @@ public class DSavePlayer { } } + @Deprecated public static void load() { FileConfiguration configFile = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "savePlayers.yml")); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/sign/EndSign.java b/src/main/java/io/github/dre2n/dungeonsxl/sign/EndSign.java index bc93e19f..24105b72 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/sign/EndSign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/sign/EndSign.java @@ -77,7 +77,7 @@ public class EndSign extends DSign { @Override public void onTrigger() { - for (DPlayer dPlayer : plugin.getDPlayers()) { + for (DPlayer dPlayer : plugin.getDPlayers().getDPlayers()) { dPlayer.finish(); } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/sign/FloorSign.java b/src/main/java/io/github/dre2n/dungeonsxl/sign/FloorSign.java index 9eebf2ef..b186d79e 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/sign/FloorSign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/sign/FloorSign.java @@ -85,7 +85,7 @@ public class FloorSign extends DSign { @Override public void onTrigger() { - for (DPlayer dPlayer : plugin.getDPlayers()) { + for (DPlayer dPlayer : plugin.getDPlayers().getDPlayers()) { dPlayer.finish(); } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/sign/LeaveSign.java b/src/main/java/io/github/dre2n/dungeonsxl/sign/LeaveSign.java index 8ae9f9c7..7963b379 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/sign/LeaveSign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/sign/LeaveSign.java @@ -78,7 +78,7 @@ public class LeaveSign extends DSign { @Override public void onTrigger() { - for (DPlayer dPlayer : plugin.getDPlayers()) { + for (DPlayer dPlayer : plugin.getDPlayers().getDPlayers()) { DPlayerEscapeEvent event = new DPlayerEscapeEvent(dPlayer); if (event.isCancelled()) { diff --git a/src/main/java/io/github/dre2n/dungeonsxl/sign/ReadySign.java b/src/main/java/io/github/dre2n/dungeonsxl/sign/ReadySign.java index 8451e793..9d47f1c7 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/sign/ReadySign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/sign/ReadySign.java @@ -96,7 +96,7 @@ public class ReadySign extends DSign { @Override public void onTrigger() { - for (DPlayer dPlayer : plugin.getDPlayers()) { + for (DPlayer dPlayer : plugin.getDPlayers().getDPlayers()) { ready(dPlayer); } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/task/LazyUpdateTask.java b/src/main/java/io/github/dre2n/dungeonsxl/task/LazyUpdateTask.java index 6e0e8328..261a1abf 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/task/LazyUpdateTask.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/task/LazyUpdateTask.java @@ -34,7 +34,7 @@ public class LazyUpdateTask extends BukkitRunnable { gameWorld.update(); } - for (DPlayer dPlayer : plugin.getDPlayers()) { + for (DPlayer dPlayer : plugin.getDPlayers().getDPlayers()) { dPlayer.update(true); } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/task/UpdateTask.java b/src/main/java/io/github/dre2n/dungeonsxl/task/UpdateTask.java index 1d3e7cd4..216ac825 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/task/UpdateTask.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/task/UpdateTask.java @@ -27,7 +27,7 @@ public class UpdateTask extends BukkitRunnable { @Override public void run() { - for (DPlayer dPlayer : DungeonsXL.getInstance().getDPlayers()) { + for (DPlayer dPlayer : DungeonsXL.getInstance().getDPlayers().getDPlayers()) { dPlayer.update(false); } } From 744e48ad39f2ab85041f4ddd032dc88db737224f Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Sun, 27 Mar 2016 21:29:06 +0200 Subject: [PATCH 02/11] Remove unused DCommands class --- .../dre2n/dungeonsxl/command/DCommands.java | 78 ------------------- 1 file changed, 78 deletions(-) delete mode 100644 src/main/java/io/github/dre2n/dungeonsxl/command/DCommands.java diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/DCommands.java b/src/main/java/io/github/dre2n/dungeonsxl/command/DCommands.java deleted file mode 100644 index 3f282008..00000000 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/DCommands.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2012-2016 Frank Baumann - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package io.github.dre2n.dungeonsxl.command; - -import io.github.dre2n.commons.command.BRCommand; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * @author Daniel Saukel - */ -public class DCommands { - - private CopyOnWriteArrayList dCommands = new CopyOnWriteArrayList<>(); - - // Methods - public DCommands() { - // Add Commands - dCommands.add(new HelpCommand()); - dCommands.add(new BreakCommand()); - dCommands.add(new ChatCommand()); - dCommands.add(new ChatSpyCommand()); - dCommands.add(new CreateCommand()); - dCommands.add(new EditCommand()); - dCommands.add(new EscapeCommand()); - dCommands.add(new GameCommand()); - dCommands.add(new GroupCommand()); - dCommands.add(new InviteCommand()); - dCommands.add(new LeaveCommand()); - dCommands.add(new ListCommand()); - dCommands.add(new LivesCommand()); - dCommands.add(new MainCommand()); - dCommands.add(new UninviteCommand()); - dCommands.add(new MsgCommand()); - dCommands.add(new PlayCommand()); - dCommands.add(new PortalCommand()); - dCommands.add(new DeletePortalCommand()); - dCommands.add(new ReloadCommand()); - dCommands.add(new SaveCommand()); - dCommands.add(new TestCommand()); - } - - /** - * @param command - * usually the first command variable - */ - public BRCommand getDCommand(String command) { - for (BRCommand dCommand : dCommands) { - if (dCommand.getCommand().equals(command)) { - return dCommand; - } - } - - return null; - } - - /** - * @return the dCommands - */ - public List getDCommands() { - return dCommands; - } - -} From 383e4b6e6126042d3e82af5b1882cb2f2581e896 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Mon, 28 Mar 2016 11:59:01 +0200 Subject: [PATCH 03/11] Fixed /dxl leave. Resolves #43 --- pom.xml | 2 +- .../io/github/dre2n/dungeonsxl/command/LeaveCommand.java | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index b3e4a0ec..ec760898 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 io.github.dre2n dungeonsxl - 0.11-SNAPSHOT + 0.11-SNAPSHOT-29 jar DungeonsXL https://dre2n.github.io diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/LeaveCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/LeaveCommand.java index 639d1139..dec69694 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/LeaveCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/LeaveCommand.java @@ -61,11 +61,6 @@ public class LeaveCommand extends BRCommand { if (dPlayer != null) { DGroup dGroup = DGroup.getByPlayer(player); - if (dGroup == null) { - MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.ERROR_NOT_IN_GROUP)); - return; - } - DPlayerEscapeEvent dPlayerEscapeEvent = new DPlayerEscapeEvent(dPlayer); DPlayerLeaveDGroupEvent dPlayerLeaveDGroupEvent = new DPlayerLeaveDGroupEvent(dPlayer, dGroup); From 3c086918663ad38d44c02e423c890390e893e906 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Mon, 28 Mar 2016 12:09:38 +0200 Subject: [PATCH 04/11] Create a DGlobalPlayer on DPlayers initialization. Resolves #43 --- pom.xml | 2 +- .../java/io/github/dre2n/dungeonsxl/player/DPlayers.java | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ec760898..b3e4a0ec 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 io.github.dre2n dungeonsxl - 0.11-SNAPSHOT-29 + 0.11-SNAPSHOT jar DungeonsXL https://dre2n.github.io diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java index a23a1b9b..c2992bd2 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java @@ -29,6 +29,12 @@ public class DPlayers { private List dGlobalPlayers = new CopyOnWriteArrayList<>(); private List dSavePlayers = new CopyOnWriteArrayList<>(); + public DPlayers() { + for (Player player : Bukkit.getOnlinePlayers()) { + new DGlobalPlayer(player); + } + } + /** * @return the DGlobalPlayer which represents the player */ From f42a85132ed3746487c02c0d832561beafcaaf98 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Mon, 28 Mar 2016 12:31:45 +0200 Subject: [PATCH 05/11] Create DGlobalPlayer after DPlayer deletion --- .../dre2n/dungeonsxl/player/DPlayer.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java index 6cec8599..07e79ce0 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java @@ -494,12 +494,11 @@ public class DPlayer extends DGlobalPlayer { // ... public void escape() { - remove(this); + delete(); savePlayer.reset(false); } public void leave() { - remove(this); if (!editing) { WorldConfig dConfig = GameWorld.getByWorld(world).getConfig(); if (finished) { @@ -604,6 +603,8 @@ public class DPlayer extends DGlobalPlayer { } } } + + delete(); } public void ready() { @@ -955,11 +956,19 @@ public class DPlayer extends DGlobalPlayer { } } - /* Statics */ - public static void remove(DPlayer player) { - plugin.getDPlayers().removePlayer(player); + /** + * Delete this DPlayer. Creates a DGlobalPlayer to replace it! + */ + public void delete() { + if (player.isOnline()) { + new DGlobalPlayer(player); + + } else { + plugin.getDPlayers().removePlayer(this); + } } + /* Statics */ public static DPlayer getByPlayer(Player player) { for (DPlayer dPlayer : plugin.getDPlayers().getDPlayers()) { if (dPlayer.getPlayer().equals(player)) { From f6bf75e051fae6ece0d1aa29224866a1d833aa21 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Mon, 28 Mar 2016 12:44:53 +0200 Subject: [PATCH 06/11] Revert "Create a DGlobalPlayer on DPlayers initialization. Resolves #43" This reverts commit 3c086918663ad38d44c02e423c890390e893e906. --- pom.xml | 2 +- .../java/io/github/dre2n/dungeonsxl/player/DPlayers.java | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index b3e4a0ec..ec760898 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 io.github.dre2n dungeonsxl - 0.11-SNAPSHOT + 0.11-SNAPSHOT-29 jar DungeonsXL https://dre2n.github.io diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java index c2992bd2..a23a1b9b 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayers.java @@ -29,12 +29,6 @@ public class DPlayers { private List dGlobalPlayers = new CopyOnWriteArrayList<>(); private List dSavePlayers = new CopyOnWriteArrayList<>(); - public DPlayers() { - for (Player player : Bukkit.getOnlinePlayers()) { - new DGlobalPlayer(player); - } - } - /** * @return the DGlobalPlayer which represents the player */ From 0afca982b2d5a0190d792c961b721b9bb6720b6e Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Mon, 28 Mar 2016 21:11:01 +0200 Subject: [PATCH 07/11] Fixed leave teleportation bug --- pom.xml | 2 +- src/main/java/io/github/dre2n/dungeonsxl/game/GameWorld.java | 1 + src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index ec760898..b3e4a0ec 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 io.github.dre2n dungeonsxl - 0.11-SNAPSHOT-29 + 0.11-SNAPSHOT jar DungeonsXL https://dre2n.github.io diff --git a/src/main/java/io/github/dre2n/dungeonsxl/game/GameWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/game/GameWorld.java index c0d52da5..a548fd5f 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/game/GameWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/game/GameWorld.java @@ -648,6 +648,7 @@ public class GameWorld { if (bestTime == 0) { return false; + } else if (worldConfig.getTimeLastPlayed() != 0) { if (System.currentTimeMillis() - bestTime > worldConfig.getTimeLastPlayed() * (long) 3600000) { return false; diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java index 07e79ce0..18d4f32e 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java @@ -499,6 +499,8 @@ public class DPlayer extends DGlobalPlayer { } public void leave() { + delete(); + if (!editing) { WorldConfig dConfig = GameWorld.getByWorld(world).getConfig(); if (finished) { @@ -603,8 +605,6 @@ public class DPlayer extends DGlobalPlayer { } } } - - delete(); } public void ready() { From ea72029d26895f82382d408b1ff9d6255868d802 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Thu, 31 Mar 2016 21:42:27 +0200 Subject: [PATCH 08/11] Update to Bukkit 1.9.2 Use BRCommons 0.2.1. --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index b3e4a0ec..99af9439 100644 --- a/pom.xml +++ b/pom.xml @@ -55,7 +55,7 @@ org.bukkit bukkit - 1.9-R0.1-SNAPSHOT + 1.9.2-R0.1-SNAPSHOT provided @@ -67,7 +67,7 @@ io.github.dre2n commons - 0.2 + 0.2.1 io.github.dre2n From 43fcee3c311b4a571c1382069a2f44f269792506 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Wed, 6 Apr 2016 22:05:03 +0200 Subject: [PATCH 09/11] Fix keepInventoryOnEscape --- .../dungeonsxl/listener/PlayerListener.java | 34 +++++------ .../dungeonsxl/player/DGlobalPlayer.java | 59 +++++++++++++++++++ .../dre2n/dungeonsxl/player/DPlayer.java | 50 ++-------------- .../dre2n/dungeonsxl/task/RespawnTask.java | 13 ++-- 4 files changed, 85 insertions(+), 71 deletions(-) diff --git a/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java b/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java index 1140bc19..663ce9c1 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java @@ -108,21 +108,6 @@ public class PlayerListener implements Listener { dPlayer.setLives(dPlayer.getLives() - dPlayerDeathEvent.getLostLives()); - if (dPlayer.getLives() == 0 && dPlayer.isReady()) { - DPlayerKickEvent dPlayerKickEvent = new DPlayerKickEvent(dPlayer, DPlayerKickEvent.Cause.DEATH); - - if (!dPlayerKickEvent.isCancelled()) { - MessageUtil.broadcastMessage(messageConfig.getMessage(Messages.PLAYER_DEATH_KICK, player.getName())); - - // TODO: This Runnable is a workaround for a bug I couldn't find, yet... - new org.bukkit.scheduler.BukkitRunnable() { - public void run() { - dPlayer.leave(); - } - }.runTaskLater(plugin, 1L); - } - } - if (dPlayer.getLives() != -1) { MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.PLAYER_DEATH, String.valueOf(dPlayer.getLives()))); @@ -131,12 +116,25 @@ public class PlayerListener implements Listener { dPlayer.setRespawnInventory(event.getEntity().getInventory().getContents()); dPlayer.setRespawnArmor(event.getEntity().getInventory().getArmorContents()); // Delete all drops - for (ItemStack istack : event.getDrops()) { - istack.setType(Material.AIR); + for (ItemStack item : event.getDrops()) { + item.setType(Material.AIR); } } } } + + if (dPlayer.getLives() == 0 && dPlayer.isReady()) { + DPlayerKickEvent dPlayerKickEvent = new DPlayerKickEvent(dPlayer, DPlayerKickEvent.Cause.DEATH); + + if (!dPlayerKickEvent.isCancelled()) { + MessageUtil.broadcastMessage(messageConfig.getMessage(Messages.PLAYER_DEATH_KICK, player.getName())); + dPlayer.leave(); + if (gameWorld.getConfig().getKeepInventoryOnEscape()) { + /*new org.bukkit.scheduler.BukkitRunnable() {public void run(){*/ + dPlayer.applyRespawnInventory();/*}}.runTaskLater(plugin, 1);*/ + } + } + } } @EventHandler(priority = EventPriority.HIGHEST) @@ -348,6 +346,8 @@ public class PlayerListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onRespawn(PlayerRespawnEvent event) { Player player = event.getPlayer(); + plugin.getDPlayers().getByPlayer(player).applyRespawnInventory(); + DPlayer dPlayer = DPlayer.getByPlayer(player); if (dPlayer == null) { return; diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java index c43ab87c..d64a26c3 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java @@ -19,6 +19,7 @@ package io.github.dre2n.dungeonsxl.player; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.global.DPortal; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; /** * Represents a player in the non-DXL worlds of the server. @@ -33,12 +34,26 @@ public class DGlobalPlayer { private boolean chatSpyMode; private DPortal creatingPortal; + private ItemStack[] respawnInventory; + private ItemStack[] respawnArmor; + public DGlobalPlayer(Player player) { this.player = player; DungeonsXL.getInstance().getDPlayers().addPlayer(this); } + public DGlobalPlayer(DGlobalPlayer dPlayer) { + player = dPlayer.getPlayer(); + breakMode = dPlayer.isInBreakMode(); + chatSpyMode = dPlayer.isInChatSpyMode(); + creatingPortal = dPlayer.getPortal(); + respawnInventory = dPlayer.getRespawnInventory(); + respawnArmor = dPlayer.getRespawnArmor(); + + DungeonsXL.getInstance().getDPlayers().addPlayer(this); + } + /** * @return the Bukkit player */ @@ -98,4 +113,48 @@ public class DGlobalPlayer { creatingPortal = dPortal; } + /** + * @return the respawnInventory + */ + public ItemStack[] getRespawnInventory() { + return respawnInventory; + } + + /** + * @param respawnInventory + * the respawnInventory to set + */ + public void setRespawnInventory(ItemStack[] respawnInventory) { + this.respawnInventory = respawnInventory; + } + + /** + * Give the saved respawn inventory to the player + */ + public void applyRespawnInventory() { + if (respawnInventory == null || respawnArmor == null) { + return; + } + + player.getInventory().setContents(respawnInventory); + player.getInventory().setArmorContents(respawnArmor); + respawnInventory = null; + respawnArmor = null; + } + + /** + * @return the respawnArmor + */ + public ItemStack[] getRespawnArmor() { + return respawnArmor; + } + + /** + * @param respawnArmor + * the respawnArmor to set + */ + public void setRespawnArmor(ItemStack[] respawnArmor) { + this.respawnArmor = respawnArmor; + } + } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java index 18d4f32e..d1865607 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java @@ -85,8 +85,6 @@ public class DPlayer extends DGlobalPlayer { private Wolf wolf; private int wolfRespawnTime = 30; private long offlineTime; - private ItemStack[] respawnInventory; - private ItemStack[] respawnArmor; private String[] linesCopy; private Inventory treasureInv = plugin.getServer().createInventory(getPlayer(), 45, messageConfig.getMessage(Messages.PLAYER_TREASURES)); @@ -402,36 +400,6 @@ public class DPlayer extends DGlobalPlayer { this.offlineTime = offlineTime; } - /** - * @return the respawnInventory - */ - public ItemStack[] getRespawnInventory() { - return respawnInventory; - } - - /** - * @param respawnInventory - * the respawnInventory to set - */ - public void setRespawnInventory(ItemStack[] respawnInventory) { - this.respawnInventory = respawnInventory; - } - - /** - * @return the respawnArmor - */ - public ItemStack[] getRespawnArmor() { - return respawnArmor; - } - - /** - * @param respawnArmor - * the respawnArmor to set - */ - public void setRespawnArmor(ItemStack[] respawnArmor) { - this.respawnArmor = respawnArmor; - } - /** * @return the linesCopy */ @@ -500,7 +468,7 @@ public class DPlayer extends DGlobalPlayer { public void leave() { delete(); - + if (!editing) { WorldConfig dConfig = GameWorld.getByWorld(world).getConfig(); if (finished) { @@ -654,13 +622,7 @@ public class DPlayer extends DGlobalPlayer { // Respawn Items if (GameWorld.getByWorld(world).getConfig().getKeepInventoryOnDeath()) { - if (respawnInventory != null || respawnArmor != null) { - getPlayer().getInventory().setContents(respawnInventory); - getPlayer().getInventory().setArmorContents(respawnArmor); - respawnInventory = null; - respawnArmor = null; - } - // P.plugin.updateInventory(this.player); + applyRespawnInventory(); } } @@ -937,10 +899,7 @@ public class DPlayer extends DGlobalPlayer { } if (respawnInventory) { - getPlayer().getInventory().setContents(getRespawnInventory()); - getPlayer().getInventory().setArmorContents(getRespawnArmor()); - setRespawnInventory(null); - setRespawnArmor(null); + applyRespawnInventory(); } if (kick) { @@ -961,7 +920,8 @@ public class DPlayer extends DGlobalPlayer { */ public void delete() { if (player.isOnline()) { - new DGlobalPlayer(player); + // Create a new DGlobalPlayer (outside a dungeon) + new DGlobalPlayer(this); } else { plugin.getDPlayers().removePlayer(this); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/task/RespawnTask.java b/src/main/java/io/github/dre2n/dungeonsxl/task/RespawnTask.java index 8f344626..a497ad33 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/task/RespawnTask.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/task/RespawnTask.java @@ -17,7 +17,8 @@ package io.github.dre2n.dungeonsxl.task; import io.github.dre2n.commons.util.playerutil.PlayerUtil; -import io.github.dre2n.dungeonsxl.player.DPlayer; +import io.github.dre2n.dungeonsxl.DungeonsXL; +import io.github.dre2n.dungeonsxl.player.DGlobalPlayer; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; @@ -41,19 +42,13 @@ public class RespawnTask extends BukkitRunnable { PlayerUtil.secureTeleport(player, location); } - DPlayer dPlayer = DPlayer.getByPlayer(player); + DGlobalPlayer dPlayer = DungeonsXL.getInstance().getDPlayers().getByPlayer(player); if (dPlayer == null) { return; } - // Respawn Items - if (dPlayer.getRespawnInventory() != null || dPlayer.getRespawnArmor() != null) { - player.getInventory().setContents(dPlayer.getRespawnInventory()); - player.getInventory().setArmorContents(dPlayer.getRespawnArmor()); - dPlayer.setRespawnInventory(null); - dPlayer.setRespawnArmor(null); - } + dPlayer.applyRespawnInventory(); } } From 5578e3ce5d174c7bfa23f20b82be7118f31571fa Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Wed, 6 Apr 2016 22:05:19 +0200 Subject: [PATCH 10/11] Remove debug message --- .../io/github/dre2n/dungeonsxl/listener/EntityListener.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/io/github/dre2n/dungeonsxl/listener/EntityListener.java b/src/main/java/io/github/dre2n/dungeonsxl/listener/EntityListener.java index 2a2e0109..83865475 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/listener/EntityListener.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/listener/EntityListener.java @@ -27,7 +27,6 @@ import io.github.dre2n.dungeonsxl.mob.DMob; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.player.DPlayer; import java.util.List; -import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; @@ -165,7 +164,6 @@ public class EntityListener implements Listener { if (attackerDGroup != null && attackedDGroup != null) { if (!friendlyFire && attackerDGroup.equals(attackedDGroup)) { - Bukkit.broadcastMessage("ff cancel"); event.setCancelled(true); } } From 8371ab21f8a2447251787780b84c27893b8f841a Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Wed, 6 Apr 2016 22:20:46 +0200 Subject: [PATCH 11/11] Update to BRCommons 0.3.1 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 99af9439..65342079 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ io.github.dre2n commons - 0.2.1 + 0.3.1 io.github.dre2n