From a01e18b0c7a1f5d9004d3e5160d9c1def78084a7 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Tue, 27 Dec 2016 16:00:54 +0100 Subject: [PATCH] Recoded floor / finish system --- .../dre2n/dungeonsxl/config/DMessages.java | 1 + .../dgroup/DGroupFinishDungeonEvent.java | 21 ++- .../event/dgroup/DGroupFinishFloorEvent.java | 11 +- .../instance/game/DGamePlayerFinishEvent.java | 19 +-- .../io/github/dre2n/dungeonsxl/game/Game.java | 1 + .../dre2n/dungeonsxl/player/DGamePlayer.java | 140 ++++-------------- .../dre2n/dungeonsxl/player/DGroup.java | 116 ++++++++++++--- .../dre2n/dungeonsxl/world/DGameWorld.java | 24 +++ 8 files changed, 177 insertions(+), 156 deletions(-) diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/config/DMessages.java b/core/src/main/java/io/github/dre2n/dungeonsxl/config/DMessages.java index b8362aa2..844a66aa 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/config/DMessages.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/config/DMessages.java @@ -181,6 +181,7 @@ public enum DMessages implements Messages { PLAYER_DEATH("Player_Death", "&4&v1 &6died and has &4&v2 &6lives left."), PLAYER_DEATH_KICK("Player_DeathKick", "&2&v1 &6lost his last life and was kicked."), PLAYER_FINISHED_DUNGEON("Player_FinishedDungeon", "&6You successfully finished the dungeon!"), + PLAYER_FINISHED_FLOOR("Player_Finished_Floor", "&6You successfully finished the floor."), PLAYER_INVITED("Player_Invited", "&4&v1&6 invited you to the group &4&v2&6."), PLAYER_UNINVITED("Player_Uninvited", "&4&v1&6 took back your invitation to the group &4&v2&6."), PLAYER_JOIN_GROUP("Player_JoinGroup", "&6You successfully joined the group!"), diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupFinishDungeonEvent.java b/core/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupFinishDungeonEvent.java index ff5072f4..fbc21134 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupFinishDungeonEvent.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupFinishDungeonEvent.java @@ -16,6 +16,7 @@ */ package io.github.dre2n.dungeonsxl.event.dgroup; +import io.github.dre2n.dungeonsxl.dungeon.Dungeon; import io.github.dre2n.dungeonsxl.player.DGroup; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; @@ -28,8 +29,26 @@ public class DGroupFinishDungeonEvent extends DGroupEvent implements Cancellable private static final HandlerList handlers = new HandlerList(); private boolean cancelled; - public DGroupFinishDungeonEvent(DGroup dGroup) { + private Dungeon dungeon; + + public DGroupFinishDungeonEvent(Dungeon dungeon, DGroup dGroup) { super(dGroup); + this.dungeon = dungeon; + } + + /** + * @return the dungeon + */ + public Dungeon getDungeon() { + return dungeon; + } + + /** + * @param dungeon + * the dungeon to set + */ + public void setDisbander(Dungeon dungeon) { + this.dungeon = dungeon; } @Override diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupFinishFloorEvent.java b/core/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupFinishFloorEvent.java index 6fab15d4..0e74ec64 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupFinishFloorEvent.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/event/dgroup/DGroupFinishFloorEvent.java @@ -18,6 +18,7 @@ package io.github.dre2n.dungeonsxl.event.dgroup; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.world.DGameWorld; +import io.github.dre2n.dungeonsxl.world.DResourceWorld; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; @@ -30,9 +31,9 @@ public class DGroupFinishFloorEvent extends DGroupEvent implements Cancellable { private boolean cancelled; private DGameWorld finished; - private String next; + private DResourceWorld next; - public DGroupFinishFloorEvent(DGroup dGroup, DGameWorld finished, String next) { + public DGroupFinishFloorEvent(DGroup dGroup, DGameWorld finished, DResourceWorld next) { super(dGroup); this.finished = finished; this.next = next; @@ -56,15 +57,15 @@ public class DGroupFinishFloorEvent extends DGroupEvent implements Cancellable { /** * @return the next */ - public String getNext() { + public DResourceWorld getNext() { return next; } /** * @param next - * the name of the DGameWorld to set + * the resource to set */ - public void setNext(String next) { + public void setNext(DResourceWorld next) { this.next = next; } diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/event/dplayer/instance/game/DGamePlayerFinishEvent.java b/core/src/main/java/io/github/dre2n/dungeonsxl/event/dplayer/instance/game/DGamePlayerFinishEvent.java index aea65090..0749490a 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/event/dplayer/instance/game/DGamePlayerFinishEvent.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/event/dplayer/instance/game/DGamePlayerFinishEvent.java @@ -28,30 +28,13 @@ public class DGamePlayerFinishEvent extends DGamePlayerEvent implements Cancella private static final HandlerList handlers = new HandlerList(); private boolean cancelled; - private boolean first; private boolean hasToWait; - public DGamePlayerFinishEvent(DGamePlayer dPlayer, boolean first, boolean hasToWait) { + public DGamePlayerFinishEvent(DGamePlayer dPlayer, boolean hasToWait) { super(dPlayer); - this.first = first; this.hasToWait = hasToWait; } - /** - * @return the first - */ - public boolean isFirst() { - return first; - } - - /** - * @param first - * the first to set - */ - public void setFirst(boolean first) { - this.first = first; - } - /** * @return the hasToWait */ diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/game/Game.java b/core/src/main/java/io/github/dre2n/dungeonsxl/game/Game.java index 0d740eb5..990102b4 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/game/Game.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/game/Game.java @@ -95,6 +95,7 @@ public class Game { tutorial = false; started = false; world = resource.instantiateAsGameWorld(); + world.setType(DGameWorld.Type.START_FLOOR); dGroups.add(dGroup); dGroup.setGameWorld(world); diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java b/core/src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java index 7a5138c9..abded0d2 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java @@ -16,14 +16,10 @@ */ package io.github.dre2n.dungeonsxl.player; -import io.github.dre2n.commons.util.NumberUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.commons.util.playerutil.PlayerUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.DMessages; -import io.github.dre2n.dungeonsxl.config.DungeonConfig; -import io.github.dre2n.dungeonsxl.event.dgroup.DGroupFinishDungeonEvent; -import io.github.dre2n.dungeonsxl.event.dgroup.DGroupRewardEvent; import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerKickEvent; import io.github.dre2n.dungeonsxl.event.dplayer.instance.DInstancePlayerUpdateEvent; import io.github.dre2n.dungeonsxl.event.dplayer.instance.game.DGamePlayerDeathEvent; @@ -737,79 +733,34 @@ public class DGamePlayer extends DInstancePlayer { * the name of the next floor */ public void finishFloor(DResourceWorld specifiedFloor) { - MessageUtil.sendMessage(getPlayer(), DMessages.PLAYER_FINISHED_DUNGEON.getMessage()); + if (!dGroup.getDungeon().isMultiFloor()) { + finish(); + return; + } + + MessageUtil.sendMessage(getPlayer(), DMessages.PLAYER_FINISHED_FLOOR.getMessage()); finished = true; + boolean hasToWait = false; if (getDGroup() == null) { return; } - if (!dGroup.isPlaying()) { return; } - dGroup.setNextFloor(specifiedFloor); - - for (Player player : dGroup.getPlayers()) { - DGamePlayer dPlayer = getByPlayer(player); - if (!dPlayer.finished) { - MessageUtil.sendMessage(this.getPlayer(), DMessages.PLAYER_WAIT_FOR_OTHER_PLAYERS.getMessage()); - return; - } + if (dGroup.isFinished()) { + dGroup.finishFloor(specifiedFloor); + } else { + MessageUtil.sendMessage(player, DMessages.PLAYER_WAIT_FOR_OTHER_PLAYERS.getMessage()); + hasToWait = true; } - boolean invalid = !dGroup.getDungeon().isMultiFloor(); - - for (Player player : dGroup.getPlayers()) { - DGamePlayer dPlayer = getByPlayer(player); - - if (invalid) { - dPlayer.finish(false); - - } else { - dPlayer.finished = false; - } + DGamePlayerFinishEvent dPlayerFinishEvent = new DGamePlayerFinishEvent(this, hasToWait); + plugin.getServer().getPluginManager().callEvent(dPlayerFinishEvent); + if (dPlayerFinishEvent.isCancelled()) { + finished = false; } - - if (invalid) { - return; - } - - DungeonConfig dConfig = dGroup.getDungeon().getConfig(); - int random = NumberUtil.generateRandomInt(0, dConfig.getFloors().size()); - DResourceWorld newFloor = dGroup.getUnplayedFloors().get(random); - if (dConfig.getFloorCount() == dGroup.getFloorCount() - 1) { - newFloor = dConfig.getEndFloor(); - - } else if (specifiedFloor != null) { - newFloor = specifiedFloor; - } - - /*DGroupFinishFloorEvent event = new DGroupFinishFloorEvent(dGroup, dGroup.getGameWorld(), newFloor); - - if (event.isCancelled()) { - return; - } - */ - Game game = dGroup.getGameWorld().getGame(); - - dGroup.removeUnplayedFloor(dGroup.getGameWorld().getResource(), false); - - DGameWorld gameWorld = null; - if (newFloor != null) { - gameWorld = newFloor.instantiateAsGameWorld(); - } - dGroup.setGameWorld(gameWorld); - - for (Player player : dGroup.getPlayers()) { - DGamePlayer dPlayer = getByPlayer(player); - dPlayer.setWorld(gameWorld.getWorld()); - dPlayer.setCheckpoint(dGroup.getGameWorld().getStartLocation(dGroup)); - if (dPlayer.getWolf() != null) { - dPlayer.getWolf().teleport(dPlayer.getCheckpoint()); - } - } - dGroup.startGame(game); } /** @@ -829,60 +780,23 @@ public class DGamePlayer extends DInstancePlayer { } finished = true; - if (getDGroup() == null) { - return; - } - - if (!dGroup.isPlaying()) { - return; - } - - boolean first = true; boolean hasToWait = false; - - for (Player player : dGroup.getPlayers()) { - DGamePlayer dPlayer = getByPlayer(player); - if (!dPlayer.finished) { - if (message) { - MessageUtil.sendMessage(this.getPlayer(), DMessages.PLAYER_WAIT_FOR_OTHER_PLAYERS.getMessage()); - } - hasToWait = true; - - } else if (dPlayer != this) { - first = false; + if (!getDGroup().isPlaying()) { + return; + } + if (dGroup.isFinished()) { + dGroup.finish(); + } else { + if (message) { + MessageUtil.sendMessage(this.getPlayer(), DMessages.PLAYER_WAIT_FOR_OTHER_PLAYERS.getMessage()); } + hasToWait = true; } - DGamePlayerFinishEvent dPlayerFinishEvent = new DGamePlayerFinishEvent(this, first, hasToWait); - + DGamePlayerFinishEvent dPlayerFinishEvent = new DGamePlayerFinishEvent(this, hasToWait); + plugin.getServer().getPluginManager().callEvent(dPlayerFinishEvent); if (dPlayerFinishEvent.isCancelled()) { finished = false; - return; - } - - if (hasToWait) { - return; - } - - DGroupFinishDungeonEvent dGroupFinishDungeonEvent = new DGroupFinishDungeonEvent(dGroup); - - if (dGroupFinishDungeonEvent.isCancelled()) { - return; - } - - Game.getByDGroup(dGroup).resetWaveKills(); - - DGroupRewardEvent dGroupRewardEvent = new DGroupRewardEvent(dGroup); - plugin.getServer().getPluginManager().callEvent(dGroupRewardEvent); - for (Player player : dGroup.getPlayers()) { - DGamePlayer dPlayer = getByPlayer(player); - dPlayer.leave(false); - - if (!dGroupRewardEvent.isCancelled()) { - for (Reward reward : dGroup.getRewards()) { - reward.giveTo(player); - } - } } } diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java b/core/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java index ce3a3cea..36ee9fe4 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java @@ -16,11 +16,16 @@ */ package io.github.dre2n.dungeonsxl.player; +import io.github.dre2n.commons.util.NumberUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.DMessages; +import io.github.dre2n.dungeonsxl.config.DungeonConfig; import io.github.dre2n.dungeonsxl.dungeon.Dungeon; import io.github.dre2n.dungeonsxl.event.dgroup.DGroupDisbandEvent; +import io.github.dre2n.dungeonsxl.event.dgroup.DGroupFinishDungeonEvent; +import io.github.dre2n.dungeonsxl.event.dgroup.DGroupFinishFloorEvent; +import io.github.dre2n.dungeonsxl.event.dgroup.DGroupRewardEvent; import io.github.dre2n.dungeonsxl.event.dgroup.DGroupStartFloorEvent; import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerJoinDGroupEvent; import io.github.dre2n.dungeonsxl.event.requirement.RequirementDemandEvent; @@ -117,7 +122,7 @@ public class DGroup { dungeon = plugin.getDungeons().getByName(identifier); if (multiFloor && dungeon != null) { // Real dungeon - unplayedFloors = dungeon.getConfig().getFloors(); + unplayedFloors = new ArrayList<>(dungeon.getConfig().getFloors()); } else { // Artificial dungeon @@ -155,7 +160,7 @@ public class DGroup { } } - this.dungeon = dungeon; + setDungeon(dungeon); playing = false; floorCount = 0; } @@ -407,11 +412,16 @@ public class DGroup { } /** + * Sets up all dungeon-related fields. + * * @param dungeon * the dungeon to set */ public void setDungeon(Dungeon dungeon) { this.dungeon = dungeon; + if (dungeon.isMultiFloor()) { + unplayedFloors = new ArrayList<>(dungeon.getConfig().getFloors()); + } } /** @@ -644,7 +654,92 @@ public class DGroup { this.lives = lives; } + /** + * @return true if all players are finished + */ + public boolean isFinished() { + for (DGamePlayer dPlayer : getDGamePlayers()) { + if (!dPlayer.isFinished()) { + return false; + } + } + return true; + } + /* Actions */ + /** + * The group finishs the dungeon. + */ + public void finish() { + DGroupFinishDungeonEvent dGroupFinishDungeonEvent = new DGroupFinishDungeonEvent(dungeon, this); + plugin.getServer().getPluginManager().callEvent(dGroupFinishDungeonEvent); + if (dGroupFinishDungeonEvent.isCancelled()) { + return; + } + + Game.getByDGroup(this).resetWaveKills(); + DGroupRewardEvent dGroupRewardEvent = new DGroupRewardEvent(this); + plugin.getServer().getPluginManager().callEvent(dGroupRewardEvent); + for (DGamePlayer player : getDGamePlayers()) { + player.leave(false); + if (!dGroupRewardEvent.isCancelled()) { + for (Reward reward : rewards) { + reward.giveTo(player.getPlayer()); + } + } + } + } + + /** + * The group finishs the current floor. + * + * @param specifiedFloor + * the name of the next floor + */ + public void finishFloor(DResourceWorld specifiedFloor) { + DungeonConfig dConfig = dungeon.getConfig(); + int floorsLeft = getUnplayedFloors().size(); + DResourceWorld newFloor = null; + DGameWorld.Type type = null; + if (gameWorld.getType() == DGameWorld.Type.END_FLOOR) { + finish(); + return; + } else if (specifiedFloor != null) { + newFloor = specifiedFloor; + type = DGameWorld.Type.DEFAULT; + } else if (floorsLeft > 0) { + int random = NumberUtil.generateRandomInt(0, floorsLeft); + newFloor = getUnplayedFloors().get(random); + type = DGameWorld.Type.DEFAULT; + } else { + newFloor = dConfig.getEndFloor(); + type = DGameWorld.Type.END_FLOOR; + } + + DGroupFinishFloorEvent event = new DGroupFinishFloorEvent(this, gameWorld, newFloor); + plugin.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + + Game game = gameWorld.getGame(); + removeUnplayedFloor(gameWorld.getResource(), false); + DGameWorld gameWorld = newFloor.instantiateAsGameWorld(); + gameWorld.setType(type); + this.gameWorld = gameWorld; + game.setWorld(gameWorld); + + for (DGamePlayer player : getDGamePlayers()) { + player.setWorld(gameWorld.getWorld()); + player.setCheckpoint(gameWorld.getStartLocation(this)); + if (player.getWolf() != null) { + player.getWolf().teleport(player.getCheckpoint()); + } + player.setFinished(false); + } + startGame(game); + } + /** * Remove the group from the List */ @@ -810,23 +905,6 @@ public class DGroup { return true; } - public boolean finishIfMembersFinished() { - boolean finish = true; - - for (DGamePlayer dPlayer : getDGamePlayers()) { - if (!dPlayer.isFinished()) { - finish = false; - break; - } - } - - if (finish && getDGamePlayers().size() > 0) { - getDGamePlayers().get(0).finishFloor(nextFloor); - } - - return finish; - } - /** * Send a message to all players in the group */ diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/world/DGameWorld.java b/core/src/main/java/io/github/dre2n/dungeonsxl/world/DGameWorld.java index adb37327..b85ffd25 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/world/DGameWorld.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/world/DGameWorld.java @@ -73,9 +73,17 @@ import org.bukkit.scheduler.BukkitRunnable; */ public class DGameWorld extends DInstanceWorld { + public enum Type { + START_FLOOR, + END_FLOOR, + DEFAULT + } + Game game; // Variables + private Type type = Type.DEFAULT; + private boolean isPlaying = false; // TO DO: Which lists actually need to be CopyOnWriteArrayLists? @@ -119,6 +127,22 @@ public class DGameWorld extends DInstanceWorld { return game; } + /** + * @return + * the type of the floor + */ + public Type getType() { + return type; + } + + /** + * @param type + * the type to set + */ + public void setType(Type type) { + this.type = type; + } + /** * @return the isPlaying */