Prepared Sponge related classes for future implementation.

This commit is contained in:
Rsl1122 2018-04-02 15:11:31 +03:00
parent fb994264e7
commit 28192ee5d5
18 changed files with 518 additions and 18 deletions

View File

@ -115,6 +115,10 @@
<id>paper-repo</id>
<url>https://repo.destroystokyo.com/repository/maven-public/</url>
</repository>
<repository>
<id>sponge-repo</id>
<url>https://repo.spongepowered.org/maven</url>
</repository>
</repositories>
<dependencies>
<dependency>
@ -247,12 +251,80 @@
<artifactId>bungeecord-chat</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>spongeapi</artifactId>
<version>7.0.0</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>error_prone_annotations</artifactId>
<groupId>com.google.errorprone</groupId>
</exclusion>
<exclusion>
<artifactId>guice</artifactId>
<groupId>com.google.inject</groupId>
</exclusion>
<exclusion>
<artifactId>caffeine</artifactId>
<groupId>com.github.ben-manes.caffeine</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.github.ben-manes.caffeine</groupId>
</exclusion>
<exclusion>
<artifactId>plugin-meta</artifactId>
<groupId>org.spongepowered</groupId>
</exclusion>
<exclusion>
<artifactId>configurate-hocon</artifactId>
<groupId>ninja.leaping.configurate</groupId>
</exclusion>
<exclusion>
<artifactId>configurate-gson</artifactId>
<groupId>ninja.leaping.configurate</groupId>
</exclusion>
<exclusion>
<artifactId>configurate-yaml</artifactId>
<groupId>ninja.leaping.configurate</groupId>
</exclusion>
<exclusion>
<artifactId>flow-math</artifactId>
<groupId>com.flowpowered</groupId>
</exclusion>
<exclusion>
<artifactId>flow-noise</artifactId>
<groupId>com.flowpowered</groupId>
</exclusion>
<exclusion>
<artifactId>asm</artifactId>
<groupId>org.ow2.asm</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>jsr305</artifactId>
<groupId>com.google.code.findbugs</groupId>
</exclusion>
<exclusion>
<artifactId>gson</artifactId>
<groupId>com.google.code.gson</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>

View File

@ -19,6 +19,10 @@
<id>paper-repo</id>
<url>https://repo.destroystokyo.com/repository/maven-public/</url>
</repository>
<repository>
<id>sponge-repo</id>
<url>https://repo.spongepowered.org/maven</url>
</repository>
</repositories>
<dependencies>
<!-- Framework for easier plugin development -->
@ -62,6 +66,14 @@
<type>jar</type>
<scope>provided</scope>
</dependency>
<!-- SpongePowered -->
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>spongeapi</artifactId>
<version>7.0.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<!-- StringUtils lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>

View File

