diff --git a/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 101bb9729..e12b5ab7e 100644 --- a/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -359,7 +359,7 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { } } }, 20); - return (ChunkGenerator) result.specify(world); + return (ChunkGenerator) result.specify(); } @Override @@ -690,7 +690,7 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { } @Override - public GeneratorWrapper wrapPlotGenerator(String world, IndependentPlotGenerator generator) { + public GeneratorWrapper wrapPlotGenerator(IndependentPlotGenerator generator) { return new BukkitPlotGenerator(generator); } diff --git a/src/main/java/com/plotsquared/sponge/SpongeHybridUtils.java b/src/main/java/com/plotsquared/sponge/SpongeHybridUtils.java index 45daf2aac..d4072ef1e 100644 --- a/src/main/java/com/plotsquared/sponge/SpongeHybridUtils.java +++ b/src/main/java/com/plotsquared/sponge/SpongeHybridUtils.java @@ -23,7 +23,7 @@ public class SpongeHybridUtils extends HybridUtils { for (int x = x1; x <= x2; x++) { for (int z = z1; z <= z2; z++) { final BlockState state = world.getBlock(x, y, z); - final PlotBlock block = SpongeMain.THIS.getPlotBlock(state); + final PlotBlock block = SpongeUtil.getPlotBlock(state); boolean same = false; for (final PlotBlock p : blocks) { if (block.id == p.id) { diff --git a/src/main/java/com/plotsquared/sponge/SpongeMain.java b/src/main/java/com/plotsquared/sponge/SpongeMain.java index 6ba200879..cb97539a2 100644 --- a/src/main/java/com/plotsquared/sponge/SpongeMain.java +++ b/src/main/java/com/plotsquared/sponge/SpongeMain.java @@ -1,27 +1,14 @@ package com.plotsquared.sponge; import java.io.File; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -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.Optional; import java.util.UUID; import org.slf4j.Logger; -import org.spongepowered.api.CatalogType; import org.spongepowered.api.Game; 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.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.game.state.GameAboutToStartServerEvent; @@ -31,11 +18,9 @@ import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.profile.GameProfileManager; import org.spongepowered.api.text.Text; -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.GenerationPopulator; +import org.spongepowered.api.world.gen.WorldGenerator; import org.spongepowered.api.world.gen.WorldGeneratorModifier; import com.google.inject.Inject; @@ -43,30 +28,34 @@ 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.ConfigurationNode; import com.intellectualcrafters.plot.config.Settings; +import com.intellectualcrafters.plot.generator.GeneratorWrapper; +import com.intellectualcrafters.plot.generator.HybridGen; import com.intellectualcrafters.plot.generator.HybridUtils; -import com.intellectualcrafters.plot.object.PlotArea; -import com.intellectualcrafters.plot.object.PlotBlock; +import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.object.SetupObject; import com.intellectualcrafters.plot.util.AbstractTitle; import com.intellectualcrafters.plot.util.ChatManager; 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.PlotQueue; import com.intellectualcrafters.plot.util.SchematicHandler; import com.intellectualcrafters.plot.util.SetupUtils; +import com.intellectualcrafters.plot.util.StringMan; import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.util.UUIDHandlerImplementation; +import com.intellectualcrafters.plot.util.WorldUtil; 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.generator.SpongePlotGenerator; +import com.plotsquared.sponge.listener.ChunkProcessor; import com.plotsquared.sponge.listener.MainListener; +import com.plotsquared.sponge.listener.WorldEvents; import com.plotsquared.sponge.util.KillRoadMobs; -import com.plotsquared.sponge.util.SpongeBlockManager; import com.plotsquared.sponge.util.SpongeChatManager; import com.plotsquared.sponge.util.SpongeChunkManager; import com.plotsquared.sponge.util.SpongeCommand; @@ -77,6 +66,9 @@ import com.plotsquared.sponge.util.SpongeMetrics; import com.plotsquared.sponge.util.SpongeTaskManager; import com.plotsquared.sponge.util.SpongeTitleManager; import com.plotsquared.sponge.util.SpongeUtil; +import com.plotsquared.sponge.util.SpongeWorldUtil; +import com.plotsquared.sponge.util.block.FastQueue; +import com.plotsquared.sponge.util.block.SlowQueue; import com.plotsquared.sponge.uuid.SpongeLowerOfflineUUIDWrapper; import com.plotsquared.sponge.uuid.SpongeOnlineUUIDWrapper; import com.plotsquared.sponge.uuid.SpongeUUIDHandler; @@ -97,9 +89,6 @@ public class SpongeMain implements IPlotMain, PluginContainer { private GameProfileManager resolver; - private WorldModify modify; - - // stuff // @Override public Logger getLogger() { return logger; @@ -121,69 +110,6 @@ public class SpongeMain implements IPlotMain, PluginContainer { return THIS; } - public Text getText(final 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(final Locale l, final Object... args) { - return m; - } - - @Override - public String get(final Locale l) { - return m; - } - }; - } - }; - } - - private final PlotBlock NULL_BLOCK = new PlotBlock((short) 0, (byte) 0); - private BlockState[][] blockMap; - private Map blockMapReverse; - - public BlockState getBlockState(final 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(final int id) { - return blockMap[id][0]; - } - - public Collection getAllStates() { - return blockMapReverse.keySet(); - } - - public PlotBlock getPlotBlock(final BlockState state) { - final PlotBlock val = blockMapReverse.get(state); - if (val == null) { - return NULL_BLOCK; - } - return val; - } - - ///////// - - ////////////////////// SPONGE PLUGIN REGISTRATION //////////////////// @Override public String getId() { return "PlotSquared"; @@ -211,188 +137,25 @@ public class SpongeMain implements IPlotMain, PluginContainer { return result; } - /////////////////////////////////////////////////////////////////////// - - ///////////////////// ON ENABLE ///////////////////// @Listener public void init(final GameInitializationEvent event) { - log("P^2 INIT"); + log("PlotSquared: Game init"); } @Listener public void onInit(final GamePreInitializationEvent event) { - // Hook for Project Worlds - hookProjectWorlds(); + log("PlotSquared: Game pre init"); } - public void hookProjectWorlds() { - Optional plugin = game.getPluginManager().getPlugin("Project Worlds"); - if (plugin.isPresent()) { - try { - Class clazz = Class.forName("com.gmail.trentech.pjw.modifiers.Modifiers"); - Method method = clazz.getMethod("put", String.class, WorldGeneratorModifier.class); - SpongeBasicGen generator = new SpongeBasicGen(null); - method.invoke(null, "plotsquared", new WorldModify(generator, false)); - log("Adding plotsquared modifier to Project Worlds"); - } - catch (Throwable e) { - e.printStackTrace(); - } - } - } - @Listener public void onServerAboutToStart(final GameAboutToStartServerEvent event) { - log("P^2 ABOUT START"); + log("PlotSquared: Server init"); THIS = this; - - // resolver = game.getServiceManager().provide(GameProfileManager.class).get(); server = game.getServer(); - // - + game.getRegistry().register(WorldGeneratorModifier.class, (WorldGeneratorModifier) new HybridGen().specify()); new PS(this, "Sponge"); - registerBlocks(); - - final ConfigurationSection worldSection = PS.get().config.getConfigurationSection("worlds"); - if (worldSection != null) { - for (final String world : worldSection.getKeys(false)) { - createWorldFromConfig(world); - } - } - } - - public World createWorldFromConfig(final String world) { - final SpongeBasicGen generator = new SpongeBasicGen(world); - final PlotArea 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: { - modify = new WorldModify(generator, false); - game.getRegistry().registerWorldGeneratorModifier(modify); - final Optional builder = game.getRegistry().createBuilder(WorldBuilder.class).name(world).enabled(true).loadsOnStartup(true).keepsSpawnLoaded(true).dimensionType(DimensionTypes.OVERWORLD) - .generator(GeneratorTypes.FLAT).usesMapFeatures(false).generatorModifiers(modify).build(); - return builder.get(); - } - // Augmented - default: { - modify = new WorldModify(generator, true); - game.getRegistry().registerWorldGeneratorModifier(modify); - final Optional builder = game.getRegistry().createBuilder(WorldBuilder.class).name(world).enabled(true).loadsOnStartup(true).keepsSpawnLoaded(true).dimensionType(DimensionTypes.OVERWORLD) - .generator(GeneratorTypes.OVERWORLD).usesMapFeatures(false).generatorModifiers(modify).build(); - return builder.get(); - } - } - } - - public void registerBlock(final PlotBlock block, final BlockState state) { - final 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(final BlockState state) { - final 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); - } - final PlotBlock block = new PlotBlock((short) 0, data); - registerBlock(block, state); - return block; - } - - public void registerBlocks() { - blockMap = new BlockState[256][]; - blockMapReverse = new HashMap(); - final HashMap states = new HashMap<>(); - - PS.get().copyFile("ids.txt", "config"); - PS.get().copyFile("data.txt", "config"); - - try { - final File id_file = new File(getDirectory(), "config" + File.separator + "ids.txt"); - final List id_lines = Files.readAllLines(id_file.toPath(), StandardCharsets.UTF_8); - - final File data_file = new File(getDirectory(), "config" + File.separator + "data.txt"); - final List data_lines = Files.readAllLines(data_file.toPath(), StandardCharsets.UTF_8); - - Field[] fields = BlockTypes.class.getDeclaredFields(); - for (final Field field : fields) { - final BlockType type = (BlockType) field.get(null); - final BlockState state = type.getDefaultState(); - if (state != null) { - try { - states.put(type.getId() + ":" + 0, state); - } catch (final Exception e) {} - } - } - final String packaze = "org.spongepowered.api.data.type."; - for (int i = 0; i < data_lines.size(); i++) { - final String classname = packaze + data_lines.get(i).trim(); - try { - final Class clazz = Class.forName(classname); - fields = clazz.getDeclaredFields(); - for (final Field field : fields) { - final CatalogType type = (CatalogType) field.get(null); - final String minecraft_id = type.getId(); - final BlockState state = states.get(minecraft_id + ":" + 0); - if (state == null) { - continue; - } - } - } catch (final Throwable e) {} - } - - PlotBlock block = null; - for (int i = 0; i < id_lines.size(); i++) { - final String line = id_lines.get(i).trim(); - switch (i % 3) { - case 0: { - block = Configuration.BLOCK.parseString(line); - break; - } - case 1: { - break; - } - case 2: { - final String minecraft_id = line; - final BlockState state = states.remove(minecraft_id + ":" + block.data); - if (state == null) { - continue; - } - registerBlock(block, state); - break; - } - } - } - for (final Entry state : states.entrySet()) { - log("REGISTERING: " + registerBlock(state.getValue()) + " | " + state.getValue().getType()); - } - } catch (final Exception e) { - e.printStackTrace(); - } } @Override @@ -405,7 +168,7 @@ public class SpongeMain implements IPlotMain, PluginContainer { logger.info(message); return; } - server.getConsole().sendMessage(Texts.of(message)); + server.getConsole().sendMessage(Text.of(message)); } @Override @@ -446,28 +209,11 @@ public class SpongeMain implements IPlotMain, PluginContainer { return new SpongeInventoryUtil(); } - @Override - public SpongeGeneratorWrapper getGenerator(final String world, final String name) { - if (name == null) { - return new SpongeGeneratorWrapper(world, null); - } - if (name.equals("PlotSquared")) { - return new SpongeGeneratorWrapper(world, new SpongeBasicGen(world)); - } else { - throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); - } - } - @Override public EconHandler getEconomyHandler() { - // TODO Auto-generated method stub - // Nothing like Vault exists yet - return new SpongeEconHandler(); - } - - @Override - public BlockManager initBlockManager() { - return new SpongeBlockManager(); + SpongeEconHandler econ = new SpongeEconHandler(); + game.getEventManager().registerListeners(this, econ); + return econ; } @Override @@ -517,8 +263,7 @@ public class SpongeMain implements IPlotMain, PluginContainer { @Override public void registerInventoryEvents() { - // TODO Auto-generated method stub - log("registerInventoryEvents is not implemented!"); + // Part of PlayerEvents - can be moved if necessary } @Override @@ -556,8 +301,7 @@ public class SpongeMain implements IPlotMain, PluginContainer { @Override public boolean initPlotMeConverter() { - // TODO Auto-generated method stub - PS.log("initPlotMeConverter NOT IMPLEMENTED YET"); + // PlotMe was never ported to sponge return false; } @@ -568,19 +312,16 @@ public class SpongeMain implements IPlotMain, PluginContainer { @Override public void registerChunkProcessor() { - // TODO Auto-generated method stub - PS.log("registerChunkProcessor NOT IMPLEMENTED YET"); + game.getEventManager().registerListeners(this, new ChunkProcessor()); } @Override public void registerWorldEvents() { - // TODO Auto-generated method stub - PS.log("registerWorldEvents NOT IMPLEMENTED YET"); + game.getEventManager().registerListeners(this, new WorldEvents()); } @Override public String getServerName() { - // TODO FIXME throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); } @@ -596,8 +337,44 @@ public class SpongeMain implements IPlotMain, PluginContainer { } @Override - public void setGenerator(final String world) { - // TODO THIS IS DONE DURING STARTUP ALREADY + 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); + } + } } @Override @@ -610,24 +387,67 @@ public class SpongeMain implements IPlotMain, PluginContainer { 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); } + // TODO FIXME offline player return null; } @Override public String getNMSPackage() { - return "1_8_R3"; + return "TODO";//TODO FIXME } @Override public ChatManager initChatManager() { return new SpongeChatManager(); } + + @Override + public PlotQueue initPlotQueue() { + if (PS.get().checkVersion(getServerVersion(), 1, 8, 0)) { + try { + return new FastQueue(); + } catch (Throwable e) { + e.printStackTrace(); + } + } + return new SlowQueue(); + } + + @Override + public WorldUtil initWorldUtil() { + return new SpongeWorldUtil(); + } + + @Override + public GeneratorWrapper getGenerator(String world, String name) { + if (name == null) { + return null; + } + Collection 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 getPluginIds() { + // TODO Auto-generated method stub + return null; + } } diff --git a/src/main/java/com/plotsquared/sponge/SpongeSchematicHandler.java b/src/main/java/com/plotsquared/sponge/SpongeSchematicHandler.java index a9fbbc1a2..ebfc1e512 100644 --- a/src/main/java/com/plotsquared/sponge/SpongeSchematicHandler.java +++ b/src/main/java/com/plotsquared/sponge/SpongeSchematicHandler.java @@ -1,5 +1,14 @@ package com.plotsquared.sponge; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Set; + +import org.spongepowered.api.block.BlockState; +import org.spongepowered.api.world.World; + import com.intellectualcrafters.jnbt.ByteArrayTag; import com.intellectualcrafters.jnbt.CompoundTag; import com.intellectualcrafters.jnbt.IntTag; @@ -16,14 +25,6 @@ import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.SchematicHandler; import com.intellectualcrafters.plot.util.TaskManager; import com.plotsquared.sponge.util.SpongeUtil; -import org.spongepowered.api.block.BlockState; -import org.spongepowered.api.world.World; - -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Set; public class SpongeSchematicHandler extends SchematicHandler { @@ -144,10 +145,7 @@ public class SpongeSchematicHandler extends SchematicHandler { final int index = i2 + rx; final BlockState state = worldObj.getBlock(x, y, z); - PlotBlock block = SpongeMain.THIS.getPlotBlock(state); - if (block == null) { - block = SpongeMain.THIS.registerBlock(state); - } + PlotBlock block = SpongeUtil.getPlotBlock(state); final int id = block.id; switch (id) { case 0: @@ -280,4 +278,11 @@ public class SpongeSchematicHandler extends SchematicHandler { }); } + @Override + public void restoreTag(CompoundTag ct, short x, short y, short z, Schematic schem) { + // TODO Auto-generated method stub + // This method should place the compound tag at a location e.g. chest contents + throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); + } + } diff --git a/src/main/java/com/plotsquared/sponge/SpongeSetupUtils.java b/src/main/java/com/plotsquared/sponge/SpongeSetupUtils.java index d927e2968..d89d175fa 100644 --- a/src/main/java/com/plotsquared/sponge/SpongeSetupUtils.java +++ b/src/main/java/com/plotsquared/sponge/SpongeSetupUtils.java @@ -1,22 +1,19 @@ package com.plotsquared.sponge; -import com.intellectualcrafters.plot.PS; -import com.intellectualcrafters.plot.config.ConfigurationNode; -import com.intellectualcrafters.plot.generator.PlotGenerator; -import com.intellectualcrafters.plot.object.PlotArea; -import com.intellectualcrafters.plot.object.PlotCluster; -import com.intellectualcrafters.plot.object.SetupObject; -import com.intellectualcrafters.plot.util.SetupUtils; -import com.plotsquared.sponge.generator.AugmentedPopulator; -import com.plotsquared.sponge.generator.SpongeBasicGen; -import com.plotsquared.sponge.generator.SpongeGeneratorWrapper; -import com.plotsquared.sponge.generator.SpongePlotGenerator; -import com.plotsquared.sponge.util.SpongeUtil; +import java.io.IOException; + import org.spongepowered.api.world.World; import org.spongepowered.api.world.gen.WorldGenerator; -import java.io.IOException; -import java.util.Map.Entry; +import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.config.ConfigurationNode; +import com.intellectualcrafters.plot.generator.HybridGen; +import com.intellectualcrafters.plot.generator.PlotGenerator; +import com.intellectualcrafters.plot.object.PlotArea; +import com.intellectualcrafters.plot.object.SetupObject; +import com.intellectualcrafters.plot.util.SetupUtils; +import com.plotsquared.sponge.generator.SpongePlotGenerator; +import com.plotsquared.sponge.util.SpongeUtil; public class SpongeSetupUtils extends SetupUtils { @@ -25,9 +22,9 @@ public class SpongeSetupUtils extends SetupUtils { if (!SetupUtils.generators.isEmpty()) { return; } + SetupUtils.generators.put("PlotSquared", new SpongePlotGenerator(new HybridGen())); // TODO get external world generators - final String testWorld = "CheckingPlotSquaredGenerator"; - SetupUtils.generators.put("PlotSquared", new SpongeGeneratorWrapper(testWorld, new SpongeBasicGen(testWorld))); + throw new UnsupportedOperationException("TODO FETCH EXTERNAL WorldGenerationModifiers"); } @Override @@ -40,15 +37,7 @@ public class SpongeSetupUtils extends SetupUtils { return null; } final WorldGenerator generator = world.getWorldGenerator(); - if (!(generator instanceof SpongePlotGenerator)) { - return null; - } - for (final Entry> entry : generators.entrySet()) { - if (entry.getValue().generator.getClass().getName().equals(generator.getClass().getName())) { - return entry.getKey(); - } - } - return null; + throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); } @Override @@ -76,12 +65,8 @@ public class SpongeSetupUtils extends SetupUtils { } catch (final IOException e) { e.printStackTrace(); } - SpongeMain.THIS.createWorldFromConfig(world); - return object.world; - } - - @Override - public void removePopulator(final String world, final PlotCluster cluster) { - AugmentedPopulator.removePopulator(world, cluster); + // TODO FIXME + throw new UnsupportedOperationException("NOT IMPLEMENTED YET: Create a new world here"); + // return object.world; } } diff --git a/src/main/java/com/plotsquared/sponge/generator/AugmentedPopulator.java b/src/main/java/com/plotsquared/sponge/generator/AugmentedPopulator.java deleted file mode 100644 index e2f897b47..000000000 --- a/src/main/java/com/plotsquared/sponge/generator/AugmentedPopulator.java +++ /dev/null @@ -1,334 +0,0 @@ -package com.plotsquared.sponge.generator; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map.Entry; -import java.util.Random; - -import org.spongepowered.api.block.BlockState; -import org.spongepowered.api.block.BlockType; -import org.spongepowered.api.util.DiscreteTransform3; -import org.spongepowered.api.world.Chunk; -import org.spongepowered.api.world.World; -import org.spongepowered.api.world.extent.ImmutableBlockVolume; -import org.spongepowered.api.world.extent.MutableBlockVolume; -import org.spongepowered.api.world.extent.StorageType; -import org.spongepowered.api.world.extent.UnmodifiableBlockVolume; -import org.spongepowered.api.world.gen.GenerationPopulator; -import org.spongepowered.api.world.gen.Populator; -import org.spongepowered.api.world.gen.WorldGenerator; - -import com.flowpowered.math.vector.Vector3i; -import com.intellectualcrafters.plot.object.Location; -import com.intellectualcrafters.plot.object.PlotArea; -import com.intellectualcrafters.plot.object.PlotBlock; -import com.intellectualcrafters.plot.object.PlotCluster; -import com.intellectualcrafters.plot.object.PlotId; -import com.intellectualcrafters.plot.object.PlotLoc; -import com.intellectualcrafters.plot.object.PlotManager; -import com.intellectualcrafters.plot.util.ChunkManager; -import com.intellectualcrafters.plot.util.SetQueue; -import com.intellectualcrafters.plot.util.TaskManager; -import com.plotsquared.sponge.SpongeMain; -import com.plotsquared.sponge.util.SpongeUtil; - -public class AugmentedPopulator implements Populator { - - public final PlotArea plotworld; - public final PlotManager manager; - public final SpongePlotGenerator generator; - public final GenerationPopulator populator; - public final PlotCluster cluster; - public final Random r = new Random(); - public final boolean p; - public final boolean b; - public final boolean o; - private final int bx; - private final int bz; - private final int tx; - private final int tz; - - public AugmentedPopulator(final String worldname, final WorldGenerator gen, final SpongePlotGenerator generator, final PlotCluster cluster, final boolean p, final boolean b) { - // MainUtil.initCache(); - // Initialize any chach that's needed - this.cluster = cluster; - this.generator = generator; - populator = generator.getBaseGenerationPopulator(); - plotworld = generator.getNewPlotWorld(worldname); - manager = generator.getPlotManager(); - this.p = p; - this.b = b; - o = (plotworld.TERRAIN == 1) || (plotworld.TERRAIN == 2); - if (cluster != null) { - final Location bl = manager.getPlotBottomLocAbs(plotworld, cluster.getP1()).subtract(1, 0, 1); - final Location tl = manager.getPlotTopLocAbs(plotworld, cluster.getP2()).add(1, 0, 1); - bx = bl.getX(); - bz = bl.getZ(); - tx = tl.getX(); - tz = tl.getZ(); - } else { - bx = Integer.MIN_VALUE; - bz = Integer.MIN_VALUE; - tx = Integer.MAX_VALUE; - tz = Integer.MAX_VALUE; - } - // Add the populator - final List populators = gen.getPopulators(); - if (o) { - populators.add(0, this); - } else { - populators.add(this); - } - } - - public static void removePopulator(final String worldname, final PlotCluster cluster) { - final World world = SpongeUtil.getWorld(worldname); - final List populators = world.getWorldGenerator().getPopulators(); - for (final Iterator iterator = populators.iterator(); iterator.hasNext();) { - final Populator populator = iterator.next(); - if (populator instanceof AugmentedPopulator) { - if (((AugmentedPopulator) populator).cluster.equals(cluster)) { - iterator.remove(); - } - } - } - } - - @Override - public void populate(final Chunk chunk, final Random r_unused) { - final Vector3i min = chunk.getBlockMin(); - final World worldObj = chunk.getWorld(); - final String world = worldObj.getName(); - final int cx = min.getX() >> 4; - final int cz = min.getZ() >> 4; - final int bx = cx << 4; - final int bz = cz << 4; - final int tx = bx + 15; - final int tz = bz + 15; - final boolean inX1 = ((bx >= this.bx) && (bx <= this.tx)); - final boolean inX2 = ((tx >= this.bx) && (tx <= this.tx)); - final boolean inZ1 = ((bz >= this.bz) && (bz <= this.tz)); - final boolean inZ2 = ((tz >= this.bz) && (tz <= this.tz)); - final boolean inX = inX1 || inX2; - final boolean inZ = inZ1 || inZ2; - if (!inX || !inZ) { - return; - } - if (plotworld.TERRAIN == 3) { - final int X = min.getX(); - final int Z = min.getZ(); - if (ChunkManager.FORCE_PASTE) { - for (short x = 0; x < 16; x++) { - for (short z = 0; z < 16; z++) { - final PlotLoc loc = new PlotLoc((short) (X + x), (short) (Z + z)); - final HashMap blocks = ChunkManager.GENERATE_BLOCKS.get(loc); - final HashMap datas = ChunkManager.GENERATE_DATA.get(loc); - for (final Entry entry : blocks.entrySet()) { - final int y = entry.getKey(); - if (datas != null) { - SetQueue.IMP.setBlock(world, x, y, z, new PlotBlock(blocks.get(y), datas.get(y))); - } else { - SetQueue.IMP.setBlock(world, x, y, z, blocks.get(y)); - } - } - } - } - return; - } - if (ChunkManager.CURRENT_PLOT_CLEAR != null) { - PlotLoc loc; - for (final Entry> entry : ChunkManager.GENERATE_DATA.entrySet()) { - final HashMap datas = ChunkManager.GENERATE_DATA.get(entry.getKey()); - for (final Entry entry2 : entry.getValue().entrySet()) { - final Short y = entry2.getKey(); - byte data; - if (datas != null) { - data = datas.get(y); - } else { - data = 0; - } - loc = entry.getKey(); - final int xx = loc.x - X; - final int zz = loc.z - Z; - if ((xx >= 0) && (xx < 16)) { - if ((zz >= 0) && (zz < 16)) { - SetQueue.IMP.setBlock(world, xx, y, zz, new PlotBlock(entry2.getValue(), data)); - } - } - } - } - } - return; - } - final boolean check; - check = !inX1 || !inX2 || !inZ1 || !inZ2; - if (plotworld.TERRAIN > 1) { - final PlotId plot1 = manager.getPlotIdAbs(plotworld, bx, 0, bz); - final PlotId plot2 = manager.getPlotIdAbs(plotworld, tx, 0, tz); - if ((plot1 != null) && (plot2 != null) && plot1.equals(plot2)) { - return; - } - } - if (o) { - populateBlocks(worldObj, chunk, cx, cz, bx, bz, check); - } else { - TaskManager.runTaskLater(new Runnable() { - @Override - public void run() { - // TODO populate biome - } - }, 20 + r.nextInt(10)); - TaskManager.runTaskLater(new Runnable() { - @Override - public void run() { - populateBlocks(worldObj, chunk, cx, cz, bx, bz, check); - } - }, 40 + r.nextInt(40)); - } - } - - private void populateBlocks(final World world, final Chunk chunk, final int X, final int Z, final int x, final int z, final boolean check) { - final String worldname = world.getName(); - final MutableBlockVolume blocks = new MutableBlockVolume() { - @Override - public void setBlock(final int x, final int y, final int z, final BlockState t) { - if (check && (((z) < bz) || ((z) > tz) || ((x) < bx) || ((x) > tx))) { - return; - } - if (p) { - if (ChunkManager.CURRENT_PLOT_CLEAR != null) { - if (ChunkManager.CURRENT_PLOT_CLEAR.isIn(x, z)) { - return; - } - } else if (manager.getPlotIdAbs(plotworld, x, 0, z) != null) { - return; - } - } - final PlotBlock block = SpongeMain.THIS.getPlotBlock(t); - if (block != null) { - SetQueue.IMP.setBlock(worldname, x, y, z, block); - } - } - - @Override - public void setBlock(final Vector3i v, final BlockState t) { - setBlock(v.getX(), v.getY(), v.getZ(), t); - } - - @Override - public BlockType getBlockType(final int x, final int y, final int z) { - return world.getBlockType(x, y, z); - } - - @Override - public BlockType getBlockType(final Vector3i v) { - return getBlockType(v.getX(), v.getY(), v.getZ()); - } - - @Override - public Vector3i getBlockSize() { - return chunk.getBlockSize(); - } - - @Override - public Vector3i getBlockMin() { - return chunk.getBlockMin(); - } - - @Override - public Vector3i getBlockMax() { - return chunk.getBlockMax(); - } - - @Override - public BlockState getBlock(final int x, final int y, final int z) { - return world.getBlock(x, y, z); - } - - @Override - public BlockState getBlock(final Vector3i v) { - return getBlock(v.getX(), v.getY(), v.getZ()); - } - - @Override - public boolean containsBlock(final int x, final int y, final int z) { - return ((x) >= bz) && ((z) <= tz) && ((x) >= bx) && ((z) <= tx); - } - - @Override - public boolean containsBlock(final Vector3i v) { - return containsBlock(v.getX(), v.getY(), v.getZ()); - } - - @Override - public void setBlockType(final int x, final int y, final int z, final BlockType t) { - if (check && (((z) < bz) || ((z) > tz) || ((x) < bx) || ((x) > tx))) { - return; - } - if (p) { - if (ChunkManager.CURRENT_PLOT_CLEAR != null) { - if (ChunkManager.CURRENT_PLOT_CLEAR.isIn(x, z)) { - return; - } - } else if (manager.getPlotIdAbs(plotworld, x, 0, z) != null) { - return; - } - } - final PlotBlock block = SpongeMain.THIS.getPlotBlock(t.getDefaultState()); - if (block != null) { - SetQueue.IMP.setBlock(worldname, x, y, z, block); - } - } - - @Override - public void setBlockType(final Vector3i v, final BlockType t) { - setBlockType(v.getX(), v.getY(), v.getZ(), t); - } - - @Override - public MutableBlockVolume getBlockCopy() { - // TODO Auto-generated method stub - return this; - } - - @Override - public MutableBlockVolume getBlockCopy(final StorageType arg0) { - // TODO Auto-generated method stub - return this; - } - - @Override - public ImmutableBlockVolume getImmutableBlockCopy() { - // TODO Auto-generated method stub - return null; - } - - @Override - public UnmodifiableBlockVolume getUnmodifiableBlockView() { - // TODO Auto-generated method stub - return null; - } - - @Override - public MutableBlockVolume getBlockView(final DiscreteTransform3 arg0) { - // TODO Auto-generated method stub - return this; - } - - @Override - public MutableBlockVolume getBlockView(final Vector3i arg0, final Vector3i arg1) { - // TODO Auto-generated method stub - return this; - } - - @Override - public MutableBlockVolume getRelativeBlockView() { - // TODO Auto-generated method stub - return this; - } - }; - populator.populate(world, blocks, null); - } - -} diff --git a/src/main/java/com/plotsquared/sponge/generator/SpongeAugmentedGenerator.java b/src/main/java/com/plotsquared/sponge/generator/SpongeAugmentedGenerator.java new file mode 100644 index 000000000..77f73718f --- /dev/null +++ b/src/main/java/com/plotsquared/sponge/generator/SpongeAugmentedGenerator.java @@ -0,0 +1,77 @@ +package com.plotsquared.sponge.generator; + +import java.util.List; + +import org.spongepowered.api.world.World; +import org.spongepowered.api.world.extent.ImmutableBiomeArea; +import org.spongepowered.api.world.extent.MutableBlockVolume; +import org.spongepowered.api.world.gen.GenerationPopulator; +import org.spongepowered.api.world.gen.WorldGenerator; + +import com.flowpowered.math.vector.Vector3i; +import com.intellectualcrafters.plot.generator.AugmentedUtils; +import com.intellectualcrafters.plot.object.LazyResult; +import com.intellectualcrafters.plot.util.PlotChunk; +import com.intellectualcrafters.plot.util.SetQueue; +import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper; +import com.plotsquared.sponge.util.SpongeUtil; + +public class SpongeAugmentedGenerator implements GenerationPopulator { + + private static SpongeAugmentedGenerator generator; + + private SpongeAugmentedGenerator() {}; + + public static SpongeAugmentedGenerator get(World world) { + WorldGenerator wg = world.getWorldGenerator(); + List populators = wg.getGenerationPopulators(); + for (GenerationPopulator poplator : populators) { + if (poplator instanceof SpongeAugmentedGenerator) { + return (SpongeAugmentedGenerator) poplator; + } + } + if (generator == null) { + generator = new SpongeAugmentedGenerator(); + } + populators.add(generator); + return generator; + } + + @Override + public void populate(final World world, final MutableBlockVolume terrain, final ImmutableBiomeArea biome) { + Vector3i min = terrain.getBlockMin(); + final int bx = min.getX(); + final int bz = min.getZ(); + final int cx = bx >> 4; + final int cz = bz >> 4; + AugmentedUtils.generate(world.getName(), cx, cz, new LazyResult>() { + @Override + public PlotChunk create() { + ChunkWrapper wrap = SetQueue.IMP.new ChunkWrapper(world.getName(), cx, cz); + return new PlotChunk(wrap) { + @Override + public ChunkWrapper getChunkAbs() { + return getChunkWrapper(); + } + @Override + public void setBlock(int x, int y, int z, int id, byte data) { + terrain.setBlock(bx + x, y, bz + z, SpongeUtil.getBlockState(id, data)); + } + @Override + public void setBiome(int x, int z, int biome) { + world.setBiome(bx + x, bz + z, null); + } + @Override + public PlotChunk clone() { + throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); + } + @Override + public PlotChunk shallowClone() { + throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); + } + }; + } + }); + } + +} diff --git a/src/main/java/com/plotsquared/sponge/generator/SpongeBasicBiomeProvider.java b/src/main/java/com/plotsquared/sponge/generator/SpongeBasicBiomeProvider.java deleted file mode 100644 index a976cb725..000000000 --- a/src/main/java/com/plotsquared/sponge/generator/SpongeBasicBiomeProvider.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.plotsquared.sponge.generator; - -import org.spongepowered.api.world.biome.BiomeType; -import org.spongepowered.api.world.biome.BiomeTypes; -import org.spongepowered.api.world.extent.MutableBiomeArea; -import org.spongepowered.api.world.gen.BiomeGenerator; - -import com.flowpowered.math.vector.Vector2i; -import com.intellectualcrafters.plot.object.PlotArea; - -public class SpongeBasicBiomeProvider implements BiomeGenerator { - - private final PlotArea plotworld; - - public SpongeBasicBiomeProvider(final PlotArea plotworld) { - this.plotworld = plotworld; - } - - @Override - public void generateBiomes(final MutableBiomeArea biomeBase) { - final Vector2i min = biomeBase.getBiomeMin(); - final int bx = min.getX(); - final int bz = min.getY(); - BiomeType biome = BiomeTypes.FOREST; - try { - biome = (BiomeType) BiomeTypes.class.getField(plotworld.PLOT_BIOME.toUpperCase()).get(null); - } catch (final Exception e) { - e.printStackTrace(); - } - for (int x = bx; x < (bx + 16); x++) { - for (int z = bz; z < (bz + 16); z++) { - biomeBase.setBiome(x, z, biome); - } - } - } - -} diff --git a/src/main/java/com/plotsquared/sponge/generator/SpongeBasicGen.java b/src/main/java/com/plotsquared/sponge/generator/SpongeBasicGen.java deleted file mode 100644 index 6e2394349..000000000 --- a/src/main/java/com/plotsquared/sponge/generator/SpongeBasicGen.java +++ /dev/null @@ -1,129 +0,0 @@ -package com.plotsquared.sponge.generator; - -import java.util.ArrayList; -import java.util.List; - -import org.spongepowered.api.block.BlockState; -import org.spongepowered.api.block.BlockTypes; -import org.spongepowered.api.world.gen.BiomeGenerator; - -import com.intellectualcrafters.plot.PS; -import com.intellectualcrafters.plot.generator.HybridPlotManager; -import com.intellectualcrafters.plot.generator.HybridPlotWorld; -import com.intellectualcrafters.plot.object.PlotArea; -import com.intellectualcrafters.plot.object.PlotManager; -import com.plotsquared.sponge.SpongeMain; - -public class SpongeBasicGen extends SpongePlotGenerator { - - public final BlockState AIR = BlockTypes.AIR.getDefaultState(); - - private static HybridPlotManager manager; - public HybridPlotWorld plotworld; - - public SpongeBasicGen(final String world) { - super(world); - } - - /** - * Some generator specific variables (implementation dependent) - */ - public int plotsize; - public int pathsize; - public int size; - public int roadheight; - public int wallheight; - public int plotheight; - public short pathWidthLower; - public short pathWidthUpper; - public boolean doState = false; - - BlockState wall; - BlockState wallfilling; - BlockState roadblock; - BlockState[] plotfloors; - BlockState[] filling; - - @Override - public void init(final PlotArea plotworld) { - if (plotworld != null) { - this.plotworld = (HybridPlotWorld) plotworld; - } - plotsize = this.plotworld.PLOT_WIDTH; - pathsize = this.plotworld.ROAD_WIDTH; - size = pathsize + plotsize; - wallheight = this.plotworld.WALL_HEIGHT; - roadheight = this.plotworld.ROAD_HEIGHT; - plotheight = this.plotworld.PLOT_HEIGHT; - if (pathsize == 0) { - pathWidthLower = (short) -1; - pathWidthUpper = (short) (plotsize + 1); - } else { - if ((pathsize % 2) == 0) { - pathWidthLower = (short) (Math.floor(pathsize / 2) - 1); - } else { - pathWidthLower = (short) (Math.floor(pathsize / 2)); - } - pathWidthUpper = (short) (pathWidthLower + plotsize + 1); - } - roadblock = SpongeMain.THIS.getBlockState(this.plotworld.ROAD_BLOCK); - wallfilling = SpongeMain.THIS.getBlockState(this.plotworld.WALL_FILLING); - wall = SpongeMain.THIS.getBlockState(this.plotworld.WALL_BLOCK); - plotfloors = new BlockState[this.plotworld.TOP_BLOCK.length]; - for (int i = 0; i < this.plotworld.TOP_BLOCK.length; i++) { - plotfloors[i] = SpongeMain.THIS.getBlockState(this.plotworld.TOP_BLOCK[i]); - } - filling = new BlockState[this.plotworld.MAIN_BLOCK.length]; - for (int i = 0; i < this.plotworld.MAIN_BLOCK.length; i++) { - filling[i] = SpongeMain.THIS.getBlockState(this.plotworld.MAIN_BLOCK[i]); - } - if ((filling.length > 1) || (plotfloors.length > 1)) { - doState = true; - } - } - - @Override - public PlotArea getNewPlotWorld(final String world) { - if (plotworld == null) { - plotworld = (HybridPlotWorld) PS.get().getPlotArea(world); - if (plotworld == null) { - plotworld = new HybridPlotWorld(world); - } - } - return plotworld; - } - - @Override - public PlotManager getPlotManager() { - if (SpongeBasicGen.manager == null) { - SpongeBasicGen.manager = new HybridPlotManager(); - } - return SpongeBasicGen.manager; - } - - @Override - public List getPlotPopulators() { - // TODO Auto-generated method stub - return new ArrayList<>(); - } - - private SpongeBasicPop generator; - - @Override - public SpongePlotPopulator getGenerator() { - if (generator == null) { - generator = new SpongeBasicPop(this); - } - return generator; - } - - private BiomeGenerator biome; - - @Override - public BiomeGenerator getPlotBiomeProvider() { - if (biome == null) { - biome = new SpongeBasicBiomeProvider(plotworld); - } - return biome; - } -} diff --git a/src/main/java/com/plotsquared/sponge/generator/SpongeBasicPop.java b/src/main/java/com/plotsquared/sponge/generator/SpongeBasicPop.java deleted file mode 100644 index 6e1d33bfe..000000000 --- a/src/main/java/com/plotsquared/sponge/generator/SpongeBasicPop.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.plotsquared.sponge.generator; - -import java.util.HashMap; -import java.util.HashSet; - -import org.spongepowered.api.world.World; - -import com.intellectualcrafters.plot.object.PlotBlock; -import com.intellectualcrafters.plot.object.PlotLoc; -import com.intellectualcrafters.plot.object.PseudoRandom; -import com.intellectualcrafters.plot.object.RegionWrapper; -import com.intellectualcrafters.plot.object.schematic.PlotItem; -import com.intellectualcrafters.plot.util.WorldUtil; -import com.plotsquared.sponge.SpongeMain; - -public class SpongeBasicPop extends SpongePlotPopulator { - - public SpongeBasicPop(final SpongeBasicGen generator) { - super(generator); - } - - @Override - public void populate(final World world, final RegionWrapper requiredRegion, final PseudoRandom random, final int cx, final int cz) { - int sx = (short) ((X - generator.plotworld.ROAD_OFFSET_X) % generator.size); - int sz = (short) ((Z - generator.plotworld.ROAD_OFFSET_Z) % generator.size); - if (sx < 0) { - sx += generator.size; - } - if (sz < 0) { - sz += generator.size; - } - for (short x = 0; x < 16; x++) { - for (short z = 0; z < 16; z++) { - final int absX = ((sx + x) % generator.size); - final int absZ = ((sz + z) % generator.size); - final boolean gx = absX > generator.pathWidthLower; - final boolean gz = absZ > generator.pathWidthLower; - final boolean lx = absX < generator.pathWidthUpper; - final boolean lz = absZ < generator.pathWidthUpper; - // inside plot - if (gx && gz && lx && lz) { - for (short y = 1; y < generator.plotheight; y++) { - setBlock(x, y, z, generator.filling); - } - setBlock(x, (short) generator.plotheight, z, generator.plotfloors); - if ((generator.plotworld.TYPE != 0) && (generator.plotworld.TERRAIN < 2)) { - for (int y = generator.plotheight + 1; y < 128; y++) { - setBlock(x, y, z, generator.AIR); - } - } - if (generator.plotworld.PLOT_SCHEMATIC) { - final PlotLoc loc = new PlotLoc(absX, absZ); - final HashMap blocks = generator.plotworld.G_SCH.get(loc); - if (blocks != null) { - final HashMap datas = generator.plotworld.G_SCH_DATA.get(loc); - if (datas != null) { - for (final short y : blocks.keySet()) { - final Byte data = datas.get(y); - setBlock(x, (short) (generator.plotheight + y), z, SpongeMain.THIS.getBlockState(new PlotBlock(blocks.get(y), data == null ? 0 : data))); - } - } else { - for (final short y : blocks.keySet()) { - setBlock(x, (short) (generator.plotheight + y), z, SpongeMain.THIS.getBlockState(blocks.get(y))); - } - } - } - if (generator.plotworld.G_SCH_STATE != null) { - final HashSet states = generator.plotworld.G_SCH_STATE.get(loc); - if (states != null) { - for (final PlotItem items : states) { - items.x = X + x; - items.z = Z + z; - WorldUtil.IMP.addItems(generator.plotworld.worldname, items); - } - } - } - } - } else if (generator.pathsize != 0) { - // wall - if (((absX >= generator.pathWidthLower) && (absX <= generator.pathWidthUpper) && (absZ >= generator.pathWidthLower) && (absZ <= generator.pathWidthUpper))) { - for (short y = 1; y <= generator.wallheight; y++) { - setBlock(x, y, z, generator.wallfilling); - } - if ((generator.plotworld.TYPE != 0) && (generator.plotworld.TERRAIN < 3)) { - for (int y = generator.wallheight + 2; y < 128; y++) { - setBlock(x, y, z, generator.AIR); - } - } - if (!generator.plotworld.ROAD_SCHEMATIC_ENABLED) { - setBlock(x, generator.wallheight + 1, z, generator.wall); - } - } - // road - else { - for (short y = 1; y <= generator.roadheight; y++) { - setBlock(x, y, z, generator.roadblock); - } - if ((generator.plotworld.TYPE != 0) && (generator.plotworld.TERRAIN < 3)) { - for (int y = generator.roadheight + 1; y < 128; y++) { - setBlock(x, y, z, generator.AIR); - } - } - } - if (generator.plotworld.ROAD_SCHEMATIC_ENABLED) { - final PlotLoc loc = new PlotLoc(absX, absZ); - final HashMap blocks = generator.plotworld.G_SCH.get(loc); - if (blocks != null) { - final HashMap datas = generator.plotworld.G_SCH_DATA.get(loc); - if (datas != null) { - for (final short y : blocks.keySet()) { - final Byte data = datas.get(y); - setBlock(x, (short) (generator.plotheight + y), z, SpongeMain.THIS.getBlockState(new PlotBlock(blocks.get(y), data == null ? 0 : data))); - } - } else { - for (final short y : blocks.keySet()) { - setBlock(x, (short) (generator.plotheight + y), z, SpongeMain.THIS.getBlockState(blocks.get(y))); - } - } - } - } - } - } - } - } -} diff --git a/src/main/java/com/plotsquared/sponge/generator/SpongeGeneratorWrapper.java b/src/main/java/com/plotsquared/sponge/generator/SpongeGeneratorWrapper.java deleted file mode 100644 index e54c0e050..000000000 --- a/src/main/java/com/plotsquared/sponge/generator/SpongeGeneratorWrapper.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.plotsquared.sponge.generator; - -import org.spongepowered.api.world.World; -import org.spongepowered.api.world.gen.WorldGenerator; - -import com.intellectualcrafters.plot.PS; -import com.intellectualcrafters.plot.generator.PlotGenerator; -import com.intellectualcrafters.plot.object.PlotArea; -import com.intellectualcrafters.plot.object.PlotCluster; -import com.intellectualcrafters.plot.object.PlotManager; -import com.intellectualcrafters.plot.object.SetupObject; -import com.plotsquared.sponge.util.SpongeUtil; - -public class SpongeGeneratorWrapper extends PlotGenerator { - - public final boolean full; - - public SpongeGeneratorWrapper(final String world, final WorldGenerator generator) { - super(world, generator); - full = (generator instanceof SpongePlotGenerator); - } - - @Override - public void initialize(final PlotArea plotworld) { - if (generator instanceof SpongePlotGenerator) { - ((SpongePlotGenerator) generator).init(plotworld); - } - } - - @Override - public void augment(final PlotCluster cluster, final PlotArea plotworld) { - if (generator instanceof SpongePlotGenerator) { - final SpongePlotGenerator plotgen = (SpongePlotGenerator) generator; - final World worldObj = SpongeUtil.getWorld(world); - if (worldObj != null) { - if (cluster != null) { - new AugmentedPopulator(world, worldObj.getWorldGenerator(), plotgen, cluster, plotworld.TERRAIN == 2, plotworld.TERRAIN != 2); - } else { - new AugmentedPopulator(world, worldObj.getWorldGenerator(), plotgen, null, plotworld.TERRAIN == 2, plotworld.TERRAIN != 2); - } - } - } - } - - @Override - public void setGenerator(final String gen_string) { - if (gen_string == null) { - generator = new SpongeBasicGen(world); - } else { - final PlotGenerator gen_wrapper = (PlotGenerator) PS.get().IMP.getGenerator(world, gen_string); - if (gen_wrapper != null) { - generator = gen_wrapper.generator; - } - } - } - - @Override - public PlotArea getNewPlotWorld(final String world) { - if (!(generator instanceof SpongePlotGenerator)) { - return null; - } - return ((SpongePlotGenerator) generator).getNewPlotWorld(world); - } - - @Override - public PlotManager getPlotManager() { - if (!(generator instanceof SpongePlotGenerator)) { - return null; - } - return ((SpongePlotGenerator) generator).getPlotManager(); - } - - @Override - public boolean isFull() { - return full; - } - - @Override - public String getName() { - if (generator == null) { - return "Null"; - } - return generator.getClass().getName(); - } - - @Override - public void processSetup(final SetupObject object) { - if (generator instanceof SpongePlotGenerator) { - ((SpongePlotGenerator) generator).processSetup(object); - } - } - -} diff --git a/src/main/java/com/plotsquared/sponge/generator/SpongePlotGenerator.java b/src/main/java/com/plotsquared/sponge/generator/SpongePlotGenerator.java index 37b3e1c89..880803be4 100644 --- a/src/main/java/com/plotsquared/sponge/generator/SpongePlotGenerator.java +++ b/src/main/java/com/plotsquared/sponge/generator/SpongePlotGenerator.java @@ -1,118 +1,153 @@ package com.plotsquared.sponge.generator; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import org.spongepowered.api.world.biome.BiomeGenerationSettings; +import org.spongepowered.api.data.DataContainer; +import org.spongepowered.api.world.World; +import org.spongepowered.api.world.WorldCreationSettings; import org.spongepowered.api.world.biome.BiomeType; +import org.spongepowered.api.world.extent.ImmutableBiomeArea; +import org.spongepowered.api.world.extent.MutableBiomeArea; +import org.spongepowered.api.world.extent.MutableBlockVolume; import org.spongepowered.api.world.gen.BiomeGenerator; import org.spongepowered.api.world.gen.GenerationPopulator; -import org.spongepowered.api.world.gen.Populator; import org.spongepowered.api.world.gen.WorldGenerator; +import org.spongepowered.api.world.gen.WorldGeneratorModifier; +import com.flowpowered.math.vector.Vector2i; +import com.flowpowered.math.vector.Vector3i; +import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.generator.GeneratorWrapper; +import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; import com.intellectualcrafters.plot.object.PlotArea; import com.intellectualcrafters.plot.object.PlotManager; -import com.intellectualcrafters.plot.object.SetupObject; +import com.intellectualcrafters.plot.object.PseudoRandom; +import com.intellectualcrafters.plot.util.ChunkManager; +import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.SetQueue; +import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper; +import com.plotsquared.sponge.util.SpongeUtil; +import com.plotsquared.sponge.util.block.GenChunk; -public abstract class SpongePlotGenerator implements WorldGenerator { +public class SpongePlotGenerator implements WorldGeneratorModifier, GeneratorWrapper { - public String world; + private final PseudoRandom random = new PseudoRandom(); + private final IndependentPlotGenerator plotGenerator; + private final List populators = new ArrayList<>(); + private final boolean loaded = false; + private PlotManager manager; + private final WorldGeneratorModifier platformGenerator; + private final boolean full; - public SpongePlotGenerator(final String world) { - this.world = world; + public SpongePlotGenerator(IndependentPlotGenerator generator) { + this.plotGenerator = generator; + this.platformGenerator = this; + this.full = true; + MainUtil.initCache(); + } + + public SpongePlotGenerator(WorldGeneratorModifier wgm) { + this.plotGenerator = null; + this.platformGenerator = wgm; + this.full = false; + MainUtil.initCache(); + } + + @Override + public String getId() { + return "PlotSquared"; } @Override - public GenerationPopulator getBaseGenerationPopulator() { - return getGenerator(); + public String getName() { + return "PlotSquared"; } @Override - public BiomeGenerator getBiomeGenerator() { - return getPlotBiomeProvider(); - } - - @Override - public List getPopulators() { - return new ArrayList<>(); - } - - @Override - public void setBiomeGenerator(final BiomeGenerator biomeGenerator) { - // TODO - throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); - } - - @Override - public BiomeGenerationSettings getBiomeSettings(BiomeType type) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); - } - - @Override - public List getGenerationPopulators() { - final List pops = new ArrayList<>(); - pops.addAll(getPlotPopulators()); - return pops; - } - - @Override - public List getGenerationPopulators(Class clazz) { - List list = getGenerationPopulators(); - Iterator iter = list.iterator(); - while (iter.hasNext()) { - GenerationPopulator pop = iter.next(); - if (!clazz.isInstance(pop)) { - iter.remove(); - } + public void modifyWorldGenerator(WorldCreationSettings settings, DataContainer data, WorldGenerator wg) { + if (full) { + final String worldname = settings.getWorldName(); + wg.getGenerationPopulators().clear(); + wg.getPopulators().clear(); + wg.setBaseGenerationPopulator(new GenerationPopulator() { + @Override + public void populate(World world, MutableBlockVolume terrain, ImmutableBiomeArea biome) { + System.out.println("POPULATE!"); + Vector3i size = terrain.getBlockSize(); + if (size.getX() != 16 || size.getZ() != 16) { + throw new UnsupportedOperationException("NON CHUNK POPULATION NOT SUPPORTED"); + } + String worldname = world.getName(); + Vector3i min = terrain.getBlockMin(); + int cx = min.getX() >> 4; + int cz = min.getZ() >> 4; + ChunkWrapper wrap = SetQueue.IMP.new ChunkWrapper(worldname, cx, cz); + // Create the result object + GenChunk result = new GenChunk(terrain, null, wrap); + // Catch any exceptions + try { + // Fill the result data if necessary + if (platformGenerator != SpongePlotGenerator.this) { + throw new UnsupportedOperationException("NOT IMPLEMENTED YET!"); + } else { + // Set random seed + random.state = (cx << 16) | (cz & 0xFFFF); + // Process the chunk + result.modified = false; + ChunkManager.preProcessChunk(result); + if (result.modified) { + return; + } + PlotArea area = PS.get().getPlotArea(world.getName(), null); + plotGenerator.generateChunk(result, area, random); + ChunkManager.postProcessChunk(result); + return; + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + }); + wg.setBiomeGenerator(new BiomeGenerator() { + @Override + public void generateBiomes(MutableBiomeArea buffer) { + PlotArea area = PS.get().getPlotArea(worldname, null); + if (area != null) { + BiomeType biome = SpongeUtil.getBiome(area.PLOT_BIOME); + Vector2i min = buffer.getBiomeMin(); + Vector2i max = buffer.getBiomeMax(); + for (int x = min.getX(); x <= max.getX(); x++) { + for (int z = min.getY(); z <= max.getY(); z++) { + buffer.setBiome(x, z, biome); + } + } + } + } + }); + } else { + throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); } - return list; } @Override - public List getPopulators(Class arg0) { - return new ArrayList<>(); + public IndependentPlotGenerator getPlotGenerator() { + return plotGenerator; } @Override - public void setBaseGenerationPopulator(GenerationPopulator arg0) { - // TODO - throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); - + public WorldGeneratorModifier getPlatformGenerator() { + return platformGenerator; } - - public abstract SpongePlotPopulator getGenerator(); - public abstract BiomeGenerator getPlotBiomeProvider(); + @Override + public void augment(PlotArea area) { + SpongeAugmentedGenerator.get(SpongeUtil.getWorld(area.worldname)); + } - public abstract List getPlotPopulators(); + @Override + public boolean isFull() { + return full; + } - /** - * This is called when the generator is initialized. - * You don't need to do anything with it necessarily. - * @param plotworld - */ - public abstract void init(final PlotArea plotworld); - - /** - * Return a new instance of the PlotArea for a world - * @param world - * @return - */ - public abstract PlotArea getNewPlotWorld(final String world); - - /** - * Get the PlotManager class for this generator - * @return - */ - public abstract PlotManager getPlotManager(); - - /** - * If you need to do anything fancy for /plot setup
- * - Otherwise it will just use the PlotArea configuration
- * Feel free to extend BukkitSetupUtils and customize world creation - * @param object - */ - public void processSetup(final SetupObject object) {} } diff --git a/src/main/java/com/plotsquared/sponge/generator/SpongePlotPopulator.java b/src/main/java/com/plotsquared/sponge/generator/SpongePlotPopulator.java deleted file mode 100644 index 322cd079c..000000000 --- a/src/main/java/com/plotsquared/sponge/generator/SpongePlotPopulator.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.plotsquared.sponge.generator; - -import org.bukkit.block.BlockState; - -import com.intellectualcrafters.plot.object.PseudoRandom; -import com.intellectualcrafters.plot.object.RegionWrapper; -import com.intellectualcrafters.plot.util.ChunkManager; - -public abstract class SpongePlotPopulator implements GenerationPopulator { - - public final T generator; - private final PseudoRandom random = new PseudoRandom(); - public int X; - public int Z; - public String worldname; - private MutableBlockVolume buffer; - - public SpongePlotPopulator(final T generator) { - this.generator = generator; - } - - // @Override - // public void populate(Chunk chunk, Random random) { - // this.world = chunk.getWorld(); - // this.worldname = world.getName(); - // Vector3i min = chunk.getBlockMin(); - - // } - - @Override - public void populate(final World world, final MutableBlockVolume buffer, final ImmutableBiomeArea biomeBase) { - try { - this.worldname = world.getName(); - this.buffer = buffer; - final Vector3i min = buffer.getBlockMin(); - this.X = min.getX(); - this.Z = min.getZ(); - final int cx = X >> 4; - final int cz = Z >> 4; - int h = 1; - final int prime = 13; - h = (prime * h) + cx; - h = (prime * h) + cz; - this.random.state = h; - - // TODO plot clearing stuff - - populate(world, ChunkManager.CURRENT_PLOT_CLEAR, random, cx, cz); - } catch (final Exception e) { - e.printStackTrace(); - } - } - - public abstract void populate(final World world, final RegionWrapper requiredRegion, final PseudoRandom random, final int cx, final int cz); - - /** - * Set the id and data at a location. (x, y, z) must be between [0,15], [0,255], [0,15] - * @param x - * @param y - * @param z - * @param state - */ - public void setBlock(final int x, final int y, final int z, final BlockState state) { - buffer.setBlock(X + x, y, Z + z, state); - } - - public void setBlock(final int x, final int y, final int z, final BlockState[] states) { - if (states.length == 1) { - setBlock(x, y, z, states[0]); - } - setBlock(x, y, z, states[random.random(states.length)]); - } - - /** - * check if a region contains a location. (x, z) must be between [0,15], [0,15] - * @param plot - * @param x - * @param z - * @return - */ - public boolean contains(final RegionWrapper plot, final int x, final int z) { - final int xx = X + x; - final int zz = Z + z; - return ((xx >= plot.minX) && (xx <= plot.maxX) && (zz >= plot.minZ) && (zz <= plot.maxZ)); - } - -} diff --git a/src/main/java/com/plotsquared/sponge/generator/WorldModify.java b/src/main/java/com/plotsquared/sponge/generator/WorldModify.java deleted file mode 100644 index 5613c44ce..000000000 --- a/src/main/java/com/plotsquared/sponge/generator/WorldModify.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.plotsquared.sponge.generator; - -import org.spongepowered.api.data.DataContainer; -import org.spongepowered.api.world.WorldCreationSettings; -import org.spongepowered.api.world.gen.WorldGenerator; -import org.spongepowered.api.world.gen.WorldGeneratorModifier; - -import com.intellectualcrafters.plot.object.PlotArea; -import com.intellectualcrafters.plot.object.PlotCluster; -import com.intellectualcrafters.plot.util.ClusterManager; - -public class WorldModify implements WorldGeneratorModifier { - - private final SpongePlotGenerator plotgen; - private final boolean augment; - - public WorldModify(final SpongePlotGenerator plotgen, final boolean augment) { - this.plotgen = plotgen; - this.augment = augment; - } - - @Override - public void modifyWorldGenerator(final WorldCreationSettings world, final DataContainer settings, final WorldGenerator gen) { - if (augment) { - final String worldname = world.getWorldName(); - plotgen.world = worldname; - final PlotArea plotworld = plotgen.getNewPlotWorld(worldname); - if (plotworld.TYPE == 2) { - for (final PlotCluster cluster : ClusterManager.getClusters(worldname)) { - new AugmentedPopulator(worldname, gen, plotgen, cluster, plotworld.TERRAIN == 2, plotworld.TERRAIN != 2); - } - } else { - new AugmentedPopulator(worldname, gen, plotgen, null, plotworld.TERRAIN == 2, plotworld.TERRAIN != 2); - } - } else { - gen.getGenerationPopulators().clear(); - gen.getPopulators().clear(); - gen.setBaseGenerationPopulator(plotgen.getBaseGenerationPopulator()); - gen.setBiomeGenerator(plotgen.getBiomeGenerator()); - } - } - - @Override - public String getName() { - return "plotsquared"; - } - - @Override - public String getId() { - return "plotsquared"; - } -} diff --git a/src/main/java/com/plotsquared/sponge/listener/ChunkProcessor.java b/src/main/java/com/plotsquared/sponge/listener/ChunkProcessor.java new file mode 100644 index 000000000..4ef05dcf4 --- /dev/null +++ b/src/main/java/com/plotsquared/sponge/listener/ChunkProcessor.java @@ -0,0 +1,5 @@ +package com.plotsquared.sponge.listener; + +public class ChunkProcessor { + // TODO FIXME +} diff --git a/src/main/java/com/plotsquared/sponge/listener/MainListener.java b/src/main/java/com/plotsquared/sponge/listener/MainListener.java index 8477a4219..d241c631f 100644 --- a/src/main/java/com/plotsquared/sponge/listener/MainListener.java +++ b/src/main/java/com/plotsquared/sponge/listener/MainListener.java @@ -9,14 +9,41 @@ import java.util.Optional; import java.util.UUID; import java.util.function.Predicate; -import org.bukkit.block.BlockState; -import org.bukkit.entity.Ambient; -import org.bukkit.entity.Boat; -import org.bukkit.entity.Explosive; -import org.bukkit.entity.Minecart; -import org.bukkit.entity.Monster; +import org.spongepowered.api.block.BlockSnapshot; +import org.spongepowered.api.block.BlockState; +import org.spongepowered.api.data.Transaction; +import org.spongepowered.api.entity.Entity; +import org.spongepowered.api.entity.EntityTypes; +import org.spongepowered.api.entity.Transform; +import org.spongepowered.api.entity.explosive.Explosive; +import org.spongepowered.api.entity.explosive.PrimedTNT; +import org.spongepowered.api.entity.living.Ambient; +import org.spongepowered.api.entity.living.Living; +import org.spongepowered.api.entity.living.animal.Animal; +import org.spongepowered.api.entity.living.monster.Monster; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.entity.vehicle.Boat; +import org.spongepowered.api.entity.vehicle.minecart.Minecart; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.action.LightningEvent; +import org.spongepowered.api.event.block.ChangeBlockEvent; +import org.spongepowered.api.event.block.GrowBlockEvent; +import org.spongepowered.api.event.block.InteractBlockEvent; +import org.spongepowered.api.event.block.MoveBlockEvent; +import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.command.SendCommandEvent; +import org.spongepowered.api.event.entity.BreedEntityEvent; +import org.spongepowered.api.event.entity.DisplaceEntityEvent; +import org.spongepowered.api.event.entity.SpawnEntityEvent; +import org.spongepowered.api.event.message.MessageEvent; +import org.spongepowered.api.event.network.ClientConnectionEvent; +import org.spongepowered.api.event.world.ExplosionEvent; +import org.spongepowered.api.profile.GameProfile; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.world.World; +import org.spongepowered.api.world.extent.Extent; -import com.avaje.ebean.Transaction; +import com.flowpowered.math.vector.Vector3d; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; @@ -42,7 +69,6 @@ import com.plotsquared.listener.PlotListener; import com.plotsquared.sponge.SpongeMain; import com.plotsquared.sponge.object.SpongePlayer; import com.plotsquared.sponge.util.SpongeUtil; -import com.sk89q.worldedit.extent.Extent; public class MainListener { @@ -123,7 +149,7 @@ public class MainListener { // } public T getCause(Cause cause, Class clazz) { - Optional root = cause.root(); + Optional root = Optional.of(cause.root()); if (root.isPresent()) { Object source = root.get(); if (clazz.isInstance(source)) { @@ -137,13 +163,13 @@ public class MainListener { public void onCommand(final BreedEntityEvent.Breed event) { final Location loc = SpongeUtil.getLocation(event.getTargetEntity()); final String world = loc.getWorld(); - final PlotArea plotworld = PS.get().getPlotArea(world); + final PlotArea plotworld = PS.get().getPlotAreaByString(world); if (plotworld == null) { return; } - final Plot plot = MainUtil.getPlot(loc); + final Plot plot = loc.getPlot(); if (plot == null) { - if (MainUtil.isPlotRoad(loc)) { + if (loc.isPlotRoad()) { event.setCancelled(true); } return; @@ -156,7 +182,7 @@ public class MainListener { @Listener public void onMobSpawn(final SpawnEntityEvent event) { World world = event.getTargetWorld(); - final PlotArea plotworld = PS.get().getPlotArea(world.getName()); + final PlotArea plotworld = PS.get().getPlotAreaByString(world.getName()); if (plotworld == null) { return; } @@ -169,9 +195,9 @@ public class MainListener { return true; } final Location loc = SpongeUtil.getLocation(entity); - final Plot plot = MainUtil.getPlot(loc); + final Plot plot = loc.getPlot(); if (plot == null) { - if (MainUtil.isPlotRoad(loc)) { + if (loc.isPlotRoad()) { return false; } return true; @@ -202,7 +228,7 @@ public class MainListener { return false; } if (mobs == null) { - mobs = MainUtil.countEntities(plot); + mobs = plot.countEntities(); } if (mobs[3] >= cap) { return false; @@ -216,7 +242,7 @@ public class MainListener { return false; } if (mobs == null) { - mobs = MainUtil.countEntities(plot); + mobs = plot.countEntities(); } if (mobs[1] >= cap) { return false; @@ -231,7 +257,7 @@ public class MainListener { return false; } if (mobs == null) { - mobs = MainUtil.countEntities(plot); + mobs = plot.countEntities(); } if (mobs[2] >= cap) { return false; @@ -248,7 +274,7 @@ public class MainListener { return false; } if (mobs == null) { - mobs = MainUtil.countEntities(plot); + mobs = plot.countEntities(); } if (mobs[4] >= cap) { return false; @@ -262,7 +288,7 @@ public class MainListener { return false; } if (mobs == null) { - mobs = MainUtil.countEntities(plot); + mobs = plot.countEntities(); } if (mobs[0] >= cap) { return false; @@ -286,9 +312,9 @@ public class MainListener { return; } if (Settings.USE_PLOTME_ALIAS) { - SpongeMain.THIS.getGame().getCommandDispatcher().process(source, ("plots " + event.getArguments()).trim()); + SpongeMain.THIS.getGame().getCommandManager().process(source, ("plots " + event.getArguments()).trim()); } else { - source.sendMessage(SpongeMain.THIS.getText(C.NOT_USING_PLOTME.s())); + source.sendMessage(SpongeUtil.text(C.NOT_USING_PLOTME.s())); } event.setCancelled(true); } @@ -304,9 +330,9 @@ public class MainListener { List> transactions = event.getTransactions(); Transaction first = transactions.get(0); Location loc = SpongeUtil.getLocation(worldname, first.getOriginal().getPosition()); - Plot plot = MainUtil.getPlot(loc); + Plot plot = loc.getPlot(); if (plot == null) { - if (!MainUtil.isPlotAreaAbs(loc)) { + if (loc.getPlotAbs() == null /*MainUtil.isPlotAreaAbs(loc)*/) { return; } event.setCancelled(true); @@ -316,7 +342,7 @@ public class MainListener { @Override public boolean test(org.spongepowered.api.world.Location loc) { - if (MainUtil.isPlotRoad(SpongeUtil.getLocation(worldname, loc))) { + if (SpongeUtil.getLocation(worldname, loc).isPlotRoad()) { return false; } return true; @@ -342,7 +368,7 @@ public class MainListener { public void printCause(String method, Cause cause) { System.out.println(method + ": " + cause.toString()); System.out.println(method + ": " + cause.getClass()); - System.out.println(method + ": " + (cause.root().isPresent() ? cause.root().get() : null)); + System.out.println(method + ": " + (cause.root())); } @Listener @@ -356,17 +382,17 @@ public class MainListener { if (!PS.get().hasPlotArea(world)) { return; } - final PlotArea plotworld = PS.get().getPlotArea(world); + final PlotArea plotworld = PS.get().getPlotAreaByString(world); final PlotPlayer plr = SpongeUtil.getPlayer(player); if (!plotworld.PLOT_CHAT && ((plr.getMeta("chat") == null) || !(Boolean) plr.getMeta("chat"))) { return; } final Location loc = SpongeUtil.getLocation(player); - final Plot plot = MainUtil.getPlot(loc); + final Plot plot = loc.getPlot(); if (plot == null) { return; } - final Text message = event.getMessage(); + final Text message = event.getMessage().get(); // TODO use display name rather than username // - Getting displayname currently causes NPE, so wait until sponge fixes that @@ -374,12 +400,12 @@ public class MainListener { final String sender = player.getName(); final PlotId id = plot.getId(); final String newMessage = StringMan.replaceAll(C.PLOT_CHAT_FORMAT.s(), "%plot_id%", id.x + ";" + id.y, "%sender%", sender); - final Text forcedMessage = event.getMessage(); + final Text forcedMessage = event.getMessage().get(); // String forcedMessage = StringMan.replaceAll(C.PLOT_CHAT_FORCED.s(), "%plot_id%", id.x + ";" + id.y, "%sender%", sender); for (Entry entry : UUIDHandler.getPlayers().entrySet()) { PlotPlayer user = entry.getValue(); String toSend; - if (plot.equals(MainUtil.getPlot(user.getLocation()))) { + if (plot.equals(user.getLocation().getPlot())) { toSend = newMessage; } else if (Permissions.hasPermission(user, C.PERMISSION_COMMANDS_CHAT)) { ((SpongePlayer) user).player.sendMessage(forcedMessage); @@ -396,9 +422,9 @@ public class MainListener { } else { prefix = message; } - components.add(Texts.of(part)); + components.add(Text.of(part)); } - ((SpongePlayer) user).player.sendMessage(Texts.join(components)); + ((SpongePlayer) user).player.sendMessage(Text.join(components)); } event.setMessage(null); } @@ -424,7 +450,7 @@ public class MainListener { event.setCancelled(true); return; } - if (originPlot == null && !MainUtil.isPlotAreaAbs(current)) { + if (originPlot == null && current.getPlotAbs() == null /*May not work*/) { return; } if (!FlagManager.isPlotFlagTrue(currentPlot, "explosion")) { @@ -467,10 +493,10 @@ public class MainListener { onBlockChange(event); } - @Listener - public void onBlockBreak(final ChangeBlockEvent.Fluid event) { - onBlockChange(event); - } + // @Listener + // public void onBlockBreak(final ChangeBlockEvent.Fluid event) { + // onBlockChange(event); + // } @Listener public void onBlockBreak(final ChangeBlockEvent.Grow event) { @@ -499,9 +525,9 @@ public class MainListener { Transaction first = transactions.get(0); BlockSnapshot pos = first.getOriginal(); Location loc = SpongeUtil.getLocation(worldname, pos.getPosition()); - Plot plot = MainUtil.getPlot(loc); + Plot plot = loc.getPlot(); if (plot == null) { - if (!MainUtil.isPlotAreaAbs(loc)) { + if (loc.getPlotAbs() == null) { return; } if (!Permissions.hasPermission(pp, C.PERMISSION_ADMIN_DESTROY_ROAD)) { @@ -523,7 +549,7 @@ public class MainListener { MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_DESTROY_OTHER); final Flag destroy = FlagManager.getPlotFlagRaw(plot, "break"); final BlockState state = pos.getState(); - if ((destroy == null) || !((HashSet) destroy.getValue()).contains(SpongeMain.THIS.getPlotBlock(state))) { + if ((destroy == null) || !((HashSet) destroy.getValue()).contains(SpongeUtil.getPlotBlock(state))) { event.setCancelled(true); return; } @@ -536,7 +562,7 @@ public class MainListener { Location loc = SpongeUtil.getLocation(worldname, l); Plot plot = loc.getPlot(); if (plot == null) { - if (!MainUtil.isPlotAreaAbs(loc)) { + if (loc.getPlotAbs() == null) { return true; } if (!Permissions.hasPermission(pp, C.PERMISSION_ADMIN_DESTROY_ROAD)) { @@ -556,7 +582,7 @@ public class MainListener { } else { final Flag destroy = FlagManager.getPlotFlagRaw(plot, "break"); final BlockState state = l.getBlock(); - if ((destroy != null) && ((HashSet) destroy.getValue()).contains(SpongeMain.THIS.getPlotBlock(state))) { + if ((destroy != null) && ((HashSet) destroy.getValue()).contains(SpongeUtil.getPlotBlock(state))) { return true; } MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_DESTROY_OTHER); @@ -583,9 +609,9 @@ public class MainListener { Transaction first = transactions.get(0); BlockSnapshot pos = first.getOriginal(); Location loc = SpongeUtil.getLocation(worldname, pos.getPosition()); - Plot plot = MainUtil.getPlot(loc); + Plot plot = loc.getPlot(); if (plot == null) { - if (!MainUtil.isPlotAreaAbs(loc)) { + if (loc.getPlotAbs() == null) { return; } if (!Permissions.hasPermission(pp, C.PERMISSION_ADMIN_BUILD_ROAD)) { @@ -607,7 +633,7 @@ public class MainListener { MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_BUILD_OTHER); final Flag BUILD = FlagManager.getPlotFlagRaw(plot, C.FLAG_PLACE.s()); final BlockState state = pos.getState(); - if ((BUILD == null) || !((HashSet) BUILD.getValue()).contains(SpongeMain.THIS.getPlotBlock(state))) { + if ((BUILD == null) || !((HashSet) BUILD.getValue()).contains(SpongeUtil.getPlotBlock(state))) { event.setCancelled(true); return; } @@ -620,7 +646,7 @@ public class MainListener { Location loc = SpongeUtil.getLocation(worldname, l); Plot plot = loc.getPlot(); if (plot == null) { - if (!MainUtil.isPlotAreaAbs(loc)) { + if (loc.getPlotAbs() == null) { return true; } if (!Permissions.hasPermission(pp, C.PERMISSION_ADMIN_BUILD_ROAD)) { @@ -640,7 +666,7 @@ public class MainListener { } else { final Flag build = FlagManager.getPlotFlagRaw(plot, C.FLAG_PLACE.s()); final BlockState state = l.getBlock(); - if ((build != null) && ((HashSet) build.getValue()).contains(SpongeMain.THIS.getPlotBlock(state))) { + if ((build != null) && ((HashSet) build.getValue()).contains(SpongeUtil.getPlotBlock(state))) { return true; } MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_BUILD_OTHER); @@ -663,10 +689,10 @@ public class MainListener { } org.spongepowered.api.world.Location l = target.get(); Location loc = SpongeUtil.getLocation(l); - Plot plot = MainUtil.getPlot(loc); + Plot plot = loc.getPlot(); PlotPlayer pp = SpongeUtil.getPlayer(player); if (plot == null) { - if (!MainUtil.isPlotAreaAbs(loc)) { + if (loc.getPlotAbs() == null) { return; } if (!Permissions.hasPermission(pp, C.PERMISSION_ADMIN_INTERACT_ROAD)) { @@ -687,7 +713,7 @@ public class MainListener { return; } else { final Flag flag = FlagManager.getPlotFlagRaw(plot, "use"); - if ((flag != null) && ((HashSet) flag.getValue()).contains(SpongeMain.THIS.getPlotBlock(l.getBlock()))) { + if ((flag != null) && ((HashSet) flag.getValue()).contains(SpongeUtil.getPlotBlock(l.getBlock()))) { return; } MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_INTERACT_OTHER); @@ -726,12 +752,12 @@ public class MainListener { }, 20); } final Location loc = SpongeUtil.getLocation(player); - final Plot plot = MainUtil.getPlot(loc); + final Plot plot = loc.getPlot(); if (plot == null) { return; } if (Settings.TELEPORT_ON_LOGIN) { - MainUtil.teleportPlayer(pp, pp.getLocation(), plot); + pp.teleport(loc); MainUtil.sendMessage(pp, C.TELEPORTED_TO_ROAD); } PlotListener.plotEntry(pp, plot); @@ -764,11 +790,11 @@ public class MainListener { pp.setMeta("location", SpongeUtil.getLocation(player)); final World world = (World) extent; final String worldname = ((World) extent).getName(); - final PlotArea plotworld = PS.get().getPlotArea(worldname); + final PlotArea plotworld = PS.get().getPlotAreaByString(worldname); if (plotworld == null) { return; } - final PlotManager plotManager = PS.get().getPlotManager(worldname); + final PlotManager plotManager = PS.get().getPlotManager(PS.get().getPlot(plotworld, plotworld.getMin())); final PlotId id = plotManager.getPlotId(plotworld, x2, 0, getInt(to.getZ())); final Plot lastPlot = (Plot) pp.getMeta("lastplot"); if (id == null) { @@ -777,7 +803,7 @@ public class MainListener { } if (!PlotListener.plotExit(pp, lastPlot)) { MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_EXIT_DENIED); - if (lastPlot.equals(MainUtil.getPlot(SpongeUtil.getLocation(worldname, from)))) { + if (lastPlot.equals(SpongeUtil.getLocation(worldname, from).getPlot())) { event.setCancelled(true); } else { event.setToTransform(new Transform<>(world.getSpawnLocation())); @@ -787,10 +813,10 @@ public class MainListener { } else if ((lastPlot != null) && id.equals(lastPlot.getId())) { return; } else { - final Plot plot = MainUtil.getPlot(worldname, id); + final Plot plot = PS.get().getPlot(PS.get().getPlotAreaByString(worldname), id); if (!PlotListener.plotEntry(pp, plot)) { MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_ENTRY_DENIED); - if (!plot.getBasePlot(false).equals(MainUtil.getPlot(SpongeUtil.getLocation(worldname, from)))) { + if (!plot.getBasePlot(false).equals(SpongeUtil.getLocation(worldname, from).getPlot())) { event.setCancelled(true); } else { event.setToTransform(new Transform<>(world.getSpawnLocation())); @@ -798,7 +824,7 @@ public class MainListener { return; } } - final Integer border = MainUtil.worldBorder.get(worldname); + final Integer border = plotworld.getBorder(); //worldBorder.get(worldname); **May not work** if (border != null) { if (x2 > border) { final Vector3d pos = to.getPosition(); @@ -826,11 +852,11 @@ public class MainListener { pp.setMeta("location", SpongeUtil.getLocation(player)); final World world = (World) extent; final String worldname = ((World) extent).getName(); - final PlotArea plotworld = PS.get().getPlotArea(worldname); + final PlotArea plotworld = PS.get().getPlotAreaByString(worldname); if (plotworld == null) { return; } - final PlotManager plotManager = PS.get().getPlotManager(worldname); + final PlotManager plotManager = PS.get().getPlotManager(PS.get().getPlot(plotworld, plotworld.getMin())); final PlotId id = plotManager.getPlotId(plotworld, x2, 0, z2); final Plot lastPlot = (Plot) pp.getMeta("lastplot"); if (id == null) { @@ -839,7 +865,7 @@ public class MainListener { } if (!PlotListener.plotExit(pp, lastPlot)) { MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_EXIT_DENIED); - if (lastPlot.equals(MainUtil.getPlot(SpongeUtil.getLocation(worldname, from)))) { + if (lastPlot.equals(SpongeUtil.getLocation(worldname, from).getPlot())) { event.setCancelled(true); } else { event.setToTransform(new Transform<>(world.getSpawnLocation())); @@ -849,10 +875,10 @@ public class MainListener { } else if ((lastPlot != null) && id.equals(lastPlot.getId())) { return; } else { - final Plot plot = MainUtil.getPlot(worldname, id); + final Plot plot = PS.get().getPlot(PS.get().getPlotAreaByString(worldname), id); if (!PlotListener.plotEntry(pp, plot)) { MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_ENTRY_DENIED); - if (!plot.equals(MainUtil.getPlot(SpongeUtil.getLocation(worldname, from)))) { + if (!plot.equals(SpongeUtil.getLocation(worldname, from).getPlot())) { event.setCancelled(true); } else { event.setToTransform(new Transform<>(world.getSpawnLocation())); @@ -860,7 +886,7 @@ public class MainListener { return; } } - final Integer border = MainUtil.worldBorder.get(worldname); + final Integer border = plotworld.getBorder(); //worldBorder.get(worldname); **May not work** if (border != null) { if (z2 > border) { final Vector3d pos = to.getPosition(); @@ -876,4 +902,4 @@ public class MainListener { } } } -} +} \ No newline at end of file diff --git a/src/main/java/com/plotsquared/sponge/listener/WorldEvents.java b/src/main/java/com/plotsquared/sponge/listener/WorldEvents.java new file mode 100644 index 000000000..92c564e48 --- /dev/null +++ b/src/main/java/com/plotsquared/sponge/listener/WorldEvents.java @@ -0,0 +1,5 @@ +package com.plotsquared.sponge.listener; + +public class WorldEvents { + // TODO FIXME +} diff --git a/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java b/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java index 757759c9c..f11b0c7e1 100644 --- a/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java +++ b/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java @@ -1,6 +1,6 @@ package com.plotsquared.sponge.object; -import java.util.Date; +import java.time.Instant; import java.util.HashSet; import java.util.UUID; @@ -11,17 +11,17 @@ import org.spongepowered.api.effect.sound.SoundTypes; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.entity.living.player.gamemode.GameMode; import org.spongepowered.api.entity.living.player.gamemode.GameModes; +import org.spongepowered.api.profile.GameProfile; import org.spongepowered.api.service.ban.BanService; +import org.spongepowered.api.text.Text; import org.spongepowered.api.text.chat.ChatTypes; import com.flowpowered.math.vector.Vector3d; import com.intellectualcrafters.plot.commands.RequiredType; -import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.EconHandler; -import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.PlotGamemode; import com.intellectualcrafters.plot.util.PlotWeather; import com.intellectualcrafters.plot.util.UUIDHandler; @@ -42,11 +42,6 @@ public class SpongePlayer extends PlotPlayer { super.populatePersistentMetaMap(); } - @Override - public void sendMessage(final C c, final String... args) { - MainUtil.sendMessage(this, c, args); - } - @Override public RequiredType getSuperCaller() { return RequiredType.PLAYER; @@ -54,9 +49,12 @@ public class SpongePlayer extends PlotPlayer { @Override public long getPreviousLogin() { - final Value data = player.getJoinData().lastPlayed(); + if (last != 0) { + return last; + } + final Value data = player.getJoinData().lastPlayed(); if (data.exists()) { - return last = data.get().getSeconds() * 1000; + return last = data.get().getEpochSecond() * 1000; } return 0; } @@ -103,7 +101,7 @@ public class SpongePlayer extends PlotPlayer { @Override public void sendMessage(final String message) { - player.sendMessage(ChatTypes.CHAT, Texts.of(message)); + player.sendMessage(ChatTypes.CHAT, Text.of(message)); } @Override @@ -205,7 +203,6 @@ public class SpongePlayer extends PlotPlayer { @Override public void setGamemode(final PlotGamemode gamemode) { - // TODO Auto-generated method stub switch (gamemode) { case ADVENTURE: player.offer(Keys.GAME_MODE, GameModes.ADVENTURE); @@ -282,12 +279,12 @@ public class SpongePlayer extends PlotPlayer { @Override public void kick(final String message) { - player.kick(SpongeMain.THIS.getText(message)); + player.kick(SpongeUtil.text(message)); } @Override public boolean isBanned() { BanService service = SpongeMain.THIS.getGame().getServiceManager().provide(BanService.class).get(); - return service.isBanned(player); + return service.isBanned((GameProfile) player); } } diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeEconHandler.java b/src/main/java/com/plotsquared/sponge/util/SpongeEconHandler.java index 51fa1a85c..faffc2090 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeEconHandler.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeEconHandler.java @@ -1,51 +1,63 @@ package com.plotsquared.sponge.util; -import java.util.UUID; +import java.math.BigDecimal; +import java.util.Optional; import org.apache.commons.lang.NotImplementedException; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.service.ChangeServiceProviderEvent; +import org.spongepowered.api.service.economy.EconomyService; +import org.spongepowered.api.service.economy.account.UniqueAccount; -import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.object.OfflinePlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.UUIDHandler; -import com.plotsquared.sponge.SpongeMain; import com.plotsquared.sponge.object.SpongePlayer; public class SpongeEconHandler extends EconHandler { - private Object TE_SERVICE; - private Object EL_SERVICE; + private EconomyService econ; - public SpongeEconHandler() { - try { - Class clazz = Class.forName("com.erigitic.service.TEService"); - this.TE_SERVICE = SpongeMain.THIS.getGame().getServiceManager().provide(clazz).get(); - - } catch (Exception e) { - try { - Class clazz = Class.forName("me.Flibio.EconomyLite.API.EconomyLiteAPI"); - this.EL_SERVICE = SpongeMain.THIS.getGame().getServiceManager().provide(clazz).get(); - } catch (Exception e2) { - PS.log("No economy service found! (EconomyLite, TotalEconomy)"); - } + @Listener + public void onChangeServiceProvider(ChangeServiceProviderEvent event) { + if (event.getService().equals(EconomyService.class)) { + this.econ = (EconomyService) event.getNewProviderRegistration().getProvider(); } } @Override public void withdrawMoney(PlotPlayer player, double amount) { - UUID uuid = ((SpongePlayer) player).player.getUniqueId(); + if (econ != null) { + Optional uOpt = econ.getAccount(player.getUUID()); + if (uOpt.isPresent()) { + UniqueAccount acc = uOpt.get(); + acc.withdraw(econ.getDefaultCurrency(), new BigDecimal(amount), Cause.of("PlotSquared")); + } + } } @Override public void depositMoney(PlotPlayer player, double amount) { - UUID uuid = ((SpongePlayer) player).player.getUniqueId(); + if (econ != null) { + Optional uOpt = econ.getAccount(player.getUUID()); + if (uOpt.isPresent()) { + UniqueAccount acc = uOpt.get(); + acc.deposit(econ.getDefaultCurrency(), new BigDecimal(amount), Cause.of("PlotSquared")); + } + } } @Override public void depositMoney(OfflinePlotPlayer player, double amount) { - UUID uuid = player.getUUID(); - + if (econ != null) { + Optional uOpt = econ.getAccount(player.getUUID()); + if (uOpt.isPresent()) { + UniqueAccount acc = uOpt.get(); + acc.deposit(econ.getDefaultCurrency(), new BigDecimal(amount), Cause.of("PlotSquared")); + } + } } @Override @@ -64,4 +76,17 @@ public class SpongeEconHandler extends EconHandler { return false; } + @Override + public double getBalance(PlotPlayer player) { + if (econ != null) { + Optional uOpt = econ.getAccount(player.getUUID()); + if (uOpt.isPresent()) { + UniqueAccount acc = uOpt.get(); + BigDecimal balance = acc.getBalance(econ.getDefaultCurrency()); + return balance.doubleValue(); + } + } + return 0; + } + } diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeMetrics.java b/src/main/java/com/plotsquared/sponge/util/SpongeMetrics.java index 96457fbf5..c3f01e383 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeMetrics.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeMetrics.java @@ -43,6 +43,15 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.zip.GZIPOutputStream; +import ninja.leaping.configurate.commented.CommentedConfigurationNode; +import ninja.leaping.configurate.hocon.HoconConfigurationLoader; +import ninja.leaping.configurate.loader.ConfigurationLoader; + +import org.spongepowered.api.Game; +import org.spongepowered.api.plugin.PluginContainer; +import org.spongepowered.api.scheduler.Task; + +import com.google.inject.Inject; import com.intellectualcrafters.plot.PS; public class SpongeMetrics { diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java b/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java index e40e7712d..278ac7168 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java @@ -1,15 +1,24 @@ package com.plotsquared.sponge.util; +import java.lang.reflect.Field; +import java.util.Locale; import java.util.Optional; +import org.spongepowered.api.block.BlockState; import org.spongepowered.api.entity.Entity; import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.translation.Translatable; +import org.spongepowered.api.text.translation.Translation; import org.spongepowered.api.world.World; +import org.spongepowered.api.world.biome.BiomeType; +import org.spongepowered.api.world.biome.BiomeTypes; import org.spongepowered.api.world.extent.Extent; import com.flowpowered.math.vector.Vector3d; import com.flowpowered.math.vector.Vector3i; import com.intellectualcrafters.plot.object.Location; +import com.intellectualcrafters.plot.object.PlotBlock; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MathMan; import com.intellectualcrafters.plot.util.UUIDHandler; @@ -25,6 +34,60 @@ public class SpongeUtil { return new Location(world, pos.getX(), pos.getY(), pos.getZ()); } + private static BiomeType[] biomes; + + public static BiomeType getBiome(int index) { + if (biomes == null) { + try { + Field[] fields = BiomeTypes.class.getFields(); + biomes = new BiomeType[fields.length]; + for (int i = 0; i < fields.length; i++) { + biomes[i] = (BiomeType) fields[i].get(null); + } + } catch (IllegalArgumentException | IllegalAccessException e) { + e.printStackTrace(); + } + } + return biomes[index]; + } + + public static Text text(String m) { + return Text.of(m); + } + + public static Translation getTranslation(final String m) { + return new Translatable() { + @Override + public Translation getTranslation() { + return new Translation() { + + @Override + public String getId() { + return m; + } + + @Override + public String get(final Locale l, final Object... args) { + return m; + } + + @Override + public String get(final Locale l) { + return m; + } + }; + } + }.getTranslation(); + } + + public static BlockState getBlockState(int id, int data) { + + } + + public static PlotBlock getPlotBlock(BlockState state) { + + } + public static Location getLocation(final org.spongepowered.api.world.Location block) { return getLocation(block.getExtent().getName(), block); } diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java b/src/main/java/com/plotsquared/sponge/util/SpongeWorldUtil.java similarity index 98% rename from src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java rename to src/main/java/com/plotsquared/sponge/util/SpongeWorldUtil.java index aef6d7d69..f0f902155 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeWorldUtil.java @@ -12,10 +12,11 @@ import com.intellectualcrafters.plot.object.PlotBlock; import com.intellectualcrafters.plot.object.schematic.PlotItem; import com.intellectualcrafters.plot.util.MathMan; import com.intellectualcrafters.plot.util.StringComparison; +import com.intellectualcrafters.plot.util.WorldUtil; import com.plotsquared.sponge.SpongeMain; import com.sk89q.worldedit.blocks.BlockType; -public class SpongeBlockManager extends BlockManager { +public class SpongeWorldUtil extends WorldUtil { @Override public boolean isBlockSolid(final PlotBlock block) { diff --git a/src/main/java/com/plotsquared/sponge/util/block/FastQueue.java b/src/main/java/com/plotsquared/sponge/util/block/FastQueue.java new file mode 100644 index 000000000..c3fe50cea --- /dev/null +++ b/src/main/java/com/plotsquared/sponge/util/block/FastQueue.java @@ -0,0 +1,5 @@ +package com.plotsquared.sponge.util.block; + +public class FastQueue extends SlowQueue { + // TODO FIXME +} diff --git a/src/main/java/com/plotsquared/sponge/util/block/GenChunk.java b/src/main/java/com/plotsquared/sponge/util/block/GenChunk.java new file mode 100644 index 000000000..e2107b5b9 --- /dev/null +++ b/src/main/java/com/plotsquared/sponge/util/block/GenChunk.java @@ -0,0 +1,54 @@ +package com.plotsquared.sponge.util.block; + +import org.spongepowered.api.world.Chunk; +import org.spongepowered.api.world.extent.MutableBiomeArea; +import org.spongepowered.api.world.extent.MutableBlockVolume; + +import com.intellectualcrafters.plot.util.PlotChunk; +import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper; +import com.plotsquared.sponge.util.SpongeUtil; + +public class GenChunk extends PlotChunk { + + public boolean modified = false; + private final MutableBlockVolume terain; + private final MutableBiomeArea biome; + private final int bz; + private final int bx; + + public GenChunk(MutableBlockVolume terain, MutableBiomeArea biome, ChunkWrapper wrap) { + super(wrap); + this.bx = wrap.x << 4; + this.bz = wrap.z << 4; + this.terain = terain; + this.biome = biome; + } + + @Override + public Chunk getChunkAbs() { + ChunkWrapper wrap = getChunkWrapper(); + return SpongeUtil.getWorld(wrap.world).getChunk(wrap.x << 4, 0, wrap.z << 4).orElse(null); + } + + @Override + public void setBiome(int x, int z, int biome) { + if (this.biome != null) { + this.biome.setBiome(bx + x, bz + z, SpongeUtil.getBiome(biome)); + } + } + + @Override + public void setBlock(int x, int y, int z, int id, byte data) { + terain.setBlock(bx + x, y, bz + z, SpongeUtil.getBlockState(id, data)); + } + + @Override + public PlotChunk clone() { + throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); + } + + @Override + public PlotChunk shallowClone() { + throw new UnsupportedOperationException("NOT IMPLEMENTED YET"); + } +} diff --git a/src/main/java/com/plotsquared/sponge/util/block/SlowChunk.java b/src/main/java/com/plotsquared/sponge/util/block/SlowChunk.java new file mode 100644 index 000000000..0fb6dba20 --- /dev/null +++ b/src/main/java/com/plotsquared/sponge/util/block/SlowChunk.java @@ -0,0 +1,66 @@ +package com.plotsquared.sponge.util.block; + +import org.spongepowered.api.world.Chunk; + +import com.intellectualcrafters.plot.object.PlotBlock; +import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.PlotChunk; +import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper; +import com.plotsquared.sponge.util.SpongeUtil; + +public class SlowChunk extends PlotChunk { + + public PlotBlock[][] result = new PlotBlock[16][]; + public int[][] biomes; + private PlotBlock lastBlock; + + public SlowChunk(ChunkWrapper chunk) { + super(chunk); + } + + @Override + public Chunk getChunkAbs() { + ChunkWrapper loc = getChunkWrapper(); + return SpongeUtil.getWorld(loc.world).getChunk(loc.x << 4, 0, loc.z << 4).get(); + } + + @Override + public void setBiome(int x, int z, int biome) { + if (biomes == null) { + biomes = new int[16][16]; + } + biomes[x][z] = biome; + } + + @Override + public void setBlock(int x, int y, int z, int id, byte data) { + if (result[y >> 4] == null) { + result[y >> 4] = new PlotBlock[4096]; + } + if (id == lastBlock.id && data == lastBlock.data) { + result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = lastBlock; + } else { + result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = new PlotBlock((short) id, data); + } + } + + @Override + public PlotChunk clone() { + SlowChunk toReturn = new SlowChunk(getChunkWrapper()); + for (int i = 0; i < result.length; i++) { + PlotBlock[] matrix = result[i]; + if (matrix != null) { + toReturn.result[i] = new PlotBlock[matrix.length]; + System.arraycopy(matrix, 0, toReturn.result[i], 0, matrix.length); + } + } + return toReturn; + } + + @Override + public PlotChunk shallowClone() { + SlowChunk toReturn = new SlowChunk(getChunkWrapper()); + toReturn.result = result; + return toReturn; + } +} diff --git a/src/main/java/com/plotsquared/sponge/util/block/SlowQueue.java b/src/main/java/com/plotsquared/sponge/util/block/SlowQueue.java new file mode 100644 index 000000000..b478f15a5 --- /dev/null +++ b/src/main/java/com/plotsquared/sponge/util/block/SlowQueue.java @@ -0,0 +1,175 @@ +package com.plotsquared.sponge.util.block; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; + +import org.spongepowered.api.block.BlockState; +import org.spongepowered.api.world.Chunk; + +import com.flowpowered.math.vector.Vector3i; +import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.object.ChunkLoc; +import com.intellectualcrafters.plot.object.PlotBlock; +import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.PlotChunk; +import com.intellectualcrafters.plot.util.PlotQueue; +import com.intellectualcrafters.plot.util.SetQueue; +import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper; +import com.plotsquared.sponge.util.SpongeUtil; + +public class SlowQueue implements PlotQueue { + + private final ConcurrentHashMap> blocks = new ConcurrentHashMap<>(); + + @Override + public boolean setBlock(String world, int x, int y, int z, short id, byte data) { + if (y > 255 || y < 0) { + return false; + } + final ChunkWrapper wrap = SetQueue.IMP.new ChunkWrapper(world, x >> 4, z >> 4); + x = x & 15; + z = z & 15; + PlotChunk result = blocks.get(wrap); + if (result == null) { + result = getChunk(wrap); + result.setBlock(x, y, z, id, data); + final PlotChunk previous = blocks.put(wrap, result); + if (previous == null) { + return true; + } + blocks.put(wrap, previous); + result = previous; + } + result.setBlock(x, y, z, id, data); + return true; + } + + @Override + public void setChunk(PlotChunk chunk) { + blocks.put(chunk.getChunkWrapper(), chunk); + } + + @Override + public PlotChunk next() { + if (!PS.get().isMainThread(Thread.currentThread())) { + throw new IllegalStateException("Must be called from main thread!"); + } + try { + if (blocks.isEmpty()) { + return null; + } + final Iterator>> iter = blocks.entrySet().iterator(); + final PlotChunk toReturn = iter.next().getValue(); + if (SetQueue.IMP.isWaiting()) { + return null; + } + iter.remove(); + execute(toReturn); + fixLighting(toReturn, true); + return toReturn; + } catch (final Throwable e) { + e.printStackTrace(); + return null; + } + } + + @Override + public PlotChunk next(ChunkWrapper wrap, boolean fixLighting) { + if (!PS.get().isMainThread(Thread.currentThread())) { + throw new IllegalStateException("Must be called from main thread!"); + } + try { + if (blocks.isEmpty()) { + return null; + } + final PlotChunk toReturn = blocks.remove(wrap); + if (toReturn == null) { + return null; + } + execute(toReturn); + fixLighting(toReturn, fixLighting); + return toReturn; + } catch (final Throwable e) { + e.printStackTrace(); + return null; + } + } + + @Override + public void clear() { + blocks.clear(); + } + + /** + * This should be overriden by any specialized queues + * @param pc + */ + public void execute(PlotChunk pc) { + SlowChunk sc = (SlowChunk) pc; + Chunk chunk = pc.getChunk(); + chunk.loadChunk(true); + Vector3i min = chunk.getBlockMin(); + int bx = min.getX(); + int bz = min.getZ(); + for (int i = 0; i < sc.result.length; i++) { + PlotBlock[] result2 = sc.result[i]; + if (result2 == null) { + continue; + } + for (int j = 0; j < 4096; j++) { + final int x = MainUtil.x_loc[i][j]; + final int y = MainUtil.y_loc[i][j]; + final int z = MainUtil.z_loc[i][j]; + PlotBlock newBlock = result2[j]; + BlockState state = SpongeUtil.getBlockState(newBlock.id, newBlock.data); + chunk.setBlock(bx + x, y, bz + z, state, false); + } + } + int[][] biomes = sc.biomes; + if (biomes != null) { + for (int x = 0; x < 16; x++) { + int[] array = biomes[x]; + if (array == null) { + continue; + } + for (int z = 0; z < 16; z++) { + int biome = array[z]; + if (biome == 0) { + continue; + } + chunk.setBiome(bx + x, bz + z, SpongeUtil.getBiome(biome)); + } + } + } + } + + /** + * This should be overriden by any specialized queues + * @param wrap + */ + @Override + public PlotChunk getChunk(ChunkWrapper wrap) { + return new SlowChunk(wrap); + } + + /** + * This should be overriden by any specialized queues + * @param fixAll + */ + @Override + public boolean fixLighting(PlotChunk chunk, boolean fixAll) { + // Do nothing + return true; + } + + /** + * This should be overriden by any specialized queues + * @param locs + */ + @Override + public void sendChunk(String world, Collection locs) { + // Do nothing + } +}