From c6d644a0b3359de8ed038579abee8d0c7f193225 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Tue, 5 Dec 2023 09:20:08 +0000 Subject: [PATCH] feat: add basic desti create and remove --- .../core/AdvancedPortalsCore.java | 5 +- .../desti/CreateDestiSubCommand.java | 29 +++----- .../desti/RemoveDestiSubCommand.java | 63 +++++++++++++++++ .../portal/CreatePortalSubCommand.java | 11 +-- ...mmand.java => RemovePortalSubCommand.java} | 11 ++- .../reusable/CreateTaggedSubCommand.java | 23 +++++-- .../core/module/AdvancedPortalsModule.java | 1 + .../core/registry/SubCommandRegistry.java | 5 +- .../core/registry/TagRegistry.java | 6 +- .../core/registry/WarpEffectRegistry.java | 6 +- .../core/repository/IJsonRepository.java | 6 +- .../impl/DestinationRepositoryImpl.java | 37 +++++----- .../repository/impl/PortalRepositoryImpl.java | 13 +++- .../core/serializeddata/DataStorage.java | 67 +++++++++++++++++-- .../core/serializeddata/PlayerLocation.java | 2 +- .../core/services/DestinationServices.java | 27 +++++--- .../core/services/PortalServices.java | 2 +- .../core/tags/activation/NameTag.java | 7 ++ .../advancedportals/core/util/InfoLogger.java | 5 +- .../advancedportals/core/util/Lang.java | 4 +- lang/src/main/resources/lang/en_GB.lang | 17 +++-- .../spigot/SpigotInfoLogger.java | 11 ++- .../container/SpigotPlayerContainer.java | 2 +- 23 files changed, 260 insertions(+), 100 deletions(-) create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/RemoveDestiSubCommand.java rename core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/{RemoveSubCommand.java => RemovePortalSubCommand.java} (85%) 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 6f019925..a359d707 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -4,6 +4,7 @@ import com.google.inject.Injector; import com.sekwah.advancedportals.core.commands.CommandWithSubCommands; import com.sekwah.advancedportals.core.commands.subcommands.desti.CreateDestiSubCommand; +import com.sekwah.advancedportals.core.commands.subcommands.desti.RemoveDestiSubCommand; import com.sekwah.advancedportals.core.commands.subcommands.portal.*; import com.sekwah.advancedportals.core.connector.commands.CommandRegister; import com.sekwah.advancedportals.core.registry.TagRegistry; @@ -45,6 +46,7 @@ public AdvancedPortalsCore(File dataStorageLoc, InfoLogger infoLogger) { this.dataStorage = new DataStorage(dataStorageLoc); this.infoLogger = infoLogger; this.module = new AdvancedPortalsModule(this); + // Provide any items that need to be provided. //this.module.addInstanceBinding(DataCollector.class, this.infoLogger); @@ -96,7 +98,7 @@ private void registerPortalCommand(CommandRegister commandRegister) { this.portalCommand.registerSubCommand("endportalblock", new EndPortalBlockSubCommand()); this.portalCommand.registerSubCommand("endgatewayblock", new EndGatewayBlockSubCommand()); this.portalCommand.registerSubCommand("create", new CreatePortalSubCommand()); - this.portalCommand.registerSubCommand("remove", new RemoveSubCommand()); + this.portalCommand.registerSubCommand("remove", new RemovePortalSubCommand()); this.portalCommand.registerSubCommand("list", new ListSubCommand()); commandRegister.registerCommand("portal", this.portalCommand); @@ -105,6 +107,7 @@ private void registerPortalCommand(CommandRegister commandRegister) { private void registerDestinationCommand(CommandRegister commandRegister) { this.destiCommand = new CommandWithSubCommands(this); this.destiCommand.registerSubCommand("create", new CreateDestiSubCommand()); + this.destiCommand.registerSubCommand("remove", new RemoveDestiSubCommand()); commandRegister.registerCommand("destination", this.destiCommand); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/CreateDestiSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/CreateDestiSubCommand.java index e45a09f6..2f1da065 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/CreateDestiSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/CreateDestiSubCommand.java @@ -48,30 +48,23 @@ public void onCommand(CommandSenderContainer sender, String[] args) { sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.error.noname")); return; } + sender.sendMessage(Lang.centeredTitle(Lang.translate("command.createdesti.prep"))); + sender.sendMessage(""); + sender.sendMessage(Lang.translate("command.create.tags")); + if(destinationTags.isEmpty()) { + sender.sendMessage(Lang.translate("desti.info.noargs")); + } + else { + this.printTags(sender, destinationTags, Tag.TagType.DESTINATION); + } + sender.sendMessage(""); Destination destination = destinationServices.createDesti(player, player.getLoc(), destinationTags); if(destination != null) { - sender.sendMessage(""); - sender.sendMessage(Lang.translate("command.create.tags")); - - ArrayList destiArgs = destination.getArgs(); - - if(destiArgs.isEmpty()) { - sender.sendMessage(Lang.translate("desti.info.noargs")); - } - else { - for (DataTag tag : destiArgs) { - if(tag.VALUES.length == 1) { - sender.sendMessage(" \u00A7a" + tag.NAME + "\u00A77:\u00A7e" + tag.VALUES[0]); - } else { - sender.sendMessage("\u00A7a" + tag.NAME + "\u00A77:\u00A7e" + tag.VALUES[0]); - } - } - } - sender.sendMessage(""); sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.createdesti.complete")); } else { + sender.sendMessage(""); sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.createdesti.error")); } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/RemoveDestiSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/RemoveDestiSubCommand.java new file mode 100644 index 00000000..905fcecc --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/desti/RemoveDestiSubCommand.java @@ -0,0 +1,63 @@ +package com.sekwah.advancedportals.core.commands.subcommands.desti; + +import com.google.inject.Inject; +import com.sekwah.advancedportals.core.commands.SubCommand; +import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer; +import com.sekwah.advancedportals.core.destination.Destination; +import com.sekwah.advancedportals.core.permissions.PortalPermissions; +import com.sekwah.advancedportals.core.portal.AdvancedPortal; +import com.sekwah.advancedportals.core.services.DestinationServices; +import com.sekwah.advancedportals.core.util.Lang; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class RemoveDestiSubCommand implements SubCommand { + + + @Inject + DestinationServices destinationServices; + + @Override + public void onCommand(CommandSenderContainer sender, String[] args) { + if(args.length > 1) { + if(destinationServices.removeDesti(args[1], sender.getPlayerContainer())) { + sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.portal.remove.complete")); + } + else { + sender.sendMessage(Lang.translate("messageprefix.negative") + + Lang.translate("command.destination.remove.error")); + } + } + else { + sender.sendMessage(Lang.translate("command.portal.remove.noname")); + } + } + + @Override + public boolean hasPermission(CommandSenderContainer sender) { + return sender.isOp() || PortalPermissions.CREATE_PORTAL.hasPermission(sender); + } + + @Override + public List onTabComplete(CommandSenderContainer sender, String[] args) { + if(args.length > 2) { + return Collections.emptyList(); + } + List destiNames = destinationServices.getDestinations(); + Collections.sort(destiNames); + return destiNames; + } + + @Override + public String getBasicHelpText() { + return Lang.translate("command.create.help"); + } + + @Override + public String getDetailedHelpText() { + return Lang.translate("command.create.detailedhelp"); + } +} 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 6600bee5..9caa6e01 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 @@ -45,16 +45,7 @@ public void onCommand(CommandSenderContainer sender, String[] args) { sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.create.complete")); sender.sendMessage(Lang.translate("command.create.tags")); sender.sendMessage("\u00A7a" + "triggerBlock\u00A77:\u00A7e" + Arrays.toString(portal.getTriggerBlocks())); - for (DataTag tag: portal.getArgs()) { - if(tag.VALUES.length == 1) { - sender.sendMessage("\u00A7a" + tag.NAME + "\u00A77:\u00A7e" + tag.VALUES[0]); - } else { - // Output in the format tag.NAME(index): value - for (int i = 0; i < tag.VALUES.length; i++) { - sender.sendMessage("\u00A7a" + tag.NAME + "(" + i + ")\u00A77:\u00A7e" + tag.VALUES[i]); - } - } - } + this.printTags(sender, portal.getArgs(), Tag.TagType.PORTAL); } sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.create.error")); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/RemoveSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/RemovePortalSubCommand.java similarity index 85% rename from core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/RemoveSubCommand.java rename to core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/RemovePortalSubCommand.java index fd9773d2..d5112668 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/RemoveSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/RemovePortalSubCommand.java @@ -1,7 +1,6 @@ package com.sekwah.advancedportals.core.commands.subcommands.portal; import com.google.inject.Inject; -import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.commands.SubCommand; import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; @@ -15,7 +14,7 @@ import java.util.List; import java.util.Map; -public class RemoveSubCommand implements SubCommand { +public class RemovePortalSubCommand implements SubCommand { @Inject @@ -25,17 +24,17 @@ public class RemoveSubCommand implements SubCommand { public void onCommand(CommandSenderContainer sender, String[] args) { if(args.length > 1) { if(portalServices.removePortal(args[1], sender.getPlayerContainer())) { - sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.remove.complete")); + sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.portal.remove.complete")); } else { sender.sendMessage(Lang.translate("messageprefix.negative") - + Lang.translate("command.remove.error")); + + Lang.translate("command.portal.remove.error")); } } else { PlayerContainer player = sender.getPlayerContainer(); if(player == null) { - sender.sendMessage(Lang.translate("command.remove.noname")); + sender.sendMessage(Lang.translate("command.portal.remove.noname")); } else { if(portalServices.removePlayerSelection(player)) { @@ -43,7 +42,7 @@ public void onCommand(CommandSenderContainer sender, String[] args) { } else { sender.sendMessage(Lang.translate("messageprefix.negative") - + Lang.translate("command.remove.error")); + + Lang.translate("command.portal.remove.error")); } } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/reusable/CreateTaggedSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/reusable/CreateTaggedSubCommand.java index 09414484..de802ae6 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/reusable/CreateTaggedSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/reusable/CreateTaggedSubCommand.java @@ -76,10 +76,12 @@ public List onTabComplete(CommandSenderContainer sender, String[] args) if(portalTag.NAME.equals(tag.getName())) { return false; } - // check the tag aliases - for (String alias : tag.getAliases()) { - if(portalTag.NAME.equals(alias)) { - return false; + var aliases = tag.getAliases(); + if(aliases != null) { + for (String alias : aliases) { + if(portalTag.NAME.equals(alias)) { + return false; + } } } } @@ -99,4 +101,17 @@ public List onTabComplete(CommandSenderContainer sender, String[] args) return suggestions; } + + protected void printTags(CommandSenderContainer sender, List dataTags, Tag.TagType tagType) { + for (DataTag tag : dataTags) { + if(tag.VALUES.length == 1) { + sender.sendMessage(" \u00A7a" + tag.NAME + "\u00A77:\u00A7e" + tag.VALUES[0]); + } else { + for (int i = 0; i < tag.VALUES.length; i++) { + sender.sendMessage(" \u00A7a" + tag.NAME + "\u00A77[" + i + "]:\u00A7e" + tag.VALUES[i]); + } + } + } + // TODO add a note saying if tags were ignored due to not being valid for the type + } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java b/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java index 8b44ca4b..8fe8430d 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java @@ -23,6 +23,7 @@ public class AdvancedPortalsModule extends AbstractModule { private Injector injector; private AdvancedPortalsCore advancedPortalsCore; + private DataStorage dataStorage; private List delayedBindings = new ArrayList<>(); diff --git a/core/src/main/java/com/sekwah/advancedportals/core/registry/SubCommandRegistry.java b/core/src/main/java/com/sekwah/advancedportals/core/registry/SubCommandRegistry.java index 9fb1d23f..6639ede1 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/registry/SubCommandRegistry.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/registry/SubCommandRegistry.java @@ -1,7 +1,6 @@ package com.sekwah.advancedportals.core.registry; import com.google.inject.Inject; -import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.commands.SubCommand; import com.sekwah.advancedportals.core.util.InfoLogger; @@ -37,12 +36,12 @@ public class SubCommandRegistry { public boolean registerSubCommand(String arg, SubCommand subCommand) { if (subCommand == null) { - this.infoLogger.logWarning("The subcommand '" + arg + "' cannot be null."); + this.infoLogger.warning("The subcommand '" + arg + "' cannot be null."); return false; } if(this.subCommandMap.containsKey(arg)){ - this.infoLogger.logWarning("The subcommand '" + arg + "' already exists."); + this.infoLogger.warning("The subcommand '" + arg + "' already exists."); return false; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java b/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java index 30eecc9f..bfa4d373 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/registry/TagRegistry.java @@ -65,7 +65,7 @@ public boolean registerTag(Tag tag) { // Check literal tags for clashes if(this.literalTags.contains(tagName)) { - this.portalsCore.getInfoLogger().logWarning("A tag with the name " + tagName + " already exists."); + this.portalsCore.getInfoLogger().warning("A tag with the name " + tagName + " already exists."); return false; } @@ -74,7 +74,7 @@ public boolean registerTag(Tag tag) { if(aliases != null) { for (String alias : aliases) { if(this.literalTags.contains(alias)) { - this.portalsCore.getInfoLogger().logWarning("A tag with the alias " + alias + " already exists."); + this.portalsCore.getInfoLogger().warning("A tag with the alias " + alias + " already exists."); return false; } } @@ -83,7 +83,7 @@ public boolean registerTag(Tag tag) { } if (tagName == null) { - this.portalsCore.getInfoLogger().logWarning("A tag cannot be null."); + this.portalsCore.getInfoLogger().warning("A tag cannot be null."); return false; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/registry/WarpEffectRegistry.java b/core/src/main/java/com/sekwah/advancedportals/core/registry/WarpEffectRegistry.java index 381edc1d..7a7d3090 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/registry/WarpEffectRegistry.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/registry/WarpEffectRegistry.java @@ -44,7 +44,7 @@ public boolean registerEffect(String name, WarpEffect effect, WarpEffect.Type ty list = this.visualEffects; break; default: - this.portalsCore.getInfoLogger().logWarning(type.toString() + this.portalsCore.getInfoLogger().warning(type.toString() + " effect type not recognised"); return false; } @@ -65,7 +65,7 @@ public WarpEffect getEffect(String name, WarpEffect.Type type){ list = this.visualEffects; break; default: - this.infoLogger.logWarning(type.toString() + this.infoLogger.warning(type.toString() + " effect type not recognised"); return null; } @@ -73,7 +73,7 @@ public WarpEffect getEffect(String name, WarpEffect.Type type){ return list.get(name); } else{ - this.infoLogger.logWarning("No effect of type:" + this.infoLogger.warning("No effect of type:" + type.toString() + " was registered with the name: " + name); return null; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/IJsonRepository.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/IJsonRepository.java index e638de71..812ce220 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/IJsonRepository.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/IJsonRepository.java @@ -3,8 +3,10 @@ import com.google.common.collect.ImmutableMap; import com.google.gson.Gson; +import java.util.List; + public interface IJsonRepository { - Gson gson = new Gson(); + boolean save(String name, T t); boolean containsKey(String name); @@ -15,5 +17,5 @@ public interface IJsonRepository { T get(String name); - ImmutableMap getAll(); + List listAll(); } 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 2a2e5b36..c6d83252 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,26 +1,31 @@ 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.util.InfoLogger; import javax.inject.Singleton; -import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; +import java.util.List; @Singleton public class DestinationRepositoryImpl implements IDestinationRepository { - private final String fileLocation = ""; + private final String fileLocation = "desti/"; + @Inject + DataStorage dataStorage; - private Map destinationCache = new HashMap(); + @Inject + InfoLogger infoLogger; - public void addDestination(String name, Destination destination) throws IOException { - gson.toJson(destination, new FileWriter(fileLocation + name + ".json")); + public void addDestination(String name, Destination destination) { + infoLogger.log("Adding destination: " + fileLocation + name + ".json"); + dataStorage.storeJson(destination, fileLocation + name + ".json"); } @Override @@ -29,17 +34,12 @@ public boolean save(String name, Destination destination) { } public boolean containsKey(String name) { - return Files.exists(Paths.get(fileLocation + "\\" + name + ".json")); + return dataStorage.fileExists(fileLocation + name + ".json"); } @Override public boolean delete(String name) { - try { - Files.deleteIfExists(Paths.get(fileLocation + "\\" + name + ".json")); - } catch (IOException e) { - e.printStackTrace(); - } - return false; + return dataStorage.deleteFile(fileLocation + name + ".json"); } @Override @@ -47,11 +47,12 @@ public boolean update(String name, Destination destination) { return false; } - public Destination get(String s) { - return null; + public Destination get(String desti) { + return dataStorage.loadJson(Destination.class, fileLocation + desti + ".json"); } - public ImmutableMap getAll() { - return null; + @Override + public List listAll() { + return dataStorage.listAllFiles(fileLocation, true); } } 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 1a02f5ae..1e5b7f9b 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 @@ -2,14 +2,23 @@ import com.google.common.collect.ImmutableMap; import com.google.inject.Singleton; +import com.sekwah.advancedportals.core.destination.Destination; +import com.sekwah.advancedportals.core.portal.AdvancedPortal; import com.sekwah.advancedportals.core.serializeddata.WorldLocation; import com.sekwah.advancedportals.core.repository.IPortalRepository; -import java.util.UUID; +import java.util.*; @Singleton public class PortalRepositoryImpl implements IPortalRepository { + /** + * In memory copy of the portal files as they will be accessed every movement tick. + * + * If we need to get it by name we can just load it from the file, but this is good for looping fast for the player move events. + */ + private List portals = new ArrayList<>(); + public String getSelectedPortal(UUID uuid) { return null; } @@ -40,7 +49,7 @@ public WorldLocation get(String name) { } @Override - public ImmutableMap getAll() { + public List listAll() { return null; } } 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 1d221283..271ded4d 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 @@ -9,6 +9,10 @@ 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; +import java.util.List; public class DataStorage { @@ -65,15 +69,24 @@ public T loadJson(Class dataHolder, String location) { return gson.fromJson(bufReader, dataHolder); } - public void storeJson(Object dataHolder, String location) { + public boolean storeJson(Object dataHolder, String location) { + // Create folders if they don't exist + File outFile = new File(this.dataFolder, location); + if (!outFile.exists()) { + if(!outFile.getParentFile().mkdirs()) { + infoLogger.warning("Failed to create folder for file: " + location); + } + } String json = gson.toJson(dataHolder); try { FileWriter fileWriter = new FileWriter(new File(this.dataFolder, location)); fileWriter.write(json); fileWriter.close(); + return true; } catch (IOException e) { - e.printStackTrace(); + infoLogger.error(e); } + return false; } /** @@ -108,15 +121,15 @@ public boolean copyDefaultFile(String sourceLoc, String fileLoc, boolean overwri writeToFile(inputStream, outFile); } catch (NullPointerException e) { e.printStackTrace(); - this.infoLogger.logWarning("Could not load " + sourceLoc + ". The file does" + + this.infoLogger.warning("Could not load " + sourceLoc + ". The file does" + "not exist or there has been an error reading the file."); return false; } catch (FileNotFoundException e) { e.printStackTrace(); - this.infoLogger.logWarning("Could not create " + sourceLoc); + this.infoLogger.warning("Could not create " + sourceLoc); } catch (IOException e) { e.printStackTrace(); - this.infoLogger.logWarning("File error reading " + sourceLoc); + this.infoLogger.warning("File error reading " + sourceLoc); } } return true; @@ -143,7 +156,7 @@ public InputStream loadResource(String location) { return this.getClass().getClassLoader().getResourceAsStream(location); } catch (NullPointerException e) { e.printStackTrace(); - this.infoLogger.logWarning("Could not load " + location + ". The file does" + + this.infoLogger.warning("Could not load " + location + ". The file does" + "not exist or there has been an error reading the file."); return null; } @@ -169,4 +182,46 @@ private void writeToFile(InputStream inputStream, File outFile) throws IOExcepti inputStream.close(); outStream.close(); } + + public boolean fileExists(String name) { + return new File(this.dataFolder, name).exists(); + } + + /** + * @param fileLocation + * @param trimExtension + * @return + */ + public List listAllFiles(String fileLocation, boolean trimExtension) { + File directory = new File(dataFolder, fileLocation); + var 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); + } + } + + list.add(fileName); + } + } + } + } else { + infoLogger.warning("Directory does not exist or is not a directory: " + fileLocation); + } + return list; + } + + public boolean deleteFile(String fileLocation) { + return new File(dataFolder, fileLocation).delete(); + } } 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 b1cdb306..29cdf590 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 @@ -4,7 +4,7 @@ public class PlayerLocation extends WorldLocation { - @SerializedName("yaw") + @SerializedName("r") private final float yaw; @SerializedName("p") diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java b/core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java index b8d986d8..f4c4e4a8 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/DestinationServices.java @@ -1,10 +1,14 @@ package com.sekwah.advancedportals.core.services; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.gson.reflect.TypeToken; import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; +import com.sekwah.advancedportals.core.portal.AdvancedPortal; +import com.sekwah.advancedportals.core.serializeddata.DataStorage; import com.sekwah.advancedportals.core.serializeddata.DataTag; import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; import com.sekwah.advancedportals.core.destination.Destination; @@ -16,6 +20,8 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * Handles logic for all destination, this is a transient layer so it should @@ -47,20 +53,13 @@ public Boolean delete(String name) { return false; } - public ImmutableMap getDestination() { - return destinationRepository.getAll(); + public List getDestinations() { + return destinationRepository.listAll(); } - public ImmutableMap getDestinations() { - return ImmutableMap.copyOf(destinationRepository.getAll()); - } - - - - public Destination createDesti(PlayerContainer player, PlayerLocation playerLocation, ArrayList tags) { // Find the tag with the "name" NAME - DataTag nameTag = tags.stream().findFirst().filter(tag -> tag.NAME.equals("name")).orElse(null); + DataTag nameTag = tags.stream().filter(tag -> tag.NAME.equals("name")).findFirst().orElse(null); String name = nameTag == null ? null : nameTag.VALUES[0]; @@ -115,4 +114,12 @@ public void saveDestinations() { } this.portalsCore.getDataStorage().storeJson(this.destiHashMap, "destinations.json");*/ } + + public boolean removeDesti(String name, PlayerContainer playerContainer) { + if(this.destinationRepository.containsKey(name)) { + this.destinationRepository.delete(name); + return true; + } + return false; + } } 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 80cf6147..890a7297 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 @@ -42,7 +42,7 @@ public boolean removePortal(String name, PlayerContainer player) { public AdvancedPortal createPortal(String name, PlayerContainer player, ArrayList portalTags) { if(name == null){ - infoLogger.logWarning("Attempted to make a portal with no name"); + infoLogger.warning("Attempted to make a portal with no name"); player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.error.noname")); return null; } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java index 4b0548ca..c21225b4 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java @@ -8,6 +8,13 @@ import java.util.List; +/** + * The name of the destination or portal. + *

+ * Most of the implementation of this tag is external, this is just to allow for the tag to be used. + *

+ * Most tags shouldn't be like this unless they are to be paired with another tag. + */ public class NameTag implements Tag.Activation, Tag.AutoComplete { private final TagType[] tagTypes = new TagType[]{ TagType.PORTAL, TagType.DESTINATION }; 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 9740d7c8..9ec156c7 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 @@ -1,16 +1,19 @@ package com.sekwah.advancedportals.core.util; + public abstract class InfoLogger { /** * Problematic messages * @param s warning message */ - public abstract void logWarning(String s); + public abstract void warning(String s); /** * General information logging * @param s info message */ public abstract void log(String s); + + public abstract void error(Exception e); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/util/Lang.java b/core/src/main/java/com/sekwah/advancedportals/core/util/Lang.java index 80f349aa..e985bc43 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/util/Lang.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/util/Lang.java @@ -113,11 +113,11 @@ private void injectTranslations(String fileName) { Map initialMap = Lang.parseLang(url.openStream()); Lang.instance.languageMap.putAll(initialMap); } else { - this.infoLogger.logWarning("Could not load " + fileName + ".lang from within Advanced Portals as it doesn't exist."); + this.infoLogger.warning("Could not load " + fileName + ".lang from within Advanced Portals as it doesn't exist."); } } catch (IOException e) { e.printStackTrace(); - this.infoLogger.logWarning("Could not load " + fileName + ".lang from within Advanced Portals."); + this.infoLogger.warning("Could not load " + fileName + ".lang from within Advanced Portals."); } Map newLangMap = this.getLanguageMap(fileName ); diff --git a/lang/src/main/resources/lang/en_GB.lang b/lang/src/main/resources/lang/en_GB.lang index 0e56f3d2..bbf65b60 100644 --- a/lang/src/main/resources/lang/en_GB.lang +++ b/lang/src/main/resources/lang/en_GB.lang @@ -71,16 +71,18 @@ value needs spaces use the format tag:"value with spaces" command.createdesti.complete= The destination has been successfully created. -command.create.tags=&aTags&e: +command.create.tags=&aTags&7: command.playeronly= Sorry but that command can only be run by a player. -command.remove.noname= You need to give the name of the portal you want to remove. -command.remove.error= There was a problem removing the portal: -command.remove.noname=No portal by that name was found -command.remove.invalidselection=The portal selection you had is no longer valid -command.remove.noselection=You don't have a portal selected -command.remove.complete= The portal has been successfully removed. +command.portal.remove.noname= You need to give the name of the portal you want to remove. +command.portal.remove.error= There was a problem removing the portal. +command.portal.remove.noname=No portal by that name was found +command.portal.remove.invalidselection=The portal selection you had is no longer valid +command.portal.remove.noselection=You don't have a portal selected +command.portal.remove.complete= The portal has been successfully removed. + +command.destination.remove.error= There was a problem removing the destination. command.list.help=Lists portals command.list=&7 Portals&e: @@ -106,6 +108,7 @@ command.error.notags= No tags have been given. You need to include at least &ena command.error.nametaken= The name &e%1$s &cis already taken. desti.error.save= There was a problem saving the destination: +desti.error.noname= You must specify a name. (name:someNameHere) error.notplayer=Only players can do that. 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 7172077f..be2ea072 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/SpigotInfoLogger.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/SpigotInfoLogger.java @@ -2,6 +2,8 @@ import com.sekwah.advancedportals.core.util.InfoLogger; +import java.util.logging.Level; + public class SpigotInfoLogger extends InfoLogger { private final AdvancedPortalsPlugin plugin; @@ -11,7 +13,7 @@ public SpigotInfoLogger(AdvancedPortalsPlugin plugin) { } @Override - public void logWarning(String s) { + public void warning(String s) { plugin.getLogger().warning(s); } @@ -19,4 +21,11 @@ public void logWarning(String s) { public void log(String s) { plugin.getLogger().info(s); } + + @Override + public void error(Exception e) { + plugin.getLogger().log(Level.SEVERE, e.getMessage(), e); + } + + } diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotPlayerContainer.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotPlayerContainer.java index 7b6de06d..037ad497 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotPlayerContainer.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotPlayerContainer.java @@ -44,7 +44,7 @@ public boolean isOp() { public PlayerLocation getLoc() { Location loc = this.player.getLocation(); - return new PlayerLocation(loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ()); + return new PlayerLocation(loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); } public double getEyeHeight() {