Skip to content

Commit

Permalink
Splits GateFormat into GateFormat and GateFormatHandler #120
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicKnarvik97 committed Feb 26, 2022
1 parent 6481ed5 commit 98ebf73
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 64 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/TheDgtl/Stargate/Stargate.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.TheDgtl.Stargate.database.SQLiteDatabase;
import net.TheDgtl.Stargate.database.StorageAPI;
import net.TheDgtl.Stargate.gate.GateFormat;
import net.TheDgtl.Stargate.gate.GateFormatHandler;
import net.TheDgtl.Stargate.listeners.BlockEventListener;
import net.TheDgtl.Stargate.listeners.MoveEventListener;
import net.TheDgtl.Stargate.listeners.PlayerEventListener;
Expand Down Expand Up @@ -281,9 +282,9 @@ private void loadGateFormats() {
List<GateFormat> gateFormats = GateFormat.loadGateFormats(new File(DATA_FOLDER, GATE_FOLDER), this);
if (gateFormats == null) {
log(Level.SEVERE, "Unable to load gate formats from the gate format folder");
GateFormat.setFormats(new ArrayList<>());
GateFormatHandler.setFormats(new ArrayList<>());
} else {
GateFormat.setFormats(gateFormats);
GateFormatHandler.setFormats(gateFormats);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.TheDgtl.Stargate.exception.NameErrorException;
import net.TheDgtl.Stargate.gate.Gate;
import net.TheDgtl.Stargate.gate.GateFormat;
import net.TheDgtl.Stargate.gate.GateFormatHandler;
import net.TheDgtl.Stargate.network.InterServerNetwork;
import net.TheDgtl.Stargate.network.Network;
import net.TheDgtl.Stargate.network.NetworkAPI;
Expand Down Expand Up @@ -426,7 +427,7 @@ private void loadAllPortals(Database database, PortalType portalType, StargateRe
}

try {
GateFormat format = GateFormat.getFormat(gateFileName);
GateFormat format = GateFormatHandler.getFormat(gateFileName);
if (format == null) {
continue;
}
Expand Down
53 changes: 2 additions & 51 deletions src/main/java/net/TheDgtl/Stargate/gate/GateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
Expand All @@ -29,8 +28,6 @@
*/
public class GateFormat implements GateFormatAPI {

private static Map<Material, List<GateFormat>> controlMaterialFormatsMap;
private static Map<String, GateFormat> gateFormatsMap;
public static int formatAmount = 0;

private final Set<Material> controlMaterials;
Expand Down Expand Up @@ -123,40 +120,6 @@ private static GateFormat loadGateFormat(File file, StargateLogger logger) throw
}
}

/**
* Adds a new gate format
*
* @param controlToGateMap <p>The map of registered control block material to gate format mapping</p>
* @param format <p>The gate format to register</p>
* @param controlMaterials <p>The allowed control block materials for the new gate format</p>
*/
private static void addGateFormat(Map<Material, List<GateFormat>> controlToGateMap, GateFormat format,
Set<Material> controlMaterials) {
for (Material controlMaterial : controlMaterials) {
//Add an empty list if the material has no entry
if (!(controlToGateMap.containsKey(controlMaterial))) {
List<GateFormat> gateFormatList = new ArrayList<>();
controlToGateMap.put(controlMaterial, gateFormatList);
}
controlToGateMap.get(controlMaterial).add(format);
}
}

/**
* Gets all gate format using the given control block material
*
* @param signParentBlockMaterial <p>The material of a placed sign's parent block</p>
* @return <p>All gate formats using the given control block</p>
*/
public static List<GateFormat> getPossibleGateFormatsFromControlBlockMaterial(Material signParentBlockMaterial) {
List<GateFormat> possibleGates = controlMaterialFormatsMap.get(signParentBlockMaterial);
if (possibleGates == null) {
return new ArrayList<>();
}
return possibleGates;

}

/**
* Gets the locations of this gate format's control blocks
*
Expand Down Expand Up @@ -196,23 +159,11 @@ public Set<Material> getControlMaterials() {
/**
* Get the {@link GateStructure} of the specified type
*
* @param type <p> The specified type of {@link GateStructure} </p>
* @return
* @param type <p>The specified type of {@link GateStructure}</p>
* @return <p>The {@link GateStructure} of the specified type</p>
*/
public GateStructure getStructure(GateStructureType type) {
return this.portalParts.get(type);
}

public static GateFormat getFormat(String gateDesignName) {
return gateFormatsMap.get(gateDesignName);
}

public static void setFormats(List<GateFormat> gateFormats) {
controlMaterialFormatsMap = new EnumMap<>(Material.class);
gateFormatsMap = new HashMap<>();
for (GateFormat format : gateFormats) {
addGateFormat(controlMaterialFormatsMap, format, format.getControlMaterials());
gateFormatsMap.put(format.getFileName(), format);
}
}
}
78 changes: 78 additions & 0 deletions src/main/java/net/TheDgtl/Stargate/gate/GateFormatHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package net.TheDgtl.Stargate.gate;

import org.bukkit.Material;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* A handler that keeps track of all known gate formats
*/
public class GateFormatHandler {

private static Map<Material, List<GateFormat>> controlMaterialToGateFormatsMap;
private static Map<String, GateFormat> knownGateFormats;

/**
* Gets the gate format corresponding to the given gate design name
*
* @param gateDesignName <p>The gate design name to get the format of</p>
* @return <p>The gate format, or null if no such gate format</p>
*/
public static GateFormat getFormat(String gateDesignName) {
return knownGateFormats.get(gateDesignName);
}

/**
* Sets the gate formats known by the gate format handler
*
* @param gateFormats <p>The new list of known gate formats</p>
*/
public static void setFormats(List<GateFormat> gateFormats) {
controlMaterialToGateFormatsMap = new EnumMap<>(Material.class);
knownGateFormats = new HashMap<>();
for (GateFormat format : gateFormats) {
addGateFormat(controlMaterialToGateFormatsMap, format, format.getControlMaterials());
knownGateFormats.put(format.getFileName(), format);
}
}

/**
* Gets all gate format using the given control block material
*
* @param signParentBlockMaterial <p>The material of a placed sign's parent block</p>
* @return <p>All gate formats using the given control block</p>
*/
public static List<GateFormat> getPossibleGateFormatsFromControlBlockMaterial(Material signParentBlockMaterial) {
List<GateFormat> possibleGates = controlMaterialToGateFormatsMap.get(signParentBlockMaterial);
if (possibleGates == null) {
return new ArrayList<>();
}
return possibleGates;

}

/**
* Adds a new gate format
*
* @param controlToGateMap <p>The map of registered control block material to gate format mapping</p>
* @param format <p>The gate format to register</p>
* @param controlMaterials <p>The allowed control block materials for the new gate format</p>
*/
private static void addGateFormat(Map<Material, List<GateFormat>> controlToGateMap, GateFormat format,
Set<Material> controlMaterials) {
for (Material controlMaterial : controlMaterials) {
//Add an empty list if the material has no entry
if (!(controlToGateMap.containsKey(controlMaterial))) {
List<GateFormat> gateFormatList = new ArrayList<>();
controlToGateMap.put(controlMaterial, gateFormatList);
}
controlToGateMap.get(controlMaterial).add(format);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.TheDgtl.Stargate.exception.NameErrorException;
import net.TheDgtl.Stargate.gate.Gate;
import net.TheDgtl.Stargate.gate.GateFormat;
import net.TheDgtl.Stargate.gate.GateFormatHandler;
import net.TheDgtl.Stargate.network.NetworkAPI;
import net.TheDgtl.Stargate.network.StargateRegistry;
import net.TheDgtl.Stargate.network.portal.Portal;
Expand Down Expand Up @@ -143,7 +144,7 @@ static private Portal readPortal(String line, World world, StargateRegistry regi
NetworkAPI network = registry.getNetwork(networkName, flags.contains(PortalFlag.FANCY_INTER_SERVER));


GateFormat format = GateFormat.getFormat(gateFormatName);
GateFormat format = GateFormatHandler.getFormat(gateFormatName);
if (format == null) {
logger.logMessage(Level.WARNING, String.format("Could not find the format ''%s''. Check the full startup log for more information", gateFormatName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.TheDgtl.Stargate.exception.NoFormatFoundException;
import net.TheDgtl.Stargate.gate.Gate;
import net.TheDgtl.Stargate.gate.GateFormat;
import net.TheDgtl.Stargate.gate.GateFormatHandler;
import net.TheDgtl.Stargate.network.NetworkAPI;
import net.TheDgtl.Stargate.network.portal.BungeePortal;
import net.TheDgtl.Stargate.network.portal.FixedPortal;
Expand Down Expand Up @@ -101,7 +102,7 @@ public static Gate createGate(Block sign, boolean alwaysOn, StargateLogger logge
//Get the block behind the sign; the material of that block is stored in a register with available gateFormats
Directional signDirection = (Directional) sign.getBlockData();
Block behind = sign.getRelative(signDirection.getFacing().getOppositeFace());
List<GateFormat> gateFormats = GateFormat.getPossibleGateFormatsFromControlBlockMaterial(behind.getType());
List<GateFormat> gateFormats = GateFormatHandler.getPossibleGateFormatsFromControlBlockMaterial(behind.getType());
return findMatchingGate(gateFormats, sign.getLocation(), signDirection.getFacing(), alwaysOn, logger);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.TheDgtl.Stargate.exception.InvalidStructureException;
import net.TheDgtl.Stargate.exception.NameErrorException;
import net.TheDgtl.Stargate.gate.GateFormat;
import net.TheDgtl.Stargate.gate.GateFormatHandler;
import net.TheDgtl.Stargate.network.Network;
import net.TheDgtl.Stargate.network.NetworkAPI;
import net.TheDgtl.Stargate.network.PortalType;
Expand Down Expand Up @@ -89,7 +90,7 @@ public DatabaseTester(Database database, TableNameConfig nameConfig, SQLQueryGen
} catch (NameErrorException e) {
e.printStackTrace();
}
GateFormat.setFormats(Objects.requireNonNull(GateFormat.loadGateFormats(testGatesDir, logger)));
GateFormatHandler.setFormats(Objects.requireNonNull(GateFormat.loadGateFormats(testGatesDir, logger)));
FakePortalGenerator portalGenerator = new FakePortalGenerator(LOCAL_PORTAL_NAME, INTER_PORTAL_NAME);

this.interServerPortals = portalGenerator.generateFakePortals(world, testNetwork, true, interServerPortalTestLength, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import net.TheDgtl.Stargate.exception.NameErrorException;
import net.TheDgtl.Stargate.gate.Gate;
import net.TheDgtl.Stargate.gate.GateFormat;
import net.TheDgtl.Stargate.gate.GateFormatHandler;
import net.TheDgtl.Stargate.network.NetworkAPI;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.util.BlockVector;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
Expand Down Expand Up @@ -85,13 +84,10 @@ public RealPortal generateFakePortal(World world, NetworkAPI portalNetwork, Stri
if (createInterServerPortal) {
flags.add(PortalFlag.FANCY_INTER_SERVER);
}
List<PortalPosition> portalPositions = new ArrayList<>();
portalPositions.add(new PortalPosition(PositionType.SIGN, new BlockVector(0, 3, 0)));
GateFormat format = GateFormat.getFormat("fileName.gate");
GateFormat format = GateFormatHandler.getFormat("fileName.gate");
Gate gate = new Gate(world.getBlockAt(0, 0, 0).getLocation(), BlockFace.EAST, false, format,
logger);


gate.addPortalPosition(new BlockVector(1, -2, 0), PositionType.BUTTON);
gate.addPortalPosition(new BlockVector(1, -2, -3), PositionType.SIGN);
//To avoid using the Portal#open method on constructor, which uses an unimplemented function in mockbuckit (blockstates)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.TheDgtl.Stargate.database.SQLiteDatabase;
import net.TheDgtl.Stargate.database.StorageAPI;
import net.TheDgtl.Stargate.gate.GateFormat;
import net.TheDgtl.Stargate.gate.GateFormatHandler;
import net.TheDgtl.Stargate.network.NetworkAPI;
import net.TheDgtl.Stargate.network.StargateRegistry;
import net.TheDgtl.Stargate.network.portal.Portal;
Expand Down Expand Up @@ -79,7 +80,7 @@ public static void setUp() throws IOException, InvalidConfigurationException, SQ
server.addSimpleWorld("pseudoknigth");
Stargate.getConfigStatic().load(defaultConfigFile);

GateFormat.setFormats(Objects.requireNonNull(GateFormat.loadGateFormats(testGatesDir, logger)));
GateFormatHandler.setFormats(Objects.requireNonNull(GateFormat.loadGateFormats(testGatesDir, logger)));
}

private static Map<String, TwoTuple<Map<String, Object>, Map<String, String>>> getSettingTestMaps() {
Expand Down

0 comments on commit 98ebf73

Please sign in to comment.