PlotSquared/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java

432 lines
13 KiB
Java
Raw Normal View History

2015-07-26 18:14:34 +02:00
package com.plotsquared.sponge;
2015-07-30 16:25:16 +02:00
import com.google.inject.Inject;
import com.intellectualcrafters.configuration.ConfigurationSection;
import com.intellectualcrafters.plot.IPlotMain;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.ConfigurationNode;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.generator.GeneratorWrapper;
import com.intellectualcrafters.plot.generator.HybridGen;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.generator.HybridUtils;
import com.intellectualcrafters.plot.generator.IndependentPlotGenerator;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.SetupObject;
import com.intellectualcrafters.plot.util.*;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
import com.plotsquared.sponge.generator.SpongePlotGenerator;
import com.plotsquared.sponge.listener.ChunkProcessor;
2015-07-30 16:25:16 +02:00
import com.plotsquared.sponge.listener.MainListener;
import com.plotsquared.sponge.listener.WorldEvents;
import com.plotsquared.sponge.util.*;
import com.plotsquared.sponge.util.SpongeSetupUtils;
import com.plotsquared.sponge.util.block.FastQueue;
import com.plotsquared.sponge.util.block.SlowQueue;
2015-07-30 16:25:16 +02:00
import com.plotsquared.sponge.uuid.SpongeLowerOfflineUUIDWrapper;
import com.plotsquared.sponge.uuid.SpongeOnlineUUIDWrapper;
import com.plotsquared.sponge.uuid.SpongeUUIDHandler;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.Server;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameAboutToStartServerEvent;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.profile.GameProfileManager;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.gen.GenerationPopulator;
import org.spongepowered.api.world.gen.WorldGenerator;
import org.spongepowered.api.world.gen.WorldGeneratorModifier;
import java.io.File;
import java.util.*;
2015-07-26 18:14:34 +02:00
/**
* Created by robin on 01/11/2014
*/
@Plugin(id = "PlotSquared", name = "PlotSquared", version = "3.3.0", dependencies = "before:WorldEdit")
public class SpongeMain implements IPlotMain {
2015-07-26 18:14:34 +02:00
public static SpongeMain THIS;
2015-11-26 04:40:22 +01:00
2015-09-11 12:09:22 +02:00
@Inject
private Logger logger;
@Inject
private Game game;
2015-07-26 18:14:34 +02:00
private Server server;
2015-11-26 04:40:22 +01:00
2015-12-19 20:30:06 +01:00
private GameProfileManager resolver;
2015-11-26 04:40:22 +01:00
// @Override
2015-09-13 06:04:31 +02:00
public Logger getLogger() {
return logger;
}
2015-11-26 04:40:22 +01:00
2015-09-13 06:04:31 +02:00
public Game getGame() {
return game;
}
2015-11-26 04:40:22 +01:00
2015-09-13 06:04:31 +02:00
public Server getServer() {
return server;
}
2015-11-26 04:40:22 +01:00
2015-12-19 20:30:06 +01:00
public GameProfileManager getResolver() {
if (resolver == null) {
resolver = game.getServer().getGameProfileManager();
}
return resolver;
}
2015-11-26 04:40:22 +01:00
2015-12-19 20:30:06 +01:00
public SpongeMain getPlugin() {
return THIS;
2015-07-30 16:25:16 +02:00
}
2015-11-26 04:40:22 +01:00
// @Override
2015-09-13 06:04:31 +02:00
public String getId() {
2015-07-26 18:14:34 +02:00
return "PlotSquared";
}
2015-11-26 04:40:22 +01:00
// @Override
2015-12-19 20:30:06 +01:00
public Optional<Object> getInstance() {
return Optional.<Object> of(THIS);
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
// @Override
2015-09-13 06:04:31 +02:00
public String getName() {
2015-07-26 18:14:34 +02:00
return "PlotSquared";
}
2015-11-26 04:40:22 +01:00
// @Override
2015-09-13 06:04:31 +02:00
public String getVersion() {
2015-09-11 12:09:22 +02:00
final int[] version = PS.get().getVersion();
2015-07-26 18:14:34 +02:00
String result = "";
String prefix = "";
2015-09-13 06:04:31 +02:00
for (final int i : version) {
2015-07-26 18:14:34 +02:00
result += prefix + i;
prefix = ".";
}
return result;
}
2015-11-26 04:40:22 +01:00
2015-10-07 08:33:33 +02:00
@Listener
public void init(final GameInitializationEvent event) {
log("PlotSquared: Game init");
}
2015-11-26 04:40:22 +01:00
2015-10-07 08:33:33 +02:00
@Listener
public void onInit(final GamePreInitializationEvent event) {
log("PlotSquared: Game pre init");
2015-12-19 20:30:06 +01:00
}
2015-10-07 08:33:33 +02:00
@Listener
public void onServerAboutToStart(final GameAboutToStartServerEvent event) {
log("PlotSquared: Server init");
2015-07-26 18:14:34 +02:00
THIS = this;
new PS(this, "Sponge");
2015-07-30 16:25:16 +02:00
server = game.getServer();
game.getRegistry().register(WorldGeneratorModifier.class, (WorldGeneratorModifier) new HybridGen().specify());
2015-07-30 16:25:16 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void log(String message) {
2015-07-30 16:25:16 +02:00
message = C.format(message, C.replacements);
2015-09-13 06:04:31 +02:00
if (!Settings.CONSOLE_COLOR) {
2015-07-30 16:25:16 +02:00
message = message.replaceAll('\u00a7' + "[a-z|0-9]", "");
}
2015-09-13 06:04:31 +02:00
if ((server == null) || (server.getConsole() == null)) {
2015-07-30 16:25:16 +02:00
logger.info(message);
return;
}
server.getConsole().sendMessage(SpongeUtil.getText(message));
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public File getDirectory() {
2015-07-26 18:14:34 +02:00
return new File("mods/PlotSquared");
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
public File getWorldContainer() {
return new File("world");
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
public void disable() {
PS.get().disable();
THIS = null;
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public int[] getPluginVersion() {
2015-09-11 12:09:22 +02:00
final PluginContainer plugin = game.getPluginManager().getPlugin("PlotSquared").get();
final String version = plugin.getVersion();
log("Checking plugin version: PlotSquared: ");
2015-09-11 12:09:22 +02:00
final String[] split = version.split("\\.");
return new int[] { Integer.parseInt(split[0]), Integer.parseInt(split[1]), (split.length == 3) ? Integer.parseInt(split[2]) : 0 };
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public int[] getServerVersion() {
log("Checking minecraft version: Sponge: ");
2015-09-11 12:09:22 +02:00
final String version = game.getPlatform().getMinecraftVersion().getName();
final String[] split = version.split("\\.");
return new int[] { Integer.parseInt(split[0]), Integer.parseInt(split[1]), (split.length == 3) ? Integer.parseInt(split[2]) : 0 };
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public InventoryUtil initInventoryUtil() {
2015-07-30 16:25:16 +02:00
return new SpongeInventoryUtil();
}
2015-11-26 04:40:22 +01:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public EconHandler getEconomyHandler() {
SpongeEconHandler econ = new SpongeEconHandler();
Sponge.getEventManager().registerListeners(this, econ);
return econ;
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public EventUtil initEventUtil() {
2015-07-30 16:25:16 +02:00
return new SpongeEventUtil();
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public ChunkManager initChunkManager() {
2015-07-30 16:25:16 +02:00
return new SpongeChunkManager();
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public SetupUtils initSetupUtils() {
2015-08-02 21:25:41 +02:00
return new SpongeSetupUtils();
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public HybridUtils initHybridUtils() {
2015-08-02 13:56:18 +02:00
return new SpongeHybridUtils();
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public SchematicHandler initSchematicHandler() {
2015-08-02 13:56:18 +02:00
return new SpongeSchematicHandler();
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public TaskManager getTaskManager() {
2015-07-30 16:25:16 +02:00
return new SpongeTaskManager();
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void runEntityTask() {
2015-07-30 16:25:16 +02:00
new KillRoadMobs().run();
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void registerCommands() {
2015-12-19 20:30:06 +01:00
getGame().getCommandManager().register(THIS, new SpongeCommand(), new String[] { "plots", "p", "plot", "ps", "plotsquared", "p2", "2" });
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void registerPlayerEvents() {
Sponge.getEventManager().registerListeners(this, new MainListener());
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void registerInventoryEvents() {
// Part of PlayerEvents - can be moved if necessary
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void registerPlotPlusEvents() {
2015-07-26 18:14:34 +02:00
// TODO Auto-generated method stub
2015-07-30 16:25:16 +02:00
log("registerPlotPlusEvents is not implemented!");
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void registerForceFieldEvents() {
2015-07-26 18:14:34 +02:00
// TODO Auto-generated method stub
2015-07-30 16:25:16 +02:00
log("registerForceFieldEvents is not implemented!");
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean initWorldEdit() {
try {
Class.forName("com.sk89q.worldedit.WorldEdit");
return true;
2015-09-13 06:04:31 +02:00
} catch (final Throwable e) {
return false;
}
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public UUIDHandlerImplementation initUUIDHandler() {
UUIDWrapper wrapper;
2015-09-13 06:04:31 +02:00
if (Settings.OFFLINE_MODE || !PS.get().checkVersion(getServerVersion(), 1, 7, 6)) {
wrapper = new SpongeLowerOfflineUUIDWrapper();
2015-09-13 06:04:31 +02:00
} else {
wrapper = new SpongeOnlineUUIDWrapper();
}
return new SpongeUUIDHandler(wrapper);
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean initPlotMeConverter() {
// PlotMe was never ported to sponge
2015-07-26 18:14:34 +02:00
return false;
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void unregister(final PlotPlayer player) {
2015-07-30 16:25:16 +02:00
SpongeUtil.removePlayer(player.getName());
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void registerChunkProcessor() {
Sponge.getEventManager().registerListeners(this, new ChunkProcessor());
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void registerWorldEvents() {
Sponge.getEventManager().registerListeners(this, new WorldEvents());
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-26 18:14:34 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String getServerName() {
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
2015-07-26 18:14:34 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-28 08:06:19 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void startMetrics() {
try {
final SpongeMetrics metrics = new SpongeMetrics(game, (PluginContainer) this);
metrics.start();
log(C.PREFIX.s() + "&6Metrics enabled.");
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
log(C.PREFIX.s() + "&cFailed to load up metrics.");
}
2015-07-28 08:06:19 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-28 08:06:19 +02:00
@Override
public void setGenerator(final String worldname) {
World world = SpongeUtil.getWorld(worldname);
if (world == null) {
// create world
final ConfigurationSection worldConfig = PS.get().config.getConfigurationSection("worlds." + worldname);
String manager = worldConfig.getString("generator.plugin");
if (manager == null) {
manager = "PlotSquared";
}
String generator = worldConfig.getString("generator.init");
if (generator == null) {
generator = manager;
}
final int type = worldConfig.getInt("generator.type");
final int terrain = worldConfig.getInt("generator.terrain");
final SetupObject setup = new SetupObject();
setup.plotManager = manager;
setup.setupGenerator = generator;
setup.type = type;
setup.terrain = terrain;
setup.step = new ConfigurationNode[0];
setup.world = worldname;
SetupUtils.manager.setupWorld(setup);
} else {
throw new IllegalArgumentException("World already loaded: " + worldname + "???");
}
WorldGenerator wg = world.getWorldGenerator();
GenerationPopulator gen = wg.getBaseGenerationPopulator();
if (gen instanceof SpongePlotGenerator) {
PS.get().loadWorld(worldname, (SpongePlotGenerator) gen);
} else if (gen != null) {
throw new UnsupportedOperationException("NOT IMPLEMENTED YET!");
} else {
if (PS.get().config.contains("worlds." + worldname)) {
PS.get().loadWorld(worldname, null);
}
}
2015-07-30 16:25:16 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public AbstractTitle initTitleManager() {
2015-07-30 16:25:16 +02:00
return new SpongeTitleManager();
2015-07-28 08:06:19 +02:00
}
2015-11-26 04:40:22 +01:00
2015-07-30 19:24:01 +02:00
@Override
2015-09-13 06:04:31 +02:00
public PlotPlayer wrapPlayer(final Object obj) {
if (obj instanceof Player) {
2015-07-30 19:24:01 +02:00
return SpongeUtil.getPlayer((Player) obj);
}
2015-09-13 06:04:31 +02:00
else if (obj instanceof String) {
2015-07-30 19:24:01 +02:00
return UUIDHandler.getPlayer((String) obj);
2015-09-13 06:04:31 +02:00
} else if (obj instanceof UUID) {
return UUIDHandler.getPlayer((UUID) obj);
2015-07-30 19:24:01 +02:00
}
// TODO FIXME offline player
2015-07-30 19:24:01 +02:00
return null;
}
2015-11-26 04:40:22 +01:00
2015-08-03 20:20:04 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String getNMSPackage() {
return "";//TODO FIXME
2015-08-03 20:20:04 +02:00
}
2015-11-26 04:40:22 +01:00
@Override
2015-09-13 06:04:31 +02:00
public ChatManager<?> initChatManager() {
return new SpongeChatManager();
}
@Override
public PlotQueue initPlotQueue() {
if (PS.get().checkVersion(getServerVersion(), 1, 8, 0)) {
try {
MainUtil.canSendChunk = true;
return new FastQueue();
} catch (Throwable e) {
e.printStackTrace();
}
}
return new SlowQueue();
}
@Override
public WorldUtil initWorldUtil() {
return new SpongeUtil();
}
@Override
public GeneratorWrapper<?> getGenerator(String world, String name) {
if (name == null) {
return null;
}
Collection<WorldGeneratorModifier> wgms = game.getRegistry().getAllOf(WorldGeneratorModifier.class);
for (WorldGeneratorModifier wgm : wgms) {
if (StringMan.isEqualIgnoreCaseToAny(name, wgm.getName(), wgm.getId())) {
if (wgm instanceof GeneratorWrapper<?>) {
return (GeneratorWrapper<?>) wgm;
}
return new SpongePlotGenerator(wgm);
}
}
return new SpongePlotGenerator(new HybridGen());
}
@Override
public GeneratorWrapper<?> wrapPlotGenerator(IndependentPlotGenerator generator) {
return new SpongePlotGenerator(generator);
}
@Override
public List<String> getPluginIds() {
2016-02-19 20:13:41 +01:00
ArrayList<String> names = new ArrayList<>();
for (PluginContainer plugin : game.getPluginManager().getPlugins()) {
names.add(plugin.getName() + ";" + plugin.getVersion() + ":" + true);
}
return names;
}
2015-09-11 12:09:22 +02:00
}