Deleted Mockito and a few other things

This commit is contained in:
Daniel 2022-10-06 20:36:50 -04:00
parent 825413fcc3
commit 540bf968cc
20 changed files with 473 additions and 36 deletions

View File

@ -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);
}
}

View File

@ -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<String> 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];
}
}

View File

@ -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<String> 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<String> getCommandHelp() {
List<String> 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");
}
}

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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;
}
}
}

View File

@ -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());
}
}

View File

@ -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());
// }
}

View File

@ -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