@ -20,6 +20,7 @@ public interface PlanPlugin extends IPlugin {
static PlanPlugin getInstance() {
boolean bukkitAvailable = Check.isBukkitAvailable();
boolean bungeeAvailable = Check.isBungeeAvailable();
boolean spongeAvailable = Check.isSpongeAvailable();
if (bukkitAvailable) {
try {
Plan instance = Plan.getInstance();
@ -38,6 +39,15 @@ public interface PlanPlugin extends IPlugin {
} catch (IllegalStateException ignored) {
}
}
if (spongeAvailable) {
try {
PlanSponge instance = PlanSponge.getInstance();
if (instance != null) {
return instance;
}
} catch (IllegalStateException ignored) {
}
}
throw new IllegalAccessError("Plugin instance not available");
}

View File

@ -0,0 +1,143 @@
package com.djrapitops.plan;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.system.SpongeSystem;
import com.djrapitops.plan.system.settings.locale.Locale;
import com.djrapitops.plan.system.settings.locale.Msg;
import com.djrapitops.plan.system.settings.theme.PlanColorScheme;
import com.djrapitops.plugin.SpongePlugin;
import com.djrapitops.plugin.StaticHolder;
import com.djrapitops.plugin.api.Benchmark;
import com.djrapitops.plugin.api.utility.log.DebugLog;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.settings.ColorScheme;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
import org.spongepowered.api.plugin.Plugin;
import java.io.File;
import java.io.InputStream;
@Plugin(id = "plan", name = "Plan", version = "4.2.0")
public class PlanSponge extends SpongePlugin implements PlanPlugin {
@Inject
private Logger logger;
@Inject
@ConfigDir(sharedRoot = false)
private File dataFolder;
private SpongeSystem system;
@Listener
public void onServerStart(GameStartedServerEvent event) {
onEnable();
}
@Listener
public void onServerStop(GameStoppingServerEvent event) {
onDisable();
}
public static PlanSponge getInstance() {
return (PlanSponge) StaticHolder.getInstance(PlanSponge.class);
}
@Override
public void onEnable() {
super.onEnable();
system = new SpongeSystem(this);
try {
system.enable();
} catch (AbstractMethodError e) {
Log.error("Plugin ran into AbstractMethodError - Server restart is required. Likely cause is updating the jar without a restart.");
} catch (EnableException e) {
Log.error("----------------------------------------");
Log.error("Error: " + e.getMessage());
Log.error("----------------------------------------");
Log.error("Plugin Failed to Initialize Correctly. If this issue is caused by config settings you can use /plan reload");
onDisable();
} catch (Exception e) {
Log.toLog(this.getClass().getSimpleName() + "-v" + getVersion(), e);
Log.error("Plugin Failed to Initialize Correctly. If this issue is caused by config settings you can use /plan reload");
Log.error("This error should be reported at https://github.com/Rsl1122/Plan-PlayerAnalytics/issues");
onDisable();
}
}
@Override
public void onDisable() {
if (system != null) {
system.disable();
}
Log.info(Locale.get(Msg.DISABLED).toString());
Benchmark.pluginDisabled(PlanSponge.class);
DebugLog.pluginDisabled(PlanSponge.class);
}
@Override
public InputStream getResource(String resource) {
return getClass().getResourceAsStream("/" + resource);
}
@Override
public ColorScheme getColorScheme() {
return PlanColorScheme.create();
}
@Override
public void onReload() {
}
@Override
public boolean isReloading() {
return false;
}
@Override
public void log(String level, String msg) {
switch (level.toUpperCase()) {
case "INFO_COLOR":
case "INFO":
case "I":
logger.info(msg);
break;
case "W":
case "WARN":
case "WARNING":
logger.warn(msg);
break;
case "E":
case "ERR":
case "ERROR":
case "SEVERE":
logger.error(msg);
break;
default:
logger.info(msg);
break;
}
}
@Override
public File getDataFolder() {
return dataFolder;
}
@Override
public String getVersion() {
return "4.2.0";
}
public SpongeSystem getSystem() {
return system;
}
}

View File

@ -14,7 +14,6 @@ import com.djrapitops.plan.system.cache.DataCache;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plugin.StaticHolder;
import com.djrapitops.plugin.api.utility.log.Log;
import java.util.Map;
@ -70,7 +69,6 @@ public class ShutdownHook extends Thread {
Log.toLog(this.getClass(), e);
}
}
StaticHolder.unRegister(Plan.class);
}
}

View File

