Skip to content

Commit

Permalink
(cleanup) Loads of nit-picks cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Jan 29, 2024
1 parent 7cf6b6f commit c7526c6
Show file tree
Hide file tree
Showing 45 changed files with 182 additions and 239 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/sgrewritten/stargate/Stargate.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ private void loadGateFormats() throws IOException {
this.gateFolder = ConfigurationHelper.getString(ConfigurationOption.GATE_FOLDER);
saveDefaultGates();
List<GateFormat> gateFormats = GateFormatHandler.loadGateFormats(new File(this.getDataFolder(), this.getGateFolder()));
if (gateFormats == null) {
if (gateFormats.isEmpty()) {
log(Level.SEVERE, "Unable to load gate formats from the gate format folder");
GateFormatRegistry.setFormats(new ArrayList<>());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.ArrayList;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -24,7 +25,7 @@
import java.util.Set;

public class BlockHandlerResolver {
private final Map<Material, List<BlockHandlerInterface>> blockHandlerMap = new HashMap<>();
private final Map<Material, List<BlockHandlerInterface>> blockHandlerMap = new EnumMap<>(Material.class);
private final Map<BlockLocation, BlockHandlerInterface> blockBlockHandlerMap = new HashMap<>();
private final StorageAPI storageAPI;
private final Set<Character> customFlags = new HashSet<>();
Expand All @@ -41,7 +42,7 @@ public BlockHandlerResolver(@NotNull StorageAPI storageAPI) {
public void addBlockHandlerInterface(BlockHandlerInterface blockHandlerInterface) {
List<BlockHandlerInterface> blockHandlerInterfaceList = this.blockHandlerMap.computeIfAbsent(blockHandlerInterface.getHandledMaterial(), k -> new ArrayList<>());
blockHandlerInterfaceList.add(blockHandlerInterface);
blockHandlerInterfaceList.sort(Comparator.comparingInt((ablockHandlerInterface) -> -ablockHandlerInterface.getPriority().getPriorityValue()));
blockHandlerInterfaceList.sort(Comparator.comparingInt(ablockHandlerInterface -> -ablockHandlerInterface.getPriority().getPriorityValue()));
}

/**
Expand All @@ -50,8 +51,7 @@ public void addBlockHandlerInterface(BlockHandlerInterface blockHandlerInterface
* @param blockHandlerInterface listener for block placement next by a portal
*/
public void removeBlockHandlerInterface(BlockHandlerInterface blockHandlerInterface) {
for (Material key : this.blockHandlerMap.keySet()) {
List<BlockHandlerInterface> blockHandlerInterfaceList = this.blockHandlerMap.get(key);
for (List<BlockHandlerInterface> blockHandlerInterfaceList : this.blockHandlerMap.values()) {
if (blockHandlerInterfaceList.remove(blockHandlerInterface)) {
return;
}
Expand All @@ -64,8 +64,7 @@ public void removeBlockHandlerInterface(BlockHandlerInterface blockHandlerInterf
* @param plugin The plugin to remove listeners from
*/
public void removeBlockHandlerInterfaces(Plugin plugin) {
for (Material key : this.blockHandlerMap.keySet()) {
List<BlockHandlerInterface> blockHandlerInterfaceList = this.blockHandlerMap.get(key);
for (List<BlockHandlerInterface> blockHandlerInterfaceList : this.blockHandlerMap.values()) {
blockHandlerInterfaceList.removeIf(blockHandlerInterface -> blockHandlerInterface.getPlugin() == plugin);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

public class GateFormatRegistry {

private GateFormatRegistry(){
throw new IllegalStateException("Utility class");
}

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

Expand Down Expand Up @@ -70,11 +74,7 @@ public static void setFormats(List<GateFormat> gateFormats) {
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.putIfAbsent(controlMaterial, new ArrayList<>());
controlToGateMap.get(controlMaterial).add(format);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public BlockLocation(Location location) {
x = location.getBlockX();
y = location.getBlockY();
z = location.getBlockZ();
World world = location.getWorld();
this.world = world == null ? "" : world.getName();
World worldObject = location.getWorld();
this.world = worldObject == null ? "" : worldObject.getName();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.sgrewritten.stargate.api.network.portal;

import com.google.gson.JsonElement;
import org.jetbrains.annotations.Nullable;
import org.sgrewritten.stargate.api.MetadataHolder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public abstract class AbstractSignLine implements SignLine {

private final List<StargateComponent> components;

public AbstractSignLine(List<StargateComponent> components) {
protected AbstractSignLine(List<StargateComponent> components) {
this.components = components;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

public class ColorNameInterpreter {

private ColorNameInterpreter(){
throw new IllegalStateException("Utility class");
}

public static ChatColor getDefaultPointerColor(String defaultString) {
try {
DyeColor dyeColor = DyeColor.valueOf(defaultString.toUpperCase());
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/org/sgrewritten/stargate/colors/ColorRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

public class ColorRegistry {
public static final Map<DyeColor, ChatColor> TEXT_COLORS = loadColors(false);
public static final Map<DyeColor, ChatColor> POINTER_COLORS = loadColors(true);
public static final Map<PortalFlag, ChatColor> FLAG_COLORS = loadFlagColors();
private ColorRegistry(){
throw new IllegalStateException("Utility class");
}

public static final Map<DyeColor, ChatColor> TEXT_COLORS = loadColors(false,"/colors/colorTable.json");
public static final Map<DyeColor, ChatColor> POINTER_COLORS = loadColors(true, "/colors/colorTable.json");
public static final Map<PortalFlag, ChatColor> FLAG_COLORS = loadFlagColors("/colors/flagColorTable.json");

private static Map<DyeColor, ChatColor> loadColors(boolean isPointer) {
String fileName = "/colors/colorTable.json";
private static Map<DyeColor, ChatColor> loadColors(boolean isPointer, String fileName) {
try (InputStream inputStream = Stargate.class.getResourceAsStream(fileName)) {
if (inputStream == null) {
throw new IOException("Could not find internal file: " + fileName);
}
try (Reader reader = new InputStreamReader(inputStream)) {
JsonElement jsonData = JsonParser.parseReader(reader);
Map<DyeColor, ChatColor> output = new HashMap<>();
Map<DyeColor, ChatColor> output = new EnumMap<>(DyeColor.class);
for (JsonElement jsonElement : jsonData.getAsJsonArray()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
DyeColor dyeColor = DyeColor.valueOf(jsonObject.get("color").getAsString());
Expand All @@ -48,15 +52,14 @@ private static Map<DyeColor, ChatColor> loadColors(boolean isPointer) {
}
}

private static Map<PortalFlag, ChatColor> loadFlagColors() {
String fileName = "/colors/flagColorTable.json";
private static Map<PortalFlag, ChatColor> loadFlagColors(String fileName) {
try (InputStream inputStream = Stargate.class.getResourceAsStream(fileName)) {
if (inputStream == null) {
throw new IOException("Could not find internal file: " + fileName);
}
try (Reader reader = new InputStreamReader(inputStream)) {
JsonElement jsonData = JsonParser.parseReader(reader);
Map<PortalFlag, ChatColor> output = new HashMap<>();
Map<PortalFlag, ChatColor> output = new EnumMap<>(PortalFlag.class);
for (JsonElement jsonElement : jsonData.getAsJsonArray()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
DyeColor dyeColor = DyeColor.valueOf(jsonObject.get("dyeColor").getAsString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.sgrewritten.stargate.config;

import org.jetbrains.annotations.NotNull;
import org.sgrewritten.stargate.Stargate;
import org.sgrewritten.stargate.api.config.ConfigurationOption;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -71,12 +73,12 @@ public static boolean getBoolean(ConfigurationOption configurationOption) {
}

@SuppressWarnings("unchecked")
public static List<String> getStringList(ConfigurationOption configurationOption) {
public static @NotNull List<String> getStringList(ConfigurationOption configurationOption) {
if (Stargate.getFileConfiguration().isSet(configurationOption.getConfigNode())) {
return Stargate.getFileConfiguration().getStringList(configurationOption.getConfigNode());
} else {
if (configurationOption.getDefaultValue() == null) {
return null;
return new ArrayList<>();
}
return (List<String>) configurationOption.getDefaultValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ private HikariDataSource setupMySql(HikariConfig config) {
*/
private HikariConfig setupConfig(DatabaseDriver driver, String address, int port, String database, String username,
String password, boolean useSSL) {
HikariConfig config = new HikariConfig();
HikariConfig hikariConfig = new HikariConfig();

config.setJdbcUrl("jdbc:" + driver.getDriver() + "://" + address + ":" + port + "/" + database);
config.setUsername(username);
config.setPassword(password);
config.addDataSourceProperty("useSSL", useSSL);
return config;
hikariConfig.setJdbcUrl("jdbc:" + driver.getDriver() + "://" + address + ":" + port + "/" + database);
hikariConfig.setUsername(username);
hikariConfig.setPassword(password);
hikariConfig.addDataSourceProperty("useSSL", useSSL);
return hikariConfig;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static Map<String, Map<String, String>> readQueryFiles(Set<String> query
final Map<String, String> queries = new HashMap<>();
String fullFolder = "/database/" + folder;
List<Path> walk = FileHelper.listFilesOfInternalDirectory(fullFolder);
if (walk == null) {
if (walk.isEmpty()) {
return new HashMap<>();
}
walk.forEach(path -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class EconomyManager implements EconomyAPI, StargateEconomyAPI {
*
* @param languageManager <p>The language manager to use for translations</p>
*/
public EconomyManager(LanguageManager languageManager) {
protected EconomyManager(LanguageManager languageManager) {
this.languageManager = languageManager;
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/sgrewritten/stargate/gate/Gate.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,7 @@ private boolean hasGateFrameConflict(RegistryAPI registry) {
* @return <p>True if there is a conflict</p>
*/
private boolean hasGateControlConflict() {
List<PortalPosition> portalPositions = this.getPortalPositions();
for (PortalPosition portalPosition : portalPositions) {
for (PortalPosition portalPosition : this.getPortalPositions()) {
Location location = getLocation(portalPosition.getRelativePositionLocation());
PortalPosition conflictingPortalPosition = registry.getPortalPosition(location);
if (conflictingPortalPosition != null) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/sgrewritten/stargate/gate/GateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public boolean isIronDoorBlockable() {
* @return <p>True if the stargate matches this format</p>
*/
public boolean matches(VectorOperation converter, Location topLeft) {
for (GateFormatStructureType structureType : portalParts.keySet()) {
Stargate.log(Level.FINER, "---Validating " + structureType);
if (!(portalParts.get(structureType).isValidState(converter, topLeft))) {
Stargate.log(Level.FINER, structureType + " returned negative");
for (Map.Entry<GateFormatStructureType, GateStructure> entry: portalParts.entrySet()) {
Stargate.log(Level.FINER, "---Validating " + entry.getKey());
if (!(entry.getValue().isValidState(converter, topLeft))) {
Stargate.log(Level.FINER, entry.getKey() + " returned negative");
return false;
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/org/sgrewritten/stargate/gate/GateFormatHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sgrewritten.stargate.gate;

import org.jetbrains.annotations.NotNull;
import org.sgrewritten.stargate.Stargate;
import org.sgrewritten.stargate.exception.ParsingErrorException;

Expand All @@ -15,31 +16,33 @@
*/
public class GateFormatHandler {

private GateFormatHandler() {
throw new IllegalStateException("Utility class");
}

/**
* Loads all gate formats from the gate folder
*
* @param dir <p>The folder to load gates from</p>
* @return <p>A map between a control block material and the corresponding gate format</p>
*/
public static List<GateFormat> loadGateFormats(File dir) {
public static @NotNull List<GateFormat> loadGateFormats(File dir) {
Stargate.log(Level.FINE, "Loading gates from " + dir.getAbsolutePath());
List<GateFormat> gateFormatMap = new ArrayList<>();
File[] files = dir.exists() ? dir.listFiles((directory, name) -> name.endsWith(".gate")) : new File[0];

if (files == null) {
return null;
return new ArrayList<>();
}

for (File file : files) {
try {
gateFormatMap.add(loadGateFormat(file));
} catch (FileNotFoundException | ParsingErrorException exception) {
Stargate.log(Level.WARNING, "Could not load Gate " + file.getName() + " - " + exception.getMessage());
if (exception instanceof ParsingErrorException && file.exists()) {
if (!file.renameTo(new File(dir, file.getName() + ".invalid"))) {
Stargate.log(Level.WARNING, "Could not add .invalid to gate. Make sure file " +
"permissions are set correctly.");
}
if (exception instanceof ParsingErrorException && file.exists() && !file.renameTo(new File(dir, file.getName() + ".invalid"))) {
Stargate.log(Level.WARNING, "Could not add .invalid to gate. Make sure file " +
"permissions are set correctly.");
}
}
}
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/org/sgrewritten/stargate/gate/GateFormatParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.bukkit.Material;
import org.bukkit.util.BlockVector;
import org.bukkit.util.BoundingBox;
import org.sgrewritten.stargate.Stargate;
import org.sgrewritten.stargate.exception.ParsingErrorException;
import org.sgrewritten.stargate.gate.structure.GateControlBlock;
Expand Down Expand Up @@ -98,12 +97,12 @@ public GateFormat parseGateFormat() throws ParsingErrorException {
* @param characterMaterialMap <p>The full map between characters and materials loaded from the gate file</p>
*/
private void loadFrameAndControlMaterials(Map<Character, Set<Material>> characterMaterialMap) {
for (Character character : characterMaterialMap.keySet()) {
if (character == CONTROL) {
for (Map.Entry<Character, Set<Material>> entry : characterMaterialMap.entrySet()) {
if (entry.getKey() == CONTROL) {
controlMaterials = characterMaterialMap.get(CONTROL);
frameMaterials.put(character, characterMaterialMap.get(character));
} else if (character != NOTHING && character != EXIT && character != ENTRANCE) {
frameMaterials.put(character, characterMaterialMap.get(character));
frameMaterials.put(entry.getKey(), entry.getValue());
} else if (entry.getKey() != NOTHING && entry.getKey() != EXIT && entry.getKey() != ENTRANCE) {
frameMaterials.put(entry.getKey(), entry.getValue());
}
}
}
Expand All @@ -116,11 +115,11 @@ private void loadFrameAndControlMaterials(Map<Character, Set<Material>> characte
* @throws ParsingErrorException <p>If unable to parse one of the materials given in the options</p>
*/
private void loadGateConfigValues(Map<String, String> config) throws ParsingErrorException {
for (String key : config.keySet()) {
String line = key + "=" + config.get(key);
switch (key) {
case "portal-open" -> irisOpen = GateFormatReader.parseMaterial(config.get(key), line);
case "portal-closed" -> irisClosed = GateFormatReader.parseMaterial(config.get(key), line);
for (Map.Entry<String,String> entry : config.entrySet()) {
String line = entry.getKey() + "=" + entry.getValue();
switch (entry.getKey()) {
case "portal-open" -> irisOpen = GateFormatReader.parseMaterial(entry.getValue(), line);
case "portal-closed" -> irisClosed = GateFormatReader.parseMaterial(entry.getValue(), line);
default -> {
//Any unknown config values are ignored
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.block.data.type.WallSign;
import org.bukkit.util.BlockVector;
import org.bukkit.util.BoundingBox;
Expand Down Expand Up @@ -49,7 +48,7 @@ public List<BlockVector> getStructureTypePositions() {

@Override
protected boolean isValidBlock(BlockVector blockVector, Material material) {
if(Tag.WALL_SIGNS.isTagged(material) || ButtonHelper.isButton(material)){
if (Tag.WALL_SIGNS.isTagged(material) || ButtonHelper.isButton(material)) {
return true;
}
return material.isAir() || material == Material.WATER;
Expand All @@ -59,9 +58,6 @@ public void generateStructure(VectorOperation converter, Location topLeft) {
BlockVector signPosition = parts.get(0);
Block signLocation = topLeft.clone().add(converter.performToRealSpaceOperation(signPosition)).getBlock();
BlockState state = signLocation.getState();
/*
* TODO: remove this hardcoded thing
*/
state.setType(Material.OAK_WALL_SIGN);
WallSign signData = (WallSign) state.getBlockData();
signData.setFacing(converter.getFacing());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ protected boolean isValidBlock(BlockVector blockVector, Material material) {

@Override
public void generateStructure(VectorOperation converter, Location topLeft) {
for (BlockVector position : parts.keySet()) {
Location location = topLeft.clone().add(converter.performToRealSpaceOperation(position));
Set<Material> materialsAtPosition = parts.get(position);
for (Map.Entry<BlockVector,Set<Material>> entry : parts.entrySet()) {
Location location = topLeft.clone().add(converter.performToRealSpaceOperation(entry.getKey()));
Set<Material> materialsAtPosition = entry.getValue();
Material material = materialsAtPosition.toArray(new Material[0])[RANDOM.nextInt(materialsAtPosition.size())];
location.getBlock().setType(material);
}
Expand Down
Loading

0 comments on commit c7526c6

Please sign in to comment.