Merge pull request #38 from craftinc/feature/command_cleanup

Command cleanup
This commit is contained in:
Tobias Ottenweller 2017-01-01 15:29:44 +01:00 committed by GitHub
commit f20882e03a
25 changed files with 236 additions and 472 deletions

View File

@ -94,9 +94,7 @@ public class Plugin extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
// Save gates
gatesManager.saveGatesToDisk(); gatesManager.saveGatesToDisk();
log("Disabled"); log("Disabled");
} }
@ -122,18 +120,14 @@ public class Plugin extends JavaPlugin {
commands.add(new CommandRemove()); commands.add(new CommandRemove());
commands.add(new CommandLocation()); commands.add(new CommandLocation());
commands.add(new CommandExit()); commands.add(new CommandExit());
commands.add(new CommandOpen()); commands.add(new CommandTriggerOpen());
commands.add(new CommandRename()); commands.add(new CommandRename());
commands.add(new CommandClose());
commands.add(new CommandList()); commands.add(new CommandList());
commands.add(new CommandInfo()); commands.add(new CommandInfo());
commands.add(new CommandHide());
commands.add(new CommandUnhide());
commands.add(new CommandExitOpen());
commands.add(new CommandNearby()); commands.add(new CommandNearby());
commands.add(new CommandAllowRiding()); commands.add(new CommandTriggerVehicles());
commands.add(new CommandDenyRiding()); commands.add(new CommandMaterial());
commands.add(new CommandTeleport());
// Register events // Register events
this.registerEventListeners(); this.registerEventListeners();
@ -168,7 +162,6 @@ public class Plugin extends JavaPlugin {
} }
} }
// -------------------------------------------- // // -------------------------------------------- //
// Commands // Commands
// -------------------------------------------- // // -------------------------------------------- //
@ -184,7 +177,6 @@ public class Plugin extends JavaPlugin {
return this.baseCommand; return this.baseCommand;
} }
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
List<String> parameters = new ArrayList<>(Arrays.asList(args)); List<String> parameters = new ArrayList<>(Arrays.asList(args));
@ -192,7 +184,6 @@ public class Plugin extends JavaPlugin {
return true; return true;
} }
private void handleCommand(CommandSender sender, List<String> parameters) { private void handleCommand(CommandSender sender, List<String> parameters) {
if (parameters.size() == 0) { if (parameters.size() == 0) {
this.commands.get(0).execute(sender, parameters); this.commands.get(0).execute(sender, parameters);
@ -220,7 +211,6 @@ public class Plugin extends JavaPlugin {
log(Level.INFO, msg); log(Level.INFO, msg);
} }
public static void log(Level level, String msg) { public static void log(Level level, String msg) {
Logger.getLogger("Minecraft").log(level, "[" + instance.getDescription().getFullName() + "] " + msg); Logger.getLogger("Minecraft").log(level, "[" + instance.getDescription().getFullName() + "] " + msg);
} }

View File

@ -33,6 +33,7 @@ import de.craftinc.gates.util.TextUtil;
public abstract class BaseCommand { public abstract class BaseCommand {
PermissionController permissionController; PermissionController permissionController;
GatesManager gatesManager;
List<String> aliases = new ArrayList<>(); List<String> aliases = new ArrayList<>();
List<String> requiredParameters = new ArrayList<>(); List<String> requiredParameters = new ArrayList<>();
@ -58,6 +59,7 @@ public abstract class BaseCommand {
} }
public BaseCommand() { public BaseCommand() {
gatesManager = Plugin.getPlugin().getGatesManager();
permissionController = Plugin.getPlugin().getPermissionController(); permissionController = Plugin.getPlugin().getPermissionController();
} }
@ -69,14 +71,14 @@ public abstract class BaseCommand {
return; return;
} }
if (this.senderMustBePlayer) { if (sender instanceof Player) {
this.player = (Player)sender; this.player = (Player)sender;
} }
this.perform(); this.perform();
if (this.shouldPersistToDisk && getSaveOnChanges()) { if (this.shouldPersistToDisk && getSaveOnChanges()) {
Plugin.getPlugin().getGatesManager().saveGatesToDisk(); gatesManager.saveGatesToDisk();
} }
} }
@ -93,12 +95,10 @@ public abstract class BaseCommand {
} }
boolean setGateUsingParameter(String param) { boolean setGateUsingParameter(String param) {
GatesManager gateManager = Plugin.getPlugin().getGatesManager(); if (!gatesManager.gateExists(param)) {
if (!gateManager.gateExists(param)) {
return false; return false;
} else { } else {
gate = gateManager.getGateWithId(param); gate = gatesManager.getGateWithId(param);
return true; return true;
} }
} }

View File