@ -5,7 +5,7 @@
package com.djrapitops.plan.api;
import com.djrapitops.plan.data.plugin.PluginData;
import com.djrapitops.plan.system.BukkitSystem;
import com.djrapitops.plan.system.ServerSystem;
import com.djrapitops.plan.system.database.databases.operation.FetchOperations;
import java.util.UUID;
@ -15,26 +15,26 @@ import java.util.UUID;
*
* @author Rsl1122
*/
public class BukkitAPI extends CommonAPI {
public class ServerAPI extends CommonAPI {
private final BukkitSystem bukkitSystem;
private final ServerSystem serverSystem;
public BukkitAPI(BukkitSystem bukkitSystem) {
this.bukkitSystem = bukkitSystem;
public ServerAPI(ServerSystem serverSystem) {
this.serverSystem = serverSystem;
}
@Override
public void addPluginDataSource(PluginData pluginData) {
bukkitSystem.getHookHandler().addPluginDataSource(pluginData);
serverSystem.getHookHandler().addPluginDataSource(pluginData);
}
@Override
public String getPlayerName(UUID uuid) {
return bukkitSystem.getCacheSystem().getDataCache().getName(uuid);
return serverSystem.getCacheSystem().getDataCache().getName(uuid);
}
@Override
public FetchOperations fetchFromPlanDB() {
return bukkitSystem.getDatabaseSystem().getActiveDatabase().fetch();
return serverSystem.getDatabaseSystem().getActiveDatabase().fetch();
}
}

View File

@ -6,7 +6,7 @@ package com.djrapitops.plan.system;
import com.djrapitops.plan.Plan;
import com.djrapitops.plan.ShutdownHook;
import com.djrapitops.plan.api.BukkitAPI;
import com.djrapitops.plan.api.ServerAPI;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.data.plugin.HookHandler;
import com.djrapitops.plan.system.database.BukkitDBSystem;
@ -27,7 +27,7 @@ import com.djrapitops.plugin.api.utility.log.Log;
*
* @author Rsl1122
*/
public class BukkitSystem extends PlanSystem {
public class BukkitSystem extends PlanSystem implements ServerSystem {
public BukkitSystem(Plan plugin) {
testSystem = this;
@ -45,7 +45,7 @@ public class BukkitSystem extends PlanSystem {
serverInfo = new BukkitServerInfo(plugin);
hookHandler = new HookHandler();
planAPI = new BukkitAPI(this);
planAPI = new ServerAPI(this);
StaticHolder.saveInstance(ShutdownHook.class, plugin.getClass());
new ShutdownHook().register();

View File

@ -63,12 +63,15 @@ public abstract class PlanSystem implements SubSystem {
public static PlanSystem getInstance() {
boolean bukkitAvailable = Check.isBukkitAvailable();
boolean bungeeAvailable = Check.isBungeeAvailable();
boolean spongeAvailable = Check.isSpongeAvailable();
if (bukkitAvailable && bungeeAvailable) {
return testSystem;
} else if (bungeeAvailable) {
return BungeeSystem.getInstance();
} else if (bukkitAvailable) {
return BukkitSystem.getInstance();
} else if (spongeAvailable) {
return SpongeSystem.getInstance();
}
throw new IllegalAccessError("PlanSystem is not available on this platform.");
}

View File

@ -0,0 +1,14 @@
package com.djrapitops.plan.system;
import com.djrapitops.plan.data.plugin.HookHandler;
import com.djrapitops.plan.system.cache.CacheSystem;
import com.djrapitops.plan.system.database.DBSystem;
public interface ServerSystem {
HookHandler getHookHandler();
CacheSystem getCacheSystem();
DBSystem getDatabaseSystem();
}

View File

@ -0,0 +1,63 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package com.djrapitops.plan.system;
import com.djrapitops.plan.PlanSponge;
import com.djrapitops.plan.ShutdownHook;
import com.djrapitops.plan.api.ServerAPI;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.data.plugin.HookHandler;
import com.djrapitops.plan.system.database.BukkitDBSystem;
import com.djrapitops.plan.system.file.FileSystem;
import com.djrapitops.plan.system.info.BukkitInfoSystem;
import com.djrapitops.plan.system.info.server.SpongeServerInfo;
import com.djrapitops.plan.system.listeners.SpongeListenerSystem;
import com.djrapitops.plan.system.settings.PlanErrorManager;
import com.djrapitops.plan.system.settings.config.BukkitConfigSystem;
import com.djrapitops.plan.system.settings.network.NetworkSettings;
import com.djrapitops.plan.system.tasks.SpongeTaskSystem;
import com.djrapitops.plan.system.update.VersionCheckSystem;
import com.djrapitops.plugin.StaticHolder;
import com.djrapitops.plugin.api.utility.log.Log;
/**
* Represents PlanSystem for PlanSponge.
*
* @author Rsl1122
*/
public class SpongeSystem extends PlanSystem implements ServerSystem {
public SpongeSystem(PlanSponge plugin) {
testSystem = this;
Log.setErrorManager(new PlanErrorManager());
versionCheckSystem = new VersionCheckSystem(plugin.getVersion());
fileSystem = new FileSystem(plugin);
configSystem = new BukkitConfigSystem();
databaseSystem = new BukkitDBSystem();
listenerSystem = new SpongeListenerSystem();
taskSystem = new SpongeTaskSystem();
infoSystem = new BukkitInfoSystem();
serverInfo = new SpongeServerInfo();
hookHandler = new HookHandler();
planAPI = new ServerAPI(this);
StaticHolder.saveInstance(ShutdownHook.class, plugin.getClass());
new ShutdownHook().register();
}
public static SpongeSystem getInstance() {
return PlanSponge.getInstance().getSystem();
}
@Override
public void enable() throws EnableException {
super.enable();
NetworkSettings.loadSettingsFromDB();
}
}

View File

@ -29,6 +29,9 @@ public class BukkitServerInfo extends ServerInfo {
private ServerInfoFile serverInfoFile;
private Database database;
BukkitServerInfo() {
}
public BukkitServerInfo(Plan plugin) {
serverProperties = new ServerProperties(plugin.getServer());
}

View File

@ -3,6 +3,7 @@ package com.djrapitops.plan.system.info.server;
import com.djrapitops.plan.system.settings.Settings;
import net.md_5.bungee.api.ProxyServer;
import org.bukkit.Server;
import org.spongepowered.api.Game;
/**
* Class responsible for holding server variable values that do not change
@ -49,6 +50,22 @@ public class ServerProperties {
onlinePlayers = server::getOnlineCount;
}
public ServerProperties(Game game) {
if (game == null) {
throw new IllegalStateException("Game did not inject.");
}
version = game.getPlatform().getMinecraftVersion().getName();
ip = () -> game.getServer().getBoundAddress().get().getAddress().getHostAddress();
name = "Sponge";
port = game.getServer().getBoundAddress().get().getPort();
implVersion = version;
id = game.getServer().getMotd().toPlain();
maxPlayers = game.getServer().getMaxPlayers();
onlinePlayers = () -> game.getServer().getOnlinePlayers().size();
}
/**
* Ip string in server.properties.
*

View File

@ -0,0 +1,10 @@
package com.djrapitops.plan.system.info.server;
import org.spongepowered.api.Sponge;
public class SpongeServerInfo extends BukkitServerInfo {
public SpongeServerInfo() {
serverProperties = new ServerProperties(Sponge.getGame());
}
}

View File

@ -0,0 +1,14 @@
package com.djrapitops.plan.system.listeners;
public class SpongeListenerSystem extends ListenerSystem {
@Override
protected void registerListeners() {
}
@Override
protected void unregisterListeners() {
}
}

View File

@ -0,0 +1,9 @@
package com.djrapitops.plan.system.tasks;
public class SpongeTaskSystem extends TaskSystem {
@Override
public void enable() {
}
}

View File

@ -27,7 +27,6 @@ public class AnalysisPageResponse extends Response {
} catch (NoServersException | ConnectionFailException e) {
ResponseCache.cacheResponse(PageId.SERVER.of(serverUUID), () -> new NotFoundResponse(e.getMessage()));
} catch (WebException e) {
// TODO Exception handling
Log.toLog(AnalysisPageResponse.class.getName(), e);
}
});

View File

@ -0,0 +1,60 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package com.djrapitops.plan.system;
import com.djrapitops.plan.PlanSponge;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.system.settings.Settings;
import org.junit.*;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import utilities.Teardown;
import utilities.mocks.SpongeMockUtil;
/**
* Test for BukkitSystem.
*
* @author Rsl1122
*/
@RunWith(MockitoJUnitRunner.Silent.class)
public class SpongeSystemTest {
@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private static PlanSponge planMock;
private SpongeSystem spongeSystem;
@BeforeClass
public static void setUpClass() throws Exception {
SpongeMockUtil mockUtil = SpongeMockUtil.setUp()
.withDataFolder(temporaryFolder.getRoot())
.withLogging()
.withResourceFetchingFromJar();
planMock = mockUtil.getPlanMock();
}
@Before
public void setUp() {
Teardown.resetSettingsTempValues();
}
@After
public void tearDown() {
if (spongeSystem != null) {
spongeSystem.disable();
}
Teardown.resetSettingsTempValues();
}
@Test
@Ignore("Sponge mock required")
public void testEnable() throws EnableException {
Settings.WEBSERVER_PORT.setTemporaryValue(9000);
spongeSystem = new SpongeSystem(planMock);
spongeSystem.enable();
}
}

View File

@ -0,0 +1,73 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package utilities.mocks;
import com.djrapitops.plan.PlanSponge;
import com.djrapitops.plugin.StaticHolder;
import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.task.ThreadRunnable;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import utilities.Teardown;
import java.io.File;
import static org.mockito.Mockito.*;
/**
* Mocking Utility for Sponge version of Plan.
*
* @author Rsl1122
*/
public class SpongeMockUtil extends MockUtil {
private PlanSponge planMock;
private SpongeMockUtil() {
}
public static SpongeMockUtil setUp() {
RunnableFactory.activateTestMode();
Teardown.resetSettingsTempValues();
return new SpongeMockUtil().mockPlugin();
}
private SpongeMockUtil mockPlugin() {
planMock = Mockito.mock(PlanSponge.class);
super.planMock = planMock;
StaticHolder.register(PlanSponge.class, planMock);
StaticHolder.register(planMock);
StaticHolder.saveInstance(MockitoJUnitRunner.class, PlanSponge.class);
StaticHolder.saveInstance(ThreadRunnable.class, PlanSponge.class);
doReturn("4.2.0").when(planMock).getVersion();
doCallRealMethod().when(planMock).getColorScheme();
return this;
}
public SpongeMockUtil withDataFolder(File tempFolder) {
when(planMock.getDataFolder()).thenReturn(tempFolder);
return this;
}
public SpongeMockUtil withLogging() {
doNothing().when(planMock).log(Mockito.anyString(), Mockito.anyString());
// TestLogger testLogger = new TestLogger();
// doReturn(testLogger).when(planMock).();
return this;
}
public SpongeMockUtil withResourceFetchingFromJar() throws Exception {
withPluginFiles();
return this;
}
public PlanSponge getPlanMock() {
return planMock;
}
}