Tab completion, fix SpongeUtil.getWorld

This commit is contained in:
Jesse Boyd 2016-02-24 05:12:07 +11:00
parent bf85ba5833
commit f73a542b04
4 changed files with 67 additions and 23 deletions

View File

@ -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;
}

View File

@ -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<Long> set = (Set<Long>) chunkProvider.getClass().getDeclaredField("droppedChunksSet").get(chunkProvider);
// Set<Long> set = (Set<Long>) chunkProvider.getClass().getDeclaredField("droppedChunksSet").get(chunkProvider);
Set<Long> set = (Set<Long>) 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();

View File

@ -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,9 +37,42 @@ public class SpongeCommand implements CommandCallable {
}
@Override
public List<String> getSuggestions(final CommandSource cmd, final String string) throws CommandException {
// TODO Auto-generated method stub
return new ArrayList<>(Collections.singletonList("TEST"));
public List<String> 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<String> tabOptions = new HashSet<>();
final String arg = split[1].toLowerCase();
ArrayList<String> labels = new ArrayList<>();
for (final Command<PlotPlayer> cmd : MainCommand.getInstance().getCommands()) {
final String label = cmd.getCommand();
HashSet<String> 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

View File

@ -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<World> 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) {