From 6e658003e013d08af651c9cd155c6b63798f76b0 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 15 Oct 2021 19:23:17 +0200 Subject: [PATCH] Improves Gate comments where possible Renames types to characterMaterialMap Simplifies writeConfig to a single method --- .../net/knarcraft/stargate/portal/Gate.java | 140 ++++++++---------- 1 file changed, 58 insertions(+), 82 deletions(-) diff --git a/src/main/java/net/knarcraft/stargate/portal/Gate.java b/src/main/java/net/knarcraft/stargate/portal/Gate.java index 92145b4..1a7b6d6 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Gate.java +++ b/src/main/java/net/knarcraft/stargate/portal/Gate.java @@ -23,13 +23,11 @@ public class Gate { private final String filename; private final GateLayout layout; - private final Map types; - + private final Map characterMaterialMap; //Gate materials private final Material portalOpenBlock; private final Material portalClosedBlock; private final Material portalButton; - //Economy information private final int useCost; private final int createCost; @@ -39,23 +37,23 @@ public class Gate { /** * Instantiates a new gate * - * @param filename

The name of the gate which equal the name of the file

- * @param layout

The character layout defined in the gate file

- * @param types

The block types the different layout characters represent

- * @param portalOpenBlock

The material to set the non-frame to when the portal is open

- * @param portalClosedBlock

The material to set the non-frame to when the portal is closed

- * @param portalButton

The material to use for the portal button

- * @param useCost

The cost of using a portal with this gate layout (-1 to disable)

- * @param createCost

The cost of creating a portal with this gate layout (-1 to disable)

- * @param destroyCost

The cost of destroying a portal with this gate layout (-1 to disable)

- * @param toOwner

Whether any payment should go to the owner of the gate, as opposed to just disappearing

+ * @param filename

The name of the gate file, including extension

+ * @param layout

The gate layout defined in the gate file

+ * @param characterMaterialMap

The material types the different layout characters represent

+ * @param portalOpenBlock

The material to set the opening to when the portal is open

+ * @param portalClosedBlock

The material to set the opening to when the portal is closed

+ * @param portalButton

The material to use for the portal button

+ * @param useCost

The cost of using a portal with this gate layout (-1 to disable)

+ * @param createCost

The cost of creating a portal with this gate layout (-1 to disable)

+ * @param destroyCost

The cost of destroying a portal with this gate layout (-1 to disable)

+ * @param toOwner

Whether any payment should go to the owner of the gate, as opposed to just disappearing

*/ - public Gate(String filename, GateLayout layout, Map types, Material portalOpenBlock, + public Gate(String filename, GateLayout layout, Map characterMaterialMap, Material portalOpenBlock, Material portalClosedBlock, Material portalButton, int useCost, int createCost, int destroyCost, boolean toOwner) { this.filename = filename; this.layout = layout; - this.types = types; + this.characterMaterialMap = characterMaterialMap; this.portalOpenBlock = portalOpenBlock; this.portalClosedBlock = portalClosedBlock; this.portalButton = portalButton; @@ -66,9 +64,9 @@ public class Gate { } /** - * Gets the layout of this gate + * Gets this gate's layout * - * @return

The layout of this gate

+ * @return

This gate's layout

*/ public GateLayout getLayout() { return layout; @@ -80,13 +78,13 @@ public class Gate { * @return

The material type used for control blocks

*/ public Material getControlBlock() { - return types.get(GateHandler.getControlBlockCharacter()); + return characterMaterialMap.get(GateHandler.getControlBlockCharacter()); } /** - * Gets the filename of this gate + * Gets the filename of this gate's file * - * @return

The filename of this gate

+ * @return

The filename of this gate's file

*/ public String getFilename() { return filename; @@ -125,7 +123,7 @@ public class Gate { * @return

The cost of using a portal with this gate

*/ public int getUseCost() { - return useCost < 0 ? EconomyHandler.getUseCost() : useCost; + return useCost < 0 ? EconomyHandler.getDefaultUseCost() : useCost; } /** @@ -169,6 +167,10 @@ public class Gate { /** * Checks whether a portal's gate matches this gate type * + *

If enabling onCreate, opening blocks with materials AIR and WATER will be allowed even if the gate closed + * material is a different one. If checking and onCreate is not enabled, any inconsistency with opening blocks + * containing AIR or WATER will cause the gate to not match.

+ * * @param topLeft

The top-left block of the portal's gate

* @param yaw

The yaw when looking directly outwards

* @param onCreate

Whether this is used in the context of creating a new gate

@@ -179,23 +181,29 @@ public class Gate { } /** - * Verifies that all border blocks of a portal gate matches this gate type + * Verifies that all border blocks of a portal matches this gate type * * @param topLeft

The top-left block of the portal

- * @param yaw

The yaw when looking directly outwards

+ * @param yaw

The yaw when looking directly outwards from the portal

* @return

True if all border blocks of the gate match the layout

*/ private boolean verifyGateBorderMatches(BlockLocation topLeft, double yaw) { - Map portalTypes = new HashMap<>(types); + Map characterMaterialMap = new HashMap<>(this.characterMaterialMap); for (RelativeBlockVector borderVector : layout.getBorder()) { int rowIndex = borderVector.getRight(); int lineIndex = borderVector.getDepth(); Character key = layout.getLayout()[lineIndex][rowIndex]; - Material materialInLayout = portalTypes.get(key); - Material materialAtLocation = getBlockAt(topLeft, borderVector, yaw).getType(); + Material materialInLayout = characterMaterialMap.get(key); + Material materialAtLocation = DirectionHelper.getBlockAt(topLeft, borderVector, yaw).getType(); if (materialInLayout == null) { - portalTypes.put(key, materialAtLocation); + /* This generally should not happen with proper checking, but just in case a material character is not + * recognized, but still allowed in previous checks, verify the gate as long as all such instances of + * the character correspond to the same material in the physical gate. All subsequent gates will also + * need to match the first verified gate. */ + characterMaterialMap.put(key, materialAtLocation); + Stargate.debug("Gate::Matches", String.format("Missing layout material in %s. Using %s from the" + + " physical portal.", getFilename(), materialAtLocation)); } else if (materialAtLocation != materialInLayout) { Stargate.debug("Gate::Matches", String.format("Block Type Mismatch: %s != %s", materialAtLocation, materialInLayout)); @@ -217,7 +225,7 @@ public class Gate { Stargate.debug("verifyGateEntrancesMatch", String.valueOf(topLeft)); for (RelativeBlockVector entranceVector : layout.getEntrances()) { Stargate.debug("verifyGateEntrancesMatch", String.valueOf(entranceVector)); - Material type = getBlockAt(topLeft, entranceVector, yaw).getType(); + Material type = DirectionHelper.getBlockAt(topLeft, entranceVector, yaw).getType(); //Ignore entrance if it's air or water, and we're creating a new gate if (onCreate && (type == Material.AIR || type == Material.WATER)) { @@ -232,16 +240,6 @@ public class Gate { return true; } - /** - * Gets the block at a relative block vector location - * - * @param vector

The relative block vector

- * @return

The block at the given relative position

- */ - private BlockLocation getBlockAt(BlockLocation topLeft, RelativeBlockVector vector, double yaw) { - return DirectionHelper.getBlockAt(topLeft, vector, yaw); - } - /** * Saves this gate to a file * @@ -253,6 +251,7 @@ public class Gate { try { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(gateFolder + filename)); + //Save main material names writeConfig(bufferedWriter, "portal-open", portalOpenBlock.name()); writeConfig(bufferedWriter, "portal-closed", portalClosedBlock.name()); writeConfig(bufferedWriter, "button", portalButton.name()); @@ -260,12 +259,12 @@ public class Gate { //Save the values necessary for economy saveEconomyValues(bufferedWriter); - //Store type material type to use for frame blocks + //Store material types to use for frame blocks saveFrameBlockTypes(bufferedWriter); bufferedWriter.newLine(); - //Save the layout + //Save the gate layout layout.save(bufferedWriter); bufferedWriter.close(); @@ -278,15 +277,18 @@ public class Gate { * Saves current economy related values using a buffered writer * * @param bufferedWriter

The buffered writer to write to

- * * @throws IOException

If unable to write to the buffered writer

+ * @throws IOException

If unable to write to the buffered writer

*/ private void saveEconomyValues(BufferedWriter bufferedWriter) throws IOException { + //Write use cost if not disabled if (useCost != -1) { writeConfig(bufferedWriter, "usecost", useCost); } + //Write create cost if not disabled if (createCost != -1) { writeConfig(bufferedWriter, "createcost", createCost); } + //Write destroy cost if not disabled if (destroyCost != -1) { writeConfig(bufferedWriter, "destroycost", destroyCost); } @@ -300,10 +302,10 @@ public class Gate { * @throws IOException

If unable to write to the buffered writer

*/ private void saveFrameBlockTypes(BufferedWriter bufferedWriter) throws IOException { - for (Map.Entry entry : types.entrySet()) { + for (Map.Entry entry : characterMaterialMap.entrySet()) { Character type = entry.getKey(); Material value = entry.getValue(); - // Skip control values + //Skip characters not part of the frame if (type.equals(GateHandler.getAnythingCharacter()) || type.equals(GateHandler.getEntranceCharacter()) || type.equals(GateHandler.getExitCharacter())) { @@ -319,53 +321,27 @@ public class Gate { } } - /** - * Writes an integer to a config - * - * @param bufferedWriter

The buffered writer to write the config to

- * @param key

The config key to save

- * @param value

The value of the config key

- * @throws IOException

If unable to write to the buffered writer

- */ - private void writeConfig(BufferedWriter bufferedWriter, String key, int value) throws IOException { - writeConfig(bufferedWriter, "%s=%d", key, value); - } - - /** - * Writes a boolean to a config - * - * @param bufferedWriter

The buffered writer to write the config to

- * @param key

The config key to save

- * @param value

The value of the config key

- * @throws IOException

If unable to write to the buffered writer

- */ - @SuppressWarnings("SameParameterValue") - private void writeConfig(BufferedWriter bufferedWriter, String key, boolean value) throws IOException { - writeConfig(bufferedWriter, "%s=%b", key, value); - } - - /** - * Writes a string to a config - * - * @param bufferedWriter

The buffered writer to write the config to

- * @param key

The config key to save

- * @param value

The value of the config key

- * @throws IOException

If unable to write to the buffered writer

- */ - private void writeConfig(BufferedWriter bufferedWriter, String key, String value) throws IOException { - writeConfig(bufferedWriter, "%s=%s", key, value); - } - /** * Writes a formatted string to a buffered writer * * @param bufferedWriter

The buffered writer to write the formatted string to

- * @param format

The format to use

* @param key

The config key to save

* @param value

The config value to save

* @throws IOException

If unable to write to the buffered writer

*/ - private void writeConfig(BufferedWriter bufferedWriter, String format, String key, Object value) throws IOException { + private void writeConfig(BufferedWriter bufferedWriter, String key, Object value) throws IOException { + //Figure out the correct formatting to use + String format = "%s="; + if (value instanceof Boolean) { + format += "%b"; + } else if (value instanceof Integer) { + format += "%d"; + } else if (value instanceof String) { + format += "%s"; + } else { + throw new IllegalArgumentException("Unrecognized config value type"); + } + bufferedWriter.append(String.format(format, key, value)); bufferedWriter.newLine(); }