From 33c03d68c10c64aeece0bc8441278f251dad99b6 Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Sun, 11 Mar 2012 20:39:05 +0100 Subject: [PATCH] added hide and unhide feature --- src/org/mcteam/ancientgates/Gate.java | 54 +++++++++++++++++-- src/org/mcteam/ancientgates/Plugin.java | 2 + .../ancientgates/commands/BaseCommand.java | 4 +- .../ancientgates/commands/CommandCreate.java | 2 +- .../commands/CommandCreateSetFrom.java | 2 - .../ancientgates/commands/CommandHelp.java | 24 +++++---- .../ancientgates/commands/CommandHide.java | 20 +++++++ .../ancientgates/commands/CommandOpen.java | 6 +-- .../ancientgates/commands/CommandSetFrom.java | 46 +++++++++------- .../ancientgates/commands/CommandUnhide.java | 22 ++++++++ .../listeners/PluginPlayerListener.java | 11 +--- 11 files changed, 140 insertions(+), 53 deletions(-) create mode 100644 src/org/mcteam/ancientgates/commands/CommandHide.java create mode 100644 src/org/mcteam/ancientgates/commands/CommandUnhide.java diff --git a/src/org/mcteam/ancientgates/Gate.java b/src/org/mcteam/ancientgates/Gate.java index b04ec07..f03d15e 100644 --- a/src/org/mcteam/ancientgates/Gate.java +++ b/src/org/mcteam/ancientgates/Gate.java @@ -29,6 +29,7 @@ public class Gate private Location from; private Location to; private boolean isHidden = false; + private boolean isOpen = false; private Integer[][] gateBlocks; @@ -57,6 +58,7 @@ public class Gate public void setFrom(Location from) { this.from = from; + setGateBlocks(FloodUtil.getGateFrameBlocks(from.getBlock())); } @@ -83,7 +85,7 @@ public class Gate } - public void setGateBlocks(Set gateBlocks) + private void setGateBlocks(Set gateBlocks) { if (gateBlocks == null) return; @@ -116,6 +118,17 @@ public class Gate if (blocks == null) return false; + if (isHidden() == false) + fillGate(blocks); + + setOpen(true); + + return true; + } + + + private void fillGate(Set blocks) + { // This is not to do an effect // It is to stop portal blocks from destroying themself as they cant rely on non created blocks :P for (Block block : blocks) @@ -123,13 +136,17 @@ public class Gate for (Block block : blocks) block.setType(Material.PORTAL); - - - return true; } public void close() + { + removeGateBlocks(); + setOpen(false); + } + + + private void removeGateBlocks() { if (from != null) { @@ -148,9 +165,20 @@ public class Gate // isHidden Setter and Getter //----------------------------------------------// - public void setHidden(boolean isHidden) + public boolean setHidden(boolean isHidden) { this.isHidden = isHidden; + + if (isHidden == true) + removeGateBlocks(); + + else if (isOpen() && !open()) + { + this.isHidden = false; + return false; + } + + return true; } @@ -160,6 +188,22 @@ public class Gate } + //----------------------------------------------// + // isOpen Setter and Getter + //----------------------------------------------// + + private void setOpen(boolean isOpen) + { + this.isOpen = isOpen; + } + + + public boolean isOpen() + { + return this.isOpen; + } + + //----------------------------------------------// // Persistance and entity management //----------------------------------------------// diff --git a/src/org/mcteam/ancientgates/Plugin.java b/src/org/mcteam/ancientgates/Plugin.java index 32f07ec..d661e98 100644 --- a/src/org/mcteam/ancientgates/Plugin.java +++ b/src/org/mcteam/ancientgates/Plugin.java @@ -64,6 +64,8 @@ public class Plugin extends JavaPlugin commands.add(new CommandClose()); commands.add(new CommandList()); commands.add(new CommandInfo()); + commands.add(new CommandHide()); + commands.add(new CommandUnhide()); // Ensure basefolder exists! this.getDataFolder().mkdirs(); diff --git a/src/org/mcteam/ancientgates/commands/BaseCommand.java b/src/org/mcteam/ancientgates/commands/BaseCommand.java index 4346097..adbd9d2 100644 --- a/src/org/mcteam/ancientgates/commands/BaseCommand.java +++ b/src/org/mcteam/ancientgates/commands/BaseCommand.java @@ -119,7 +119,7 @@ public class BaseCommand // -------------------------------------------- // // Help and usage description // -------------------------------------------- // - public String getUseageTemplate(boolean withColor, boolean withDescription) { + public String getUsageTemplate(boolean withColor, boolean withDescription) { String ret = ""; if (withColor) { @@ -151,7 +151,7 @@ public class BaseCommand } public String getUseageTemplate(boolean withColor) { - return getUseageTemplate(withColor, false); + return getUsageTemplate(withColor, false); } public String getUseageTemplate() { diff --git a/src/org/mcteam/ancientgates/commands/CommandCreate.java b/src/org/mcteam/ancientgates/commands/CommandCreate.java index a674f2e..8c6d2eb 100644 --- a/src/org/mcteam/ancientgates/commands/CommandCreate.java +++ b/src/org/mcteam/ancientgates/commands/CommandCreate.java @@ -24,7 +24,7 @@ public class CommandCreate extends BaseCommand { Gate.create(id); sendMessage("Gate with id \"" + id + "\" was created. Now you should:"); - sendMessage(new CommandSetFrom().getUseageTemplate(true, true)); + sendMessage(new CommandSetFrom().getUsageTemplate(true, true)); Gate.save(); } diff --git a/src/org/mcteam/ancientgates/commands/CommandCreateSetFrom.java b/src/org/mcteam/ancientgates/commands/CommandCreateSetFrom.java index afd3d70..fbc7ebc 100644 --- a/src/org/mcteam/ancientgates/commands/CommandCreateSetFrom.java +++ b/src/org/mcteam/ancientgates/commands/CommandCreateSetFrom.java @@ -63,12 +63,10 @@ public class CommandCreateSetFrom extends BaseCommand if (playerBlock.getType() == Material.AIR) { gate.setFrom(player.getLocation()); - gate.setGateBlocks(gateBlocks); } else if (upBlock.getType() == Material.AIR) { gate.setFrom(playerUpLocation); - gate.setGateBlocks(gateBlocks); } else { diff --git a/src/org/mcteam/ancientgates/commands/CommandHelp.java b/src/org/mcteam/ancientgates/commands/CommandHelp.java index 0ad30b0..3edf5c0 100644 --- a/src/org/mcteam/ancientgates/commands/CommandHelp.java +++ b/src/org/mcteam/ancientgates/commands/CommandHelp.java @@ -65,17 +65,19 @@ public class CommandHelp extends BaseCommand { ArrayList pageLines; pageLines = new ArrayList(); - pageLines.add( new CommandHelp().getUseageTemplate(true, true) ); - pageLines.add( new CommandCreate().getUseageTemplate(true, true) ); - pageLines.add( new CommandCreateSetFrom().getUseageTemplate(true, true) ); - pageLines.add( new CommandDelete().getUseageTemplate(true, true) ); - pageLines.add( new CommandSetFrom().getUseageTemplate(true, true) ); - pageLines.add( new CommandSetTo().getUseageTemplate(true, true) ); - pageLines.add( new CommandOpen().getUseageTemplate(true, true) ); - pageLines.add( new CommandRename().getUseageTemplate(true, true) ); - pageLines.add( new CommandClose().getUseageTemplate(true, true) ); - pageLines.add( new CommandList().getUseageTemplate(true, true) ); - pageLines.add( new CommandInfo().getUseageTemplate(true, true) ); + pageLines.add( new CommandHelp().getUsageTemplate(true, true) ); + pageLines.add( new CommandCreate().getUsageTemplate(true, true) ); + pageLines.add( new CommandCreateSetFrom().getUsageTemplate(true, true) ); + pageLines.add( new CommandDelete().getUsageTemplate(true, true) ); + pageLines.add( new CommandSetFrom().getUsageTemplate(true, true) ); + pageLines.add( new CommandSetTo().getUsageTemplate(true, true) ); + pageLines.add( new CommandOpen().getUsageTemplate(true, true) ); + pageLines.add( new CommandRename().getUsageTemplate(true, true) ); + pageLines.add( new CommandClose().getUsageTemplate(true, true) ); + pageLines.add( new CommandList().getUsageTemplate(true, true) ); + pageLines.add( new CommandInfo().getUsageTemplate(true, true) ); + pageLines.add( new CommandHide().getUsageTemplate(true, true) ); + pageLines.add( new CommandUnhide().getUsageTemplate(true, true) ); helpPages.add(pageLines); } diff --git a/src/org/mcteam/ancientgates/commands/CommandHide.java b/src/org/mcteam/ancientgates/commands/CommandHide.java new file mode 100644 index 0000000..52a17ef --- /dev/null +++ b/src/org/mcteam/ancientgates/commands/CommandHide.java @@ -0,0 +1,20 @@ +package org.mcteam.ancientgates.commands; + +public class CommandHide extends BaseCommand +{ + + public CommandHide() + { + aliases.add("hide"); + + requiredParameters.add("id"); + + helpDescription = "Hide that gate"; + } + + public void perform() + { + gate.setHidden(true); + sendMessage("The gate " + gate.getId() + " is now hidden."); + } +} \ No newline at end of file diff --git a/src/org/mcteam/ancientgates/commands/CommandOpen.java b/src/org/mcteam/ancientgates/commands/CommandOpen.java index d0d49ec..e16ca77 100644 --- a/src/org/mcteam/ancientgates/commands/CommandOpen.java +++ b/src/org/mcteam/ancientgates/commands/CommandOpen.java @@ -15,13 +15,13 @@ public class CommandOpen extends BaseCommand { public void perform() { if (gate.getFrom() == null) { sendMessage("You must set the from location first. To fix that:"); - sendMessage(new CommandSetFrom().getUseageTemplate(true, true)); + sendMessage(new CommandSetFrom().getUsageTemplate(true, true)); return; } if (gate.getTo() == null) { sendMessage("Sure, but note that this gate does not point anywhere :P"); - sendMessage("To fix that: " + new CommandSetTo().getUseageTemplate(true, true)); + sendMessage("To fix that: " + new CommandSetTo().getUsageTemplate(true, true)); } if (gate.getFrom().getBlock().getType() != Material.AIR) { @@ -33,7 +33,7 @@ public class CommandOpen extends BaseCommand { sendMessage("The gate was opened."); } else { sendMessage("Failed to open the gate. Have you built a frame?"); - sendMessage("More info here: " + new CommandHelp().getUseageTemplate(true, true)); + sendMessage("More info here: " + new CommandHelp().getUsageTemplate(true, true)); } } } diff --git a/src/org/mcteam/ancientgates/commands/CommandSetFrom.java b/src/org/mcteam/ancientgates/commands/CommandSetFrom.java index 684239c..83bd9a3 100644 --- a/src/org/mcteam/ancientgates/commands/CommandSetFrom.java +++ b/src/org/mcteam/ancientgates/commands/CommandSetFrom.java @@ -9,9 +9,11 @@ import org.mcteam.ancientgates.Conf; import org.mcteam.ancientgates.Gate; import org.mcteam.ancientgates.util.FloodUtil; -public class CommandSetFrom extends BaseCommand { +public class CommandSetFrom extends BaseCommand +{ - public CommandSetFrom() { + public CommandSetFrom() + { aliases.add("setfrom"); aliases.add("sf"); @@ -20,37 +22,41 @@ public class CommandSetFrom extends BaseCommand { helpDescription = "Set \"from\" to your location."; } - public void perform() { + public void perform() + { // The player might stand in a halfblock or a sign or whatever // Therefore we load som extra locations and blocks Block playerBlock = player.getLocation().getBlock(); Block upBlock = playerBlock.getRelative(BlockFace.UP); Location playerUpLocation = new Location(player.getLocation().getWorld(), - player.getLocation().getX(), - player.getLocation().getY() + 1, - player.getLocation().getZ(), - player.getLocation().getYaw(), - player.getLocation().getPitch()); + player.getLocation().getX(), + player.getLocation().getY() + 1, + player.getLocation().getZ(), + player.getLocation().getYaw(), + player.getLocation().getPitch()); - Set gateBlocks = FloodUtil.getGateFrameBlocks(player.getLocation().getBlock()); - if (gateBlocks == null) { - sendMessage("There is no portal here, or your portal is too large.\nMax size is: " + Conf.getGateMaxArea() + " Blocks."); - return; - } + Set gateBlocks = FloodUtil.getGateFrameBlocks(player.getLocation().getBlock()); + + if (gateBlocks == null) + { + sendMessage("There is no portal here, or your portal is too large.\nMax size is: " + Conf.getGateMaxArea() + " Blocks."); + return; + } - if (playerBlock.getType() == Material.AIR) { + if (playerBlock.getType() == Material.AIR) + { gate.setFrom(player.getLocation()); - gate.setGateBlocks(gateBlocks); - } else if (upBlock.getType() == Material.AIR) { + } + else if (upBlock.getType() == Material.AIR) + { gate.setFrom(playerUpLocation); - gate.setGateBlocks(gateBlocks); - } else { + } + else + { sendMessage("There is not enough room for a gate to open here"); return; } - - sendMessage("From location for gate \""+gate.getId()+"\" is now where you stand."); sendMessage("Your gate includes " + gateBlocks.size() + " Blocks."); diff --git a/src/org/mcteam/ancientgates/commands/CommandUnhide.java b/src/org/mcteam/ancientgates/commands/CommandUnhide.java new file mode 100644 index 0000000..dfc8a12 --- /dev/null +++ b/src/org/mcteam/ancientgates/commands/CommandUnhide.java @@ -0,0 +1,22 @@ +package org.mcteam.ancientgates.commands; + +public class CommandUnhide extends BaseCommand +{ + + public CommandUnhide() + { + aliases.add("unhide"); + + requiredParameters.add("id"); + + helpDescription = "Unhide that gate"; + } + + public void perform() + { + if (gate.setHidden(false)) + sendMessage("The gate " + gate.getId() + " is no longer hidden."); + else + sendMessage("Failed to unhide the gate. Does the portal have a frame?"); + } +} \ No newline at end of file diff --git a/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java b/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java index 773ce34..be65b8f 100644 --- a/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java +++ b/src/org/mcteam/ancientgates/listeners/PluginPlayerListener.java @@ -71,15 +71,8 @@ public class PluginPlayerListener implements Listener }*/ } - if (nearestGate != null) - { - if (nearestGate.isHidden() == false) - { - // Check if player is standing inside portal blocks - if (blockTo.getType() != Material.PORTAL && blockToUp.getType() != Material.PORTAL) - return; - } - + if (nearestGate != null && nearestGate.isOpen()) + { checkChunkLoad(nearestGate.getTo().getBlock()); Float newYaw = nearestGate.getFrom().getYaw() - nearestGate.getTo().getYaw() + playerLocation.getYaw(); Location teleportToLocation = new Location( nearestGate.getTo().getWorld(),