Improves Gate comments where possible

Renames types to characterMaterialMap
Simplifies writeConfig to a single method
This commit is contained in:
Kristian Knarvik 2021-10-15 19:23:17 +02:00
parent 382156a719
commit 6e658003e0

View File

@ -23,13 +23,11 @@ public class Gate {
private final String filename; private final String filename;
private final GateLayout layout; private final GateLayout layout;
private final Map<Character, Material> types; private final Map<Character, Material> characterMaterialMap;
//Gate materials //Gate materials
private final Material portalOpenBlock; private final Material portalOpenBlock;
private final Material portalClosedBlock; private final Material portalClosedBlock;
private final Material portalButton; private final Material portalButton;
//Economy information //Economy information
private final int useCost; private final int useCost;
private final int createCost; private final int createCost;
@ -39,23 +37,23 @@ public class Gate {
/** /**
* Instantiates a new gate * Instantiates a new gate
* *
* @param filename <p>The name of the gate which equal the name of the file</p> * @param filename <p>The name of the gate file, including extension</p>
* @param layout <p>The character layout defined in the gate file</p> * @param layout <p>The gate layout defined in the gate file</p>
* @param types <p>The block types the different layout characters represent</p> * @param characterMaterialMap <p>The material types the different layout characters represent</p>
* @param portalOpenBlock <p>The material to set the non-frame to when the portal is open</p> * @param portalOpenBlock <p>The material to set the opening to when the portal is open</p>
* @param portalClosedBlock <p>The material to set the non-frame to when the portal is closed</p> * @param portalClosedBlock <p>The material to set the opening to when the portal is closed</p>
* @param portalButton <p>The material to use for the portal button</p> * @param portalButton <p>The material to use for the portal button</p>
* @param useCost <p>The cost of using a portal with this gate layout (-1 to disable)</p> * @param useCost <p>The cost of using a portal with this gate layout (-1 to disable)</p>
* @param createCost <p>The cost of creating a portal with this gate layout (-1 to disable)</p> * @param createCost <p>The cost of creating a portal with this gate layout (-1 to disable)</p>
* @param destroyCost <p>The cost of destroying a portal with this gate layout (-1 to disable)</p> * @param destroyCost <p>The cost of destroying a portal with this gate layout (-1 to disable)</p>
* @param toOwner <p>Whether any payment should go to the owner of the gate, as opposed to just disappearing</p> * @param toOwner <p>Whether any payment should go to the owner of the gate, as opposed to just disappearing</p>
*/ */
public Gate(String filename, GateLayout layout, Map<Character, Material> types, Material portalOpenBlock, public Gate(String filename, GateLayout layout, Map<Character, Material> characterMaterialMap, Material portalOpenBlock,
Material portalClosedBlock, Material portalButton, int useCost, int createCost, int destroyCost, Material portalClosedBlock, Material portalButton, int useCost, int createCost, int destroyCost,
boolean toOwner) { boolean toOwner) {
this.filename = filename; this.filename = filename;
this.layout = layout; this.layout = layout;
this.types = types; this.characterMaterialMap = characterMaterialMap;
this.portalOpenBlock = portalOpenBlock; this.portalOpenBlock = portalOpenBlock;
this.portalClosedBlock = portalClosedBlock; this.portalClosedBlock = portalClosedBlock;
this.portalButton = portalButton; this.portalButton = portalButton;
@ -66,9 +64,9 @@ public class Gate {
} }
/** /**
* Gets the layout of this gate * Gets this gate's layout
* *
* @return <p>The layout of this gate</p> * @return <p>This gate's layout</p>
*/ */
public GateLayout getLayout() { public GateLayout getLayout() {
return layout; return layout;
@ -80,13 +78,13 @@ public class Gate {
* @return <p>The material type used for control blocks</p> * @return <p>The material type used for control blocks</p>
*/ */
public Material getControlBlock() { 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 <p>The filename of this gate</p> * @return <p>The filename of this gate's file</p>
*/ */
public String getFilename() { public String getFilename() {
return filename; return filename;
@ -125,7 +123,7 @@ public class Gate {
* @return <p>The cost of using a portal with this gate</p> * @return <p>The cost of using a portal with this gate</p>
*/ */
public int getUseCost() { 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 * Checks whether a portal's gate matches this gate type
* *
* <p>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.</p>
*
* @param topLeft <p>The top-left block of the portal's gate</p> * @param topLeft <p>The top-left block of the portal's gate</p>
* @param yaw <p>The yaw when looking directly outwards</p> * @param yaw <p>The yaw when looking directly outwards</p>
* @param onCreate <p>Whether this is used in the context of creating a new gate</p> * @param onCreate <p>Whether this is used in the context of creating a new gate</p>
@ -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 <p>The top-left block of the portal</p> * @param topLeft <p>The top-left block of the portal</p>
* @param yaw <p>The yaw when looking directly outwards</p> * @param yaw <p>The yaw when looking directly outwards from the portal</p>
* @return <p>True if all border blocks of the gate match the layout</p> * @return <p>True if all border blocks of the gate match the layout</p>
*/ */
private boolean verifyGateBorderMatches(BlockLocation topLeft, double yaw) { private boolean verifyGateBorderMatches(BlockLocation topLeft, double yaw) {
Map<Character, Material> portalTypes = new HashMap<>(types); Map<Character, Material> characterMaterialMap = new HashMap<>(this.characterMaterialMap);
for (RelativeBlockVector borderVector : layout.getBorder()) { for (RelativeBlockVector borderVector : layout.getBorder()) {
int rowIndex = borderVector.getRight(); int rowIndex = borderVector.getRight();
int lineIndex = borderVector.getDepth(); int lineIndex = borderVector.getDepth();
Character key = layout.getLayout()[lineIndex][rowIndex]; Character key = layout.getLayout()[lineIndex][rowIndex];
Material materialInLayout = portalTypes.get(key); Material materialInLayout = characterMaterialMap.get(key);
Material materialAtLocation = getBlockAt(topLeft, borderVector, yaw).getType(); Material materialAtLocation = DirectionHelper.getBlockAt(topLeft, borderVector, yaw).getType();
if (materialInLayout == null) { 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) { } else if (materialAtLocation != materialInLayout) {
Stargate.debug("Gate::Matches", String.format("Block Type Mismatch: %s != %s", Stargate.debug("Gate::Matches", String.format("Block Type Mismatch: %s != %s",
materialAtLocation, materialInLayout)); materialAtLocation, materialInLayout));
@ -217,7 +225,7 @@ public class Gate {
Stargate.debug("verifyGateEntrancesMatch", String.valueOf(topLeft)); Stargate.debug("verifyGateEntrancesMatch", String.valueOf(topLeft));
for (RelativeBlockVector entranceVector : layout.getEntrances()) { for (RelativeBlockVector entranceVector : layout.getEntrances()) {
Stargate.debug("verifyGateEntrancesMatch", String.valueOf(entranceVector)); 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 //Ignore entrance if it's air or water, and we're creating a new gate
if (onCreate && (type == Material.AIR || type == Material.WATER)) { if (onCreate && (type == Material.AIR || type == Material.WATER)) {
@ -232,16 +240,6 @@ public class Gate {
return true; return true;
} }
/**
* Gets the block at a relative block vector location
*
* @param vector <p>The relative block vector</p>
* @return <p>The block at the given relative position</p>
*/
private BlockLocation getBlockAt(BlockLocation topLeft, RelativeBlockVector vector, double yaw) {
return DirectionHelper.getBlockAt(topLeft, vector, yaw);
}
/** /**
* Saves this gate to a file * Saves this gate to a file
* *
@ -253,6 +251,7 @@ public class Gate {
try { try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(gateFolder + filename)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(gateFolder + filename));
//Save main material names
writeConfig(bufferedWriter, "portal-open", portalOpenBlock.name()); writeConfig(bufferedWriter, "portal-open", portalOpenBlock.name());
writeConfig(bufferedWriter, "portal-closed", portalClosedBlock.name()); writeConfig(bufferedWriter, "portal-closed", portalClosedBlock.name());
writeConfig(bufferedWriter, "button", portalButton.name()); writeConfig(bufferedWriter, "button", portalButton.name());
@ -260,12 +259,12 @@ public class Gate {
//Save the values necessary for economy //Save the values necessary for economy
saveEconomyValues(bufferedWriter); saveEconomyValues(bufferedWriter);
//Store type material type to use for frame blocks //Store material types to use for frame blocks
saveFrameBlockTypes(bufferedWriter); saveFrameBlockTypes(bufferedWriter);
bufferedWriter.newLine(); bufferedWriter.newLine();
//Save the layout //Save the gate layout
layout.save(bufferedWriter); layout.save(bufferedWriter);
bufferedWriter.close(); bufferedWriter.close();
@ -278,15 +277,18 @@ public class Gate {
* Saves current economy related values using a buffered writer * Saves current economy related values using a buffered writer
* *
* @param bufferedWriter <p>The buffered writer to write to</p> * @param bufferedWriter <p>The buffered writer to write to</p>
* * @throws IOException <p>If unable to write to the buffered writer</p> * @throws IOException <p>If unable to write to the buffered writer</p>
*/ */
private void saveEconomyValues(BufferedWriter bufferedWriter) throws IOException { private void saveEconomyValues(BufferedWriter bufferedWriter) throws IOException {
//Write use cost if not disabled
if (useCost != -1) { if (useCost != -1) {
writeConfig(bufferedWriter, "usecost", useCost); writeConfig(bufferedWriter, "usecost", useCost);
} }
//Write create cost if not disabled
if (createCost != -1) { if (createCost != -1) {
writeConfig(bufferedWriter, "createcost", createCost); writeConfig(bufferedWriter, "createcost", createCost);
} }
//Write destroy cost if not disabled
if (destroyCost != -1) { if (destroyCost != -1) {
writeConfig(bufferedWriter, "destroycost", destroyCost); writeConfig(bufferedWriter, "destroycost", destroyCost);
} }
@ -300,10 +302,10 @@ public class Gate {
* @throws IOException <p>If unable to write to the buffered writer</p> * @throws IOException <p>If unable to write to the buffered writer</p>
*/ */
private void saveFrameBlockTypes(BufferedWriter bufferedWriter) throws IOException { private void saveFrameBlockTypes(BufferedWriter bufferedWriter) throws IOException {
for (Map.Entry<Character, Material> entry : types.entrySet()) { for (Map.Entry<Character, Material> entry : characterMaterialMap.entrySet()) {
Character type = entry.getKey(); Character type = entry.getKey();
Material value = entry.getValue(); Material value = entry.getValue();
// Skip control values //Skip characters not part of the frame
if (type.equals(GateHandler.getAnythingCharacter()) || if (type.equals(GateHandler.getAnythingCharacter()) ||
type.equals(GateHandler.getEntranceCharacter()) || type.equals(GateHandler.getEntranceCharacter()) ||
type.equals(GateHandler.getExitCharacter())) { type.equals(GateHandler.getExitCharacter())) {
@ -319,53 +321,27 @@ public class Gate {
} }
} }
/**
* Writes an integer to a config
*
* @param bufferedWriter <p>The buffered writer to write the config to</p>
* @param key <p>The config key to save</p>
* @param value <p>The value of the config key</p>
* @throws IOException <p>If unable to write to the buffered writer</p>
*/
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 <p>The buffered writer to write the config to</p>
* @param key <p>The config key to save</p>
* @param value <p>The value of the config key</p>
* @throws IOException <p>If unable to write to the buffered writer</p>
*/
@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 <p>The buffered writer to write the config to</p>
* @param key <p>The config key to save</p>
* @param value <p>The value of the config key</p>
* @throws IOException <p>If unable to write to the buffered writer</p>
*/
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 * Writes a formatted string to a buffered writer
* *
* @param bufferedWriter <p>The buffered writer to write the formatted string to</p> * @param bufferedWriter <p>The buffered writer to write the formatted string to</p>
* @param format <p>The format to use</p>
* @param key <p>The config key to save</p> * @param key <p>The config key to save</p>
* @param value <p>The config value to save</p> * @param value <p>The config value to save</p>
* @throws IOException <p>If unable to write to the buffered writer</p> * @throws IOException <p>If unable to write to the buffered writer</p>
*/ */
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.append(String.format(format, key, value));
bufferedWriter.newLine(); bufferedWriter.newLine();
} }