diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java index 64f2f4d1f..0b816b550 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java @@ -27,12 +27,12 @@ import com.github.intellectualsites.plotsquared.bukkit.util.BukkitEconHandler; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitEventUtil; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitHybridUtils; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitInventoryUtil; +import com.github.intellectualsites.plotsquared.bukkit.util.BukkitLegacyMappings; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitSchematicHandler; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitSetupUtils; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitTaskManager; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitUtil; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitVersion; -import com.github.intellectualsites.plotsquared.bukkit.util.LegacyMappings; import com.github.intellectualsites.plotsquared.bukkit.util.Metrics; import com.github.intellectualsites.plotsquared.bukkit.util.SendChunk; import com.github.intellectualsites.plotsquared.bukkit.util.SetGenCB; @@ -71,6 +71,7 @@ import com.github.intellectualsites.plotsquared.plot.util.ConsoleColors; import com.github.intellectualsites.plotsquared.plot.util.EconHandler; import com.github.intellectualsites.plotsquared.plot.util.EventUtil; import com.github.intellectualsites.plotsquared.plot.util.InventoryUtil; +import com.github.intellectualsites.plotsquared.plot.util.LegacyMappings; import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.ReflectionUtils; import com.github.intellectualsites.plotsquared.plot.util.SchematicHandler; @@ -115,7 +116,7 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; -public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain { +public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain { @Getter private static WorldEdit worldEdit; private static Map pluginMap; @@ -202,7 +203,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< this.pluginName = getDescription().getName(); getServer().getName(); - LegacyMappings.initialize(); + PlotPlayer.registerConverter(Player.class, BukkitUtil::getPlayer); new PlotSquared(this, "Bukkit"); if (Settings.Enabled_Components.METRICS) { diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java index c0dde8a91..380e7b640 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java @@ -5,7 +5,6 @@ import com.github.intellectualsites.plotsquared.bukkit.object.BukkitLazyBlock; import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitUtil; import com.github.intellectualsites.plotsquared.bukkit.util.BukkitVersion; -import com.github.intellectualsites.plotsquared.bukkit.util.LegacyMappings; import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.C; import com.github.intellectualsites.plotsquared.plot.config.Settings; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java index 352c2fc3a..a1ea8e725 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java @@ -12,7 +12,7 @@ import com.github.intellectualsites.plotsquared.plot.util.block.QueueProvider; import java.io.File; import java.util.List; -public interface IPlotMain extends ILogger { +public interface IPlotMain extends ILogger { /** * Log a message to console. @@ -274,7 +274,7 @@ public interface IPlotMain extends ILogger { List getPluginIds(); - BlockRegistry getBlockRegistry(); + BlockRegistry getBlockRegistry(); LegacyMappings getLegacyMappings(); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java index f2e6f3db4..e131fd5c8 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java @@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot; import com.github.intellectualsites.plotsquared.configuration.ConfigurationSection; import com.github.intellectualsites.plotsquared.configuration.MemorySection; import com.github.intellectualsites.plotsquared.configuration.file.YamlConfiguration; +import com.github.intellectualsites.plotsquared.configuration.serialization.ConfigurationSerialization; import com.github.intellectualsites.plotsquared.plot.commands.WE_Anywhere; import com.github.intellectualsites.plotsquared.plot.config.C; import com.github.intellectualsites.plotsquared.plot.config.Configuration; @@ -96,6 +97,12 @@ import java.util.zip.ZipInputStream; this.logger = iPlotMain; Settings.PLATFORM = platform; + // + // Register configuration serializable classes + // + ConfigurationSerialization.registerClass(PlotBlock.class); + ConfigurationSerialization.registerClass(BlockBucket.class); + try { new ReflectionUtils(this.IMP.getNMSPackage()); try { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Area.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Area.java index ec7d08672..ef7a92b56 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Area.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Area.java @@ -191,19 +191,19 @@ public class Area extends SubCommand { break; case "f": case "floor": - pa.TOP_BLOCK = Configuration.BLOCKLIST.parseString(pair[1]); + pa.TOP_BLOCK = Configuration.BLOCK_BUCKET.parseString(pair[1]); break; case "m": case "main": - pa.MAIN_BLOCK = Configuration.BLOCKLIST.parseString(pair[1]); + pa.MAIN_BLOCK = Configuration.BLOCK_BUCKET.parseString(pair[1]); break; case "w": case "wall": - pa.WALL_FILLING = Configuration.BLOCK.parseString(pair[1]); + pa.WALL_FILLING = Configuration.BLOCK_BUCKET.parseString(pair[1]); break; case "b": case "border": - pa.WALL_BLOCK = Configuration.BLOCK.parseString(pair[1]); + pa.WALL_BLOCK = Configuration.BLOCK_BUCKET.parseString(pair[1]); break; case "terrain": pa.TERRAIN = Integer.parseInt(pair[1]); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java index b0acab576..bd4a19d79 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java @@ -4,6 +4,7 @@ import com.github.intellectualsites.plotsquared.commands.Command; import com.github.intellectualsites.plotsquared.commands.CommandDeclaration; import com.github.intellectualsites.plotsquared.plot.config.C; import com.github.intellectualsites.plotsquared.plot.config.Configuration; +import com.github.intellectualsites.plotsquared.plot.config.Configuration.UnknownBlockException; import com.github.intellectualsites.plotsquared.plot.flag.Flag; import com.github.intellectualsites.plotsquared.plot.flag.FlagManager; import com.github.intellectualsites.plotsquared.plot.flag.Flags; @@ -49,46 +50,37 @@ public class Set extends SubCommand { C.PERMISSION_SET_COMPONENT.f(component)); return false; } - PlotBlock[] blocks; + // PlotBlock[] blocks; + BlockBucket bucket; try { if (args.length < 2) { MainUtil.sendMessage(player, C.NEED_BLOCK); return true; } String[] split = material.split(","); - blocks = Configuration.BLOCKLIST.parseString(material); - for (int i = 0; i < blocks.length; i++) { - PlotBlock block = blocks[i]; - if (block == null) { - MainUtil.sendMessage(player, C.NOT_VALID_BLOCK, split[i]); - String name; - if (split[i].contains("%")) { - name = split[i].split("%")[1]; - } else { - name = split[i]; + // blocks = Configuration.BLOCKLIST.parseString(material); + + try { + bucket = Configuration.BLOCK_BUCKET.parseString(material); + } catch (final UnknownBlockException unknownBlockException) { + final String unknownBlock = unknownBlockException.getUnknownValue(); + C.NOT_VALID_BLOCK.send(player, unknownBlock); + StringComparison.ComparisonResult match = + WorldUtil.IMP.getClosestBlock(unknownBlock); + if (match != null) { + final String found = WorldUtil.IMP.getClosestMatchingName(match.best); + if (found != null) { + MainUtil.sendMessage(player, C.DID_YOU_MEAN, + found.toLowerCase()); } - StringComparison.ComparisonResult match = - WorldUtil.IMP.getClosestBlock(name); - if (match != null) { - name = WorldUtil.IMP.getClosestMatchingName(match.best); - if (name != null) { - MainUtil.sendMessage(player, C.DID_YOU_MEAN, - name.toLowerCase()); - } - } - return false; - } else if (!allowUnsafe && (!block.isAir() && !WorldUtil.IMP - .isBlockSolid(block))) { - MainUtil - .sendMessage(player, C.NOT_ALLOWED_BLOCK, block.toString()); - return false; } + return false; } + if (!allowUnsafe) { - for (PlotBlock block : blocks) { + for (final PlotBlock block : bucket.getBlocks()) { if (!block.isAir() && !WorldUtil.IMP.isBlockSolid(block)) { - MainUtil.sendMessage(player, C.NOT_ALLOWED_BLOCK, - block.toString()); + C.NOT_ALLOWED_BLOCK.send(player, block.toString()); return false; } } @@ -103,7 +95,7 @@ public class Set extends SubCommand { } plot.addRunning(); for (Plot current : plot.getConnectedPlots()) { - current.setComponent(component, blocks); + current.setComponent(component, bucket); } MainUtil.sendMessage(player, C.GENERATING_COMPONENT); GlobalBlockQueue.IMP.addTask(new Runnable() { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Configuration.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Configuration.java index 96c0d94eb..da6e339dd 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Configuration.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Configuration.java @@ -1,10 +1,13 @@ package com.github.intellectualsites.plotsquared.plot.config; +import com.github.intellectualsites.plotsquared.plot.object.BlockBucket; import com.github.intellectualsites.plotsquared.plot.object.PlotBlock; import com.github.intellectualsites.plotsquared.plot.util.StringComparison; import com.github.intellectualsites.plotsquared.plot.util.WorldUtil; import java.util.ArrayList; +import lombok.Getter; +import lombok.NonNull; /** * Main Configuration Utility @@ -101,6 +104,87 @@ public class Configuration { return value.best; } }; + + public static final class UnknownBlockException extends IllegalArgumentException { + + @Getter + private final String unknownValue; + + public UnknownBlockException(@NonNull final String unknownValue) { + super(String.format("\"%s\" is not a valid block", unknownValue)); + this.unknownValue = unknownValue; + } + + } + + + public static final SettingValue BLOCK_BUCKET = + new SettingValue("BLOCK_BUCKET") { + @Override + public BlockBucket parseString(final String string) { + if (string == null || string.isEmpty()) { + return new BlockBucket(); + } + final BlockBucket blockBucket = new BlockBucket(); + final String[] parts = string.split(","); + for (final String part : parts) { + String block; + int chance = -1; + + if (part.contains(":")) { + final String[] innerParts = part.split(":"); + if (innerParts.length > 1) { + chance = Integer.parseInt(innerParts[1]); + } + block = innerParts[0]; + } else { + block = part; + } + final StringComparison.ComparisonResult value = + WorldUtil.IMP.getClosestBlock(block); + if (value == null) { + throw new UnknownBlockException(block); + } + blockBucket.addBlock(value.best, chance); + } + blockBucket.compile(); // Pre-compile :D + return blockBucket; + } + + @Override + public boolean validateValue(final String string) { + try { + if (string == null || string.isEmpty()) { + return false; + } + final String[] parts = string.split(","); + for (final String part : parts) { + String block; + if (part.contains(":")) { + final String[] innerParts = part.split(":"); + if (innerParts.length > 1) { + final int chance = Integer.parseInt(innerParts[1]); + if (chance < 1 || chance > 100) { + return false; + } + } + block = innerParts[0]; + } else { + block = part; + } + StringComparison.ComparisonResult value = + WorldUtil.IMP.getClosestBlock(block); + if (value == null || value.match > 1) { + return false; + } + } + } catch (final Throwable exception) { + return false; + } + return true; + } + }; + public static final SettingValue BLOCKLIST = new SettingValue("BLOCKLIST") { @Override public boolean validateValue(String string) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java index 847bf9fff..f69ea6785 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java @@ -13,7 +13,7 @@ import java.util.ArrayList; public class ClassicPlotManager extends SquarePlotManager { @Override public boolean setComponent(PlotArea plotArea, PlotId plotId, String component, - PlotBlock[] blocks) { + BlockBucket blocks) { switch (component) { case "floor": setFloor(plotArea, plotId, blocks); @@ -45,15 +45,13 @@ public class ClassicPlotManager extends SquarePlotManager { @Override public boolean unclaimPlot(PlotArea plotArea, Plot plot, Runnable whenDone) { ClassicPlotWorld dpw = (ClassicPlotWorld) plotArea; - setWallFilling(dpw, plot.getId(), new PlotBlock[] {dpw.WALL_FILLING}); - if (!dpw.WALL_BLOCK.isAir() || !dpw.WALL_BLOCK.equals(dpw.CLAIMED_WALL_BLOCK)) { - setWall(dpw, plot.getId(), new PlotBlock[] {dpw.WALL_BLOCK}); - } + setWallFilling(dpw, plot.getId(), dpw.WALL_FILLING); + setWall(dpw, plot.getId(), dpw.WALL_BLOCK); GlobalBlockQueue.IMP.addTask(whenDone); return true; } - public boolean setFloor(PlotArea plotArea, PlotId plotId, PlotBlock[] blocks) { + public boolean setFloor(PlotArea plotArea, PlotId plotId, BlockBucket blocks) { Plot plot = plotArea.getPlotAbs(plotId); LocalBlockQueue queue = plotArea.getQueue(false); if (plot.isBasePlot()) { @@ -70,7 +68,7 @@ public class ClassicPlotManager extends SquarePlotManager { return true; } - public boolean setAll(PlotArea plotArea, PlotId plotId, PlotBlock[] blocks) { + public boolean setAll(PlotArea plotArea, PlotId plotId, BlockBucket blocks) { Plot plot = plotArea.getPlotAbs(plotId); if (!plot.isBasePlot()) { return false; @@ -86,7 +84,7 @@ public class ClassicPlotManager extends SquarePlotManager { return true; } - public boolean setAir(PlotArea plotArea, PlotId plotId, PlotBlock[] blocks) { + public boolean setAir(PlotArea plotArea, PlotId plotId, BlockBucket blocks) { Plot plot = plotArea.getPlotAbs(plotId); if (!plot.isBasePlot()) { return false; @@ -104,7 +102,7 @@ public class ClassicPlotManager extends SquarePlotManager { return true; } - public boolean setMain(PlotArea plotArea, PlotId plotId, PlotBlock[] blocks) { + public boolean setMain(PlotArea plotArea, PlotId plotId, BlockBucket blocks) { Plot plot = plotArea.getPlotAbs(plotId); if (!plot.isBasePlot()) { return false; @@ -121,7 +119,7 @@ public class ClassicPlotManager extends SquarePlotManager { return true; } - public boolean setMiddle(PlotArea plotArea, PlotId plotId, PlotBlock[] blocks) { + public boolean setMiddle(PlotArea plotArea, PlotId plotId, BlockBucket blocks) { Plot plot = plotArea.getPlotAbs(plotId); if (!plot.isBasePlot()) { return false; @@ -132,12 +130,12 @@ public class ClassicPlotManager extends SquarePlotManager { int x = MathMan.average(corners[0].getX(), corners[1].getX()); int z = MathMan.average(corners[0].getZ(), corners[1].getZ()); - queue.setBlock(x, dpw.PLOT_HEIGHT, z, blocks[0]); + queue.setBlock(x, dpw.PLOT_HEIGHT, z, blocks.getBlock()); queue.enqueue(); return true; } - public boolean setOutline(PlotArea plotArea, PlotId plotId, PlotBlock[] blocks) { + public boolean setOutline(PlotArea plotArea, PlotId plotId, BlockBucket blocks) { ClassicPlotWorld dpw = (ClassicPlotWorld) plotArea; if (dpw.ROAD_WIDTH == 0) { return false; @@ -145,14 +143,13 @@ public class ClassicPlotManager extends SquarePlotManager { Plot plot = plotArea.getPlotAbs(plotId); Location bottom = plot.getBottomAbs(); Location top = plot.getExtendedTopAbs(); - PseudoRandom random = new PseudoRandom(); LocalBlockQueue queue = plotArea.getQueue(false); int maxY = plotArea.getPlotManager().getWorldHeight(); if (!plot.getMerged(0)) { int z = bottom.getZ(); for (int x = bottom.getX(); x <= top.getX(); x++) { for (int y = dpw.PLOT_HEIGHT; y <= maxY; y++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } } @@ -160,7 +157,7 @@ public class ClassicPlotManager extends SquarePlotManager { int x = bottom.getX(); for (int z = bottom.getZ(); z <= top.getZ(); z++) { for (int y = dpw.PLOT_HEIGHT; y <= maxY; y++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } } @@ -169,7 +166,7 @@ public class ClassicPlotManager extends SquarePlotManager { int z = top.getZ(); for (int x = bottom.getX(); x <= top.getX(); x++) { for (int y = dpw.PLOT_HEIGHT; y <= maxY; y++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } } @@ -177,7 +174,7 @@ public class ClassicPlotManager extends SquarePlotManager { int x = top.getX(); for (int z = bottom.getZ(); z <= top.getZ(); z++) { for (int y = dpw.PLOT_HEIGHT; y <= maxY; y++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } } @@ -192,7 +189,7 @@ public class ClassicPlotManager extends SquarePlotManager { return true; } - public boolean setWallFilling(PlotArea plotArea, PlotId plotId, PlotBlock[] blocks) { + public boolean setWallFilling(PlotArea plotArea, PlotId plotId, BlockBucket blocks) { ClassicPlotWorld dpw = (ClassicPlotWorld) plotArea; if (dpw.ROAD_WIDTH == 0) { return false; @@ -201,13 +198,12 @@ public class ClassicPlotManager extends SquarePlotManager { Location bot = plot.getExtendedBottomAbs() .subtract(plot.getMerged(3) ? 0 : 1, 0, plot.getMerged(0) ? 0 : 1); Location top = plot.getExtendedTopAbs().add(1, 0, 1); - PseudoRandom random = new PseudoRandom(); LocalBlockQueue queue = plotArea.getQueue(false); if (!plot.getMerged(0)) { int z = bot.getZ(); for (int x = bot.getX(); x < top.getX(); x++) { for (int y = 1; y <= dpw.WALL_HEIGHT; y++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } } @@ -215,7 +211,7 @@ public class ClassicPlotManager extends SquarePlotManager { int x = bot.getX(); for (int z = bot.getZ(); z < top.getZ(); z++) { for (int y = 1; y <= dpw.WALL_HEIGHT; y++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } } @@ -223,7 +219,7 @@ public class ClassicPlotManager extends SquarePlotManager { int z = top.getZ(); for (int x = bot.getX(); x < top.getX() + (plot.getMerged(1) ? 0 : 1); x++) { for (int y = 1; y <= dpw.WALL_HEIGHT; y++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } } @@ -231,7 +227,7 @@ public class ClassicPlotManager extends SquarePlotManager { int x = top.getX(); for (int z = bot.getZ(); z < top.getZ() + (plot.getMerged(2) ? 0 : 1); z++) { for (int y = 1; y <= dpw.WALL_HEIGHT; y++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } } @@ -239,7 +235,7 @@ public class ClassicPlotManager extends SquarePlotManager { return true; } - public boolean setWall(PlotArea plotArea, PlotId plotId, PlotBlock[] blocks) { + public boolean setWall(PlotArea plotArea, PlotId plotId, BlockBucket blocks) { ClassicPlotWorld dpw = (ClassicPlotWorld) plotArea; if (dpw.ROAD_WIDTH == 0) { return false; @@ -254,25 +250,25 @@ public class ClassicPlotManager extends SquarePlotManager { if (!plot.getMerged(0)) { int z = bot.getZ(); for (int x = bot.getX(); x < top.getX(); x++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } if (!plot.getMerged(3)) { int x = bot.getX(); for (int z = bot.getZ(); z < top.getZ(); z++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } if (!plot.getMerged(2)) { int z = top.getZ(); for (int x = bot.getX(); x < top.getX() + (plot.getMerged(1) ? 0 : 1); x++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } if (!plot.getMerged(1)) { int x = top.getX(); for (int z = bot.getZ(); z < top.getZ() + (plot.getMerged(2) ? 0 : 1); z++) { - queue.setBlock(x, y, z, blocks[random.random(blocks.length)]); + queue.setBlock(x, y, z, blocks.getBlock()); } } queue.enqueue(); @@ -429,24 +425,14 @@ public class ClassicPlotManager extends SquarePlotManager { * Finishing off plot merging by adding in the walls surrounding the plot (OPTIONAL)(UNFINISHED). */ @Override public boolean finishPlotMerge(PlotArea plotArea, ArrayList plotIds) { - PlotBlock block = ((ClassicPlotWorld) plotArea).CLAIMED_WALL_BLOCK; - PlotBlock unclaim = ((ClassicPlotWorld) plotArea).WALL_BLOCK; - if (!block.isAir() || !block.equals(unclaim)) { - for (PlotId id : plotIds) { - setWall(plotArea, id, new PlotBlock[] {block}); - } - } + final BlockBucket block = ((ClassicPlotWorld) plotArea).CLAIMED_WALL_BLOCK; + plotIds.forEach(id -> setWall(plotArea, id, block)); return true; } @Override public boolean finishPlotUnlink(PlotArea plotArea, ArrayList plotIds) { - PlotBlock block = ((ClassicPlotWorld) plotArea).CLAIMED_WALL_BLOCK; - PlotBlock unclaim = ((ClassicPlotWorld) plotArea).WALL_BLOCK; - for (PlotId id : plotIds) { - if (block.isAir() || !block.equals(unclaim)) { - setWall(plotArea, id, new PlotBlock[] {block}); - } - } + final BlockBucket block = ((ClassicPlotWorld) plotArea).CLAIMED_WALL_BLOCK; + plotIds.forEach(id -> setWall(plotArea, id, block)); return true; } @@ -459,11 +445,8 @@ public class ClassicPlotManager extends SquarePlotManager { } @Override public boolean claimPlot(PlotArea plotArea, Plot plot) { - PlotBlock unclaim = ((ClassicPlotWorld) plotArea).WALL_BLOCK; - PlotBlock claim = ((ClassicPlotWorld) plotArea).CLAIMED_WALL_BLOCK; - if (!claim.isAir() || !claim.equals(unclaim)) { - setWall(plotArea, plot.getId(), new PlotBlock[] {claim}); - } + final BlockBucket claim = ((ClassicPlotWorld) plotArea).CLAIMED_WALL_BLOCK; + setWall(plotArea, plot.getId(), claim); return true; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotWorld.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotWorld.java index b3e9ad30e..5a1fdaa43 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotWorld.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotWorld.java @@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.generator; import com.github.intellectualsites.plotsquared.configuration.ConfigurationSection; import com.github.intellectualsites.plotsquared.plot.config.Configuration; import com.github.intellectualsites.plotsquared.plot.config.ConfigurationNode; +import com.github.intellectualsites.plotsquared.plot.object.BlockBucket; import com.github.intellectualsites.plotsquared.plot.object.PlotBlock; import com.github.intellectualsites.plotsquared.plot.object.PlotId; import com.github.intellectualsites.plotsquared.plot.util.StringMan; @@ -12,12 +13,12 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld { public int ROAD_HEIGHT = 64; public int PLOT_HEIGHT = 64; public int WALL_HEIGHT = 64; - public PlotBlock[] MAIN_BLOCK = new PlotBlock[] {PlotBlock.get("stone")}; - public PlotBlock[] TOP_BLOCK = new PlotBlock[] {PlotBlock.get("grass")}; - public PlotBlock WALL_BLOCK = PlotBlock.get((short) 44, (byte) 0); - public PlotBlock CLAIMED_WALL_BLOCK = PlotBlock.get((short) 44, (byte) 1); - public PlotBlock WALL_FILLING = PlotBlock.get((short) 1, (byte) 0); - public PlotBlock ROAD_BLOCK = PlotBlock.get((short) 155, (byte) 0); + public BlockBucket MAIN_BLOCK = BlockBucket.withSingle(PlotBlock.get("stone")); // new PlotBlock[] {PlotBlock.get("stone")}; + public BlockBucket TOP_BLOCK = BlockBucket.withSingle(PlotBlock.get("grass")); //new PlotBlock[] {PlotBlock.get("grass")}; + public BlockBucket WALL_BLOCK = BlockBucket.withSingle(PlotBlock.get("stone_slab")); // PlotBlock.get((short) 44, (byte) 0); + public BlockBucket CLAIMED_WALL_BLOCK = BlockBucket.withSingle(PlotBlock.get("sandstone_slab")); // PlotBlock.get((short) 44, (byte) 1); + public BlockBucket WALL_FILLING = BlockBucket.withSingle(PlotBlock.get("stone")); //PlotBlock.get((short) 1, (byte) 0); + public BlockBucket ROAD_BLOCK = BlockBucket.withSingle(PlotBlock.get("quartz_block")); // PlotBlock.get((short) 155, (byte) 0); public boolean PLOT_BEDROCK = true; public ClassicPlotWorld(String worldName, String id, IndependentPlotGenerator generator, @@ -39,22 +40,22 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld { new ConfigurationNode("plot.size", this.PLOT_WIDTH, "Plot width", Configuration.INTEGER), new ConfigurationNode("plot.filling", this.MAIN_BLOCK, "Plot block", - Configuration.BLOCKLIST), + Configuration.BLOCK_BUCKET), new ConfigurationNode("plot.floor", this.TOP_BLOCK, "Plot floor block", - Configuration.BLOCKLIST), + Configuration.BLOCK_BUCKET), new ConfigurationNode("wall.block", this.WALL_BLOCK, "Top wall block", - Configuration.BLOCK), + Configuration.BLOCK_BUCKET), new ConfigurationNode("wall.block_claimed", this.CLAIMED_WALL_BLOCK, "Wall block (claimed)", - Configuration.BLOCK), + Configuration.BLOCK_BUCKET), new ConfigurationNode("road.width", this.ROAD_WIDTH, "Road width", Configuration.INTEGER), new ConfigurationNode("road.height", this.ROAD_HEIGHT, "Road height", Configuration.INTEGER), new ConfigurationNode("road.block", this.ROAD_BLOCK, "Road block", - Configuration.BLOCK), + Configuration.BLOCK_BUCKET), new ConfigurationNode("wall.filling", this.WALL_FILLING, "Wall filling block", - Configuration.BLOCK), + Configuration.BLOCK_BUCKET), new ConfigurationNode("wall.height", this.WALL_HEIGHT, "Wall height", Configuration.INTEGER), new ConfigurationNode("plot.bedrock", this.PLOT_BEDROCK, "Plot bedrock generation", @@ -69,16 +70,16 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld { super.loadConfiguration(config); this.PLOT_BEDROCK = config.getBoolean("plot.bedrock"); this.PLOT_HEIGHT = Math.min(255, config.getInt("plot.height")); - this.MAIN_BLOCK = Configuration.BLOCKLIST + this.MAIN_BLOCK = Configuration.BLOCK_BUCKET .parseString(StringMan.join(config.getStringList("plot.filling"), ',')); - this.TOP_BLOCK = Configuration.BLOCKLIST + this.TOP_BLOCK = Configuration.BLOCK_BUCKET .parseString(StringMan.join(config.getStringList("plot.floor"), ',')); - this.WALL_BLOCK = Configuration.BLOCK.parseString(config.getString("wall.block")); + this.WALL_BLOCK = Configuration.BLOCK_BUCKET.parseString(config.getString("wall.block")); this.ROAD_HEIGHT = Math.min(255, config.getInt("road.height")); - this.ROAD_BLOCK = Configuration.BLOCK.parseString(config.getString("road.block")); - this.WALL_FILLING = Configuration.BLOCK.parseString(config.getString("wall.filling")); + this.ROAD_BLOCK = Configuration.BLOCK_BUCKET.parseString(config.getString("road.block")); + this.WALL_FILLING = Configuration.BLOCK_BUCKET.parseString(config.getString("wall.filling")); this.WALL_HEIGHT = Math.min(254, config.getInt("wall.height")); this.CLAIMED_WALL_BLOCK = - Configuration.BLOCK.parseString(config.getString("wall.block_claimed")); + Configuration.BLOCK_BUCKET.parseString(config.getString("wall.block_claimed")); } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java index d0c84a65e..e238c0593 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java @@ -98,7 +98,7 @@ public class HybridGen extends IndependentPlotGenerator { for (short z = 0; z < 16; z++) { // Road for (int y = 1; y <= hpw.ROAD_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.ROAD_BLOCK); + result.setBlock(x, y, z, hpw.ROAD_BLOCK.getBlock()); } if (hpw.ROAD_SCHEMATIC_ENABLED) { placeSchem(hpw, result, rx[x], rz[z], x, z); @@ -109,7 +109,7 @@ public class HybridGen extends IndependentPlotGenerator { if (gz[z]) { // road for (int y = 1; y <= hpw.ROAD_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.ROAD_BLOCK); + result.setBlock(x, y, z, hpw.ROAD_BLOCK.getBlock()); } if (hpw.ROAD_SCHEMATIC_ENABLED) { placeSchem(hpw, result, rx[x], rz[z], x, z); @@ -117,10 +117,10 @@ public class HybridGen extends IndependentPlotGenerator { } else { // wall for (int y = 1; y <= hpw.WALL_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.WALL_FILLING); + result.setBlock(x, y, z, hpw.WALL_FILLING.getBlock()); } if (!hpw.ROAD_SCHEMATIC_ENABLED) { - result.setBlock(x, hpw.WALL_HEIGHT + 1, z, hpw.WALL_BLOCK); + result.setBlock(x, hpw.WALL_HEIGHT + 1, z, hpw.WALL_BLOCK.getBlock()); } else { placeSchem(hpw, result, rx[x], rz[z], x, z); } @@ -131,7 +131,7 @@ public class HybridGen extends IndependentPlotGenerator { if (gz[z]) { // road for (int y = 1; y <= hpw.ROAD_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.ROAD_BLOCK); + result.setBlock(x, y, z, hpw.ROAD_BLOCK.getBlock()); } if (hpw.ROAD_SCHEMATIC_ENABLED) { placeSchem(hpw, result, rx[x], rz[z], x, z); @@ -139,10 +139,10 @@ public class HybridGen extends IndependentPlotGenerator { } else if (wz[z]) { // wall for (int y = 1; y <= hpw.WALL_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.WALL_FILLING); + result.setBlock(x, y, z, hpw.WALL_FILLING.getBlock()); } if (!hpw.ROAD_SCHEMATIC_ENABLED) { - result.setBlock(x, hpw.WALL_HEIGHT + 1, z, hpw.WALL_BLOCK); + result.setBlock(x, hpw.WALL_HEIGHT + 1, z, hpw.WALL_BLOCK.getBlock()); } else { placeSchem(hpw, result, rx[x], rz[z], x, z); } @@ -150,10 +150,10 @@ public class HybridGen extends IndependentPlotGenerator { // plot for (int y = 1; y < hpw.PLOT_HEIGHT; y++) { result.setBlock(x, y, z, - hpw.MAIN_BLOCK[random.random(hpw.MAIN_BLOCK.length)]); + hpw.MAIN_BLOCK.getBlock()); } result.setBlock(x, hpw.PLOT_HEIGHT, z, - hpw.TOP_BLOCK[random.random(hpw.TOP_BLOCK.length)]); + hpw.TOP_BLOCK.getBlock()); if (hpw.PLOT_SCHEMATIC) { placeSchem(hpw, result, rx[x], rz[z], x, z); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java index b727e0960..355c1a5a3 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java @@ -157,8 +157,8 @@ public class HybridPlotManager extends ClassicPlotManager { final boolean canRegen = (plotArea.TYPE == 0) && (plotArea.TERRAIN == 0) && REGENERATIVE_CLEAR; // The component blocks - final PlotBlock[] plotfloor = dpw.TOP_BLOCK; - final PlotBlock[] filling = dpw.MAIN_BLOCK; + final BlockBucket plotfloor = dpw.TOP_BLOCK; + final BlockBucket filling = dpw.MAIN_BLOCK; final PlotBlock bedrock; if (dpw.PLOT_BEDROCK) { bedrock = PlotBlock.get((short) 7, (byte) 0); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotWorld.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotWorld.java index ff5a9f247..2cc9a8bfa 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotWorld.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotWorld.java @@ -277,7 +277,7 @@ public class HybridPlotWorld extends ClassicPlotWorld { } this.ROAD_SCHEMATIC_ENABLED = true; // Do not populate road if using schematic population - this.ROAD_BLOCK = PlotBlock.getEmptyData(this.ROAD_BLOCK); // PlotBlock.get(this.ROAD_BLOCK.id, (byte) 0); + // TODO: What? this.ROAD_BLOCK = BlockBucket.empty(); // PlotBlock.getEmptyData(this.ROAD_BLOCK); // PlotBlock.get(this.ROAD_BLOCK.id, (byte) 0); short[] ids1 = schematic1.getIds(); byte[] datas1 = schematic1.getDatas(); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridUtils.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridUtils.java index cad0e1685..7d102784d 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridUtils.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridUtils.java @@ -123,63 +123,6 @@ public abstract class HybridUtils { return chunks; } - /** - * Checks all connected plots. - * - * @param plot - * @param whenDone - */ - public void checkModified(final Plot plot, final RunnableVal whenDone) { - if (whenDone == null) { - return; - } - PlotArea plotArea = plot.getArea(); - if (!(plotArea instanceof ClassicPlotWorld)) { - whenDone.value = -1; - TaskManager.runTask(whenDone); - return; - } - whenDone.value = 0; - final ClassicPlotWorld cpw = (ClassicPlotWorld) plotArea; - final ArrayDeque zones = new ArrayDeque<>(plot.getRegions()); - final LocalBlockQueue queue = GlobalBlockQueue.IMP.getNewQueue(cpw.worldname, false); - Runnable run = new Runnable() { - @Override public void run() { - if (zones.isEmpty()) { - - TaskManager.runTask(whenDone); - return; - } - RegionWrapper region = zones.poll(); - Location pos1 = - new Location(plot.getWorldName(), region.minX, region.minY, region.minZ); - Location pos2 = - new Location(plot.getWorldName(), region.maxX, region.maxY, region.maxZ); - ChunkManager.chunkTask(pos1, pos2, new RunnableVal() { - @Override public void run(int[] value) { - ChunkLoc loc = new ChunkLoc(value[0], value[1]); - ChunkManager.manager.loadChunk(plot.getWorldName(), loc, false); - int bx = value[2]; - int bz = value[3]; - int ex = value[4]; - int ez = value[5]; - whenDone.value += - checkModified(queue, bx, ex, 1, cpw.PLOT_HEIGHT - 1, bz, ez, - cpw.MAIN_BLOCK); - whenDone.value += - checkModified(queue, bx, ex, cpw.PLOT_HEIGHT, cpw.PLOT_HEIGHT, bz, ez, - cpw.TOP_BLOCK); - whenDone.value += - checkModified(queue, bx, ex, cpw.PLOT_HEIGHT + 1, 255, bz, ez, - new PlotBlock[] {PlotBlock.get((short) 0, (byte) 0)}); - } - }, this, 5); - - } - }; - run.run(); - } - public boolean scheduleRoadUpdate(PlotArea area, int extend) { if (HybridUtils.UPDATE) { return false; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/BlockBucket.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/BlockBucket.java index 49d921ad8..fb4716d8d 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/BlockBucket.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/BlockBucket.java @@ -1,11 +1,17 @@ package com.github.intellectualsites.plotsquared.plot.object; +import com.github.intellectualsites.plotsquared.configuration.serialization.ConfigurationSerializable; import com.github.intellectualsites.plotsquared.plot.config.C; +import com.github.intellectualsites.plotsquared.plot.config.Configuration; +import com.google.common.collect.ImmutableMap; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Random; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -16,8 +22,9 @@ import lombok.RequiredArgsConstructor; * A block bucket is a container of block types, where each block * has a specified chance of being randomly picked */ +@EqualsAndHashCode @SuppressWarnings({"unused", "WeakerAccess"}) -public final class BlockBucket implements Iterable { +public final class BlockBucket implements Iterable, ConfigurationSerializable { private final Random random = new Random(); private final Map ranges = new HashMap<>(); @@ -25,6 +32,18 @@ public final class BlockBucket implements Iterable { private final BucketIterator bucketIterator = new BucketIterator(); private boolean compiled; + public static BlockBucket withSingle(@NonNull final PlotBlock block) { + final BlockBucket blockBucket = new BlockBucket(); + blockBucket.addBlock(block); + return blockBucket; + } + + public static BlockBucket empty() { + final BlockBucket bucket = new BlockBucket(); + bucket.compiled = true; + return bucket; + } + public BlockBucket() { this.blocks = new HashMap<>(); } @@ -38,10 +57,51 @@ public final class BlockBucket implements Iterable { this.compiled = false; } + public boolean isEmpty() { + if (isCompiled()) { + return ranges.isEmpty(); + } + return blocks.isEmpty(); + } + + /** + * Get all blocks that are configured in the bucket + * + * @return Immutable collection containing all blocks that can + * be found in the bucket + */ + public Collection getBlocks() { + if (!isCompiled()) { + this.compile(); + } + return Collections.unmodifiableCollection(this.ranges.values()); + } + + /** + * Get a collection containing a specified amount of randomly selected blocks + * + * @param count Number of blocks + * @return Immutable collection containing randomly selected blocks + */ + public Collection getBlocks(final int count) { + final List blocks = new ArrayList<>(count); + for (int i = 0; i < count; i++) { + blocks.add(getBlock()); + } + return Collections.unmodifiableCollection(blocks); + } + public void compile() { if (isCompiled()) { return; } + + if (blocks.size() == 1) { + this.ranges.put(new Range(0, 100, true), blocks.keySet().toArray(new PlotBlock[1])[0]); + this.compiled = true; + return; + } + final Map temp = new HashMap<>(blocks.size()); final List unassigned = new ArrayList<>(blocks.size()); @@ -91,7 +151,7 @@ public final class BlockBucket implements Iterable { final int rangeStart = start; final int rangeEnd = rangeStart + entry.getValue(); start = rangeEnd + 1; - final Range range = new Range(rangeStart, rangeEnd); + final Range range = new Range(rangeStart, rangeEnd, unassigned.contains(entry.getKey())); this.ranges.put(range, entry.getKey()); } this.blocks.clear(); @@ -116,6 +176,9 @@ public final class BlockBucket implements Iterable { if (!isCompiled()) { this.compile(); } + if (this.isEmpty()) { + return StringPlotBlock.EVERYTHING; + } final int number = random.nextInt(101); for (final Map.Entry entry : ranges.entrySet()) { if (entry.getKey().isInRange(number)) { @@ -126,6 +189,34 @@ public final class BlockBucket implements Iterable { return getBlock(); } + @Override public String toString() { + final StringBuilder builder = new StringBuilder(); + final Iterator> iterator = this.ranges.entrySet().iterator(); + while (iterator.hasNext()) { + final Entry entry = iterator.next(); + builder.append(entry.getValue().getRawId()); + if (!entry.getKey().isAutomatic()) { + builder.append(":").append(entry.getKey().getWeight()); + } + if (iterator.hasNext()) { + builder.append(","); + } + } + return builder.toString(); + } + + @Override + public Map serialize() { + return ImmutableMap.of("blocks", this.toString()); + } + + public static BlockBucket deserialize(@NonNull final Map map) { + if (!map.containsKey("blocks")) { + return null; + } + return Configuration.BLOCK_BUCKET.parseString(map.get("blocks").toString()); + } + private final class BucketIterator implements Iterator { @Override @@ -146,6 +237,12 @@ public final class BlockBucket implements Iterable { private final int min; private final int max; + @Getter + private final boolean automatic; + + public int getWeight() { + return max - min; + } public boolean isInRange(final int num) { return num <= max && num >= min; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index 99ac66073..0d3721262 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -1495,8 +1495,8 @@ public class Plot { * (components are generator specific) */ public boolean setComponent(String component, String blocks) { - PlotBlock[] parsed = Configuration.BLOCKLIST.parseString(blocks); - return !(parsed == null || parsed.length == 0) && this.setComponent(component, parsed); + BlockBucket parsed = Configuration.BLOCK_BUCKET.parseString(blocks); + return !(parsed == null || parsed.isEmpty()) && this.setComponent(component, parsed); } /** @@ -2722,7 +2722,7 @@ public class Plot { * @param blocks * @return */ - public boolean setComponent(String component, PlotBlock[] blocks) { + public boolean setComponent(String component, BlockBucket blocks) { if (StringMan .isEqualToAny(component, getManager().getPlotComponents(this.area, this.getId()))) { EventUtil.manager.callComponentSet(this, component); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotBlock.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotBlock.java index 4599c0d14..70ec0eb6d 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotBlock.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotBlock.java @@ -1,12 +1,14 @@ package com.github.intellectualsites.plotsquared.plot.object; +import com.github.intellectualsites.plotsquared.configuration.serialization.ConfigurationSerializable; import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.Settings; +import com.google.common.collect.ImmutableMap; +import java.util.Collection; +import java.util.Map; import lombok.NonNull; -import java.util.Collection; - -public abstract class PlotBlock { +public abstract class PlotBlock implements ConfigurationSerializable { private static Class conversionType; private static BlockRegistry blockRegistry; @@ -38,6 +40,18 @@ public abstract class PlotBlock { } } + public static PlotBlock deserialize(@NonNull final Map map) { + if (map.containsKey("material")) { + final Object object = map.get("material"); + return get(object); + } + return null; + } + + @Override public Map serialize() { + return ImmutableMap.of("material", this.getRawId()); + } + public abstract boolean isAir(); public static StringPlotBlock get(@NonNull final String itemId) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotManager.java index b6c887bdf..4a7b3b8a2 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotManager.java @@ -42,7 +42,7 @@ public abstract class PlotManager { public abstract String[] getPlotComponents(PlotArea plotArea, PlotId plotId); public abstract boolean setComponent(PlotArea plotArea, PlotId plotId, String component, - PlotBlock[] blocks); + BlockBucket blocks); /* * PLOT MERGING (return false if your generator does not support plot diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java index 51d5f7885..bc34ac2ec 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java @@ -17,12 +17,31 @@ import java.nio.ByteBuffer; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; +import lombok.NonNull; /** * The abstract class supporting {@code BukkitPlayer} and {@code SpongePlayer}. */ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { + public interface PlotPlayerConverter { + PlotPlayer convert(BaseObject object); + } + private static final Map converters = new HashMap<>(); + + public static PlotPlayer from(@NonNull final T object) { + if (!converters.containsKey(object.getClass())) { + throw new IllegalArgumentException( + String.format("There is no registered PlotPlayer converter for type %s", + object.getClass().getSimpleName())); + } + return converters.get(object.getClass()).convert(object); + } + + public static void registerConverter(@NonNull final Class clazz, final PlotPlayerConverter converter) { + converters.put(clazz, converter); + } + public static final String META_LAST_PLOT = "lastplot"; public static final String META_LOCATION = "location"; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotManager.java index 13566671e..0222dc815 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotManager.java @@ -60,7 +60,7 @@ public class SinglePlotManager extends PlotManager { } @Override public boolean setComponent(PlotArea plotArea, PlotId plotId, String component, - PlotBlock[] blocks) { + BlockBucket blocks) { return false; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/LocalBlockQueue.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/LocalBlockQueue.java index 688b5bb14..748a985af 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/LocalBlockQueue.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/LocalBlockQueue.java @@ -109,13 +109,13 @@ public abstract class LocalBlockQueue { } } - public void setCuboid(Location pos1, Location pos2, PlotBlock[] blocks) { + public void setCuboid(Location pos1, Location pos2, BlockBucket blocks) { for (int y = pos1.getY(); y <= Math.min(255, pos2.getY()); y++) { for (int x = pos1.getX(); x <= pos2.getX(); x++) { for (int z = pos1.getZ(); z <= pos2.getZ(); z++) { - int i = PseudoRandom.random.random(blocks.length); - PlotBlock block = blocks[i]; - setBlock(x, y, z, block); + // int i = PseudoRandom.random.random(blocks.length); + // PlotBlock block = blocks[i]; + setBlock(x, y, z, blocks.getBlock()); } } }