Fixing some more things

This commit is contained in:
boy0001 2015-07-31 00:25:16 +10:00
parent bfa877e607
commit e1dad77d8f
264 changed files with 6920 additions and 2146 deletions

View file

@ -1,24 +1,29 @@
package com.plotsquared.sponge;
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.Settings;
import com.intellectualcrafters.plot.generator.HybridUtils;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.*;
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
import com.plotsquared.listener.APlotListener;
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;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.spongepowered.api.CatalogType;
import org.spongepowered.api.Game;
import org.spongepowered.api.GameRegistry;
import org.spongepowered.api.MinecraftVersion;
import org.spongepowered.api.Server;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.data.manipulator.block.StoneData;
import org.spongepowered.api.entity.player.Player;
import org.spongepowered.api.entity.player.gamemode.GameModes;
import org.spongepowered.api.event.EventHandler;
import org.spongepowered.api.event.Subscribe;
import org.spongepowered.api.event.entity.player.PlayerChatEvent;
import org.spongepowered.api.event.state.PreInitializationEvent;
@ -26,13 +31,56 @@ import org.spongepowered.api.event.state.ServerAboutToStartEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.service.profile.GameProfileResolver;
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;
import org.spongepowered.api.world.DimensionTypes;
import org.spongepowered.api.world.GeneratorTypes;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.gen.WorldGeneratorModifier;
import java.io.File;
import java.util.Collection;
import java.util.UUID;
import com.google.common.base.Optional;
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.generator.PlotGenerator;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.PlotManager;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.util.AbstractTitle;
import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.ConsoleColors;
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;
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.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;
import com.plotsquared.sponge.util.SpongeUtil;
import com.plotsquared.sponge.uuid.SpongeLowerOfflineUUIDWrapper;
import com.plotsquared.sponge.uuid.SpongeOnlineUUIDWrapper;
import com.plotsquared.sponge.uuid.SpongeUUIDHandler;
/**
* Created by robin on 01/11/2014
@ -70,7 +118,68 @@ public class SpongeMain implements IPlotMain, PluginContainer {
}
public Object getPlugin() {
return this.plugin;
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;
}
/////////
@ -114,43 +223,159 @@ public class SpongeMain implements IPlotMain, PluginContainer {
log("INIT");
THIS = this;
// resolver
//
resolver = game.getServiceManager().provide(GameProfileResolver.class).get();
plugin = game.getPluginManager().getPlugin("PlotSquared").get().getInstance();
log("PLUGIN IS THIS: " + (plugin == this));
plugin = this;
server = game.getServer();
//
PS.instance = new PS(this);
// Set the generators for each world...
server = game.getServer();
Collection<World> worlds = server.getWorlds();
if (worlds.size() > 0) {
log("INJECTING WORLDS!!!!!!!");
for (World world : server.getWorlds()) {
log("INJECTING WORLD: " + world.getName());
world.setWorldGenerator(new SpongePlotGenerator(world.getName()));
}
}
// TODO Until P^2 has json chat stuff for sponge, disable this
Settings.FANCY_CHAT = false;
// done
registerBlocks();
ConfigurationSection worldSection = PS.get().config.getConfigurationSection("worlds");
if (worldSection != null) {
for (String world : worldSection.getKeys(false)) {
this.modify = new WorldModify(this);
SpongeBasicGen generator = new SpongeBasicGen(world);
PS.get().loadWorld(world, new SpongeGeneratorWrapper(world, generator));
this.modify = new WorldModify(generator);
Game game = event.getGame();
game.getRegistry().registerWorldGeneratorModifier(modify);
game.getRegistry().getWorldBuilder()
Optional<World> builder = game.getRegistry().getWorldBuilder()
.name(world)
.enabled(true)
.loadsOnStartup(true)
.keepsSpawnLoaded(true)
.dimensionType(DimensionTypes.OVERWORLD)
.generator(GeneratorTypes.DEBUG)
.generator(GeneratorTypes.FLAT)
.gameMode(GameModes.CREATIVE)
.usesMapFeatures(false)
.generatorModifiers(modify)
.build();
World worldObj = builder.get();
}
}
}
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][];
blockMapReverse = new ConcurrentHashMap<BlockState, PlotBlock>();
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;
}
state.getManipulator(StoneData.class);
}
}
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();
}
}
@Subscribe
public void onPlayerChat(PlayerChatEvent event) {
// This is how events sort of work?
@ -160,8 +385,15 @@ public class SpongeMain implements IPlotMain, PluginContainer {
@Override
public void log(String message) {
message = ConsoleColors.fromString(message);
logger.info(message);
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));
}
@Override
@ -191,6 +423,66 @@ public class SpongeMain implements IPlotMain, PluginContainer {
String[] split = version.split("\\.");
return new int[] { Integer.parseInt(split[0]), Integer.parseInt(split[1]), (split.length == 3) ? Integer.parseInt(split[2]) : 0 };
}
@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")) {
return new SpongeGeneratorWrapper(world, null);
}
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;
}
@Override
public BlockManager initBlockManager() {
return new SpongeBlockManager();
}
@Override
public EventUtil initEventUtil() {
return new SpongeEventUtil();
}
@Override
public ChunkManager initChunkManager() {
return new SpongeChunkManager();
}
@Override
public SetupUtils initSetupUtils() {
// TODO Auto-generated method stub
return null;
}
@Override
public HybridUtils initHybridUtils() {
// TODO Auto-generated method stub
return null;
}
@Override
public SchematicHandler initSchematicHandler() {
// TODO Auto-generated method stub
return null;
}
@Override
public TaskManager getTaskManager() {
@ -199,20 +491,17 @@ public class SpongeMain implements IPlotMain, PluginContainer {
@Override
public void runEntityTask() {
// TODO Auto-generated method stub
log("runEntityTask is not implemented!");
new KillRoadMobs().run();
}
@Override
public void registerCommands() {
// TODO Auto-generated method stub
log("registerCommands is not implemented!");
getGame().getCommandDispatcher().register(plugin, new SpongeCommand(), "plots", "p", "plot", "ps", "plotsquared", "p2");
}
@Override
public void registerPlayerEvents() {
// TODO Auto-generated method stub
log("registerPlayerEvents is not implemented!");
game.getEventManager().register(this, new MainListener());
}
@Override
@ -245,42 +534,6 @@ public class SpongeMain implements IPlotMain, PluginContainer {
log("registerTNTListener is not implemented!");
}
@Override
public EconHandler getEconomyHandler() {
// TODO Auto-generated method stub
return null;
}
@Override
public BlockManager initBlockManager() {
// TODO Auto-generated method stub
return null;
}
@Override
public EventUtil initEventUtil() {
// TODO Auto-generated method stub
return null;
}
@Override
public ChunkManager initChunkManager() {
// TODO Auto-generated method stub
return null;
}
@Override
public SetupUtils initSetupUtils() {
// TODO Auto-generated method stub
return null;
}
@Override
public HybridUtils initHybridUtils() {
// TODO Auto-generated method stub
return null;
}
@Override
public UUIDHandlerImplementation initUUIDHandler() {
UUIDWrapper wrapper;
@ -293,57 +546,33 @@ public class SpongeMain implements IPlotMain, PluginContainer {
return new SpongeUUIDHandler(wrapper);
}
@Override
public InventoryUtil initInventoryUtil() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean initPlotMeConverter() {
// TODO Auto-generated method stub
PS.log("initPlotMeConverter NOT IMPLEMENTED YET");
return false;
}
@Override
public void unregister(PlotPlayer player) {
// TODO Auto-generated method stub
}
@Override
public SpongeGeneratorWrapper getGenerator(String world, String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public APlotListener initPlotListener() {
// TODO Auto-generated method stub
return null;
SpongeUtil.removePlayer(player.getName());
}
@Override
public void registerChunkProcessor() {
// TODO Auto-generated method stub
PS.log("registerChunkProcessor NOT IMPLEMENTED YET");
}
@Override
public void registerWorldEvents() {
// TODO Auto-generated method stub
}
@Override
public PlayerManager initPlayerManager() {
// TODO Auto-generated method stub
return null;
PS.log("registerWorldEvents NOT IMPLEMENTED YET");
}
@Override
public String getServerName() {
// TODO FIXME
// TODO FIXME
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
}
@ -360,7 +589,11 @@ public class SpongeMain implements IPlotMain, PluginContainer {
@Override
public void setGenerator(String world) {
// TODO Auto-generated method stub
// THIS IS DONE DURING STARTUP ALREADY
}
@Override
public AbstractTitle initTitleManager() {
return new SpongeTitleManager();
}
}