Skip to content

Commit

Permalink
List command and response rework
Browse files Browse the repository at this point in the history
Workaround for #9 with /list carpet removeOrphan
  • Loading branch information
ishland committed Oct 3, 2020
1 parent e707857 commit 8b9edf8
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.ishland.bukkit.carpetplugin;

import com.ishland.bukkit.carpetplugin.commands.ListCommand;
import com.ishland.bukkit.carpetplugin.commands.PlayerCommand;
import org.bukkit.plugin.java.JavaPlugin;

public final class CarpetPlugin extends JavaPlugin {

public static CarpetPlugin instance;
private PlayerCommand playerCommand;
private ListCommand listCommand;

@Override
public void onEnable() {
instance = this;
playerCommand = new PlayerCommand();
playerCommand.register();
listCommand = new ListCommand();
listCommand.register();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.ishland.bukkit.carpetplugin.commands;

import com.ishland.bukkit.carpetplugin.lib.fakeplayer.base.FakeEntityPlayer;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.minecraft.server.ChatMessage;
import net.minecraft.server.CommandListenerWrapper;
import net.minecraft.server.DedicatedPlayerList;
import net.minecraft.server.EntityPlayer;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftChatMessage;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import static com.ishland.bukkit.carpetplugin.utils.BrigadierUtils.literal;
import static com.ishland.bukkit.carpetplugin.utils.BrigadierUtils.sendMessage;

public class ListCommand {

public void register() {
final CommandDispatcher<CommandListenerWrapper> commandDispatcher =
((CraftServer) Bukkit.getServer()).getHandle().getServer()
.getCommandDispatcher().a();
commandDispatcher.register(literal("list")
.then(literal("carpet")
.then(literal("removeOrphan")
.executes(ListCommand::removeOrphan)
)
.executes(ListCommand::listCarpet)
)
);
}

private static int removeOrphan(CommandContext<CommandListenerWrapper> ctx) {
DedicatedPlayerList playerList = ((CraftServer) Bukkit.getServer()).getServer().getPlayerList();
List<EntityPlayer> players =
new CopyOnWriteArrayList<>(playerList.players);
for (EntityPlayer player : players)
if (playerList.getPlayer(player.getName()) != player) {
ctx.getSource().getBukkitSender().sendMessage(
"Trying to get rid of " + player.getName()
+ " (" + player.getClass().getName() + "), " +
"(UUID of " + player.getUniqueID() + ")");
if (player.networkManager != null) try {
player.networkManager.j().a(new ChatMessage("Remove orphan"));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
try {
String disconnectMessage = playerList.disconnect(player);
if(disconnectMessage != null && disconnectMessage.length() > 0)
playerList.sendMessage(CraftChatMessage.fromString(disconnectMessage));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
playerList.players.remove(player);
}
return 1;
}

private static int listCarpet(CommandContext<CommandListenerWrapper> ctx) {
DedicatedPlayerList playerList = ((CraftServer) Bukkit.getServer()).getServer().getPlayerList();
List<EntityPlayer> players =
new CopyOnWriteArrayList<>(playerList.players);
sendMessage(ctx, new ComponentBuilder()
.append("There are ").append(String.valueOf(players.size())).append(" of a max of ")
.append(String.valueOf(playerList.getMaxPlayers())).append(" players online: ")
.create());
for (EntityPlayer player : players) {
boolean isPossibleOrphan = playerList.getPlayer(player.getName()) != player;
sendMessage(ctx, new ComponentBuilder()
.append(
isPossibleOrphan
? "[Possible Orphan player] "
: ""
).color(ChatColor.RED)
.append(
player instanceof FakeEntityPlayer
? "CarpetPlugin player "
: "Others (" + player.getClass().getName() + ") "
)
.append(player.getName())
.append("(UUID of " + player.getUniqueID().toString() + ")")
.create()
);
}
return 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static com.ishland.bukkit.carpetplugin.utils.BrigadierUtils.getPlayers;
import static com.ishland.bukkit.carpetplugin.utils.BrigadierUtils.hasPermission;
import static com.ishland.bukkit.carpetplugin.utils.BrigadierUtils.literal;
import static com.ishland.bukkit.carpetplugin.utils.BrigadierUtils.sendMessage;
import static com.ishland.bukkit.carpetplugin.utils.BrigadierUtils.suggestMatching;

public class PlayerCommand {
Expand Down Expand Up @@ -200,12 +201,12 @@ private static int lookAtBlock(CommandContext<CommandListenerWrapper> ctx) {

private static int actions(CommandContext<CommandListenerWrapper> ctx) {
return manipulate(ctx, ap -> {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Activated actions: ").color(ChatColor.GOLD)
.create()
);
for (FakeEntityPlayerActionPack.Action action : ap.getActivatedActions())
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append(action.toString()).color(ChatColor.AQUA)
.create()
);
Expand All @@ -214,7 +215,7 @@ private static int actions(CommandContext<CommandListenerWrapper> ctx) {

private static int manipulate(CommandContext<CommandListenerWrapper> ctx, Consumer<FakeEntityPlayerActionPack> consumer) {
if (!isFakePlayer(ctx)) {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Only fake players can manipulate").color(ChatColor.RED).append("").reset()
.create()
);
Expand Down Expand Up @@ -243,7 +244,7 @@ public void shutdown() {

private static int kill(CommandContext<CommandListenerWrapper> ctx) {
if (!isFakePlayer(ctx)) {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Only fake players can be killed").color(ChatColor.RED).append("").reset()
.create()
);
Expand All @@ -259,7 +260,7 @@ private static int spawnAsync(CommandContext<CommandListenerWrapper> ctx) {
Preconditions.checkNotNull(playerName);
Preconditions.checkArgument(!playerName.isEmpty());
if (playerName.length() > 40) {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Player name ").color(ChatColor.RED).append("").reset()
.append(playerName).color(ChatColor.RED).bold(true).append("").reset()
.append(" is longer than 40 characters").color(ChatColor.RED).append("").reset()
Expand All @@ -273,12 +274,12 @@ private static int spawnAsync(CommandContext<CommandListenerWrapper> ctx) {
|| server.getUserCache().getProfile(playerName) != null)
server.execute(() -> spawn(ctx));
else
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Player not found").color(ChatColor.RED).bold(true).append("").reset()
.create()
);
});
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Command queued, please wait... ").color(ChatColor.GREEN).bold(true).append("").reset()
.create()
);
Expand All @@ -291,7 +292,7 @@ private static int spawn(CommandContext<CommandListenerWrapper> ctx) {
Preconditions.checkNotNull(playerName);
Preconditions.checkArgument(!playerName.isEmpty());
if (playerName.length() > 16) {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Player name ").color(ChatColor.RED).append("").reset()
.append(playerName).color(ChatColor.RED).bold(true).append("").reset()
.append(" is longer than 16 characters").color(ChatColor.RED).append("").reset()
Expand All @@ -317,17 +318,17 @@ private static int spawn(CommandContext<CommandListenerWrapper> ctx) {
worldServer,
gameMode);
if (entityPlayer == null) {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Player ").color(ChatColor.RED).append("").reset()
.append(playerName).color(ChatColor.RED).bold(true).append("").reset()
.append(" cannot get spawned").color(ChatColor.RED).append("").reset()
.create()
);
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Possible problems: ").color(ChatColor.RED).append("").reset()
.create()
);
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("1. Player doesn't exists and cannot spawn in online mode. ").color(ChatColor.RED).append("").reset()
.create()
);
Expand All @@ -340,15 +341,15 @@ private static boolean canSpawn(CommandContext<CommandListenerWrapper> ctx) {
String playerName = StringArgumentType.getString(ctx, "player");
final Location location = ctx.getSource().getBukkitLocation();
if (location == null) {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Player can only spawned with location").color(ChatColor.RED).append("").reset()
.create()
);
return false;
}
final Player player = Bukkit.getPlayerExact(playerName);
if (player != null) {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Player ").color(ChatColor.RED).append("").reset()
.append(playerName).color(ChatColor.RED).bold(true).append("").reset()
.append(" is already logged on").color(ChatColor.RED).append("").reset()
Expand All @@ -362,7 +363,7 @@ private static boolean canSpawn(CommandContext<CommandListenerWrapper> ctx) {
GameProfile profile = server.getUserCache().getProfileIfCached(playerName);
if (profile == null) profile = server.getUserCache().getProfile(playerName);
if (playerList.getProfileBans().isBanned(profile)) {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Player ").color(ChatColor.RED).append("").reset()
.append(playerName).color(ChatColor.RED).bold(true).append("").reset()
.append(" is banned").color(ChatColor.RED).append("").reset()
Expand All @@ -373,7 +374,7 @@ private static boolean canSpawn(CommandContext<CommandListenerWrapper> ctx) {

if (playerList.getHasWhitelist() && profile != null && playerList.isWhitelisted(profile)
&& !ctx.getSource().getBukkitSender().hasPermission("carpet.player.spawn.whitelist")) {
ctx.getSource().getBukkitSender().sendMessage(new ComponentBuilder()
sendMessage(ctx, new ComponentBuilder()
.append("Player ").color(ChatColor.RED).append("").reset()
.append(playerName).color(ChatColor.RED).bold(true).append("").reset()
.append(" is whitelisted and insufficient permission").color(ChatColor.RED).append("").reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.minecraft.server.ChatMessage;
import net.minecraft.server.CommandListenerWrapper;
import net.minecraft.server.EntityPlayer;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.craftbukkit.CraftServer;

import java.util.Collections;
import java.util.Locale;
Expand Down Expand Up @@ -44,13 +48,17 @@ public static CompletableFuture<Suggestions> suggestMatching(Iterable<String> it

public static Set<String> getPlayers() {
Set<String> players = Sets.newHashSet("Steve", "Alex");
for (Player player : Bukkit.getOnlinePlayers())
if (((CraftPlayer) player).getHandle() instanceof FakeEntityPlayer)
for (EntityPlayer player : ((CraftServer) Bukkit.getServer()).getServer().getPlayerList().getPlayers())
if (player instanceof FakeEntityPlayer)
players.add(player.getName());
return Collections.unmodifiableSet(players);
}

public static Predicate<CommandListenerWrapper> hasPermission(String s) {
return (player) -> player.getBukkitSender().hasPermission(s);
}

public static void sendMessage(CommandContext<CommandListenerWrapper> ctx, BaseComponent... components) {
ctx.getSource().sendMessage(new ChatMessage(new TextComponent(components).toLegacyText()), false);
}
}

0 comments on commit 8b9edf8

Please sign in to comment.