diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java index 4e9a9b925..ce63beb61 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java @@ -16,6 +16,7 @@ import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.TaskManager; import com.plotsquared.general.commands.CommandDeclaration; +import java.util.Set; @CommandDeclaration(command = "auto", permission = "plots.auto", @@ -134,22 +135,7 @@ public class Auto extends SubCommand { } // TODO handle type 2 the same as normal worlds! if (size_x == 1 && size_z == 1) { - final String finalSchematic = schematic; - autoClaimSafe(player, plotarea, null, new RunnableVal() { - @Override - public void run(final Plot plot) { - TaskManager.IMP.sync(new RunnableVal() { - @Override - public void run(Object ignore) { - if (plot == null) { - MainUtil.sendMessage(player, C.NO_FREE_PLOTS); - } else { - plot.claim(player, true, finalSchematic, false); - } - } - }); - } - }); + autoClaimSafe(player, plotarea, null, schematic); return true; } else { if (plotarea.TYPE == 2) { @@ -179,6 +165,12 @@ public class Auto extends SubCommand { } } + /** + * Get the next plot id (spiral out from 0,0) + * @param start + * @return + */ + @Deprecated public static PlotId getNextPlot(PlotId start) { int plots; PlotId center; @@ -197,7 +189,48 @@ public class Auto extends SubCommand { return null; } - public void autoClaimSafe(final PlotPlayer player, final PlotArea area, PlotId start, final RunnableVal whenDone) { + /** + * Teleport the player home, or claim a new plot + * @param player + * @param area + * @param start + * @param schem + */ + public static void homeOrAuto(final PlotPlayer player, final PlotArea area, PlotId start, final String schem) { + Set plots = player.getPlots(); + if (!plots.isEmpty()) { + plots.iterator().next().teleportPlayer(player); + } else { + autoClaimSafe(player, area, start, schem); + } + } + + /** + * Claim a new plot for a player + * @param player + * @param area + * @param start + * @param schem + */ + public static void autoClaimSafe(final PlotPlayer player, final PlotArea area, PlotId start, final String schem) { + autoClaimFromDatabase(player, area, start, new RunnableVal() { + @Override + public void run(final Plot plot) { + TaskManager.IMP.sync(new RunnableVal() { + @Override + public void run(Object ignore) { + if (plot == null) { + MainUtil.sendMessage(player, C.NO_FREE_PLOTS); + } else { + plot.claim(player, true, schem, false); + } + } + }); + } + }); + } + + private static void autoClaimFromDatabase(final PlotPlayer player, final PlotArea area, PlotId start, final RunnableVal whenDone) { final Plot plot = area.getNextFreePlot(player, start); if (plot == null) { whenDone.run(null); @@ -208,7 +241,7 @@ public class Auto extends SubCommand { DBFunc.createPlotSafe(plot, whenDone, new Runnable() { @Override public void run() { - autoClaimSafe(player, area, plot.getId(), whenDone); + autoClaimFromDatabase(player, area, plot.getId(), whenDone); } }); } diff --git a/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java b/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java index dd537f821..314f1fe97 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java @@ -56,6 +56,7 @@ import com.plotsquared.sponge.util.block.SpongeLocalQueue; import com.plotsquared.sponge.uuid.SpongeLowerOfflineUUIDWrapper; import com.plotsquared.sponge.uuid.SpongeOnlineUUIDWrapper; import com.plotsquared.sponge.uuid.SpongeUUIDHandler; +import java.io.IOException; import net.minecrell.mcstats.SpongeStatsLite; import org.slf4j.Logger; import org.spongepowered.api.Game; @@ -158,7 +159,6 @@ public class SpongeMain implements IPlotMain { public void unload() { PlotAreaManager manager = PS.get().getPlotAreaManager(); if (manager instanceof SinglePlotAreaManager) { - long start = System.currentTimeMillis(); SinglePlotArea area = ((SinglePlotAreaManager) manager).getArea(); for (World world : Sponge.getServer().getWorlds()) { String name = world.getName(); @@ -168,13 +168,21 @@ public class SpongeMain implements IPlotMain { if (plot != null) { List players = plot.getPlayersInPlot(); if (players.isEmpty() && PlotPlayer.wrap(plot.owner) == null) { + try { + world.save(); + } catch (IOException e) { + e.printStackTrace(); + return; + } + long start = System.currentTimeMillis(); for (Chunk chunk : world.getLoadedChunks()) { chunk.unloadChunk(); - if (System.currentTimeMillis() - start > 20) { + if (System.currentTimeMillis() - start > 10) { return; } } Sponge.getServer().unloadWorld(world); + return; } } } diff --git a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java index 1d4357ba6..1ec24af0a 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java @@ -17,7 +17,16 @@ import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.util.WorldUtil; import com.plotsquared.sponge.SpongeMain; import com.plotsquared.sponge.object.SpongePlayer; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Optional; import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; import net.minecraft.world.biome.Biome; import org.spongepowered.api.Sponge; import org.spongepowered.api.block.BlockState; @@ -41,24 +50,11 @@ import org.spongepowered.api.world.biome.BiomeType; import org.spongepowered.api.world.biome.BiomeTypes; import org.spongepowered.api.world.extent.Extent; -import java.io.IOException; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Optional; - public class SpongeUtil extends WorldUtil { public static Cause CAUSE = Cause.of(NamedCause.source(Sponge.getPluginManager().fromInstance(SpongeMain.THIS).get())); private static BiomeType[] biomes; private static HashMap biomeMap; - private static HashMap stateMap; - private static BlockState[] stateArray; private static Player lastPlayer = null; private static PlotPlayer lastPlotPlayer = null; private static World lastWorld; @@ -141,40 +137,19 @@ public class SpongeUtil extends WorldUtil { }; } - private static void initBlockCache() { - try { - PS.debug("Caching block id/data: Please wait..."); - stateArray = new BlockState[Character.MAX_VALUE]; - stateMap = new HashMap<>(); - Method methodGetByCombinedId = ReflectionUtils - .findMethod(Class.forName("net.minecraft.block.Block"), true, Class.forName("net.minecraft.block.state.IBlockState"), int.class); - for (int i = 0; i < Character.MAX_VALUE; i++) { - try { - BlockState state = (BlockState) methodGetByCombinedId.invoke(null, i); - if (state.getType() == BlockTypes.AIR) { - continue; - } - PlotBlock plotBlock = PlotBlock.get((short) (i & 0xFFF), (byte) (i >> 12 & 0xF)); - stateArray[i] = state; - stateMap.put(state, plotBlock); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ignored) { - } - } - PS.debug("Done!"); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - } - public static BlockState getBlockState(int id, int data) { return (BlockState) Block.getBlockById(id).getStateFromMeta(data); } public static PlotBlock getPlotBlock(BlockState state) { - if (stateMap == null) { - initBlockCache(); + if (state == null) { + return PlotBlock.get(0, 0); } - return stateMap.get(state); + IBlockState ibs = ((IBlockState) state); + Block block = ibs.getBlock(); + int id = Block.getIdFromBlock(block); + int data = block.getMetaFromState(ibs); + return PlotBlock.get(id, data); } public static Location getLocation(org.spongepowered.api.world.Location block) {