@ -1,53 +0,0 @@
/* Craft Inc. Gates
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.gates.commands;
import java.util.logging.Level;
import de.craftinc.gates.controllers.PermissionController;
import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin;
public class CommandClose extends BaseCommand {
public CommandClose() {
aliases.add("close");
aliases.add("c");
requiredParameters.add("id");
helpDescription = "Closes a gate to prevent players from using it.";
requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
senderMustBePlayer = false;
}
@Override
public void perform() {
try {
gate.setOpen(false);
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "The gate was closed.");
} catch (Exception e) {
sendMessage(ChatColor.RED + "Opening the gate failed! See server log for more information");
Plugin.log(Level.WARNING, e.getMessage());
e.printStackTrace();
}
}
}

View File

@ -1,41 +0,0 @@
/* Craft Inc. Gates
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.gates.commands;
import de.craftinc.gates.controllers.PermissionController;
import org.bukkit.ChatColor;
public class CommandDenyRiding extends BaseCommand {
public CommandDenyRiding() {
aliases.add("denyRiding");
aliases.add("dr");
requiredParameters.add("id");
helpDescription = "Deny players to travel while riding.";
requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
senderMustBePlayer = false;
}
@Override
protected void perform() {
gate.setAllowsVehicles(false);
sendMessage(ChatColor.GREEN + "Traveling while riding is now disabled for this gate.");
}
}

View File

@ -30,25 +30,20 @@ public class CommandExit extends BaseCommand {
public CommandExit() { public CommandExit() {
aliases.add("exit"); aliases.add("exit");
aliases.add("e"); aliases.add("e");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Change exit of location."; helpDescription = "Change exit of location.";
requiredPermission = PermissionController.permissionManage; requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = true; needsPermissionAtCurrentLocation = true;
shouldPersistToDisk = true; shouldPersistToDisk = true;
senderMustBePlayer = true; senderMustBePlayer = true;
} }
public void perform() { public void perform() {
try { try {
Location oldExit = gate.getExit(); Location oldExit = gate.getExit();
gate.setExit(player.getLocation()); gate.setExit(player.getLocation());
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand."); sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
Plugin.getPlugin().getGatesManager().handleGateExitChange(gate, oldExit); gatesManager.handleGateExitChange(gate, oldExit);
} catch (Exception e) { } catch (Exception e) {
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.RED + "Setting the exit for the gate failed! See server log for more information"); sendMessage(ChatColor.RED + "Setting the exit for the gate failed! See server log for more information");

View File

@ -1,76 +0,0 @@
/* Craft Inc. Gates
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.gates.commands;
import de.craftinc.gates.controllers.PermissionController;
import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import java.util.logging.Level;
public class CommandExitOpen extends BaseCommand {
public CommandExitOpen() {
aliases.add("exitopen");
aliases.add("eo");
requiredParameters.add("id");
helpDescription = "Change exit of location and open that gate afterwards.";
requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = true;
shouldPersistToDisk = true;
senderMustBePlayer = true;
}
public void perform() {
try {
Location oldExit = gate.getExit();
gate.setExit(player.getLocation());
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
Plugin.getPlugin().getGatesManager().handleGateExitChange(gate, oldExit);
try {
boolean needsGateManagerUpdate = false;
if (gate.getGateBlockLocations().isEmpty()) {
needsGateManagerUpdate = true;
}
gate.setOpen(true);
GateBlockChangeSender.updateGateBlocks(gate);
if (needsGateManagerUpdate) {
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, null, null, null);
}
sendMessage(ChatColor.GREEN + "The gate was opened.");
} catch (Exception e) {
sendMessage(ChatColor.RED + e.getMessage());
}
} catch (Exception e) {
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.RED + "Setting the exit for the gate failed! This gate is now closed! (See server log for more information.)");
Plugin.log(Level.WARNING, e.getMessage());
e.printStackTrace();
}
}
}

View File

@ -19,88 +19,50 @@ package de.craftinc.gates.commands;
import de.craftinc.gates.controllers.PermissionController; import de.craftinc.gates.controllers.PermissionController;
import de.craftinc.gates.util.TextUtil; import de.craftinc.gates.util.TextUtil;
import java.util.ArrayList; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class CommandHelp extends BaseCommand { public class CommandHelp extends BaseCommand {
private static List<List<String>> helpPages; private static List<String> help;
static {
// sort the usage strings
List<String> allUsageStrings = new ArrayList<>();
allUsageStrings.add(new CommandHelp().getUsageTemplate(true));
allUsageStrings.add(new CommandNew().getUsageTemplate(true));
allUsageStrings.add(new CommandRemove().getUsageTemplate(true));
allUsageStrings.add(new CommandLocation().getUsageTemplate(true));
allUsageStrings.add(new CommandExit().getUsageTemplate(true));
allUsageStrings.add(new CommandOpen().getUsageTemplate(true));
allUsageStrings.add(new CommandRename().getUsageTemplate(true));
allUsageStrings.add(new CommandClose().getUsageTemplate(true));
allUsageStrings.add(new CommandList().getUsageTemplate(true));
allUsageStrings.add(new CommandInfo().getUsageTemplate(true));
allUsageStrings.add(new CommandHide().getUsageTemplate(true));
allUsageStrings.add(new CommandUnhide().getUsageTemplate(true));
allUsageStrings.add(new CommandExitOpen().getUsageTemplate(true));
allUsageStrings.add(new CommandNearby().getUsageTemplate(true));
Collections.sort(allUsageStrings);
// put 5 commands on one page
helpPages = new ArrayList<>();
while (!allUsageStrings.isEmpty()) {
int toIndex = allUsageStrings.size() >= 6 ? 5 : allUsageStrings.size();
List<String> currentHelpPage = new ArrayList<>(allUsageStrings.subList(0, toIndex));
helpPages.add(currentHelpPage);
allUsageStrings.removeAll(currentHelpPage);
}
}
public CommandHelp() { public CommandHelp() {
aliases.add("help"); aliases.add("help");
aliases.add("?"); aliases.add("?");
optionalParameters.add("page");
helpDescription = "prints this help page"; helpDescription = "prints this help page";
requiredPermission = PermissionController.permissionInfo; requiredPermission = PermissionController.permissionInfo;
hasGateParam = false; hasGateParam = false;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = false; shouldPersistToDisk = false;
senderMustBePlayer = false; senderMustBePlayer = false;
} }
public void perform() { public void perform() {
int page; sendMessage(TextUtil.titleSize("Craft Inc. Gates Help"));
sendMessage(getHelp());
if (parameters.size() > 0) {
try {
page = Integer.parseInt(parameters.get(0));
} catch (NumberFormatException e) {
// wasn't an integer
page = 1;
}
} else {
page = 1;
} }
sendMessage(TextUtil.titleSize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")")); private List<String> getHelp() {
if (help == null) {
page -= 1; help = Arrays.asList(
if (page < 0 || page >= helpPages.size()) { new CommandHelp().getUsageTemplate(true),
sendMessage("This page does not exist"); new CommandNew().getUsageTemplate(true),
return; new CommandRemove().getUsageTemplate(true),
new CommandLocation().getUsageTemplate(true),
new CommandExit().getUsageTemplate(true),
new CommandTriggerOpen().getUsageTemplate(true),
new CommandRename().getUsageTemplate(true),
new CommandList().getUsageTemplate(true),
new CommandInfo().getUsageTemplate(true),
new CommandNearby().getUsageTemplate(true),
new CommandTriggerVehicles().getUsageTemplate(true),
new CommandTeleport().getUsageTemplate(true),
new CommandMaterial().getUsageTemplate(true)
);
Collections.sort(help);
} }
sendMessage(helpPages.get(page)); return help;
} }
} }

View File

@ -1,57 +0,0 @@
/* Craft Inc. Gates
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.gates.commands;
import java.util.logging.Level;
import de.craftinc.gates.controllers.PermissionController;
import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin;
public class CommandHide extends BaseCommand {
public CommandHide() {
aliases.add("hide");
aliases.add("h");
requiredParameters.add("id");
helpDescription = "Makes a gate NOT consist of gate blocks while open.";
requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
senderMustBePlayer = false;
}
public void perform() {
try {
gate.setHidden(true);
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "The gate '" + gate.getId() + "' is now hidden.");
} catch (Exception e) {
sendMessage(ChatColor.RED + "Hiding the gate failed! See server log for more information");
Plugin.log(Level.WARNING, e.getMessage());
e.printStackTrace();
}
}
}

View File

@ -32,20 +32,15 @@ public class CommandInfo extends BaseCommand {
public CommandInfo() { public CommandInfo() {
aliases.add("info"); aliases.add("info");
aliases.add("i"); aliases.add("i");
optionalParameters.add("id"); optionalParameters.add("id");
helpDescription = "Print detailed information about a certain or the closest gate."; helpDescription = "Print detailed information about a certain or the closest gate.";
requiredPermission = PermissionController.permissionInfo; requiredPermission = PermissionController.permissionInfo;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = false; shouldPersistToDisk = false;
senderMustBePlayer = false; senderMustBePlayer = false;
hasGateParam = false; hasGateParam = false;
} }
public void perform() { public void perform() {
if (this.parameters.size() > 0) { if (this.parameters.size() > 0) {
@ -64,7 +59,7 @@ public class CommandInfo extends BaseCommand {
} }
Player p = (Player) this.sender; Player p = (Player) this.sender;
this.gate = Plugin.getPlugin().getGatesManager().getNearestGate(p.getLocation()); this.gate = gatesManager.getNearestGate(p.getLocation());
if (!this.hasPermission() || this.gate == null) { if (!this.hasPermission() || this.gate == null) {
sendMessage(ChatColor.RED + "There is either no gate nearby or you do not have permission to " + this.helpDescription.toLowerCase()); sendMessage(ChatColor.RED + "There is either no gate nearby or you do not have permission to " + this.helpDescription.toLowerCase());
@ -76,19 +71,16 @@ public class CommandInfo extends BaseCommand {
sendMessage(TextUtil.titleSize("Information about closest gate: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'")); sendMessage(TextUtil.titleSize("Information about closest gate: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
} }
String openHiddenMessage = ChatColor.DARK_AQUA + "This gate is"; String openMessage = ChatColor.DARK_AQUA + "This gate is";
if (gate.isOpen()) if (gate.isOpen())
openHiddenMessage += ChatColor.AQUA + " open"; openMessage += ChatColor.AQUA + " open";
else else
openHiddenMessage += ChatColor.AQUA + " closed"; openMessage += ChatColor.AQUA + " closed";
if (gate.isHidden()) openMessage += ".\n";
openHiddenMessage += ChatColor.DARK_AQUA + " and" + ChatColor.AQUA + " hidden";
openHiddenMessage += ".\n"; sendMessage(openMessage);
sendMessage(openHiddenMessage);
if (gate.getLocation() != null) if (gate.getLocation() != null)
sendMessage(ChatColor.DARK_AQUA + "location: " + ChatColor.AQUA + "( " + (int) gate.getLocation().getX() + sendMessage(ChatColor.DARK_AQUA + "location: " + ChatColor.AQUA + "( " + (int) gate.getLocation().getX() +
@ -108,6 +100,8 @@ public class CommandInfo extends BaseCommand {
if (gate.getAllowsVehicles()) if (gate.getAllowsVehicles())
sendMessage(ChatColor.DARK_AQUA + "You can ride through this gate."); sendMessage(ChatColor.DARK_AQUA + "You can ride through this gate.");
sendMessage(ChatColor.DARK_AQUA + "This gate is made of "
+ ChatColor.AQUA + gate.getMaterial() + ChatColor.DARK_AQUA + ".");
if (this.sender instanceof Player) { if (this.sender instanceof Player) {
HashSet<Gate> set = new HashSet<>(); HashSet<Gate> set = new HashSet<>();

View File

@ -25,12 +25,11 @@ import de.craftinc.gates.controllers.PermissionController;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import de.craftinc.gates.models.Gate; import de.craftinc.gates.models.Gate;
import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.TextUtil; import de.craftinc.gates.util.TextUtil;
public class CommandList extends BaseCommand { public class CommandList extends BaseCommand {
private static final int linesPerPage = 10; private static final int linesPerPage = 15;
/* this is actually not true. the font used by Minecraft is not /* this is actually not true. the font used by Minecraft is not
monospaced. but there seems to be no (easy) way to calculate monospaced. but there seems to be no (easy) way to calculate
@ -64,7 +63,7 @@ public class CommandList extends BaseCommand {
} }
if (page > allPages.size() || page < 1) { if (page > allPages.size() || page < 1) {
sendMessage(ChatColor.RED + "The requested page is not availible"); sendMessage(ChatColor.RED + "The requested page is not available");
return; return;
} }
@ -148,7 +147,7 @@ public class CommandList extends BaseCommand {
* Method for getting a collection of gates the player is allowed to see. * Method for getting a collection of gates the player is allowed to see.
*/ */
private Collection<Gate> getAllGates() { private Collection<Gate> getAllGates() {
Collection<Gate> gates = Plugin.getPlugin().getGatesManager().allGates(); Collection<Gate> gates = gatesManager.allGates();
// create a copy since we cannot iterate over a collection while modifying it! // create a copy since we cannot iterate over a collection while modifying it!
Collection<Gate> gatesCopy = new ArrayList<>(gates); Collection<Gate> gatesCopy = new ArrayList<>(gates);

View File

@ -23,7 +23,6 @@ import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import de.craftinc.gates.Plugin;
import org.bukkit.block.Block; import org.bukkit.block.Block;
public class CommandLocation extends BaseLocationCommand { public class CommandLocation extends BaseLocationCommand {
@ -62,9 +61,9 @@ public class CommandLocation extends BaseLocationCommand {
sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location."); sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location.");
} catch (Exception e) { } catch (Exception e) {
sendMessage(ChatColor.RED + "There seems to be no frame at your new location! The gate got closed!" + ChatColor.AQUA + " You should build a frame now and execute:"); sendMessage(ChatColor.RED + "There seems to be no frame at your new location! The gate got closed!" + ChatColor.AQUA + " You should build a frame now and execute:");
sendMessage(new CommandOpen().getUsageTemplate(true)); sendMessage(new CommandTriggerOpen().getUsageTemplate(true));
} finally { } finally {
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, oldLocation, oldGateBlockLocations, oldFrameBlocks); gatesManager.handleGateLocationChange(gate, oldLocation, oldGateBlockLocations, oldFrameBlocks);
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
} }
} }

