Fixes some code style issues, and attempts to fix test failures

This commit is contained in:
2024-02-20 18:54:04 +01:00
parent e57aa4097c
commit ee9bc21099
5 changed files with 137 additions and 60 deletions

View File

@@ -116,13 +116,13 @@ public class ConfigTabCompleter implements TabCompleter {
* @param args <p>The arguments given by the user</p> * @param args <p>The arguments given by the user</p>
* @return <p>Some or all of the valid values for the option</p> * @return <p>Some or all of the valid values for the option</p>
*/ */
@Nullable @NotNull
private List<String> getPossibleStringListOptionValues(@NotNull ConfigOption selectedOption, private List<String> getPossibleStringListOptionValues(@NotNull ConfigOption selectedOption,
@NotNull String[] args) { @NotNull String[] args) {
if (selectedOption == ConfigOption.PER_SIGN_COLORS) { if (selectedOption == ConfigOption.PER_SIGN_COLORS) {
return getPerSignColorCompletion(args); return getPerSignColorCompletion(args);
} else { } else {
return null; return new ArrayList<>();
} }
} }

View File

@@ -11,9 +11,8 @@ import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* This class is responsible for loading all strings which are translated into several languages * This class is responsible for loading all strings which are translated into several languages
@@ -153,11 +152,14 @@ public final class LanguageLoader {
//If a key is not found in the language file, add the one in the internal file. Must update the external file //If a key is not found in the language file, add the one in the internal file. Must update the external file
if (!internalLanguageValues.keySet().equals(currentLanguageValues.keySet())) { if (!internalLanguageValues.keySet().equals(currentLanguageValues.keySet())) {
Map<Message, String> newLanguageValues = new HashMap<>(); Map<Message, String> newLanguageValues = new EnumMap<>(Message.class);
boolean updateNecessary = false; boolean updateNecessary = false;
for (Message key : internalLanguageValues.keySet()) { for (Map.Entry<Message, String> entry : internalLanguageValues.entrySet()) {
Message key = entry.getKey();
String value = entry.getValue();
if (currentLanguageValues.get(key) == null) { if (currentLanguageValues.get(key) == null) {
newLanguageValues.put(key, internalLanguageValues.get(key)); newLanguageValues.put(key, value);
//Found at least one value in the internal file not in the external file. Need to update //Found at least one value in the internal file not in the external file. Need to update
updateNecessary = true; updateNecessary = true;
} else { } else {
@@ -186,15 +188,15 @@ public final class LanguageLoader {
BufferedWriter bufferedWriter = FileHelper.getBufferedWriterFromString(languageFolder + language + ".txt"); BufferedWriter bufferedWriter = FileHelper.getBufferedWriterFromString(languageFolder + language + ".txt");
//Output normal Language data //Output normal Language data
for (Message key : languageStrings.keySet()) { for (Map.Entry<Message, String> entry : languageStrings.entrySet()) {
bufferedWriter.write(key + "=" + languageStrings.get(key)); bufferedWriter.write(entry.getKey() + "=" + entry.getValue());
bufferedWriter.newLine(); bufferedWriter.newLine();
} }
bufferedWriter.newLine(); bufferedWriter.newLine();
//Output any custom language strings the user had //Output any custom language strings the user had
if (customLanguageStrings != null) { if (customLanguageStrings != null) {
for (Message key : customLanguageStrings.keySet()) { for (Map.Entry<Message, String> entry : customLanguageStrings.entrySet()) {
bufferedWriter.write(key + "=" + customLanguageStrings.get(key)); bufferedWriter.write(entry.getKey() + "=" + entry.getValue());
bufferedWriter.newLine(); bufferedWriter.newLine();
} }
} }
@@ -240,25 +242,24 @@ public final class LanguageLoader {
*/ */
public void debug() { public void debug() {
if (loadedStringTranslations != null) { if (loadedStringTranslations != null) {
Set<Message> keys = loadedStringTranslations.keySet(); for (Map.Entry<Message, String> entry : loadedStringTranslations.entrySet()) {
for (Message key : keys) { Stargate.debug("LanguageLoader::Debug::loadedStringTranslations", entry.getKey() +
Stargate.debug("LanguageLoader::Debug::loadedStringTranslations", key + " => " + " => " + entry.getValue());
loadedStringTranslations.get(key));
} }
} }
if (loadedBackupStrings == null) { if (loadedBackupStrings == null) {
return; return;
} }
Set<Message> keys = loadedBackupStrings.keySet();
for (Message key : keys) { for (Map.Entry<Message, String> entry : loadedBackupStrings.entrySet()) {
Stargate.debug("LanguageLoader::Debug::loadedBackupStrings", key + " => " + Stargate.debug("LanguageLoader::Debug::loadedBackupStrings", entry.getKey() + " => " +
loadedBackupStrings.get(key)); entry.getValue());
} }
} }
@NotNull @NotNull
private Map<Message, String> fromStringMap(@NotNull Map<String, String> configurationStrings) { private Map<Message, String> fromStringMap(@NotNull Map<String, String> configurationStrings) {
Map<Message, String> output = new HashMap<>(); Map<Message, String> output = new EnumMap<>(Message.class);
for (Map.Entry<String, String> entry : configurationStrings.entrySet()) { for (Map.Entry<String, String> entry : configurationStrings.entrySet()) {
Message message = Message.getFromKey(entry.getKey()); Message message = Message.getFromKey(entry.getKey());
if (message == null) { if (message == null) {

View File

@@ -30,6 +30,8 @@ import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* The stargate config is responsible for keeping track of all configuration values * The stargate config is responsible for keeping track of all configuration values
@@ -429,10 +431,14 @@ public final class StargateConfig {
portalFolder = (String) configOptions.get(ConfigOption.PORTAL_FOLDER); portalFolder = (String) configOptions.get(ConfigOption.PORTAL_FOLDER);
if (portalFolder.isEmpty()) { if (portalFolder.isEmpty()) {
portalFolder = dataFolderPath + "/portals/"; portalFolder = dataFolderPath + "/portals/";
} else {
portalFolder = replacePluginFolderPath(portalFolder);
} }
gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER); gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER);
if (gateFolder.isEmpty()) { if (gateFolder.isEmpty()) {
gateFolder = dataFolderPath + "/gates/"; gateFolder = dataFolderPath + "/gates/";
} else {
gateFolder = replacePluginFolderPath(gateFolder);
} }
//If users have an outdated config, assume they also need to update their default gates //If users have an outdated config, assume they also need to update their default gates
@@ -450,6 +456,23 @@ public final class StargateConfig {
Stargate.getInstance().saveConfig(); Stargate.getInstance().saveConfig();
} }
/**
* Replaces "plugins/Stargate" in a folder path, and replaces it with the full path relative to the data folder
*
* @param input <p>The input string to replace in</p>
* @return <p>The replaced path, or the input if not applicable</p>
*/
@NotNull
private String replacePluginFolderPath(@NotNull String input) {
Pattern pattern = Pattern.compile("(?i)^plugins/Stargate");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
return dataFolderPath + matcher.replaceAll("");
} else {
return input;
}
}
/** /**
* Gets the object containing configuration values regarding gates * Gets the object containing configuration values regarding gates
* *

View File

@@ -254,18 +254,8 @@ public class PlayerEventListener implements Listener {
} }
//Allow players with permissions to apply dye to signs //Allow players with permissions to apply dye to signs
EquipmentSlot hand = event.getHand(); if (dyeSign(event, player, portal)) {
if (hand != null && (PermissionHelper.hasPermission(player, "stargate.admin.dye") || return;
portal.isOwner(player))) {
ItemStack item = player.getInventory().getItem(hand);
if (item != null) {
String itemName = item.getType().toString();
if (itemName.endsWith("DYE") || itemName.endsWith("INK_SAC")) {
event.setUseInteractedBlock(Event.Result.ALLOW);
Bukkit.getScheduler().scheduleSyncDelayedTask(Stargate.getInstance(), portal::drawSign, 1);
return;
}
}
} }
event.setUseInteractedBlock(Event.Result.DENY); event.setUseInteractedBlock(Event.Result.DENY);
@@ -295,6 +285,39 @@ public class PlayerEventListener implements Listener {
} }
} }
/**
* Tries to take care of a sign dye interaction
*
* @param event <p>The triggered player interaction event</p>
* @param player <p>The involved player</p>
* @param portal <p>The involved portal</p>
* @return <p>True if a sign was dyed</p>
*/
private boolean dyeSign(@NotNull PlayerInteractEvent event, @NotNull Player player, @NotNull Portal portal) {
EquipmentSlot hand = event.getHand();
// Check if the player is allowed to dye the sign
if (hand == null || (!PermissionHelper.hasPermission(player, "stargate.admin.dye") &&
!portal.isOwner(player))) {
return false;
}
// Check if the player is holding an item
ItemStack item = player.getInventory().getItem(hand);
if (item == null) {
return false;
}
String itemName = item.getType().toString();
// Check if the player's item can be used to dye the sign
if (itemName.endsWith("DYE") || itemName.endsWith("INK_SAC")) {
event.setUseInteractedBlock(Event.Result.ALLOW);
Bukkit.getScheduler().scheduleSyncDelayedTask(Stargate.getInstance(), portal::drawSign, 1);
return true;
} else {
return false;
}
}
/** /**
* Check if a player should be denied from accessing (using) a portal * Check if a player should be denied from accessing (using) a portal
* *

View File

@@ -15,6 +15,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.vehicle.VehicleMoveEvent; import org.bukkit.event.vehicle.VehicleMoveEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.List;
@@ -92,48 +93,27 @@ public class VehicleEventListener implements Listener {
rootEntity = rootEntity.getVehicle(); rootEntity = rootEntity.getVehicle();
} }
List<Player> players = TeleportHelper.getPlayers(rootEntity.getPassengers()); List<Player> players = TeleportHelper.getPlayers(rootEntity.getPassengers());
Portal destinationPortal = null; Portal destinationPortal = getDestinationPortal(players, entrancePortal);
for (Player player : players) {
//The entrance portal must be open for one player for the teleportation to happen
if (!entrancePortal.getPortalOpener().isOpenFor(player)) {
continue;
}
//Check if any of the players has selected the destination
Portal possibleDestinationPortal = entrancePortal.getPortalActivator().getDestination(player);
if (possibleDestinationPortal != null) {
destinationPortal = possibleDestinationPortal;
break;
}
}
//Cancel the teleport if no players activated the portal, or if any players are denied access //Cancel the teleport if no players activated the portal, or if any players are denied access
boolean cancelTeleport = false; boolean cancelTeleportation = false;
for (Player player : players) { for (Player player : players) {
if (destinationPortal == null) { if (destinationPortal == null) {
cancelTeleport = true; cancelTeleportation = true;
if (!entrancePortal.getOptions().isSilent()) { if (!entrancePortal.getOptions().isSilent()) {
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.INVALID_DESTINATION)); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.INVALID_DESTINATION));
} }
} else if (!TeleportHelper.playerCanTeleport(player, entrancePortal, destinationPortal)) { } else if (!TeleportHelper.playerCanTeleport(player, entrancePortal, destinationPortal)) {
cancelTeleport = true; cancelTeleportation = true;
} }
} }
if (cancelTeleport || destinationPortal == null) { if (cancelTeleportation || destinationPortal == null) {
return; return;
} }
//Take payment from all players //Take payment from all players
for (Player player : players) { if (!takePayment(players, entrancePortal, destinationPortal)) {
//To prevent the case where the first passenger pays and then the second passenger is denied, this has to be return;
// run after it has been confirmed that all passengers are able to pay
int cost = EconomyHelper.getUseCost(player, entrancePortal, destinationPortal);
if (cost > 0) {
if (EconomyHelper.cannotPayTeleportFee(entrancePortal, player, cost)) {
return;
}
}
} }
//Teleport the vehicle and inform the user if the vehicle was teleported //Teleport the vehicle and inform the user if the vehicle was teleported
@@ -148,4 +128,54 @@ public class VehicleEventListener implements Listener {
} }
} }
/**
* Tries to get the destination portal selected by one of the players included in the teleportation
*
* @param players <p>The players to be teleported</p>
* @param entrancePortal <p>The portal the players are entering</p>
* @return <p>The destination portal, or null if not found</p>
*/
@Nullable
private static Portal getDestinationPortal(@NotNull List<Player> players, @NotNull Portal entrancePortal) {
for (Player player : players) {
//The entrance portal must be open for one player for the teleportation to happen
if (!entrancePortal.getPortalOpener().isOpenFor(player)) {
continue;
}
//Check if any of the players has selected the destination
Portal possibleDestinationPortal = entrancePortal.getPortalActivator().getDestination(player);
if (possibleDestinationPortal != null) {
return possibleDestinationPortal;
}
}
return null;
}
/**
* Takes payment for the given players
*
* @param players <p>The players to take payment from</p>
* @param entrancePortal <p>The portal the players are travelling from</p>
* @param destinationPortal <p>The portal the players are travelling to</p>
* @return <p>True if payment was successfully taken, false otherwise</p>
*/
private static boolean takePayment(@NotNull List<Player> players, @NotNull Portal entrancePortal,
@NotNull Portal destinationPortal) {
for (Player player : players) {
//To prevent the case where the first passenger pays and then the second passenger is denied, this has to be
// run after it has been confirmed that all passengers are able to pay. Also note that some players might
// not have to pay, and thus the cost check has to be in the loop,
int cost = EconomyHelper.getUseCost(player, entrancePortal, destinationPortal);
if (cost > 0) {
if (EconomyHelper.cannotPayTeleportFee(entrancePortal, player, cost)) {
return false;
}
}
}
return true;
}
} }