diff --git a/build.gradle b/build.gradle index 0acb29cc..39a66b05 100644 --- a/build.gradle +++ b/build.gradle @@ -244,16 +244,16 @@ tasks.register('copyPlugin') { def destinationFilePath = Paths.get("$buildDir/MinecraftServer/plugins/Advanced-Portals.jar") println "Handling file: $destinationFilePath" - + byte[] newContent = Files.readAllBytes(sourceFilePath) - + if (Files.exists(destinationFilePath)) { println "File exists. Overwriting with new binary content." - + Files.write(destinationFilePath, newContent, StandardOpenOption.TRUNCATE_EXISTING) } else { println "File does not exist. Copying from source." - + Files.copy(sourceFilePath, destinationFilePath, StandardCopyOption.REPLACE_EXISTING) } } diff --git a/core/build.gradle b/core/build.gradle index 0b2f6656..eb429d6b 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -19,6 +19,8 @@ dependencies { implementation group: 'com.google.code.gson', name: 'gson', version:'2.8.9' includeLibs group: 'com.google.inject', name: 'guice', version:'5.0.1' implementation group: 'com.google.inject', name: 'guice', version:'5.0.1' + implementation group: 'org.yaml', name: 'snakeyaml', version: '2.2' + includeLibs group: 'org.yaml', name: 'snakeyaml', version: '2.2' implementation group: 'com.google.guava', name: 'guava', version: '33.2.0-jre' implementation group: 'io.netty', name: 'netty-buffer', version: '4.1.109.Final' implementation group: 'io.netty', name: 'netty-codec', version: '4.1.109.Final' diff --git a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java index 90211fc5..a7d276db 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -9,6 +9,7 @@ import com.sekwah.advancedportals.core.connector.commands.CommandRegister; import com.sekwah.advancedportals.core.connector.containers.ServerContainer; import com.sekwah.advancedportals.core.registry.TagRegistry; +import com.sekwah.advancedportals.core.repository.IPlayerDataRepository; import com.sekwah.advancedportals.core.serializeddata.DataStorage; import com.sekwah.advancedportals.core.module.AdvancedPortalsModule; import com.sekwah.advancedportals.core.repository.ConfigRepository; @@ -67,6 +68,10 @@ public class AdvancedPortalsCore { @Inject private GameScheduler gameScheduler; + // TEMP REMOVE THIS THIS IS JUST FOR DEV + @Inject + private IPlayerDataRepository tempDataRepository; + public AdvancedPortalsCore(String mcVersion, File dataStorageLoc, InfoLogger infoLogger, ServerContainer serverContainer) { instance = this; this.serverContainer = serverContainer; @@ -74,11 +79,11 @@ public AdvancedPortalsCore(String mcVersion, File dataStorageLoc, InfoLogger inf this.infoLogger = infoLogger; int[] mcVersionTemp; - infoLogger.log("Loading Advanced Portals Core v" + version + " for MC: " + mcVersion); + infoLogger.info("Loading Advanced Portals Core v" + version + " for MC: " + mcVersion); try { mcVersionTemp = Arrays.stream(mcVersion.split("\\.")).mapToInt(Integer::parseInt).toArray(); } catch (NumberFormatException e) { - infoLogger.log("Failed to parse MC version: " + mcVersion); + infoLogger.info("Failed to parse MC version: " + mcVersion); e.printStackTrace(); mcVersionTemp = new int[]{0, 0, 0}; } @@ -110,7 +115,7 @@ public void onEnable() { this.portalServices.loadPortals(); this.destinationServices.loadDestinations(); - this.infoLogger.log(Lang.translate("logger.pluginenable")); + this.infoLogger.info(Lang.translate("logger.pluginenable")); } private void registerTags() { @@ -172,14 +177,14 @@ public boolean registerDestiCommand(String arg, SubCommand subCommand, String... */ public void loadPortalConfig() { this.configRepository.loadConfig(this.dataStorage); - this.dataStorage.storeJson(this.configRepository, "config.json"); + this.configRepository.storeConfig(); } public void onDisable() { for(var playerContainer : this.serverContainer.getPlayers()) { playerDataRepository.playerLeave(playerContainer); } - this.infoLogger.log(Lang.translate("logger.plugindisable")); + this.infoLogger.info(Lang.translate("logger.plugindisable")); } public InfoLogger getInfoLogger() { diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java index bef35c69..5960dd9b 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java @@ -48,8 +48,8 @@ public void onCommand(CommandSenderContainer sender, String[] args) { // Find the tag with the "name" NAME DataTag nameTag = portalTags.stream().filter(tag -> { - this.infoLogger.log("Tag: " + tag.NAME); - this.infoLogger.log("Equals: " + tag.NAME.equals(NameTag.TAG_NAME)); + this.infoLogger.info("Tag: " + tag.NAME); + this.infoLogger.info("Equals: " + tag.NAME.equals(NameTag.TAG_NAME)); return tag.NAME.equals(NameTag.TAG_NAME); }).findFirst().orElse(null); diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java index fc764ae7..d187fa7e 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java @@ -105,36 +105,39 @@ public void registered() { } - if (tempData.getPos1() != null && tempData.getPos2() != null && tempData.getPos1().worldName.equals(player.getWorldName()) && tempData.getPos2().worldName.equals(player.getWorldName())) { + if (tempData.getPos1() != null && tempData.getPos2() != null && tempData.getPos1().getWorldName().equals(player.getWorldName()) && tempData.getPos2().getWorldName().equals(player.getWorldName())) { debugVisuals(player, tempData.getPos1(), tempData.getPos2(), SELECTION_COLOR, SHOW_TICKS); } - if(tempData.getPos1() != null && tempData.getPos1().worldName.equals(player.getWorldName())) { + if(tempData.getPos1() != null && tempData.getPos1().getWorldName().equals(player.getWorldName())) { Debug.addMarker(player, tempData.getPos1(), "Pos1", POS1_COLOR, SHOW_TICKS); } - if(tempData.getPos2() != null && tempData.getPos2().worldName.equals(player.getWorldName())) { + if(tempData.getPos2() != null && tempData.getPos2().getWorldName().equals(player.getWorldName())) { Debug.addMarker(player, tempData.getPos2(), "Pos2", POS2_COLOR, SHOW_TICKS); } var world = player.getWorld(); for (var portal : portalServices.getPortals()) { - if(Objects.equals(portal.getMinLoc().worldName, player.getWorldName()) && portal.isLocationInPortal(player.getLoc(), config.getVisibleRange())) { + if(Objects.equals(portal.getMinLoc().getWorldName(), player.getWorldName()) && portal.isLocationInPortal(player.getLoc(), config.getVisibleRange())) { BlockLocation minLoc = portal.getMinLoc(); BlockLocation maxLoc = portal.getMaxLoc(); - int midX = (minLoc.posX + maxLoc.posX) / 2; - int midZ = (minLoc.posZ + maxLoc.posZ) / 2; - BlockLocation midPoint = new BlockLocation(minLoc.worldName, midX, maxLoc.posY, midZ); + int midX = (minLoc.getPosX() + maxLoc.getPosX()) / 2; + int midZ = (minLoc.getPosZ() + maxLoc.getPosZ()) / 2; + BlockLocation midPoint = new BlockLocation(minLoc.getWorldName(), midX, maxLoc.getPosY(), midZ); Color color; if(portal.isTriggerBlock(world.getBlock(midPoint))) { color = TRIGGER_OUTLINE_COLOR; } else { - if(midPoint.posX == minLoc.posX || midPoint.posX == maxLoc.posX || midPoint.posZ == minLoc.posZ || midPoint.posZ == maxLoc.posZ) + if(midPoint.getPosX() == minLoc.getPosX() || midPoint.getPosX() == maxLoc.getPosX() || midPoint.getPosZ() == minLoc.getPosZ() || midPoint.getPosZ() == maxLoc.getPosZ()) color = OUTLINE_COLOR; else color = new Color(0, 0, 0, 0); } debugVisuals(player, portal, OUTLINE_COLOR, SHOW_TICKS, TRIGGER_COLOR); - Debug.addMarker(player, midPoint, portal.getArgValues(NameTag.TAG_NAME)[0], color, SHOW_TICKS); + var name = portal.getArgValues(NameTag.TAG_NAME); + if(name != null && name.length > 0) { + Debug.addMarker(player, midPoint, name[0], color, SHOW_TICKS); + } } } } @@ -150,13 +153,13 @@ private void debugVisuals(PlayerContainer player, AdvancedPortal portal, Color c } private void debugVisuals(PlayerContainer player, BlockLocation pos1, BlockLocation pos2, Color color, int time, Color triggerColor, AdvancedPortal portal) { - int minX = Math.min(pos1.posX, pos2.posX); - int minY = Math.min(pos1.posY, pos2.posY); - int minZ = Math.min(pos1.posZ, pos2.posZ); + int minX = Math.min(pos1.getPosX(), pos2.getPosX()); + int minY = Math.min(pos1.getPosY(), pos2.getPosY()); + int minZ = Math.min(pos1.getPosZ(), pos2.getPosZ()); - int maxX = Math.max(pos1.posX, pos2.posX); - int maxY = Math.max(pos1.posY, pos2.posY); - int maxZ = Math.max(pos1.posZ, pos2.posZ); + int maxX = Math.max(pos1.getPosX(), pos2.getPosX()); + int maxY = Math.max(pos1.getPosY(), pos2.getPosY()); + int maxZ = Math.max(pos1.getPosZ(), pos2.getPosZ()); var world = player.getWorld(); @@ -171,7 +174,7 @@ private void debugVisuals(PlayerContainer player, BlockLocation pos1, BlockLocat for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { for (int z = minZ; z <= maxZ; z++) { - var pos = new BlockLocation(pos1.worldName, x, y, z); + var pos = new BlockLocation(pos1.getWorldName(), x, y, z); boolean isTrigger = portal != null && portal.isTriggerBlock(world.getBlock(pos)); boolean isOutline = (y == minY || y == maxY) && (x == minX || x == maxX || z == minZ || z == maxZ) || (z == minZ || z == maxZ) && (x == minX || x == maxX); if (isTrigger && isOutline && alternate_show_trigger) { @@ -188,23 +191,23 @@ private void debugVisuals(PlayerContainer player, BlockLocation pos1, BlockLocat } else { for (int x = minX; x <= maxX; x++) { - Debug.addMarker(player, new BlockLocation(pos1.worldName, x, minY, minZ), "", color, time); - Debug.addMarker(player, new BlockLocation(pos1.worldName, x, minY, maxZ), "", color, time); - Debug.addMarker(player, new BlockLocation(pos1.worldName, x, maxY, minZ), "", color, time); - Debug.addMarker(player, new BlockLocation(pos1.worldName, x, maxY, maxZ), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), x, minY, minZ), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), x, minY, maxZ), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), x, maxY, minZ), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), x, maxY, maxZ), "", color, time); } for (int z = minZ + 1; z < maxZ; z++) { - Debug.addMarker(player, new BlockLocation(pos1.worldName, minX, minY, z), "", color, time); - Debug.addMarker(player, new BlockLocation(pos1.worldName, maxX, minY, z), "", color, time); - Debug.addMarker(player, new BlockLocation(pos1.worldName, minX, maxY, z), "", color, time); - Debug.addMarker(player, new BlockLocation(pos1.worldName, maxX, maxY, z), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), minX, minY, z), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), maxX, minY, z), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), minX, maxY, z), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), maxX, maxY, z), "", color, time); } for (int y = minY + 1; y < maxY; y++) { - Debug.addMarker(player, new BlockLocation(pos1.worldName, minX, y, minZ), "", color, time); - Debug.addMarker(player, new BlockLocation(pos1.worldName, maxX, y, minZ), "", color, time); - Debug.addMarker(player, new BlockLocation(pos1.worldName, minX, y, maxZ), "", color, time); - Debug.addMarker(player, new BlockLocation(pos1.worldName, maxX, y, maxZ), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), minX, y, minZ), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), maxX, y, minZ), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), minX, y, maxZ), "", color, time); + Debug.addMarker(player, new BlockLocation(pos1.getWorldName(), maxX, y, maxZ), "", color, time); } } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/EntityContainer.java b/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/EntityContainer.java index a0a612e6..867ed5b3 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/EntityContainer.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/EntityContainer.java @@ -1,24 +1,24 @@ -package com.sekwah.advancedportals.core.connector.containers; - -import com.sekwah.advancedportals.core.serializeddata.BlockLocation; -import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; -import com.sekwah.advancedportals.core.serializeddata.Vector; - -public interface EntityContainer { - - PlayerLocation getLoc(); - - double getHeight(); - - BlockLocation getBlockLoc(); - - boolean teleport(PlayerLocation location); - - WorldContainer getWorld(); - - String getName(); - - String getWorldName(); - - void setVelocity(Vector vector); -} +package com.sekwah.advancedportals.core.connector.containers; + +import com.sekwah.advancedportals.core.serializeddata.BlockLocation; +import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; +import com.sekwah.advancedportals.core.serializeddata.Vector; + +public interface EntityContainer { + + PlayerLocation getLoc(); + + double getHeight(); + + BlockLocation getBlockLoc(); + + boolean teleport(PlayerLocation location); + + WorldContainer getWorld(); + + String getName(); + + String getWorldName(); + + void setVelocity(Vector vector); +} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/destination/Destination.java b/core/src/main/java/com/sekwah/advancedportals/core/destination/Destination.java index 91102731..9ab94fe3 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/destination/Destination.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/destination/Destination.java @@ -1,6 +1,5 @@ package com.sekwah.advancedportals.core.destination; -import com.google.gson.annotations.SerializedName; import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.registry.TagTarget; @@ -20,7 +19,7 @@ * to add permissions to block access to certain areas and such. Could be a different permission system or just * it takes the tags on the destination and automatically applies them when a portal wants to warp to there. * (Of course it would not work cross server unless the data was communicated and checked first however that - * could effect performance and would definitely effect speed) + * could affect performance and would definitely affect speed) * * @author sekwah41 */ @@ -29,14 +28,16 @@ public class Destination implements TagTarget { @Inject transient TagRegistry tagRegistry; - @SerializedName("l") private PlayerLocation loc; - @SerializedName("a") private HashMap args = new HashMap<>(); private transient Set argsCol; + public Destination() { + this.loc = new PlayerLocation(); + } + public Destination(PlayerLocation loc) { this.loc = loc; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java b/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java index 20401ccf..50634580 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java @@ -1,6 +1,5 @@ package com.sekwah.advancedportals.core.portal; -import com.google.gson.annotations.SerializedName; import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.registry.TagTarget; @@ -25,13 +24,10 @@ public class AdvancedPortal implements TagTarget { @Inject private transient TagRegistry tagRegistry; - @SerializedName("max") private BlockLocation maxLoc; - @SerializedName("min") private BlockLocation minLoc; - @SerializedName("a") private final HashMap args = new HashMap<>(); @Inject @@ -40,6 +36,11 @@ public class AdvancedPortal implements TagTarget { @Inject transient ConfigRepository configRepository; + public AdvancedPortal() { + this.minLoc = new BlockLocation(); + this.maxLoc = new BlockLocation(); + } + public AdvancedPortal(BlockLocation minLoc, BlockLocation maxLoc, TagRegistry tagRegistry, PlayerDataServices playerDataServices) { this.tagRegistry = tagRegistry; this.playerDataServices = playerDataServices; @@ -59,6 +60,7 @@ public String[] getArgValues(String argName) { return this.args.get(argName); } + @Override public void setArgValues(String argName, String[] argValues) { this.args.put(argName, argValues); @@ -66,7 +68,7 @@ public void setArgValues(String argName, String[] argValues) { @Override public void addArg(String argName, String argValues) { - + // TODO need to add the ability to add args after creation } @Override @@ -81,16 +83,16 @@ public void removeArg(String arg) { * @param loc2 The second location. */ public void updateBounds(BlockLocation loc1, BlockLocation loc2) { - int minX = Math.min(loc1.posX, loc2.posX); - int minY = Math.min(loc1.posY, loc2.posY); - int minZ = Math.min(loc1.posZ, loc2.posZ); + int minX = Math.min(loc1.getPosX(), loc2.getPosX()); + int minY = Math.min(loc1.getPosY(), loc2.getPosY()); + int minZ = Math.min(loc1.getPosZ(), loc2.getPosZ()); - int maxX = Math.max(loc1.posX, loc2.posX); - int maxY = Math.max(loc1.posY, loc2.posY); - int maxZ = Math.max(loc1.posZ, loc2.posZ); + int maxX = Math.max(loc1.getPosX(), loc2.getPosX()); + int maxY = Math.max(loc1.getPosY(), loc2.getPosY()); + int maxZ = Math.max(loc1.getPosZ(), loc2.getPosZ()); - this.minLoc = new BlockLocation(loc1.worldName, minX, minY, minZ); - this.maxLoc = new BlockLocation(loc2.worldName, maxX, maxY, maxZ); + this.minLoc = new BlockLocation(loc1.getWorldName(), minX, minY, minZ); + this.maxLoc = new BlockLocation(loc2.getWorldName(), maxX, maxY, maxZ); } /*public boolean hasTriggerBlock(String blockMaterial) { @@ -117,7 +119,8 @@ public boolean activate(PlayerContainer player, boolean moveActivated) { player.sendMessage(Lang.translateInsertVariables("portal.cooldown.join", cooldown, Lang.translate(cooldown == 1 ? "time.second" : "time.seconds"))); if(configRepository.playFailSound()) { - player.playSound("block.portal.travel", 0.05f, new Random().nextFloat() * 0.4F + 0.8F); + var rand = new Random(); + player.playSound("block.portal.travel", 0.05f, rand.nextFloat() * 0.4F + 0.8F); } return false; } @@ -171,16 +174,16 @@ public boolean isLocationInPortal(PlayerLocation loc, int additionalArea) { } public boolean isLocationInPortal(BlockLocation loc, int additionalArea) { - double playerX = loc.posX; - double playerY = loc.posY; - double playerZ = loc.posZ; - - return Objects.equals(loc.worldName, this.minLoc.worldName) && playerX >= this.minLoc.posX - additionalArea && - playerX < this.maxLoc.posX + 1 + additionalArea && - playerY >= this.minLoc.posY - additionalArea && - playerY < this.maxLoc.posY + 1 + additionalArea && - playerZ >= this.minLoc.posZ - additionalArea && - playerZ < this.maxLoc.posZ + 1 + additionalArea; + double playerX = loc.getPosX(); + double playerY = loc.getPosY(); + double playerZ = loc.getPosZ(); + + return Objects.equals(loc.getWorldName(), this.minLoc.getWorldName()) && playerX >= this.minLoc.getPosX() - additionalArea && + playerX < this.maxLoc.getPosX() + 1 + additionalArea && + playerY >= this.minLoc.getPosY() - additionalArea && + playerY < this.maxLoc.getPosY() + 1 + additionalArea && + playerZ >= this.minLoc.getPosZ() - additionalArea && + playerZ < this.maxLoc.getPosZ() + 1 + additionalArea; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/ConfigRepository.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/ConfigRepository.java index 7c4c7c14..c164bc85 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/ConfigRepository.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/ConfigRepository.java @@ -31,4 +31,6 @@ public interface ConfigRepository { double getThrowbackStrength(); boolean playFailSound(); + + void storeConfig(); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java index a55afde9..1780d1e7 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java @@ -12,6 +12,7 @@ public class ConfigRepositoryImpl implements ConfigRepository { private HashMap configs; private Config config; + private DataStorage dataStorage; public ConfigRepositoryImpl() { configs = new HashMap(); @@ -89,7 +90,8 @@ public double getThrowbackStrength() { @Override public void loadConfig(DataStorage dataStorage) { - this.config = dataStorage.loadJson(Config.class, "config.json"); + this.dataStorage = dataStorage; + this.config = dataStorage.loadFile(Config.class, "config.yaml"); } @Override @@ -97,4 +99,9 @@ public boolean playFailSound() { return this.config.playFailSound; } + @Override + public void storeConfig() { + this.dataStorage.storeFile(this.config, "config.yaml"); + } + } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java index d55eb8ce..055a3182 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java @@ -1,17 +1,12 @@ package com.sekwah.advancedportals.core.repository.impl; -import com.google.common.collect.ImmutableMap; import com.google.inject.Inject; import com.sekwah.advancedportals.core.destination.Destination; import com.sekwah.advancedportals.core.repository.IDestinationRepository; import com.sekwah.advancedportals.core.serializeddata.DataStorage; import com.sekwah.advancedportals.core.tags.activation.NameTag; -import com.sekwah.advancedportals.core.util.InfoLogger; import javax.inject.Singleton; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -24,20 +19,20 @@ public class DestinationRepositoryImpl implements IDestinationRepository { @Override public boolean save(String name, Destination destination) { - return dataStorage.storeJson(destination, fileLocation + name + ".json"); + return dataStorage.storeFile(destination, fileLocation + name + ".yaml"); } public boolean containsKey(String name) { - return dataStorage.fileExists(fileLocation + name + ".json"); + return dataStorage.fileExists(fileLocation + name + ".yaml"); } @Override public boolean delete(String name) { - return dataStorage.deleteFile(fileLocation + name + ".json"); + return dataStorage.deleteFile(fileLocation + name + ".yaml"); } public Destination get(String name) { - return dataStorage.loadJson(Destination.class, fileLocation + name + ".json"); + return dataStorage.loadFile(Destination.class, fileLocation + name + ".yaml"); } @Override diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java index cbb118e7..0c0458a3 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PlayerDataRepositoryImpl.java @@ -1,17 +1,11 @@ package com.sekwah.advancedportals.core.repository.impl; import com.google.inject.Inject; -import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; -import com.sekwah.advancedportals.core.destination.Destination; -import com.sekwah.advancedportals.core.repository.ConfigRepository; import com.sekwah.advancedportals.core.repository.IPlayerDataRepository; -import com.sekwah.advancedportals.core.serializeddata.BlockLocation; import com.sekwah.advancedportals.core.serializeddata.DataStorage; -import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; import com.sekwah.advancedportals.core.serializeddata.PlayerData; import java.util.List; -import java.util.UUID; public class PlayerDataRepositoryImpl implements IPlayerDataRepository { @@ -22,7 +16,7 @@ public class PlayerDataRepositoryImpl implements IPlayerDataRepository { @Override public boolean save(String name, PlayerData playerData) { - return dataStorage.storeJson(playerData, fileLocation + name + ".json"); + return dataStorage.storeFile(playerData, fileLocation + name + ".yaml"); } @Override @@ -32,12 +26,12 @@ public boolean containsKey(String name) { @Override public boolean delete(String name) { - return dataStorage.deleteFile(fileLocation + name + ".json"); + return dataStorage.deleteFile(fileLocation + name + ".yaml"); } @Override public PlayerData get(String name) { - return dataStorage.loadJson(PlayerData.class, fileLocation + name + ".json"); + return dataStorage.loadFile(PlayerData.class, fileLocation + name + ".yaml"); } @Override diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java index 948f3e0a..1f3586ff 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java @@ -27,29 +27,31 @@ public class PortalRepositoryImpl implements IPortalRepository { @Override public boolean save(String name, AdvancedPortal portal) { - return dataStorage.storeJson(portal, fileLocation + name + ".json"); + return dataStorage.storeFile(portal, fileLocation + name + ".yaml"); } @Override public boolean containsKey(String name) { - return dataStorage.fileExists(fileLocation + name + ".json"); + return dataStorage.fileExists(fileLocation + name + ".yaml"); } @Override public boolean delete(String name) { - return dataStorage.deleteFile(fileLocation + name + ".json"); + return dataStorage.deleteFile(fileLocation + name + ".yaml"); } @Override public AdvancedPortal get(String name) { - var portal = dataStorage.loadJson(AdvancedPortal.class, fileLocation + name + ".json"); - AdvancedPortalsCore.getInstance().getModule().getInjector().injectMembers(portal); + var portal = dataStorage.loadFile(AdvancedPortal.class, fileLocation + name + ".yaml"); + if(portal != null) { + AdvancedPortalsCore.getInstance().getModule().getInjector().injectMembers(portal); + } return portal; } @Override public List getAllNames() { - return dataStorage.listAllFiles(fileLocation, true); + return dataStorage.listAllFiles(fileLocation, true, "yaml"); } @Override @@ -57,11 +59,11 @@ public List getAll() { List portals = new ArrayList<>(); List allFiles = dataStorage.listAllFiles(fileLocation, false); for (String fileName : allFiles) { - AdvancedPortal portal = dataStorage.loadJson(AdvancedPortal.class, fileLocation + fileName); + AdvancedPortal portal = dataStorage.loadFile(AdvancedPortal.class, fileLocation + fileName); // Forces the name tag to be up-to-date on load String[] name = portal.getArgValues(NameTag.TAG_NAME); if(name != null && name.length > 0) { - portal.setArgValues(NameTag.TAG_NAME, new String[]{fileName.replace(".json", "")}); + portal.setArgValues(NameTag.TAG_NAME, new String[]{fileName.replace(".yaml", "")}); } portals.add(portal); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/BlockLocation.java b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/BlockLocation.java index 1424eb15..2a5d6afd 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/BlockLocation.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/BlockLocation.java @@ -1,21 +1,26 @@ package com.sekwah.advancedportals.core.serializeddata; -import com.google.gson.annotations.SerializedName; import com.sekwah.advancedportals.core.data.Direction; +import java.io.Serializable; + public class BlockLocation { - @SerializedName("x") - public final int posX; + // These should be treated as final, they only are not for serialization purposes + private final int posX; + + private final int posY; - @SerializedName("y") - public final int posY; + private final int posZ; - @SerializedName("z") - public final int posZ; + private final String worldName; - @SerializedName("w") - public final String worldName; + public BlockLocation() { + this.worldName = ""; + this.posX = 0; + this.posY = 0; + this.posZ = 0; + } public BlockLocation(String worldName, int posX, int posY, int posZ) { this.worldName = worldName; @@ -44,6 +49,10 @@ public int getPosZ() { return posZ; } + public String getWorldName() { + return worldName; + } + public boolean equals(BlockLocation location) { return location.posX == this.posX && location.posY == this.posY && location.posZ == this.posZ && location.worldName.equals(this.worldName); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/DataStorage.java b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/DataStorage.java index 0f17ddae..3c408a50 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/DataStorage.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/DataStorage.java @@ -1,14 +1,17 @@ package com.sekwah.advancedportals.core.serializeddata; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.inject.Inject; import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.util.InfoLogger; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.inspector.TagInspector; +import org.yaml.snakeyaml.nodes.Tag; +import org.yaml.snakeyaml.representer.Representer; import java.io.*; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Type; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; @@ -16,9 +19,7 @@ public class DataStorage { - private Gson gson = new GsonBuilder().setPrettyPrinting().create(); - - private File dataFolder; + private final File dataFolder; @Inject private AdvancedPortalsCore portalsCore; @@ -30,12 +31,26 @@ public DataStorage(File dataStorageLoc) { this.dataFolder = dataStorageLoc; } - /** - * Copies the default file, defaults to true to keep true to the name - * - * @param fileLoc - * @return - */ + private Yaml getYaml(Class clazz) { + + LoaderOptions loaderOptions = new LoaderOptions(); + + TagInspector tagInspector = tag -> tag.getClassName().equals(clazz.getName()); + + loaderOptions.setTagInspector(tagInspector); + + var options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + var representer = new ReflectiveRepresenter(options); + representer.addClassTag(clazz, Tag.MAP); + representer.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + var constructor = new ReflectiveConstructor(clazz, loaderOptions); + + AdvancedPortalsCore.getInstance().getModule().getInjector().injectMembers(constructor); + + return new Yaml(constructor, representer); + } + public boolean copyDefaultFile(String fileLoc) { return this.copyDefaultFile(fileLoc, true); } @@ -46,18 +61,9 @@ public void copyDefaultFiles(boolean override, String... fileLocs) { } } - public T loadJson(Type dataHolder, String location) { - InputStream jsonResource = this.loadResource(location); - if(jsonResource == null) { - return null; - } - BufferedReader bufReader = new BufferedReader(new InputStreamReader(jsonResource)); - T object = gson.fromJson(bufReader, dataHolder); - return object; - } - public T loadJson(Class dataHolder, String location) { - InputStream jsonResource = this.loadResource(location); - if(jsonResource == null) { + public T loadFile(Class dataHolder, String location) { + InputStream yamlResource = this.loadResource(location); + if (yamlResource == null) { try { return dataHolder.getDeclaredConstructor().newInstance(); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { @@ -65,29 +71,26 @@ public T loadJson(Class dataHolder, String location) { } return null; } - BufferedReader bufReader = new BufferedReader(new InputStreamReader(jsonResource)); - T data = gson.fromJson(bufReader, dataHolder); - try { - bufReader.close(); - } catch (IOException e) { - throw new RuntimeException(e); + Yaml yaml = getYaml(dataHolder); + try (BufferedReader bufReader = new BufferedReader(new InputStreamReader(yamlResource))) { + return yaml.loadAs(bufReader, dataHolder); + } catch (Exception e) { + infoLogger.warning("Failed to load file: " + location); + infoLogger.error(e); + return null; } - return data; } - public boolean storeJson(Object dataHolder, String location) { - // Create folders if they don't exist + public boolean storeFile(Object dataHolder, String location) { + Yaml yaml = getYaml(dataHolder.getClass()); File outFile = new File(this.dataFolder, location); - if (!outFile.getParentFile().exists()) { // Check if parent folder exists - if(!outFile.getParentFile().mkdirs()) { - infoLogger.warning("Failed to create folder for file: " + location); - } + if (!outFile.getParentFile().exists() && !outFile.getParentFile().mkdirs()) { + infoLogger.warning("Failed to create folder for file: " + location); } - String json = gson.toJson(dataHolder); - try { - FileWriter fileWriter = new FileWriter(outFile); // Use outFile directly here - fileWriter.write(json); - fileWriter.close(); + + String yamlFile = yaml.dump(dataHolder); + try (FileWriter fileWriter = new FileWriter(outFile)) { + fileWriter.write(yamlFile); return true; } catch (IOException e) { infoLogger.error(e); @@ -179,45 +182,51 @@ public void writeResource(InputStream inputStream, String location) { } private void writeToFile(InputStream inputStream, File outFile) throws IOException { - FileOutputStream outStream = new FileOutputStream(outFile); - byte[] buf = new byte[1024]; - int len; - while ((len = inputStream.read(buf)) > 0) { - outStream.write(buf, 0, len); + try (FileOutputStream outStream = new FileOutputStream(outFile)) { + byte[] buf = new byte[1024]; + int len; + while ((len = inputStream.read(buf)) > 0) { + outStream.write(buf, 0, len); + } } inputStream.close(); - outStream.close(); } public boolean fileExists(String name) { return new File(this.dataFolder, name).exists(); } + public List listAllFiles(String fileLocation, boolean trimExtension) { + return listAllFiles(fileLocation, trimExtension, null); + } + /** - * @param fileLocation - * @param trimExtension + * @param fileLocation - location of the folder to list + * @param trimExtension - if true will remove the file extension + * @param extension - if null will not filter by extension * @return */ - public List listAllFiles(String fileLocation, boolean trimExtension) { + public List listAllFiles(String fileLocation, boolean trimExtension, String extension) { File directory = new File(dataFolder, fileLocation); - var list = new ArrayList(); + List list = new ArrayList<>(); if (directory.exists() && directory.isDirectory()) { File[] files = directory.listFiles(); - if (files != null) { for (File file : files) { if (file.isFile()) { String fileName = file.getName(); - - if (trimExtension) { - int i = fileName.lastIndexOf('.'); - if (i > 0) { - fileName = fileName.substring(0, i); + boolean extensionMatches = (extension == null || fileName.endsWith("." + extension)); + + if (extensionMatches) { + if (trimExtension) { + int i = fileName.lastIndexOf('.'); + if (i > 0) { + fileName = fileName.substring(0, i); + } } + list.add(fileName); } - - list.add(fileName); } } } @@ -232,7 +241,7 @@ public boolean deleteFile(String fileLocation) { Files.delete(Paths.get(dataFolder.getAbsolutePath(), fileLocation)); return true; } catch (IOException e) { - e.printStackTrace(); + infoLogger.error(e); return false; } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerLocation.java b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerLocation.java index 2735099f..4d7b7890 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerLocation.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerLocation.java @@ -1,15 +1,18 @@ package com.sekwah.advancedportals.core.serializeddata; -import com.google.gson.annotations.SerializedName; public class PlayerLocation extends WorldLocation { - @SerializedName("r") private final float yaw; - @SerializedName("p") private final float pitch; + public PlayerLocation() { + super("", 0, 0, 0); + this.yaw = 0; + this.pitch = 0; + } + public PlayerLocation(String worldName, double posX, double posY, double posZ) { super(worldName, posX, posY, posZ); this.yaw = 0; @@ -23,15 +26,15 @@ public PlayerLocation(String worldName, double posX, double posY, double posZ, f } public double getPosX() { - return X; + return x; } public double getPosY() { - return Y; + return y; } public double getPosZ() { - return Z; + return z; } public String getWorldName() { diff --git a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/ReflectiveConstructor.java b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/ReflectiveConstructor.java new file mode 100644 index 00000000..041da2ba --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/ReflectiveConstructor.java @@ -0,0 +1,199 @@ +package com.sekwah.advancedportals.core.serializeddata; + +import com.sekwah.advancedportals.core.util.InfoLogger; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.constructor.Constructor; +import org.yaml.snakeyaml.nodes.*; +import sun.misc.Unsafe; + +import javax.inject.Inject; +import java.lang.reflect.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReflectiveConstructor extends Constructor { + private static final Unsafe unsafe = getUnsafe(); + private final Class clazz; + + @Inject + private InfoLogger infoLogger; + + public ReflectiveConstructor(Class clazz, LoaderOptions loadingConfig) { + super(clazz, loadingConfig); + this.clazz = clazz; + } + + @Override + protected Object constructObject(Node node) { + return this.constructObject(clazz, node); + } + + private Object constructObject(Class currentClass, Node node) { + if (node instanceof MappingNode) { + return constructFromMappingNode(currentClass, (MappingNode) node); + } else if (node instanceof ScalarNode scalarNode) { + return constructFromScalarNode(scalarNode); + } else if(node instanceof SequenceNode sequenceNode) { + return constructFromSequenceNode(sequenceNode); + } else { + infoLogger.warning("Unexpected node type encountered: " + node.getClass().getSimpleName()); + return null; + } + } + + private Object constructFromSequenceNode(SequenceNode sequenceNode) { + List list = (List) super.constructObject(sequenceNode); + + if (list == null || list.isEmpty()) { + return list; + } + + Class componentType = list.get(0).getClass(); + Object array = Array.newInstance(componentType, list.size()); + for (int i = 0; i < list.size(); i++) { + Array.set(array, i, list.get(i)); + } + + return array; + } + + private Object constructFromMappingNode(Class currentClass, MappingNode mappingNode) { + + // if the class is a hashmap, loop over the values and construct the objects + if (currentClass.equals(HashMap.class)) { + Map values = new HashMap<>(); + for (NodeTuple tuple : mappingNode.getValue()) { + var key = (String) constructObject(tuple.getKeyNode()); + + var node = tuple.getValueNode(); + + if (node instanceof ScalarNode scalarNode) { + var constructedItem = constructFromScalarNode(scalarNode); + values.put(key, constructedItem); + } else if(node instanceof SequenceNode sequenceNode) { + var constructedItem = constructFromSequenceNode(sequenceNode); + values.put(key, constructedItem); + } else if (node instanceof MappingNode mappingNodeChild) { + try { + Object value = constructFromMappingNode(Object.class, mappingNodeChild); + values.put(key, value); + } catch (Exception e) { + infoLogger.warning("Failed to construct object from mapping node: " + e.getMessage()); + } + } else { + infoLogger.warning("Unexpected node type encountered: " + node.getClass().getSimpleName()); + } + } + return values; + } + + try { + Object instance; + try { + instance = currentClass.getDeclaredConstructor().newInstance(); + } catch (NoSuchMethodException e) { + infoLogger.info("No default constructor found for " + currentClass.getName() + ", using unsafe allocation."); + instance = unsafe.allocateInstance(currentClass); + } + + Map mappedValues = mapMappingNode(currentClass, mappingNode); + + Field[] fields = getAllFields(currentClass); + for (Field field : fields) { + if (Modifier.isTransient(field.getModifiers())) { + continue; + } + + try { + if (mappedValues.containsKey(field.getName())) { + Object value = mappedValues.get(field.getName()); + + setField(instance, field, value); + } else { + infoLogger.warning("Field " + field.getName() + " not found in mapping node " + instance.getClass().getName() + " will use default value."); + } + } + catch (Exception e) { + infoLogger.warning("Failed to set field " + field.getName() + " in " + currentClass.getName() + ": " + e.getMessage()); + infoLogger.error(e); + throw new RuntimeException("Failed to set field " + field.getName() + " in " + currentClass.getName(), e); + } + } + return instance; + } catch (Exception e) { + infoLogger.warning("Failed to instantiate " + currentClass.getName() + ": " + e.getMessage()); + throw new RuntimeException("Failed to instantiate " + currentClass.getName(), e); + } + } + + private Field[] getAllFields(Class clazz) { + Map fields = new HashMap<>(); + while (clazz != null) { + for (Field field : clazz.getDeclaredFields()) { + fields.putIfAbsent(field.getName(), field); + } + clazz = clazz.getSuperclass(); + } + return fields.values().toArray(new Field[0]); + } + + private Object constructFromScalarNode(ScalarNode scalarNode) { + return super.constructObject(scalarNode); + } + + private Map mapMappingNode(Class currentClass, MappingNode mappingNode) { + Map values = new HashMap<>(); + for (NodeTuple tuple : mappingNode.getValue()) { + var key = (String) super.constructObject(tuple.getKeyNode()); + + var node = tuple.getValueNode(); + + if (node instanceof ScalarNode scalarNode) { + values.put(key, constructFromScalarNode(scalarNode)); + } else if (node instanceof MappingNode mappingNodeChild) { + try { + var field = currentClass.getDeclaredField(key); + Object value = constructFromMappingNode(field.getType(), mappingNodeChild); + values.put(key, value); + } catch (NoSuchFieldException e) { + infoLogger.warning("Field " + key + " not found on " + currentClass.getName()); + } + } else { + infoLogger.warning("Expected mapping node: " + node.getClass().getSimpleName()); + } + } + return values; + } + + /** + * Check and convert value types e.g. double to float + */ + private void setField(Object instance, Field field, Object value) throws IllegalAccessException { + + // Check for numeric type compatibility and cast if necessary + if (field.getType() == float.class && value instanceof Double) { + value = ((Double) value).floatValue(); + } else if (field.getType() == int.class && value instanceof Long) { + value = ((Long) value).intValue(); + } else if (field.getType() == short.class && value instanceof Integer) { + value = ((Integer) value).shortValue(); + } else if (field.getType() == byte.class && value instanceof Integer) { + value = ((Integer) value).byteValue(); + } + + field.setAccessible(true); + field.set(instance, value); + } + + + private static Unsafe getUnsafe() { + try { + Field f = Unsafe.class.getDeclaredField("theUnsafe"); + f.setAccessible(true); + return (Unsafe) f.get(null); + } catch (Exception e) { + throw new RuntimeException("Failed to get Unsafe instance", e); + } + } +} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/ReflectiveRepresenter.java b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/ReflectiveRepresenter.java new file mode 100644 index 00000000..6117aa16 --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/ReflectiveRepresenter.java @@ -0,0 +1,13 @@ +package com.sekwah.advancedportals.core.serializeddata; + +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.introspector.BeanAccess; +import org.yaml.snakeyaml.representer.Representer; + +public class ReflectiveRepresenter extends Representer { + + public ReflectiveRepresenter(DumperOptions options) { + super(options); + this.getPropertyUtils().setBeanAccess(BeanAccess.FIELD); + } +} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/Vector.java b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/Vector.java index f35304f5..80453b09 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/Vector.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/Vector.java @@ -1,33 +1,28 @@ package com.sekwah.advancedportals.core.serializeddata; -import com.google.gson.annotations.SerializedName; - public class Vector { - @SerializedName("x") - public final double X; + protected final double x; - @SerializedName("y") - public final double Y; + protected final double y; - @SerializedName("z") - public final double Z; + protected final double z; public Vector(double X, double Y, double Z) { - this.X = X; - this.Y = Y; - this.Z = Z; + this.x = X; + this.y = Y; + this.z = Z; } public Vector add(Vector vec) { - return new Vector(this.X + vec.X, this.Y + vec.Y, this.Z + vec.Z); + return new Vector(this.x + vec.x, this.y + vec.y, this.z + vec.z); } public Vector multiply(double value) { - return new Vector(this.X * value, this.Y * value, this.Z * value); + return new Vector(this.x * value, this.y * value, this.z * value); } public Vector setY(double y) { - return new Vector(this.X, y, this.Z); + return new Vector(this.x, y, this.z); } public double distanceTo(Vector pos) { @@ -35,22 +30,22 @@ public double distanceTo(Vector pos) { } private double distanceToSq(Vector pos) { - double dx = this.X - pos.X; - double dy = this.Y - pos.Y; - double dz = this.Z - pos.Z; + double dx = this.x - pos.x; + double dy = this.y - pos.y; + double dz = this.z - pos.z; return dx * dx + dy * dy + dz * dz; } public double getX() { - return this.X; + return this.x; } public double getY() { - return this.Y; + return this.y; } public double getZ() { - return this.Z; + return this.z; } public Vector normalize() { @@ -58,6 +53,6 @@ public Vector normalize() { } private double length() { - return Math.sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z); + return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/WorldLocation.java b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/WorldLocation.java index 916cb0fa..3d65511b 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/WorldLocation.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/WorldLocation.java @@ -1,10 +1,7 @@ package com.sekwah.advancedportals.core.serializeddata; -import com.google.gson.annotations.SerializedName; - public class WorldLocation extends Vector { - @SerializedName("w") public final String worldName; public WorldLocation(String worldName, double posX, double posY, double posZ) { @@ -13,6 +10,6 @@ public WorldLocation(String worldName, double posX, double posY, double posZ) { } public BlockLocation toBlockPos() { - return new BlockLocation(this.worldName, (int) Math.floor(this.X), (int) Math.floor(this.Y), (int) Math.floor(this.Z)); + return new BlockLocation(this.worldName, (int) Math.floor(this.x), (int) Math.floor(this.y), (int) Math.floor(this.z)); } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/PlayerDataServices.java b/core/src/main/java/com/sekwah/advancedportals/core/services/PlayerDataServices.java index d2095041..c8ff2577 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/services/PlayerDataServices.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/PlayerDataServices.java @@ -56,6 +56,6 @@ public void playerSelectorActivate(PlayerContainer player, BlockLocation blockLo } else { tempData.setPos2(blockLoc); } - player.sendMessage(Lang.translateInsertVariables("portal.selector.poschange", leftClick ? "1" : "2", blockLoc.posX, blockLoc.posY, blockLoc.posZ)); + player.sendMessage(Lang.translateInsertVariables("portal.selector.poschange", leftClick ? "1" : "2", blockLoc.getPosX(), blockLoc.getPosY(), blockLoc.getPosZ())); } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java b/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java index a5b3763c..0f2be0e4 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java @@ -40,6 +40,9 @@ public void loadPortals() { portalCache.clear(); for (String name : portalNames) { AdvancedPortal portal = portalRepository.get(name); + if(portal == null) { + continue; + } portalCache.put(name, portal); portal.updateBounds(portal.getMinLoc(), portal.getMaxLoc()); } @@ -132,7 +135,7 @@ public AdvancedPortal createPortal(PlayerContainer player, ArrayList ta return null; } - if(!tempData.getPos1().worldName.equals(tempData.getPos2().worldName)) { + if(!tempData.getPos1().getWorldName().equals(tempData.getPos2().getWorldName())) { player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("portal.error.selection.differentworlds")); return null; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/util/FriendlyDataOutput.java b/core/src/main/java/com/sekwah/advancedportals/core/util/FriendlyDataOutput.java index 19423ae3..11e02464 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/util/FriendlyDataOutput.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/util/FriendlyDataOutput.java @@ -113,8 +113,8 @@ public void writeBlock(BlockLocation blockLoc) { private long blockAsLong(BlockLocation blockLoc) { long i = 0L; - i |= ((long)blockLoc.posX & PACKED_X_MASK) << X_OFFSET; - i |= ((long)blockLoc.posY & PACKED_Y_MASK) << 0; - return i | ((long)blockLoc.posZ & PACKED_Z_MASK) << Z_OFFSET; + i |= ((long)blockLoc.getPosX() & PACKED_X_MASK) << X_OFFSET; + i |= ((long)blockLoc.getPosY() & PACKED_Y_MASK) << 0; + return i | ((long)blockLoc.getPosZ() & PACKED_Z_MASK) << Z_OFFSET; } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/util/InfoLogger.java b/core/src/main/java/com/sekwah/advancedportals/core/util/InfoLogger.java index 9ec156c7..656275d7 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/util/InfoLogger.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/util/InfoLogger.java @@ -13,7 +13,7 @@ public abstract class InfoLogger { * General information logging * @param s info message */ - public abstract void log(String s); + public abstract void info(String s); public abstract void error(Exception e); } diff --git a/lang/src/main/resources/lang/en_GB.lang b/lang/src/main/resources/lang/en_GB.lang index 6ca5e306..32f419df 100644 --- a/lang/src/main/resources/lang/en_GB.lang +++ b/lang/src/main/resources/lang/en_GB.lang @@ -175,4 +175,3 @@ tag.name.error.nospaces= The name cannot contain spaces. tag.triggerblock.description=Sets the trigger block/s of the portal. Comma seperated or multi tag. tag.cooldown.fail= The cooldown must be a number. - diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/SpigotInfoLogger.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/SpigotInfoLogger.java index be2ea072..54b5cef9 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/SpigotInfoLogger.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/SpigotInfoLogger.java @@ -18,7 +18,7 @@ public void warning(String s) { } @Override - public void log(String s) { + public void info(String s) { plugin.getLogger().info(s); } diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/UpdatePortalSubCommand.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/UpdatePortalSubCommand.java index 37c611cf..121ed5d0 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/UpdatePortalSubCommand.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/UpdatePortalSubCommand.java @@ -45,7 +45,7 @@ public void onCommand(CommandSenderContainer sender, String[] args) { } private int importPortals() { - ConfigAccessor portalConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "portals.yml"); + ConfigAccessor portalConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "portals.yaml"); var config = portalConfig.getConfig(); Set portalSet = config.getKeys(false); @@ -114,7 +114,7 @@ public String getArg(List tags, String arg) { } public int importDestinations() { - ConfigAccessor destiConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "destinations.yml"); + ConfigAccessor destiConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "destinations.yaml"); var config = destiConfig.getConfig(); Set destiSet = config.getKeys(false); @@ -133,7 +133,7 @@ public int importDestinations() { } public int getDestinationCount() { - ConfigAccessor destiConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "destinations.yml"); + ConfigAccessor destiConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "destinations.yaml"); var config = destiConfig.getConfig(); Set destiSet = config.getKeys(false); @@ -141,7 +141,7 @@ public int getDestinationCount() { } public int getPortalCount() { - ConfigAccessor portalConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "portals.yml"); + ConfigAccessor portalConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "portals.yaml"); var config = portalConfig.getConfig(); Set portalSet = config.getKeys(false); diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/update/ConfigAccessor.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/update/ConfigAccessor.java index 0379eb77..e2363657 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/update/ConfigAccessor.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/update/ConfigAccessor.java @@ -21,7 +21,7 @@ public ConfigAccessor(JavaPlugin plugin, String fileName) { this.fileName = fileName; } - // gets all of the + // gets all of the public void reloadConfig() { if (configFile == null) { File dataFolder = plugin.getDataFolder(); @@ -52,7 +52,7 @@ public void saveConfig() { } } - // Saves + // Saves /** * public void saveDefaultConfig() { diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotWorldContainer.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotWorldContainer.java index 3723fb4d..820b623e 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotWorldContainer.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotWorldContainer.java @@ -18,16 +18,16 @@ public SpigotWorldContainer(World world) { public void setBlock(BlockLocation location, String material) { Material mat = Material.getMaterial(material, false); - if(mat != null) this.world.getBlockAt(location.posX, location.posY, location.posZ).setType(mat); + if(mat != null) this.world.getBlockAt(location.getPosX(), location.getPosY(), location.getPosZ()).setType(mat); } public String getBlock(BlockLocation location) { - return this.world.getBlockAt(location.posX, location.posY, location.posZ).getType().toString(); + return this.world.getBlockAt(location.getPosX(), location.getPosY(), location.getPosZ()).getType().toString(); } @Override public BlockAxis getBlockAxis(BlockLocation location) { - var block = world.getBlockAt(location.posX, location.posY, location.posZ); + var block = world.getBlockAt(location.getPosX(), location.getPosY(), location.getPosZ()); var matData = block.getState().getBlockData(); if(matData instanceof Orientable rotatable) { try { @@ -41,7 +41,7 @@ public BlockAxis getBlockAxis(BlockLocation location) { @Override public void setBlockAxis(BlockLocation location, BlockAxis axis) { - var block = world.getBlockAt(location.posX, location.posY, location.posZ); + var block = world.getBlockAt(location.getPosX(), location.getPosY(), location.getPosZ()); var matData = block.getState().getBlockData(); if(matData instanceof Orientable rotatable) { rotatable.setAxis(Axis.valueOf(axis.toString())); diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/metrics/Metrics.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/metrics/Metrics.java index 3eeed5b4..d6ddbdc2 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/metrics/Metrics.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/metrics/Metrics.java @@ -83,7 +83,7 @@ public Metrics(Plugin plugin) { // Get the config file File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats"); - File configFile = new File(bStatsFolder, "config.yml"); + File configFile = new File(bStatsFolder, "config.yaml"); YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); // Check if the config file exists diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/utils/ContainerHelpers.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/utils/ContainerHelpers.java index a18a4920..8e8f26bb 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/utils/ContainerHelpers.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/utils/ContainerHelpers.java @@ -1,18 +1,18 @@ -package com.sekwah.advancedportals.spigot.utils; - -import com.sekwah.advancedportals.core.serializeddata.BlockLocation; -import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; -import org.bukkit.Location; - -public class ContainerHelpers { - - public static PlayerLocation toPlayerLocation(Location loc) { - return new PlayerLocation(loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); - } - - public static BlockLocation toBlockLocation(Location loc) { - return new BlockLocation(loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); - } - - -} +package com.sekwah.advancedportals.spigot.utils; + +import com.sekwah.advancedportals.core.serializeddata.BlockLocation; +import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; +import org.bukkit.Location; + +public class ContainerHelpers { + + public static PlayerLocation toPlayerLocation(Location loc) { + return new PlayerLocation(loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); + } + + public static BlockLocation toBlockLocation(Location loc) { + return new BlockLocation(loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); + } + + +}