View File

@ -0,0 +1,45 @@
package de.craftinc.gates.commands;
import de.craftinc.gates.controllers.PermissionController;
import de.craftinc.gates.models.GateMaterial;
import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor;
import java.security.InvalidParameterException;
public class CommandMaterial extends BaseCommand {
public CommandMaterial() {
aliases.add("material");
aliases.add("m");
requiredParameters.add("id");
requiredParameters.add("material");
senderMustBePlayer = false;
hasGateParam = true;
helpDescription = "Change the material of a gate";
requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
}
public void perform() {
GateMaterial material;
try {
material = new GateMaterial(parameters.get(1));
} catch (InvalidParameterException e) {
sendMessage(ChatColor.RED + "Invalid material!");
return;
}
try {
gate.setMaterial(material);
} catch (Exception e) {
sendMessage(ChatColor.RED + "Frame invalid. Gate is now closed!");
}
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "Gate " + gate.getId() + " uses now " + material.toString() + " as material.");
}
}

View File

@ -1,9 +1,7 @@
package de.craftinc.gates.commands; package de.craftinc.gates.commands;
import de.craftinc.gates.models.Gate; import de.craftinc.gates.models.Gate;
import de.craftinc.gates.controllers.GatesManager;
import de.craftinc.gates.controllers.PermissionController; import de.craftinc.gates.controllers.PermissionController;
import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.GateBlockChangeSender; import de.craftinc.gates.util.GateBlockChangeSender;
import de.craftinc.gates.util.TextUtil; import de.craftinc.gates.util.TextUtil;
@ -25,8 +23,7 @@ public class CommandNearby extends BaseLocationCommand {
} }
public void perform() { public void perform() {
GatesManager manager = Plugin.getPlugin().getGatesManager(); Set<Gate> nearbyGates = gatesManager.getNearbyGates(player.getLocation().getChunk());
Set<Gate> nearbyGates = manager.getNearbyGates(player.getLocation().getChunk());
if (nearbyGates == null) { if (nearbyGates == null) {
player.sendMessage("There are no gates near you!"); player.sendMessage("There are no gates near you!");

View File

@ -21,13 +21,12 @@ import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import de.craftinc.gates.models.Gate; import de.craftinc.gates.models.Gate;
import de.craftinc.gates.controllers.GatesManager;
import de.craftinc.gates.Plugin;
public class CommandNew extends BaseLocationCommand { public class CommandNew extends BaseLocationCommand {
public CommandNew() { public CommandNew() {
aliases.add("new"); aliases.add("new");
aliases.add("create");
aliases.add("n"); aliases.add("n");
requiredParameters.add("id"); requiredParameters.add("id");
@ -43,7 +42,6 @@ public class CommandNew extends BaseLocationCommand {
public void perform() { public void perform() {
String id = parameters.get(0); String id = parameters.get(0);
GatesManager gatesManager = Plugin.getPlugin().getGatesManager();
if (gatesManager.gateExists(id)) { if (gatesManager.gateExists(id)) {
sendMessage(ChatColor.RED + "Creating the gate failed! " + "A gate with the supplied id already exists!"); sendMessage(ChatColor.RED + "Creating the gate failed! " + "A gate with the supplied id already exists!");

View File

@ -20,8 +20,6 @@ import de.craftinc.gates.controllers.PermissionController;
import de.craftinc.gates.util.GateBlockChangeSender; import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin;
public class CommandRemove extends BaseCommand { public class CommandRemove extends BaseCommand {
public CommandRemove() { public CommandRemove() {
@ -43,7 +41,7 @@ public class CommandRemove extends BaseCommand {
} }
public void perform() { public void perform() {
Plugin.getPlugin().getGatesManager().handleDeletion(gate); gatesManager.handleDeletion(gate);
GateBlockChangeSender.updateGateBlocks(gate, true); GateBlockChangeSender.updateGateBlocks(gate, true);
sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted."); sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted.");
} }

View File

@ -19,9 +19,6 @@ package de.craftinc.gates.commands;
import de.craftinc.gates.controllers.PermissionController; import de.craftinc.gates.controllers.PermissionController;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import de.craftinc.gates.controllers.GatesManager;
import de.craftinc.gates.Plugin;
public class CommandRename extends BaseCommand { public class CommandRename extends BaseCommand {
public CommandRename() { public CommandRename() {
@ -30,23 +27,16 @@ public class CommandRename extends BaseCommand {
hasGateParam = true; hasGateParam = true;
senderMustBePlayer = false; senderMustBePlayer = false;
requiredParameters.add("current name"); requiredParameters.add("current name");
requiredParameters.add("new name"); requiredParameters.add("new name");
helpDescription = "Changes the id of a gate."; helpDescription = "Changes the id of a gate.";
requiredPermission = PermissionController.permissionManage; requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true; shouldPersistToDisk = true;
senderMustBePlayer = false;
} }
public void perform() { public void perform() {
String newId = parameters.get(1); String newId = parameters.get(1);
GatesManager gatesManager = Plugin.getPlugin().getGatesManager();
if (gatesManager.gateExists(newId)) { if (gatesManager.gateExists(newId)) {
sendMessage(ChatColor.RED + "Cannot rename " + gate.getId() + ". There is already a gate named " + newId + "."); sendMessage(ChatColor.RED + "Cannot rename " + gate.getId() + ". There is already a gate named " + newId + ".");

View File

@ -0,0 +1,23 @@
package de.craftinc.gates.commands;
import de.craftinc.gates.controllers.PermissionController;
public class CommandTeleport extends BaseCommand {
public CommandTeleport() {
aliases.add("teleport");
aliases.add("t");
requiredParameters.add("id");
senderMustBePlayer = true;
hasGateParam = true;
helpDescription = "Teleport to the location of a gate";
requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = false;
}
public void perform() {
}
}

View File

@ -20,16 +20,14 @@ import de.craftinc.gates.controllers.PermissionController;
import de.craftinc.gates.util.GateBlockChangeSender; import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin; public class CommandTriggerOpen extends BaseCommand {
public class CommandOpen extends BaseCommand { public CommandTriggerOpen() {
aliases.add("triggerOpen");
public CommandOpen() {
aliases.add("open");
aliases.add("o"); aliases.add("o");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Open a gate so players can use it."; helpDescription = "Open/close a gate.";
requiredPermission = PermissionController.permissionManage; requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
@ -39,21 +37,16 @@ public class CommandOpen extends BaseCommand {
public void perform() { public void perform() {
try { try {
boolean needsGateManagerUpdate = false; if (!gate.isOpen() && gate.getExit() == null && player != null) {
gate.setExit(player.getLocation());
if (gate.getGateBlockLocations().isEmpty()) { sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
needsGateManagerUpdate = true;
} }
gate.setOpen(true); gate.setOpen(!gate.isOpen());
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
gatesManager.handleGateLocationChange(gate, null, null, null);
if (needsGateManagerUpdate) { sendMessage(ChatColor.GREEN + "The gate is now " + (gate.isOpen() ? "open." : "closed."));
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, null, null, null);
}
sendMessage(ChatColor.GREEN + "The gate was opened.");
} catch (Exception e) { } catch (Exception e) {
sendMessage(ChatColor.RED + e.getMessage()); sendMessage(ChatColor.RED + e.getMessage());
} }

View File

@ -19,25 +19,29 @@ package de.craftinc.gates.commands;
import de.craftinc.gates.controllers.PermissionController; import de.craftinc.gates.controllers.PermissionController;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
public class CommandAllowRiding extends BaseCommand { public class CommandTriggerVehicles extends BaseCommand {
public CommandAllowRiding() { public CommandTriggerVehicles() {
aliases.add("allowRiding"); aliases.add("vehicles");
aliases.add("ar"); aliases.add("v");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Allow players to travel while riding."; helpDescription = "Allow/deny players to travel while riding.";
requiredPermission = PermissionController.permissionManage; requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true; shouldPersistToDisk = true;
senderMustBePlayer = false; senderMustBePlayer = false;
} }
@Override @Override
protected void perform() { protected void perform() {
gate.setAllowsVehicles(true); gate.setAllowsVehicles(gate.getAllowsVehicles());
if (gate.getAllowsVehicles()) {
sendMessage(ChatColor.GREEN + "Traveling while riding is now enabled for this gate."); sendMessage(ChatColor.GREEN + "Traveling while riding is now enabled for this gate.");
} else {
sendMessage(ChatColor.GREEN + "Traveling while riding is now disabled for this gate.");
}
} }
} }

View File

@ -1,46 +0,0 @@
/* Craft Inc. Gates
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.gates.commands;
import de.craftinc.gates.controllers.PermissionController;
import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor;
public class CommandUnhide extends BaseCommand {
public CommandUnhide() {
aliases.add("unhide");
aliases.add("u");
requiredParameters.add("id");
helpDescription = "Make that gate visible";
requiredPermission = PermissionController.permissionManage;
needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
senderMustBePlayer = false;
}
public void perform() {
try {
gate.setHidden(false);
GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "The gate " + gate.getId() + " is now visible.");
} catch (Exception e) {
sendMessage(ChatColor.RED + e.getMessage());
}
}
}

View File

@ -19,6 +19,7 @@ package de.craftinc.gates.listeners;
import de.craftinc.gates.models.Gate; import de.craftinc.gates.models.Gate;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.GateBlockChangeSender; import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.Material;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -34,7 +35,7 @@ public class BlockBreakListener implements Listener {
Gate gate = Plugin.getPlugin().getGatesManager().getGateAtFrameLocation(event.getBlock().getLocation()); Gate gate = Plugin.getPlugin().getGatesManager().getGateAtFrameLocation(event.getBlock().getLocation());
if (gate != null && !gate.isHidden()) { if (gate != null && gate.getMaterial().getMaterial() != Material.AIR) {
try { try {
gate.setOpen(false); gate.setOpen(false);
} catch (Exception ignored) {} } catch (Exception ignored) {}

View File

@ -33,10 +33,10 @@ public class Gate implements ConfigurationSerializable {
private Set<Location> gateBlockLocations = new HashSet<>(); /* Locations of the blocks inside the gate */ private Set<Location> gateBlockLocations = new HashSet<>(); /* Locations of the blocks inside the gate */
private Set<Block> gateFrameBlocks = new HashSet<>(); private Set<Block> gateFrameBlocks = new HashSet<>();
private Location exit; private Location exit;
private boolean isHidden = false;
private boolean isOpen = false; private boolean isOpen = false;
private boolean allowsVehicles = true; private boolean allowsVehicles = true;
private String id; private String id;
private GateMaterial material = new GateMaterial(Material.PORTAL);
/** /**
* You should never create two gates with the same 'id'. Also see 'setId(String id)'. * You should never create two gates with the same 'id'. Also see 'setId(String id)'.
@ -63,6 +63,15 @@ public class Gate implements ConfigurationSerializable {
} }
} }
public GateMaterial getMaterial() {
return material;
}
public void setMaterial(GateMaterial material) throws Exception {
this.material = material;
validate();
}
/** /**
* @return This method might return a 'null' data. * @return This method might return a 'null' data.
*/ */
@ -127,15 +136,6 @@ public class Gate implements ConfigurationSerializable {
this.id = id.toLowerCase(); this.id = id.toLowerCase();
} }
public boolean isHidden() {
return isHidden;
}
public void setHidden(boolean isHidden) throws Exception {
this.isHidden = isHidden;
this.validate();
}
public boolean isOpen() { public boolean isOpen() {
return isOpen; return isOpen;
} }
@ -216,7 +216,16 @@ public class Gate implements ConfigurationSerializable {
throw new Exception("Gate got closed. The frame is missing or broken. (no gate blocks)"); throw new Exception("Gate got closed. The frame is missing or broken. (no gate blocks)");
} }
if (!isHidden() && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confCheckForBrokenGateFramesKey)) { validateFrame();
}
private void validateFrame() throws Exception {
boolean isAir = material.getMaterial() == Material.AIR;
boolean ignore = !Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confCheckForBrokenGateFramesKey);
if (isAir || ignore) {
return;
}
for (Block b : gateFrameBlocks) { for (Block b : gateFrameBlocks) {
@ -229,7 +238,6 @@ public class Gate implements ConfigurationSerializable {
} }
} }
} }
}
/* /*
* INTERFACE: ConfigurationSerializable * INTERFACE: ConfigurationSerializable
@ -238,7 +246,7 @@ public class Gate implements ConfigurationSerializable {
static private String locationKey = "location"; static private String locationKey = "location";
static private String gateBlocksKey = "gateBlocks"; static private String gateBlocksKey = "gateBlocks";
static private String exitKey = "exit"; static private String exitKey = "exit";
static private String isHiddenKey = "hidden"; static private String materialKey = "material";
static private String isOpenKey = "open"; static private String isOpenKey = "open";
static private String allowsVehiclesKey = "allowsVehiclesKey"; static private String allowsVehiclesKey = "allowsVehiclesKey";
@ -248,7 +256,7 @@ public class Gate implements ConfigurationSerializable {
id = map.get(idKey).toString().toLowerCase(); id = map.get(idKey).toString().toLowerCase();
location = (Location) map.get(locationKey); location = (Location) map.get(locationKey);
exit = (Location) map.get(exitKey); exit = (Location) map.get(exitKey);
isHidden = (Boolean) map.get(isHiddenKey); material = new GateMaterial((String)map.get(materialKey));
isOpen = (Boolean) map.get(isOpenKey); isOpen = (Boolean) map.get(isOpenKey);
allowsVehicles = (Boolean) map.get(allowsVehiclesKey); allowsVehicles = (Boolean) map.get(allowsVehiclesKey);
gateBlockLocations = (Set<Location>) map.get(gateBlocksKey); gateBlockLocations = (Set<Location>) map.get(gateBlocksKey);
@ -272,10 +280,10 @@ public class Gate implements ConfigurationSerializable {
Map<String, Object> retVal = new HashMap<>(); Map<String, Object> retVal = new HashMap<>();
retVal.put(idKey, id); retVal.put(idKey, id);
retVal.put(isHiddenKey, isHidden);
retVal.put(isOpenKey, isOpen); retVal.put(isOpenKey, isOpen);
retVal.put(allowsVehiclesKey, allowsVehicles); retVal.put(allowsVehiclesKey, allowsVehicles);
retVal.put(gateBlocksKey, gateBlockLocations); retVal.put(gateBlocksKey, gateBlockLocations);
retVal.put(materialKey, material.toString());
if (exit != null) { if (exit != null) {
retVal.put(exitKey, exit.serialize()); retVal.put(exitKey, exit.serialize());

View File

@ -5,13 +5,19 @@ import org.bukkit.Material;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
public class GateMaterial { public class GateMaterial {
private Material material; private Material material;
GateMaterial(Material material) {
this.material = material;
}
public GateMaterial(String materialString) throws InvalidParameterException { public GateMaterial(String materialString) throws InvalidParameterException {
Material material; Material material;
switch (materialString) { switch (materialString) {
case "air":
material = Material.AIR;
break;
case "sapling": case "sapling":
material = Material.SAPLING; material = Material.SAPLING;
break; break;
@ -85,6 +91,60 @@ public class GateMaterial {
this.material = material; this.material = material;
} }
@Override
public String toString() {
switch (material) {
case AIR:
return "air";
case SAPLING:
return "sapling";
case STATIONARY_WATER:
return "water";
case STATIONARY_LAVA:
return "lava";
case WEB:
return "cobweb";
case LONG_GRASS:
return "grass";
case DEAD_BUSH:
return "dead bush";
case YELLOW_FLOWER:
return "dandelion";
case RED_ROSE:
return "poppy";
case BROWN_MUSHROOM:
return "brown mushroom";
case RED_MUSHROOM:
return "red mushroom";
case TORCH:
return "torch";
case REDSTONE_TORCH_OFF:
return "redstone torch (off)";
case REDSTONE_TORCH_ON:
return "redstone torch (on)";
case FENCE:
return "fence";
case PORTAL:
return "nether portal";
case IRON_FENCE:
return "iron bars";
case THIN_GLASS:
return "glass pane";
case FENCE_GATE:
return "fence gate";
case NETHER_FENCE:
return "nether brick fence";
case NETHER_WARTS:
return "nether wart";
case ENDER_PORTAL:
return "end portal";
case COBBLE_WALL:
return "cobblestone wall";
default:
return "nether portal";
}
}
public Material getMaterial() { public Material getMaterial() {
return material; return material;
} }

View File

@ -16,12 +16,6 @@
*/ */
package de.craftinc.gates.util; package de.craftinc.gates.util;
import de.craftinc.gates.Plugin;
import de.craftinc.gates.models.GateMaterial;
import java.security.InvalidParameterException;
import java.util.logging.Level;
public class ConfigurationUtil { public class ConfigurationUtil {
public static final String confMaxGateBlocksKey = "maxGateBlocks"; public static final String confMaxGateBlocksKey = "maxGateBlocks";
public static final String confPlayerGateBlockUpdateRadiusKey = "playerGateBlockUpdateRadius"; public static final String confPlayerGateBlockUpdateRadiusKey = "playerGateBlockUpdateRadius";
@ -33,17 +27,4 @@ public class ConfigurationUtil {
public static final String confShowTeleportNoPermissionMessageKey = "showTeleportNoPermissionMessage"; public static final String confShowTeleportNoPermissionMessageKey = "showTeleportNoPermissionMessage";
public static final String confSaveOnChangesKey = "saveOnChanges"; public static final String confSaveOnChangesKey = "saveOnChanges";
public static final String confHighlightDurationKey = "highlightDuration"; public static final String confHighlightDurationKey = "highlightDuration";
public static final String confGateMaterialKey = "gateMaterial";
static GateMaterial getPortalMaterial() {
String materialString = Plugin.getPlugin().getConfig().getString(confGateMaterialKey);
try {
return new GateMaterial(materialString);
} catch (InvalidParameterException ignored) {
Plugin.log(Level.WARNING, "Gate material invalid! Please check and correct your configuration file!");
return new GateMaterial("nether portal");
}
}
} }

View File

@ -76,7 +76,7 @@ public class GateBlockChangeSender {
} }
for (Gate g : gatesNearby) { for (Gate g : gatesNearby) {
if (!g.isOpen() || g.isHidden()) { if (!g.isOpen()) {
continue; continue;
} }
sendGateBlockChanges(g, true, player); sendGateBlockChanges(g, true, player);
@ -124,7 +124,7 @@ public class GateBlockChangeSender {
} }
} }
boolean isVisible = gate.isOpen() && !gate.isHidden() && !remove; boolean isVisible = gate.isOpen() && !remove;
for (Player p : playersNearby) { for (Player p : playersNearby) {
sendGateBlockChanges(gate, isVisible, p); sendGateBlockChanges(gate, isVisible, p);
} }
@ -157,7 +157,7 @@ public class GateBlockChangeSender {
Material material; Material material;
if (isVisible) { if (isVisible) {
GateMaterial gm = getPortalMaterial(); GateMaterial gm = gate.getMaterial();
data = gm.getData(gate.getDirection()); data = gm.getData(gate.getDirection());
material = gm.getMaterial(); material = gm.getMaterial();
} else { } else {