From f73a542b047f1ae3588356c5af2bd5074d567483 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Wed, 24 Feb 2016 05:12:07 +1100 Subject: [PATCH] Tab completion, fix SpongeUtil.getWorld --- .../plot/util/ReflectionUtils.java | 2 +- .../sponge/util/SpongeChunkManager.java | 29 +++++++---- .../sponge/util/SpongeCommand.java | 50 +++++++++++++++---- .../plotsquared/sponge/util/SpongeUtil.java | 9 ++-- 4 files changed, 67 insertions(+), 23 deletions(-) diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java b/Core/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java index 79e637c4a..ca99e96f9 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java @@ -74,7 +74,7 @@ public class ReflectionUtils { public static Field findField(Class clazz, Class fieldClass) { for (Field field : clazz.getFields()) { - if (field.getClass() == fieldClass) { + if (fieldClass == field.getType() || fieldClass.isAssignableFrom(field.getType())) { field.setAccessible(true); return field; } diff --git a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeChunkManager.java b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeChunkManager.java index 0906cf320..b55491b7a 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeChunkManager.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeChunkManager.java @@ -69,7 +69,7 @@ public class SpongeChunkManager extends ChunkManager { // TODO save world; return super.getChunkChunks(world); } - + @Override public void regenerateChunk(final String world, final ChunkLoc loc) { final World spongeWorld = SpongeUtil.getWorld(world); @@ -83,22 +83,31 @@ public class SpongeChunkManager extends ChunkManager { PS.debug("Not valid world generator for: " + world); return; } - ChunkProviderServer chunkProvider = (ChunkProviderServer) provider; - long pos = loc.x & 4294967295L | (loc.z & 4294967295L) << 32; + + ChunkProviderServer chunkServer = (ChunkProviderServer) provider; + IChunkProvider chunkProvider = chunkServer.serverChunkGenerator; + + long pos = ChunkCoordIntPair.chunkXZ2Int(loc.x, loc.z); + System.out.println((loc.x & 4294967295L | (loc.z & 4294967295L) << 32) + ":" + pos); net.minecraft.world.chunk.Chunk mcChunk = (net.minecraft.world.chunk.Chunk) spongeChunk; - if (provider.chunkExists(loc.x, loc.z)) { - mcChunk = chunkProvider.loadChunk(loc.x, loc.z); + if (chunkServer.chunkExists(loc.x, loc.z)) { + mcChunk = chunkServer.loadChunk(loc.x, loc.z); mcChunk.onChunkUnload(); } - Set set = (Set) chunkProvider.getClass().getDeclaredField("droppedChunksSet").get(chunkProvider); +// Set set = (Set) chunkProvider.getClass().getDeclaredField("droppedChunksSet").get(chunkProvider); + Set set = (Set) ReflectionUtils.findField(chunkServer.getClass(), Set.class).get(chunkServer); set.remove(pos); - chunkProvider.id2ChunkMap.remove(pos); - mcChunk = provider.provideChunk(loc.x, loc.z); - chunkProvider.id2ChunkMap.add(pos, mcChunk); - chunkProvider.loadedChunks.add(mcChunk); + chunkServer.id2ChunkMap.remove(pos); + mcChunk = chunkProvider.provideChunk(loc.x, loc.z); + chunkServer.id2ChunkMap.add(pos, mcChunk); + chunkServer.loadedChunks.add(mcChunk); if (mcChunk != null) { mcChunk.onChunkLoad(); mcChunk.populateChunk(chunkProvider, chunkProvider, loc.x, loc.z); + System.out.println("WORKED?"); + } + else { + PS.debug("CHUNK IS NULL!?"); } } catch (Throwable e){ e.printStackTrace(); diff --git a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeCommand.java b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeCommand.java index 83567fcf7..91bba3862 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeCommand.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeCommand.java @@ -3,6 +3,9 @@ package com.plotsquared.sponge.util; import com.intellectualcrafters.plot.commands.MainCommand; import com.intellectualcrafters.plot.object.ConsolePlayer; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.Permissions; +import com.intellectualcrafters.plot.util.StringComparison; +import com.plotsquared.general.commands.Command; import com.plotsquared.sponge.SpongeMain; import org.spongepowered.api.command.CommandCallable; import org.spongepowered.api.command.CommandException; @@ -11,11 +14,7 @@ import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.text.Text; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.UUID; +import java.util.*; public class SpongeCommand implements CommandCallable { @@ -38,10 +37,43 @@ public class SpongeCommand implements CommandCallable { } @Override - public List getSuggestions(final CommandSource cmd, final String string) throws CommandException { - // TODO Auto-generated method stub - return new ArrayList<>(Collections.singletonList("TEST")); - } + public List getSuggestions(final CommandSource source, final String string) throws CommandException { + if (!(source instanceof Player)) { + return null; + } + final PlotPlayer player = SpongeUtil.getPlayer((Player) source); + String[] split = string.split(" "); + if (split.length < 2) { + return Collections.singletonList("plots"); + } + if (split.length > 2) { + return null; + } + final Set tabOptions = new HashSet<>(); + final String arg = split[1].toLowerCase(); + ArrayList labels = new ArrayList<>(); + for (final Command cmd : MainCommand.getInstance().getCommands()) { + final String label = cmd.getCommand(); + HashSet aliases = new HashSet<>(cmd.getAliases()); + aliases.add(label); + for (String alias : aliases) { + labels.add(alias); + if (alias.startsWith(arg)) { + if (Permissions.hasPermission(player, cmd.getPermission())) { + tabOptions.add(label); + } else { + break; + } + } + } + } + String best = new StringComparison<>(arg, labels).getBestMatch(); + tabOptions.add(best); + if (!tabOptions.isEmpty()) { + return new ArrayList<>(tabOptions); + } + return null; +} @Override public boolean testPermission(final CommandSource cmd) { 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 0c4cd175f..8f21465df 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java @@ -206,16 +206,19 @@ public class SpongeUtil extends WorldUtil { } private static World lastWorld; + private static String last; public static World getWorld(final String world) { - if (world.equals(lastWorld.getName())) { + if (StringMan.isEqual(world, last)) { return lastWorld; } final Optional optional = Sponge.getServer().getWorld(world); if (!optional.isPresent()) { - return null; + last = null; + return lastWorld = null; } - return optional.get(); + last = world; + return lastWorld = optional.get(); } public static void removePlayer(final String player) {