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

642 lines
20 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 java.io.File;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
2015-07-30 19:24:01 +02:00
import java.util.UUID;
2015-07-28 08:06:19 +02:00
2015-07-26 18:14:34 +02:00
import org.slf4j.Logger;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.CatalogType;
2015-07-26 18:14:34 +02:00
import org.spongepowered.api.Game;
import org.spongepowered.api.Server;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.entity.living.player.PlayerChatEvent;
2015-07-26 20:38:08 +02:00
import org.spongepowered.api.plugin.Plugin;
2015-07-26 18:14:34 +02:00
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.service.profile.GameProfileResolver;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.translation.Translatable;
import org.spongepowered.api.text.translation.Translation;
2015-07-26 18:14:34 +02:00
import org.spongepowered.api.world.DimensionTypes;
import org.spongepowered.api.world.GeneratorTypes;
import org.spongepowered.api.world.World;
2015-07-30 16:25:16 +02:00
import com.google.common.base.Optional;
import com.google.common.eventbus.Subscribe;
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.Configuration;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.generator.HybridUtils;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.PlotPlayer;
2015-08-02 21:25:41 +02:00
import com.intellectualcrafters.plot.object.PlotWorld;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.AbstractTitle;
import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.ChatManager;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.EconHandler;
import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.InventoryUtil;
import com.intellectualcrafters.plot.util.SchematicHandler;
import com.intellectualcrafters.plot.util.SetupUtils;
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-30 19:24:01 +02:00
import com.intellectualcrafters.plot.util.UUIDHandler;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.UUIDHandlerImplementation;
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
import com.plotsquared.sponge.generator.SpongeBasicGen;
import com.plotsquared.sponge.generator.SpongeGeneratorWrapper;
import com.plotsquared.sponge.generator.WorldModify;
import com.plotsquared.sponge.listener.MainListener;
import com.plotsquared.sponge.util.KillRoadMobs;
import com.plotsquared.sponge.util.SpongeBlockManager;
import com.plotsquared.sponge.util.SpongeChatManager;
2015-07-30 19:24:01 +02:00
import com.plotsquared.sponge.util.SpongeChunkManager;
2015-07-30 16:25:16 +02:00
import com.plotsquared.sponge.util.SpongeCommand;
import com.plotsquared.sponge.util.SpongeEventUtil;
import com.plotsquared.sponge.util.SpongeInventoryUtil;
import com.plotsquared.sponge.util.SpongeMetrics;
import com.plotsquared.sponge.util.SpongeTaskManager;
2015-07-30 19:24:01 +02:00
import com.plotsquared.sponge.util.SpongeTitleManager;
2015-07-30 16:25:16 +02:00
import com.plotsquared.sponge.util.SpongeUtil;
import com.plotsquared.sponge.uuid.SpongeLowerOfflineUUIDWrapper;
import com.plotsquared.sponge.uuid.SpongeOnlineUUIDWrapper;
import com.plotsquared.sponge.uuid.SpongeUUIDHandler;
2015-07-26 18:14:34 +02:00
/**
* Created by robin on 01/11/2014
*/
2015-07-26 20:38:08 +02:00
@Plugin(id = "PlotSquared", name = "PlotSquared", version = "3.0.0")
2015-07-26 18:14:34 +02:00
public class SpongeMain implements IPlotMain, PluginContainer {
public static SpongeMain THIS;
@Inject private Logger logger;
@Inject private Game game;
private Server server;
private GameProfileResolver resolver;
2015-07-26 18:14:34 +02:00
private WorldModify modify;
private Object plugin;
// stuff //
public Logger getLogger() {
return logger;
}
public Game getGame() {
return game;
}
public Server getServer() {
return server;
}
public GameProfileResolver getResolver() {
return resolver;
}
public Object getPlugin() {
2015-07-30 16:25:16 +02:00
return plugin;
}
public Text getText(String m) {
return Texts.of(m);
}
public Translatable getTranslation(final String m) {
return new Translatable() {
@Override
public Translation getTranslation() {
return new Translation() {
@Override
public String getId() {
return m;
}
@Override
public String get(Locale l, Object... args) {
return m;
}
@Override
public String get(Locale l) {
return m;
}
};
}
};
}
private PlotBlock NULL_BLOCK = new PlotBlock((short) 0, (byte) 0);
private BlockState[][] blockMap;
private Map<BlockState, PlotBlock> blockMapReverse;
public BlockState getBlockState(PlotBlock block) {
if (blockMap[block.id] == null) {
log("UNKNOWN BLOCK: " + block.toString());
return null;
}
else if (blockMap[block.id].length <= block.data) {
log("UNKNOWN BLOCK: " + block.toString() + " -> Using " + block.id + ":0 instead");
return blockMap[block.id][0];
}
return blockMap[block.id][block.data];
}
public BlockState getBlockState(int id) {
return blockMap[id][0];
}
public Collection<BlockState> getAllStates() {
return this.blockMapReverse.keySet();
}
public PlotBlock getPlotBlock(BlockState state) {
PlotBlock val = blockMapReverse.get(state);
if (val == null) {
return NULL_BLOCK;
}
return val;
}
/////////
2015-07-26 18:14:34 +02:00
////////////////////// SPONGE PLUGIN REGISTRATION ////////////////////
@Override
public String getId() {
return "PlotSquared";
}
@Override
public Object getInstance() {
return THIS;
}
@Override
public String getName() {
return "PlotSquared";
}
@Override
public String getVersion() {
int[] version = PS.get().getVersion();
String result = "";
String prefix = "";
for (int i : version) {
result += prefix + i;
prefix = ".";
}
return result;
}
///////////////////////////////////////////////////////////////////////
///////////////////// ON ENABLE /////////////////////
@Subscribe
public void onInit(PreInitializationEvent event) {
2015-07-26 20:38:08 +02:00
log("PRE INIT");
}
@Subscribe
public void onServerAboutToStart(ServerAboutToStartEvent event) {
log("INIT");
2015-07-26 18:14:34 +02:00
THIS = this;
2015-07-26 20:38:08 +02:00
2015-07-30 16:25:16 +02:00
//
resolver = game.getServiceManager().provide(GameProfileResolver.class).get();
plugin = game.getPluginManager().getPlugin("PlotSquared").get().getInstance();
2015-07-30 16:25:16 +02:00
log("PLUGIN IS THIS: " + (plugin == this));
plugin = this;
server = game.getServer();
//
PS.instance = new PS(this, "Sponge");
2015-07-26 20:38:08 +02:00
2015-07-30 16:25:16 +02:00
registerBlocks();
2015-07-26 20:38:08 +02:00
ConfigurationSection worldSection = PS.get().config.getConfigurationSection("worlds");
if (worldSection != null) {
for (String world : worldSection.getKeys(false)) {
2015-08-02 21:25:41 +02:00
createWorldFromConfig(world);
}
}
}
public World createWorldFromConfig(String world) {
SpongeBasicGen generator = new SpongeBasicGen(world);
PlotWorld plotworld = generator.getNewPlotWorld(world);
SpongeGeneratorWrapper wrapper;
if (plotworld.TYPE == 0) {
wrapper = new SpongeGeneratorWrapper(world, generator);
}
else {
wrapper = new SpongeGeneratorWrapper(world, null);
}
PS.get().loadWorld(world, wrapper);
switch (plotworld.TYPE) {
// Normal
case 0: {
this.modify = new WorldModify(generator, false);
game.getRegistry().registerWorldGeneratorModifier(modify);
Optional<World> builder = game.getRegistry().createWorldBuilder()
.name(world)
.enabled(true)
.loadsOnStartup(true)
.keepsSpawnLoaded(true)
.dimensionType(DimensionTypes.OVERWORLD)
2015-07-30 16:25:16 +02:00
.generator(GeneratorTypes.FLAT)
.usesMapFeatures(false)
.generatorModifiers(modify)
.build();
2015-08-02 21:25:41 +02:00
return builder.get();
}
// Augmented
default: {
this.modify = new WorldModify(generator, true);
game.getRegistry().registerWorldGeneratorModifier(modify);
Optional<World> builder = game.getRegistry().createWorldBuilder()
2015-08-02 21:25:41 +02:00
.name(world)
.enabled(true)
.loadsOnStartup(true)
.keepsSpawnLoaded(true)
.dimensionType(DimensionTypes.OVERWORLD)
.generator(GeneratorTypes.OVERWORLD)
.usesMapFeatures(false)
.generatorModifiers(modify)
.build();
return builder.get();
}
2015-07-26 20:38:08 +02:00
}
2015-07-26 18:14:34 +02:00
}
2015-07-30 16:25:16 +02:00
public void registerBlock(PlotBlock block, BlockState state) {
BlockState[] val = blockMap[block.id];
if (val == null) {
blockMap[block.id] = new BlockState[block.data + 1];
}
else if (val.length <= block.data) {
blockMap[block.id] = Arrays.copyOf(val, block.data + 1);
}
else if (val[block.data] != null) {
return;
}
blockMap[block.id][block.data] = state;
blockMapReverse.put(state, block);
}
public PlotBlock registerBlock(BlockState state) {
PlotBlock val = blockMapReverse.get(state);
if (val != null) {
return val;
}
byte data;
if (blockMap[0] == null) {
blockMap[0] = new BlockState[1];
data = 0;
}
else {
data = (byte) (blockMap[0].length);
}
PlotBlock block = new PlotBlock((short) 0, data);
registerBlock(block, state);
return block;
}
public void registerBlocks() {
blockMap = new BlockState[256][];
2015-08-13 14:39:29 +02:00
blockMapReverse = new HashMap<BlockState, PlotBlock>();
2015-07-30 16:25:16 +02:00
HashMap<String, BlockState> states = new HashMap<>();
PS.get().copyFile("ids.txt", "config");
PS.get().copyFile("data.txt", "config");
try {
File id_file = new File(getDirectory(), "config" + File.separator + "ids.txt");
List<String> id_lines = Files.readAllLines(id_file.toPath(), StandardCharsets.UTF_8);
File data_file = new File(getDirectory(), "config" + File.separator + "data.txt");
List<String> data_lines = Files.readAllLines(data_file.toPath(), StandardCharsets.UTF_8);
Field[] fields = BlockTypes.class.getDeclaredFields();
for (Field field : fields) {
BlockType type = (BlockType) field.get(null);
BlockState state = type.getDefaultState();
if (state != null) {
try {
states.put(type.getId() + ":" + 0, state);
}
catch (Exception e) {}
}
}
String packaze = "org.spongepowered.api.data.type.";
for (int i = 0; i < data_lines.size(); i++) {
String classname = packaze + data_lines.get(i).trim();
try {
Class<?> clazz = Class.forName(classname);
fields = clazz.getDeclaredFields();
for (Field field : fields) {
CatalogType type = (CatalogType) field.get(null);
String minecraft_id = type.getId();
BlockState state = states.get(minecraft_id + ":" + 0);
if (state == null) {
continue;
}
2015-07-30 16:25:16 +02:00
}
}
catch (Throwable e) {}
}
PlotBlock block = null;
for (int i = 0; i < id_lines.size(); i++) {
String line = id_lines.get(i).trim();
switch(i%3) {
case 0: {
block = Configuration.BLOCK.parseString(line);
break;
}
case 1: {
break;
}
case 2: {
String minecraft_id = line;
BlockState state = states.remove(minecraft_id + ":" + block.data);
if (state == null) {
continue;
}
registerBlock(block, state);
break;
}
}
}
for (Entry<String, BlockState> state : states.entrySet()) {
log("REGISTERING: " + registerBlock(state.getValue()) + " | " + state.getValue().getType());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
2015-07-26 18:14:34 +02:00
@Subscribe
public void onPlayerChat(PlayerChatEvent event) {
// This is how events sort of work?
Player player = event.getUser();
log(player.getWorld().getName());
}
@Override
public void log(String message) {
2015-07-30 16:25:16 +02:00
message = C.format(message, C.replacements);
if (!Settings.CONSOLE_COLOR) {
message = message.replaceAll('\u00a7' + "[a-z|0-9]", "");
}
if (server == null || server.getConsole() == null) {
logger.info(message);
return;
}
server.getConsole().sendMessage(Texts.of(message));
2015-07-26 18:14:34 +02:00
}
@Override
public File getDirectory() {
return new File("mods/PlotSquared");
}
@Override
public void disable() {
PS.get().disable();
THIS = null;
}
@Override
public int[] getPluginVersion() {
PluginContainer plugin = game.getPluginManager().getPlugin("PlotSquared").get();
String version = plugin.getVersion();
log("Checking plugin version: PlotSquared: ");
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
}
@Override
public int[] getServerVersion() {
log("Checking minecraft version: Sponge: ");
String version = game.getPlatform().getMinecraftVersion().getName();
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-07-30 16:25:16 +02:00
@Override
public InventoryUtil initInventoryUtil() {
return new SpongeInventoryUtil();
}
@Override
public SpongeGeneratorWrapper getGenerator(String world, String name) {
if (name == null) {
return new SpongeGeneratorWrapper(world, null);
}
if (name.equals("PlotSquared")) {
2015-08-02 21:25:41 +02:00
return new SpongeGeneratorWrapper(world, new SpongeBasicGen(world));
2015-07-30 16:25:16 +02:00
}
else {
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
}
}
@Override
public EconHandler getEconomyHandler() {
// TODO Auto-generated method stub
// Nothing like Vault exists yet
PS.log("getEconomyHandler NOT IMPLEMENTED YET");
return null;
}
2015-07-26 18:14:34 +02:00
@Override
2015-07-30 16:25:16 +02:00
public BlockManager initBlockManager() {
return new SpongeBlockManager();
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +02:00
public EventUtil initEventUtil() {
return new SpongeEventUtil();
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +02:00
public ChunkManager initChunkManager() {
return new SpongeChunkManager();
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +02:00
public SetupUtils initSetupUtils() {
2015-08-02 21:25:41 +02:00
return new SpongeSetupUtils();
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +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-07-30 16:25:16 +02:00
2015-07-26 18:14:34 +02:00
@Override
2015-07-30 16:25:16 +02:00
public SchematicHandler initSchematicHandler() {
2015-08-02 13:56:18 +02:00
return new SpongeSchematicHandler();
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +02:00
public TaskManager getTaskManager() {
return new SpongeTaskManager();
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +02:00
public void runEntityTask() {
new KillRoadMobs().run();
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +02:00
public void registerCommands() {
2015-08-04 14:21:12 +02:00
getGame().getCommandDispatcher().register(plugin, new SpongeCommand(), "plots", "p", "plot", "ps", "plotsquared", "p2", "2");
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +02:00
public void registerPlayerEvents() {
game.getEventManager().register(this, new MainListener());
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +02:00
public void registerInventoryEvents() {
2015-07-26 18:14:34 +02:00
// TODO Auto-generated method stub
2015-07-30 16:25:16 +02:00
log("registerInventoryEvents is not implemented!");
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +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
}
@Override
2015-07-30 16:25:16 +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
}
@Override
2015-07-30 16:25:16 +02:00
public void registerWorldEditEvents() {
2015-07-26 18:14:34 +02:00
// TODO Auto-generated method stub
2015-07-30 16:25:16 +02:00
log("registerWorldEditEvents is not implemented!");
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-30 16:25:16 +02:00
public void registerTNTListener() {
2015-07-26 18:14:34 +02:00
// TODO Auto-generated method stub
2015-07-30 16:25:16 +02:00
log("registerTNTListener is not implemented!");
2015-07-26 18:14:34 +02:00
}
@Override
2015-07-27 11:14:28 +02:00
public UUIDHandlerImplementation initUUIDHandler() {
UUIDWrapper wrapper;
if (Settings.OFFLINE_MODE || !PS.get().checkVersion(this.getServerVersion(), 1, 7, 6)) {
wrapper = new SpongeLowerOfflineUUIDWrapper();
}
else {
wrapper = new SpongeOnlineUUIDWrapper();
}
return new SpongeUUIDHandler(wrapper);
2015-07-26 18:14:34 +02:00
}
@Override
public boolean initPlotMeConverter() {
// TODO Auto-generated method stub
2015-07-30 16:25:16 +02:00
PS.log("initPlotMeConverter NOT IMPLEMENTED YET");
2015-07-26 18:14:34 +02:00
return false;
}
@Override
public void unregister(PlotPlayer player) {
2015-07-30 16:25:16 +02:00
SpongeUtil.removePlayer(player.getName());
2015-07-26 18:14:34 +02:00
}
@Override
public void registerChunkProcessor() {
// TODO Auto-generated method stub
2015-07-30 16:25:16 +02:00
PS.log("registerChunkProcessor NOT IMPLEMENTED YET");
2015-07-26 18:14:34 +02:00
}
@Override
public void registerWorldEvents() {
// TODO Auto-generated method stub
2015-07-30 16:25:16 +02:00
PS.log("registerWorldEvents NOT IMPLEMENTED YET");
2015-07-26 18:14:34 +02:00
}
@Override
public String getServerName() {
2015-07-30 16:25:16 +02:00
// TODO FIXME
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
2015-07-26 18:14:34 +02:00
}
2015-07-28 08:06:19 +02:00
@Override
public void startMetrics() {
try {
final SpongeMetrics metrics = new SpongeMetrics(game, this);
metrics.start();
log(C.PREFIX.s() + "&6Metrics enabled.");
} catch (final Exception e) {
log(C.PREFIX.s() + "&cFailed to load up metrics.");
}
2015-07-28 08:06:19 +02:00
}
@Override
public void setGenerator(String world) {
2015-08-02 21:25:41 +02:00
// TODO THIS IS DONE DURING STARTUP ALREADY
2015-07-30 16:25:16 +02:00
}
@Override
public AbstractTitle initTitleManager() {
return new SpongeTitleManager();
2015-07-28 08:06:19 +02:00
}
2015-07-30 19:24:01 +02:00
@Override
public PlotPlayer wrapPlayer(Object obj) {
if (obj instanceof Player) {
return SpongeUtil.getPlayer((Player) obj);
}
// else if (obj instanceof OfflinePlayer) {
// return BukkitUtil.getPlayer((OfflinePlayer) obj);
// }
else if (obj instanceof String) {
return UUIDHandler.getPlayer((String) obj);
}
else if (obj instanceof UUID) {
return UUIDHandler.getPlayer((UUID) obj);
}
return null;
}
2015-08-03 20:20:04 +02:00
@Override
public String getNMSPackage() {
return "1_8_R3";
}
@Override
public ChatManager<?> initChatManager() {
return new SpongeChatManager();
}
2015-07-26 18:14:34 +02:00
}