diff --git a/src/main/java/de/butzlabben/world/WorldSystem.java b/src/main/java/de/butzlabben/world/WorldSystem.java index 67fdec6..1bac44d 100644 --- a/src/main/java/de/butzlabben/world/WorldSystem.java +++ b/src/main/java/de/butzlabben/world/WorldSystem.java @@ -4,32 +4,37 @@ import de.butzlabben.world.commands.WorldSystemCommandExecuter; import de.butzlabben.world.commands.commands.DebugCommandTest; import de.butzlabben.world.commands.commands.WorldSystemTabComplete; import de.butzlabben.world.config.PluginConfig; -import org.bukkit.plugin.PluginDescriptionFile; +import de.butzlabben.world.utils.PluginRunner; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPluginLoader; import java.io.File; import java.io.FileNotFoundException; +import java.io.InputStream; -public class WorldSystem extends JavaPlugin +public class WorldSystem { + private PluginRunner runner; + protected static WorldSystem activeInst = null; private static PluginConfig cfg; - public WorldSystem() - { - super(); + + private WorldSystem(PluginRunner run) { + runner = run; } - protected WorldSystem(JavaPluginLoader loader, PluginDescriptionFile description, File dataFolder, File file) - { - super(loader, description, dataFolder, file); + protected WorldSystem() { + runner = null; + } + + public void saveDefaultConfig() + { + runner.saveDefaultConfig(); } - @Override public void onEnable() { createConfigs(); - createCommands(); } @@ -42,12 +47,12 @@ public class WorldSystem extends JavaPlugin cmdExecuter.addCommand("test", new DebugCommandTest()); } - this.getCommand("ws").setExecutor(cmdExecuter); - this.getCommand("ws").setTabCompleter(new WorldSystemTabComplete(cmdExecuter)); + runner.getCommand("ws").setExecutor(cmdExecuter); + runner.getCommand("ws").setTabCompleter(new WorldSystemTabComplete(cmdExecuter)); } private void createConfigs() { - File folder = getInstance().getDataFolder(); + File folder = runner.getInstance().getDataFolder(); try { cfg = new PluginConfig(new File(folder, "config.yml")); @@ -63,7 +68,14 @@ public class WorldSystem extends JavaPlugin } public static WorldSystem getInstance() { - return JavaPlugin.getPlugin(WorldSystem.class); + if (activeInst == null) { + activeInst = new WorldSystem(JavaPlugin.getPlugin(PluginRunner.class)); + } + return activeInst; + } + + public InputStream getResource(String filename) { + return runner.getResource(filename); } } diff --git a/src/main/java/de/butzlabben/world/commands/commands/BaseCommand.java b/src/main/java/de/butzlabben/world/commands/commands/BaseCommand.java index 1abc1c3..0a5288c 100644 --- a/src/main/java/de/butzlabben/world/commands/commands/BaseCommand.java +++ b/src/main/java/de/butzlabben/world/commands/commands/BaseCommand.java @@ -1,2 +1,33 @@ -package de.butzlabben.world.commands.commands;public class BaseCommand { +package de.butzlabben.world.commands.commands; + +import de.butzlabben.world.WorldSystem; +import de.butzlabben.world.commands.WorldSystemCommand; +import org.bukkit.command.CommandSender; + +import java.util.List; + +public class BaseCommand implements WorldSystemCommand +{ + @Override + public boolean run(CommandSender sender) + { + return false; + +// String prefix = WorldSystem.getWSConfig().getPrefix(); +// sender.sendMessage( +// prefix + "WorldSystem by Butzlabben v" + WorldSystem.getInstance().getDescription().getVersion()); +// sender.sendMessage(prefix + "Contributors: Jubeki, montlikadani, jstoeckm2"); +// List cmdHelp = MessageConfig.getCommandHelp(); +// cmdHelp.forEach(s -> sender.sendMessage(prefix + s)); +// if (sender.hasPermission("ws.delete")) +// { +// sender.sendMessage(MessageConfig.getDeleteCommandHelp()); +// } + } + + @Override + public String[] arguments() + { + return new String[0]; + } } diff --git a/src/main/java/de/butzlabben/world/config/LanguageConfig.java b/src/main/java/de/butzlabben/world/config/LanguageConfig.java index 5452ea9..d8e4569 100644 --- a/src/main/java/de/butzlabben/world/config/LanguageConfig.java +++ b/src/main/java/de/butzlabben/world/config/LanguageConfig.java @@ -1,11 +1,21 @@ package de.butzlabben.world.config; -import java.io.File; +import com.fastasyncworldedit.core.configuration.file.YamlConfiguration; +import de.butzlabben.world.WorldSystem; +import org.bukkit.ChatColor; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; -public class MessageConfig +public class LanguageConfig { + + //TODO NEXT Logger private static final List defaultCmdHelp = new ArrayList<>(20); private static File languageFile; @@ -25,4 +35,256 @@ public class MessageConfig defaultCmdHelp.add("/ws reset §8- §7Will reset your World"); } + //limit Constructor Access + private LanguageConfig() { + } + + public static void checkConfig(File f) { + languageFile = f; + if (!languageFile.exists()) { + try { + String[] fileName = f.getName().split("/"); + InputStream in = WorldSystem.getInstance().getResource("languages/" + fileName[fileName.length - 1]); + + if (in == null) { + in = WorldSystem.getInstance().getResource("custom.yml"); + } + Files.copy(in, languageFile.toPath()); + } catch (IOException e) { + System.err.println("Wasn't able to create Message file"); + e.printStackTrace(); //TODO Replace with Logger + } + } + } + + private static YamlConfiguration getConfig() { + try { + return YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(languageFile), StandardCharsets.UTF_8)); + } catch (FileNotFoundException e) { + e.printStackTrace();//TODO Replace with Logger + } + return null; + } + + private static String getRawMessage(String path, String alt) { + return ChatColor.translateAlternateColorCodes('&', getConfig().getString(path, alt)); + } + + private static String getMessage(String path, String alt) { + return WorldSystem.getWSConfig().getPrefix() + getRawMessage(path, alt); + } + + public static String getNoPermission() { + return getMessage("nopermission", "§cYou don't have permissions!"); + } + + public static String getSettingUpWorld() { + return getMessage("world.setting_up", "§aSetting up world..."); + } + + public static String getPlayerList() { + return getMessage("world.playerlist", "Player in this world: %player"); + } + + public static String getLagDetection() { + return getMessage("lagdetection", "Lagdetection in world from: §c%world"); + } + + public static String getWrongUsage() { + return getMessage("wrong_usage", "§c%usage"); + } + + public static String getNoWorldOwn() { + return getMessage("world.does_not_exists.own", "§cYou don't have a world!"); + } + + public static String getNoWorldOther() { + return getMessage("world.does_not_exists.other", "§cThis player doesn't has a world!"); + } + + public static String getNotRegistered() { + return getMessage("not_registered", "§cThis player hasn't joined yet!"); + } + + public static String getAlreadyMember() { + return getMessage("member.already_added", "§cThis player is already a member!"); + } + + public static String getMemberAdded() { + return getMessage("member.added", "You have added &c%player&6 to your World!"); + } + + public static String getUnknownError() { + return getMessage("unknown_error", "§cSomething went wrong..."); + } + + public static String getDeleteWorldOwn() { + return getMessage("world.delete.own", "§cYour world was deleted!"); + } + + public static String getDeleteWorldOther() { + return getMessage("world.delete.other", "You deleted the world of §c%player§6!"); + } + + public static String getNoMemberOwn() { + return getMessage("member.not_added.own", "§cThis player isn't a member!"); + } + + public static String getMemberRemoved() { + return getMessage("member.removed", "You removed §c%player§6 from your world!"); + } + + public static String getNoMemberAdded() { + return getMessage("member.no_one_added", "§cThere are no members added"); + } + + public static String getWorldAlreadyExists() { + return getMessage("world.already_exists", "§cYou already have a world!"); + } + + public static String getWorldCreated() { + return getMessage("world.created", "Your world is now ready. Get there with §a/ws home"); + } + + public static String getWorldStillCreating() { + return getMessage("world.still_creating", "§cWorld is still creating"); + } + + public static String getNotOnWorld() { + return getMessage("world.not_on", "§cYou are not on a world!"); + } + + public static String getWorldStillLoaded() { + return getMessage("world.still_loaded", "§cYour world is still loaded!"); + } + + public static String getNoRequestSend() { + return getMessage("request.not_sent", "§cYou didn't send a request!"); + } + + public static String getWorldReseted() { + return getMessage("world.reseted", "Your world was reseted!"); + } + + public static String getInvalidInput() { + return getMessage("request.invalid_input", "§c%input is not a valid input!"); + } + + public static String getRequestAlreadySent() { + return getMessage("request.already_sent", "§cYou already sent a request!"); + } + + public static String getRequestExpired() { + return getMessage("request.expired", "§cYou request is expired!"); + } + + public static String getTimeUntilExpires() { + return getMessage("request.until_expire", "§cYour request expires in %time seconds!"); + } + + public static String getConfirmRequest() { + return getMessage("request.confirm", "§cPlease confirm reset of your world: %command"); + } + + public static String getNoMemberOther() { + return getMessage("member.not_added.other", "§cYou are not added to this world!"); + } + + public static String getInfoOwner() { + return getMessage("info.owner", "Owner: %data"); + } + + public static String getInfoId() { + return getMessage("info.id", "ID: %data"); + } + + public static String getInfoMember() { + return getMessage("info.member", "Member: %data"); + } + + public static String getInfoTnt() { + return getMessage("info.tnt", "TNT: %data"); + } + + public static String getInfoFire() { + return getMessage("info.fire", "Fire: %data"); + } + + public static String getInfoEnabled() { + return getRawMessage("info.enabled", "§aOn"); + } + + public static String getInfoDisabled() { + return getRawMessage("info.disabled", "§cOff"); + } + + public static String getToggleGameModeEnabled() { + return getMessage("toggle.gamemode.enabled", "§a%player§6 can now change his gamemode!"); + } + + public static String getToggleGameModeDisabled() { + return getMessage("toggle.gamemode.disabled", "§c%player§6 can no longer change his gamemode!"); + } + + public static String getToggleTeleportEnabled() { + return getMessage("toggle.teleport.enabled", "§a%player§6 can now teleport!"); + } + + public static String getToggleTeleportDisabled() { + return getMessage("toggle.teleport.disabled", "§c%player§6 can no longer teleport!"); + } + + public static String getToggleBuildEnabled() { + return getMessage("toggle.build.enabled", "§a%player§6 can now build!"); + } + + public static String getToggleBuildDisabled() { + return getMessage("toggle.build.disabled", "§c%player§6 can no longer build!"); + } + + public static String getToggleWorldeditEnabled() { + return getMessage("toggle.worldedit.enabled", "§a%player§6 can now use WorldEdit!"); + } + + public static String getToggleWorldeditDisabled() { + return getMessage("toggle.worldedit.disabled", "§c%player§6 can no longer use WorldEdit!"); + } + + + public static String getToggleFireEnabled() { + return getMessage("toggle.fire.enabled", "§aYou activated fire!"); + } + + public static String getToggleFireDisabled() { + return getMessage("toggle.fire.disabled", "§cYou deactivated fire!"); + } + + public static String getToggleTntEnabled() { + return getMessage("toggle.tnt.enabled", "§aYou activated TNT-Damage!"); + } + + public static String getToggleTntDisabled() { + return getMessage("toggle.tnt.disabled", "§cYou deactivated TNT-Damage!"); + } + + public static String getDeleteCommandHelp() { + return getMessage("command_help.delete_command", "/ws delete §8- §7Will delete a World"); + } + + public static List getCommandHelp() { + List list = getConfig().getStringList("command_help.list"); + if (list == null) + list = defaultCmdHelp; + list = list.stream().map(s -> ChatColor.translateAlternateColorCodes('&', s)).collect(Collectors.toList()); + return list; + } + + public static String getHomeSet() { + return getMessage("world.set_home", "You set the home"); + } + + public static String getNotEnoughMoney() { + return getMessage("not_enough_money", "You do not have enough money"); + } + } diff --git a/src/main/java/de/butzlabben/world/config/PluginConfig.java b/src/main/java/de/butzlabben/world/config/PluginConfig.java index a739fc5..029692c 100644 --- a/src/main/java/de/butzlabben/world/config/PluginConfig.java +++ b/src/main/java/de/butzlabben/world/config/PluginConfig.java @@ -29,7 +29,7 @@ public class PluginConfig { public PluginConfig(File configFile) throws FileNotFoundException { this.configFile = configFile; if (!configFile.exists()) { - JavaPlugin.getPlugin(WorldSystem.class).saveDefaultConfig(); + WorldSystem.getInstance().saveDefaultConfig(); } try { diff --git a/src/main/java/de/butzlabben/world/utils/PluginRunner.java b/src/main/java/de/butzlabben/world/utils/PluginRunner.java index f96416b..8422324 100644 --- a/src/main/java/de/butzlabben/world/utils/PluginRunner.java +++ b/src/main/java/de/butzlabben/world/utils/PluginRunner.java @@ -1,29 +1,22 @@ -package de.butzlabben.world; +package de.butzlabben.world.utils; -import de.butzlabben.world.commands.WorldSystemCommandExecuter; -import de.butzlabben.world.commands.commands.DebugCommandTest; -import de.butzlabben.world.commands.commands.WorldSystemTabComplete; -import de.butzlabben.world.config.PluginConfig; -import org.bukkit.World; -import org.bukkit.plugin.PluginDescriptionFile; +import de.butzlabben.world.WorldSystem; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.plugin.java.JavaPluginLoader; -import java.io.File; -import java.io.FileNotFoundException; - -public class main extends JavaPlugin +public class PluginRunner extends JavaPlugin { private WorldSystem plugin; + @Override public void onEnable() { - plugin = new WorldSystem(); + plugin = WorldSystem.getInstance(); plugin.onEnable(); } - public static main getInstance() { - return JavaPlugin.getPlugin(main.class); + public static PluginRunner getInstance() { + return JavaPlugin.getPlugin(PluginRunner.class); } + } diff --git a/src/test/java/de/butzlabben/world/MockWorldSystem.java b/src/test/java/de/butzlabben/world/MockWorldSystem.java index c07272b..5249e8c 100644 --- a/src/test/java/de/butzlabben/world/MockWorldSystem.java +++ b/src/test/java/de/butzlabben/world/MockWorldSystem.java @@ -1,2 +1,42 @@ -package de.butzlabben.world;public class MockWorldSystem { +package de.butzlabben.world; + +import org.apache.commons.io.FileUtils; + +import java.io.*; + +public class MockWorldSystem extends WorldSystem +{ + private File configFile; + + public MockWorldSystem(File cfg) { + activeInst = this; + configFile = cfg; + } + + public MockWorldSystem() { + activeInst = this; + configFile = null; + } + + @Override + public void saveDefaultConfig() { + File source = new File("src/main/resources/config.yml"); + try { + FileUtils.copyFile(source, configFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public InputStream getResource(String filename) { + try + { + return new FileInputStream("src/main/resources/" + filename); + } + catch (FileNotFoundException e) + { + return null; + } + } } diff --git a/src/test/java/de/butzlabben/world/TestWorldSystem.java b/src/test/java/de/butzlabben/world/TestWorldSystem.java index f19dece..bb309b0 100644 --- a/src/test/java/de/butzlabben/world/TestWorldSystem.java +++ b/src/test/java/de/butzlabben/world/TestWorldSystem.java @@ -1,2 +1,15 @@ -package de.butzlabben.world;public class TestWorldSystem { +package de.butzlabben.world; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestWorldSystem +{ + @Test + public void testMockInstance() { + WorldSystem mock = new MockWorldSystem(null); + assertEquals(mock, WorldSystem.getInstance()); + } + } diff --git a/src/test/java/de/butzlabben/world/config/TestLanguageConfig.java b/src/test/java/de/butzlabben/world/config/TestLanguageConfig.java index c9407ab..7513806 100644 --- a/src/test/java/de/butzlabben/world/config/TestLanguageConfig.java +++ b/src/test/java/de/butzlabben/world/config/TestLanguageConfig.java @@ -1,2 +1,68 @@ -package de.butzlabben.world.config;public class TestLanguageConfig { +package de.butzlabben.world.config; + +import de.butzlabben.world.MockWorldSystem; +import de.butzlabben.world.WorldSystem; +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestLanguageConfig +{ + @BeforeAll + static void CleanLastTest() throws IOException + { + FileUtils.cleanDirectory(new File("TestFiles/workingDir/")); + } + + @Test + public void testInitEN() { + File languages = new File("TestFiles/en.yml"); + + //Fails if something went Wrong + LanguageConfig.checkConfig(languages); + } + + @Test + public void testInitES() { + File languages = new File("TestFiles/es.yml"); + + //Fails if something went Wrong + LanguageConfig.checkConfig(languages); + } + + @Test + public void testInitENnoExist() { + WorldSystem mock = new MockWorldSystem(); + File languages = new File("TestFiles/workingDir/en.yml"); + + //Fails if something went Wrong + LanguageConfig.checkConfig(languages); + } + + @Test + public void testHelpEN() { + File languages = new File("TestFiles/en.yml"); + + //Fails if something went Wrong + LanguageConfig.checkConfig(languages); + + assertEquals(16,LanguageConfig.getCommandHelp().size()); + + assertEquals( "/ws get §8- §7Will give you a world",LanguageConfig.getCommandHelp().get(0)); + } + +// @Test +// public void testNoPermEN() { +// File languages = new File("TestFiles/en.yml"); +// +// //Fails if something went Wrong +// LanguageConfig.checkConfig(languages); +// +// assertEquals( "§cYou do not have permission to that command!", LanguageConfig.getNoPermission()); +// } } diff --git a/src/test/java/de/butzlabben/world/config/TestPluginConfig.java b/src/test/java/de/butzlabben/world/config/TestPluginConfig.java index df40d2a..e4e6b7b 100644 --- a/src/test/java/de/butzlabben/world/config/TestPluginConfig.java +++ b/src/test/java/de/butzlabben/world/config/TestPluginConfig.java @@ -1,19 +1,30 @@ package de.butzlabben.world.config; import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.MockPlugin; import be.seeseemelk.mockbukkit.ServerMock; +import de.butzlabben.world.MockWorldSystem; import de.butzlabben.world.WorldSystem; +import org.apache.commons.io.FileUtils; import org.bukkit.Difficulty; import org.bukkit.GameMode; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import static org.junit.jupiter.api.Assertions.*; public class TestPluginConfig { + @BeforeAll + static void CleanLastTest() throws IOException + { + FileUtils.cleanDirectory(new File("TestFiles/workingDir/")); + } @Test public void testPluginConfigInit() throws FileNotFoundException { @@ -21,6 +32,15 @@ public class TestPluginConfig { PluginConfig cfg = new PluginConfig(cfgFile); } + @Test + public void testNoPluginConfigInit() throws FileNotFoundException { + File cfgFile = new File("TestFiles/workingDir/TestConfigNoExist.yml"); + WorldSystem mock = new MockWorldSystem(cfgFile); + + PluginConfig cfg = new PluginConfig(cfgFile); + } + + //TODO ADD Invalid Config Test diff --git a/target/classes/de/butzlabben/world/WorldSystem.class b/target/classes/de/butzlabben/world/WorldSystem.class index 4d179c6..ea7a1e6 100644 Binary files a/target/classes/de/butzlabben/world/WorldSystem.class and b/target/classes/de/butzlabben/world/WorldSystem.class differ diff --git a/target/classes/de/butzlabben/world/commands/WorldSystemCommandExecuter.class b/target/classes/de/butzlabben/world/commands/WorldSystemCommandExecuter.class index 42766f4..f6c1dbc 100644 Binary files a/target/classes/de/butzlabben/world/commands/WorldSystemCommandExecuter.class and b/target/classes/de/butzlabben/world/commands/WorldSystemCommandExecuter.class differ diff --git a/target/classes/de/butzlabben/world/commands/commands/BaseCommand.class b/target/classes/de/butzlabben/world/commands/commands/BaseCommand.class new file mode 100644 index 0000000..dd9fcb0 Binary files /dev/null and b/target/classes/de/butzlabben/world/commands/commands/BaseCommand.class differ diff --git a/target/classes/de/butzlabben/world/commands/commands/WorldSystemTabComplete.class b/target/classes/de/butzlabben/world/commands/commands/WorldSystemTabComplete.class index cf0fb71..33b638b 100644 Binary files a/target/classes/de/butzlabben/world/commands/commands/WorldSystemTabComplete.class and b/target/classes/de/butzlabben/world/commands/commands/WorldSystemTabComplete.class differ diff --git a/target/classes/de/butzlabben/world/config/LanguageConfig.class b/target/classes/de/butzlabben/world/config/LanguageConfig.class new file mode 100644 index 0000000..2378177 Binary files /dev/null and b/target/classes/de/butzlabben/world/config/LanguageConfig.class differ diff --git a/target/classes/de/butzlabben/world/config/PluginConfig.class b/target/classes/de/butzlabben/world/config/PluginConfig.class index 1389a7c..9629852 100644 Binary files a/target/classes/de/butzlabben/world/config/PluginConfig.class and b/target/classes/de/butzlabben/world/config/PluginConfig.class differ diff --git a/target/classes/de/butzlabben/world/utils/PluginRunner.class b/target/classes/de/butzlabben/world/utils/PluginRunner.class new file mode 100644 index 0000000..9c0496f Binary files /dev/null and b/target/classes/de/butzlabben/world/utils/PluginRunner.class differ diff --git a/target/test-classes/de/butzlabben/world/MockWorldSystem.class b/target/test-classes/de/butzlabben/world/MockWorldSystem.class new file mode 100644 index 0000000..ac978b7 Binary files /dev/null and b/target/test-classes/de/butzlabben/world/MockWorldSystem.class differ diff --git a/target/test-classes/de/butzlabben/world/TestWorldSystem.class b/target/test-classes/de/butzlabben/world/TestWorldSystem.class new file mode 100644 index 0000000..3d12a2e Binary files /dev/null and b/target/test-classes/de/butzlabben/world/TestWorldSystem.class differ diff --git a/target/test-classes/de/butzlabben/world/config/TestLanguageConfig.class b/target/test-classes/de/butzlabben/world/config/TestLanguageConfig.class new file mode 100644 index 0000000..d97315f Binary files /dev/null and b/target/test-classes/de/butzlabben/world/config/TestLanguageConfig.class differ diff --git a/target/test-classes/de/butzlabben/world/config/TestPluginConfig.class b/target/test-classes/de/butzlabben/world/config/TestPluginConfig.class index 0a9cab4..ec6dc84 100644 Binary files a/target/test-classes/de/butzlabben/world/config/TestPluginConfig.class and b/target/test-classes/de/butzlabben/world/config/TestPluginConfig.class differ