Added ability to define matrial for each gate seperatly.

This commit is contained in:
Tobias Ottenweller 2017-01-01 15:07:45 +01:00
parent 4c7f1201bd
commit 465533f4fe
8 changed files with 126 additions and 56 deletions

View File

@ -71,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() +
@ -103,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

@ -1,6 +1,11 @@
package de.craftinc.gates.commands; package de.craftinc.gates.commands;
import de.craftinc.gates.controllers.PermissionController; 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 class CommandMaterial extends BaseCommand {
@ -9,6 +14,7 @@ public class CommandMaterial extends BaseCommand {
aliases.add("m"); aliases.add("m");
requiredParameters.add("id"); requiredParameters.add("id");
requiredParameters.add("material");
senderMustBePlayer = false; senderMustBePlayer = false;
hasGateParam = true; hasGateParam = true;
@ -19,5 +25,21 @@ public class CommandMaterial extends BaseCommand {
} }
public void perform() { 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

@ -42,11 +42,11 @@ public class CommandTriggerOpen extends BaseCommand {
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.");
} }
gate.setOpen(gate.isOpen()); gate.setOpen(!gate.isOpen());
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
gatesManager.handleGateLocationChange(gate, null, null, null); gatesManager.handleGateLocationChange(gate, null, null, null);
sendMessage(ChatColor.GREEN + "The gate was opened."); sendMessage(ChatColor.GREEN + "The gate is now " + (gate.isOpen() ? "open." : "closed."));
} catch (Exception e) { } catch (Exception e) {
sendMessage(ChatColor.RED + e.getMessage()); 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

@ -34,10 +34,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)'.
@ -64,6 +64,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.
*/ */
@ -128,15 +137,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;
} }
@ -217,7 +217,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) {
@ -230,8 +239,6 @@ public class Gate implements ConfigurationSerializable {
} }
} }
} }
}
/* /*
* INTERFACE: ConfigurationSerializable * INTERFACE: ConfigurationSerializable
@ -240,7 +247,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 locationYawKey = "locationYaw"; static private String locationYawKey = "locationYaw";
static private String locationPitchKey = "locationPitch"; static private String locationPitchKey = "locationPitch";
@ -254,11 +261,11 @@ public class Gate implements ConfigurationSerializable {
try { try {
id = map.get(idKey).toString().toLowerCase(); id = map.get(idKey).toString().toLowerCase();
isHidden = (Boolean) map.get(isHiddenKey);
isOpen = (Boolean) map.get(isOpenKey); isOpen = (Boolean) map.get(isOpenKey);
location = LocationUtil.deserializeLocation((Map<String, Object>) map.get(locationKey)); location = LocationUtil.deserializeLocation((Map<String, Object>) map.get(locationKey));
exit = LocationUtil.deserializeLocation((Map<String, Object>) map.get(exitKey)); exit = LocationUtil.deserializeLocation((Map<String, Object>) map.get(exitKey));
material = new GateMaterial((String)map.get(materialKey));
if (map.containsKey(exitPitchKey)) { if (map.containsKey(exitPitchKey)) {
exit.setPitch(((Number) map.get(exitPitchKey)).floatValue()); exit.setPitch(((Number) map.get(exitPitchKey)).floatValue());
@ -303,9 +310,9 @@ public class Gate implements ConfigurationSerializable {
retVal.put(idKey, id); retVal.put(idKey, id);
retVal.put(locationKey, LocationUtil.serializeLocation(location)); retVal.put(locationKey, LocationUtil.serializeLocation(location));
retVal.put(exitKey, LocationUtil.serializeLocation(exit)); retVal.put(exitKey, LocationUtil.serializeLocation(exit));
retVal.put(isHiddenKey, isHidden);
retVal.put(isOpenKey, isOpen); retVal.put(isOpenKey, isOpen);
retVal.put(allowsVehiclesKey, allowsVehicles); retVal.put(allowsVehiclesKey, allowsVehicles);
retVal.put(materialKey, material.toString());
if (exit != null) { if (exit != null) {
retVal.put(exitPitchKey, exit.getPitch()); retVal.put(exitPitchKey, exit.getPitch());

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 {