From 6d0b6f3b6e421f84424662f19d68b84f228e1613 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Wed, 13 Jan 2016 00:11:53 +0100 Subject: [PATCH] Implement world events --- .../dre2n/dungeonsxl/dungeon/EditWorld.java | 34 ++++++++++++++++++ .../dungeonsxl/dungeon/game/GameWorld.java | 13 +++++++ .../dungeonsxl/event/dsign/DSignEvent.java | 4 +-- .../editworld/EditWorldGenerateEvent.java | 36 +++++++++++++++++++ .../event/editworld/EditWorldLoadEvent.java | 26 +++++++++++--- .../event/editworld/EditWorldSaveEvent.java | 36 +++++++++++++++++++ .../event/editworld/EditWorldUnloadEvent.java | 20 ++++++++++- .../event/gameworld/GameWorldLoadEvent.java | 26 +++++++++++--- 8 files changed, 182 insertions(+), 13 deletions(-) create mode 100644 src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldGenerateEvent.java create mode 100644 src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldSaveEvent.java diff --git a/src/io/github/dre2n/dungeonsxl/dungeon/EditWorld.java b/src/io/github/dre2n/dungeonsxl/dungeon/EditWorld.java index 9e866948..2046f7f4 100644 --- a/src/io/github/dre2n/dungeonsxl/dungeon/EditWorld.java +++ b/src/io/github/dre2n/dungeonsxl/dungeon/EditWorld.java @@ -1,6 +1,10 @@ package io.github.dre2n.dungeonsxl.dungeon; import io.github.dre2n.dungeonsxl.DungeonsXL; +import io.github.dre2n.dungeonsxl.event.editworld.EditWorldGenerateEvent; +import io.github.dre2n.dungeonsxl.event.editworld.EditWorldLoadEvent; +import io.github.dre2n.dungeonsxl.event.editworld.EditWorldSaveEvent; +import io.github.dre2n.dungeonsxl.event.editworld.EditWorldUnloadEvent; import io.github.dre2n.dungeonsxl.player.DPlayer; import io.github.dre2n.dungeonsxl.util.FileUtil; import io.github.dre2n.dungeonsxl.util.messageutil.MessageUtil; @@ -65,10 +69,22 @@ public class EditWorld { creator.type(WorldType.FLAT); creator.generateStructures(false); + EditWorldGenerateEvent event = new EditWorldGenerateEvent(this); + + if (event.isCancelled()) { + return; + } + world = plugin.getServer().createWorld(creator); } public void save() { + EditWorldSaveEvent event = new EditWorldSaveEvent(this); + + if (event.isCancelled()) { + return; + } + world.save(); try { @@ -97,6 +113,12 @@ public class EditWorld { } public void delete() { + EditWorldUnloadEvent event = new EditWorldUnloadEvent(this, true); + + if (event.isCancelled()) { + return; + } + plugin.getEditWorlds().remove(this); for (Player player : world.getPlayers()) { DPlayer dPlayer = DPlayer.getByPlayer(player); @@ -111,6 +133,12 @@ public class EditWorld { } public void deleteNoSave() { + EditWorldUnloadEvent event = new EditWorldUnloadEvent(this, false); + + if (event.isCancelled()) { + return; + } + plugin.getEditWorlds().remove(this); for (Player player : world.getPlayers()) { DPlayer dPlayer = DPlayer.getByPlayer(player); @@ -152,6 +180,12 @@ public class EditWorld { } public static EditWorld load(String name) { + EditWorldLoadEvent event = new EditWorldLoadEvent(name); + + if (event.isCancelled()) { + return null; + } + for (EditWorld editWorld : plugin.getEditWorlds()) { if (editWorld.mapName.equalsIgnoreCase(name)) { diff --git a/src/io/github/dre2n/dungeonsxl/dungeon/game/GameWorld.java b/src/io/github/dre2n/dungeonsxl/dungeon/game/GameWorld.java index a7626a51..eee9e914 100644 --- a/src/io/github/dre2n/dungeonsxl/dungeon/game/GameWorld.java +++ b/src/io/github/dre2n/dungeonsxl/dungeon/game/GameWorld.java @@ -5,7 +5,9 @@ import io.github.dre2n.dungeonsxl.dungeon.Dungeon; import io.github.dre2n.dungeonsxl.dungeon.DungeonConfig; import io.github.dre2n.dungeonsxl.dungeon.WorldConfig; import io.github.dre2n.dungeonsxl.dungeon.EditWorld; +import io.github.dre2n.dungeonsxl.event.gameworld.GameWorldLoadEvent; import io.github.dre2n.dungeonsxl.event.gameworld.GameWorldStartGameEvent; +import io.github.dre2n.dungeonsxl.event.gameworld.GameWorldUnloadEvent; import io.github.dre2n.dungeonsxl.mob.DMob; import io.github.dre2n.dungeonsxl.player.DPlayer; import io.github.dre2n.dungeonsxl.requirement.Requirement; @@ -498,6 +500,12 @@ public class GameWorld { } public void delete() { + GameWorldUnloadEvent event = new GameWorldUnloadEvent(this); + + if (event.isCancelled()) { + return; + } + plugin.getGameWorlds().remove(this); plugin.getServer().unloadWorld(world, true); File dir = new File("DXL_Game_" + id); @@ -505,6 +513,11 @@ public class GameWorld { } public static GameWorld load(String name) { + GameWorldLoadEvent event = new GameWorldLoadEvent(name); + + if (event.isCancelled()) { + return null; + } File file = new File(plugin.getDataFolder(), "/maps/" + name); diff --git a/src/io/github/dre2n/dungeonsxl/event/dsign/DSignEvent.java b/src/io/github/dre2n/dungeonsxl/event/dsign/DSignEvent.java index 7cab352c..ea0849fb 100644 --- a/src/io/github/dre2n/dungeonsxl/event/dsign/DSignEvent.java +++ b/src/io/github/dre2n/dungeonsxl/event/dsign/DSignEvent.java @@ -15,7 +15,7 @@ public abstract class DSignEvent extends Event { /** * @return the dSign */ - public DSign getdSign() { + public DSign getDSign() { return dSign; } @@ -23,7 +23,7 @@ public abstract class DSignEvent extends Event { * @param dSign * the dSign to set */ - public void setdSign(DSign dSign) { + public void setDSign(DSign dSign) { this.dSign = dSign; } diff --git a/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldGenerateEvent.java b/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldGenerateEvent.java new file mode 100644 index 00000000..97a486f8 --- /dev/null +++ b/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldGenerateEvent.java @@ -0,0 +1,36 @@ +package io.github.dre2n.dungeonsxl.event.editworld; + +import io.github.dre2n.dungeonsxl.dungeon.EditWorld; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +public class EditWorldGenerateEvent extends EditWorldEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + + public EditWorldGenerateEvent(EditWorld editWorld) { + super(editWorld); + } + + @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/io/github/dre2n/dungeonsxl/event/editworld/EditWorldLoadEvent.java b/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldLoadEvent.java index 0a3d94b6..38790bdc 100644 --- a/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldLoadEvent.java +++ b/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldLoadEvent.java @@ -1,17 +1,33 @@ package io.github.dre2n.dungeonsxl.event.editworld; -import io.github.dre2n.dungeonsxl.dungeon.EditWorld; - import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -public class EditWorldLoadEvent extends EditWorldEvent implements Cancellable { +public class EditWorldLoadEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); private boolean cancelled; - public EditWorldLoadEvent(EditWorld editWorld) { - super(editWorld); + private String name; + + public EditWorldLoadEvent(String name) { + this.name = name; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name + * the name to set + */ + public void setName(String name) { + this.name = name; } @Override diff --git a/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldSaveEvent.java b/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldSaveEvent.java new file mode 100644 index 00000000..c1a666ee --- /dev/null +++ b/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldSaveEvent.java @@ -0,0 +1,36 @@ +package io.github.dre2n.dungeonsxl.event.editworld; + +import io.github.dre2n.dungeonsxl.dungeon.EditWorld; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +public class EditWorldSaveEvent extends EditWorldEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + + public EditWorldSaveEvent(EditWorld editWorld) { + super(editWorld); + } + + @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/io/github/dre2n/dungeonsxl/event/editworld/EditWorldUnloadEvent.java b/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldUnloadEvent.java index 5875af6d..16f3c7cf 100644 --- a/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldUnloadEvent.java +++ b/src/io/github/dre2n/dungeonsxl/event/editworld/EditWorldUnloadEvent.java @@ -10,8 +10,26 @@ public class EditWorldUnloadEvent extends EditWorldEvent implements Cancellable private static final HandlerList handlers = new HandlerList(); private boolean cancelled; - public EditWorldUnloadEvent(EditWorld editWorld) { + private boolean save; + + public EditWorldUnloadEvent(EditWorld editWorld, boolean save) { super(editWorld); + this.save = save; + } + + /** + * @return the save + */ + public boolean getSave() { + return save; + } + + /** + * @param save + * the save to set + */ + public void setSave(boolean save) { + this.save = save; } @Override diff --git a/src/io/github/dre2n/dungeonsxl/event/gameworld/GameWorldLoadEvent.java b/src/io/github/dre2n/dungeonsxl/event/gameworld/GameWorldLoadEvent.java index 8edc7d5f..9ec3d6b7 100644 --- a/src/io/github/dre2n/dungeonsxl/event/gameworld/GameWorldLoadEvent.java +++ b/src/io/github/dre2n/dungeonsxl/event/gameworld/GameWorldLoadEvent.java @@ -1,17 +1,33 @@ package io.github.dre2n.dungeonsxl.event.gameworld; -import io.github.dre2n.dungeonsxl.dungeon.game.GameWorld; - import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -public class GameWorldLoadEvent extends GameWorldEvent implements Cancellable { +public class GameWorldLoadEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); private boolean cancelled; - public GameWorldLoadEvent(GameWorld gameWorld) { - super(gameWorld); + private String name; + + public GameWorldLoadEvent(String name) { + this.name = name; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name + * the name to set + */ + public void setName(String name) { + this.name = name; } @Override