Skip to content
Permalink
Browse files

Port God Component commands to Piston

  • Loading branch information
DarkArc committed Jan 1, 2020
1 parent ee712a5 commit 3a00baab44d7eb589fb4b1708cb4b5419157d0c8
@@ -24,6 +24,7 @@
import com.sk89q.commandbook.config.LegacyCommandBookConfigurationMigrator;
import com.sk89q.util.yaml.YAMLFormat;
import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
import com.sk89q.worldedit.internal.command.CommandUtil;
import com.zachsthings.libcomponents.InjectComponent;
import com.zachsthings.libcomponents.InjectComponentAnnotationHandler;
@@ -39,11 +40,13 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.enginehub.piston.CommandManager;
import org.yaml.snakeyaml.error.YAMLException;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;

@@ -82,6 +85,10 @@ public static void registerEvents(Listener listener) {
server().getPluginManager().registerEvents(listener, inst());
}

public static void registerComponentCommands(BiConsumer<CommandManager, CommandRegistrationHandler> op) {
inst().commandManager.registerComponentCommands(op);
}

private void publishPistonCommands() {
commandManager.registerCommandsWith(this);
}
@@ -36,6 +36,7 @@

import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -90,6 +91,12 @@ private void registerCoreCommands() {
CommandBookCommands.register(commandManagerService, commandManager, registration);
}

public void registerComponentCommands(BiConsumer<CommandManager, CommandRegistrationHandler> op) {
CommandManager componentManager = commandManagerService.newCommandManager();
op.accept(componentManager, registration);
commandManager.registerManager(componentManager);
}

public void registerCommandsWith(CommandBook commandBook) {
BukkitCommandInspector inspector = new BukkitCommandInspector(commandBook, commandManager);

@@ -144,7 +151,7 @@ private void handleUnknownException(Actor actor, Throwable t) {
log.error("An unexpected error while handling a CommandBook command", t);
}

protected void handleCommand(CommandSender sender, String arguments) {
public void handleCommand(CommandSender sender, String arguments) {
Actor actor = WorldEditAdapter.adapt(sender);
String[] split = parseArgs(arguments).map(Substring::getSubstring).toArray(String[]::new);

@@ -205,7 +212,7 @@ protected void handleCommand(CommandSender sender, String arguments) {
}
}

protected List<Substring> handleCommandSuggestion(CommandSender sender, String arguments) {
public List<Substring> handleCommandSuggestion(CommandSender sender, String arguments) {
try {
List<Substring> split = parseArgs(arguments).collect(Collectors.toList());
List<String> argStrings = split.stream()
@@ -0,0 +1,135 @@
package com.sk89q.commandbook.component.god;

import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.command.argument.MultiPlayerTarget;
import com.sk89q.commandbook.util.ChatUtil;
import com.sk89q.commandbook.util.entity.player.PlayerUtil;
import com.sk89q.minecraft.util.commands.CommandException;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
import org.enginehub.piston.annotation.param.Arg;
import org.enginehub.piston.annotation.param.Switch;

@CommandContainer
class GodCommands {
private GodComponent component;

public GodCommands(GodComponent component) {
this.component = component;
}

@Command(name = "god", desc = "Enable godmode on a player")
public void godCmd(CommandSender sender,
@Switch (name = 's', desc = "silent") boolean silent,
@Arg(desc = "Player(s) to target", def = "") MultiPlayerTarget targetPlayers)
throws CommandException {

boolean included = false;

if (targetPlayers == null) {
targetPlayers = new MultiPlayerTarget(PlayerUtil.checkPlayer(sender));
}

// Check permissions!
for (Player player : targetPlayers) {
if (player == sender) {
CommandBook.inst().checkPermission(sender, "commandbook.god");
} else {
CommandBook.inst().checkPermission(sender, "commandbook.god.other");
break;
}
}

for (Player player : targetPlayers) {
if (!component.hasGodMode(player)) {
component.enableGodMode(player);
} else {
if (player == sender) {
player.sendMessage(ChatColor.RED + "You already have god mode!");
included = true;
} else {
sender.sendMessage(ChatColor.RED + player.getName() + " already has god mode!");
}
continue;
}

// Tell the user
if (player.equals(sender)) {
player.sendMessage(ChatColor.YELLOW + "God mode enabled! Use /ungod to disable.");

// Keep track of this
included = true;
} else {
if (!silent)
player.sendMessage(ChatColor.YELLOW + "God enabled by "
+ ChatUtil.toColoredName(sender, ChatColor.YELLOW) + ".");

}
}

// The player didn't receive any items, then we need to send the
// user a message so s/he know that something is indeed working
if (!included) {
sender.sendMessage(ChatColor.YELLOW.toString() + "Players now have god mode.");
}
}

@Command(name = "ungod", desc = "Disable godmode on a player")
public void ungodCmd(CommandSender sender,
@Switch (name = 's', desc = "silent") boolean silent,
@Arg(desc = "Player to target") MultiPlayerTarget targetPlayers)
throws CommandException {

boolean included = false;

if (targetPlayers == null) {
targetPlayers = new MultiPlayerTarget(PlayerUtil.checkPlayer(sender));
}

// Check permissions!
for (Player player : targetPlayers) {
if (player == sender) {
CommandBook.inst().checkPermission(sender, "commandbook.god");
} else {
CommandBook.inst().checkPermission(sender, "commandbook.god.other");
break;
}
}

for (Player player : targetPlayers) {
if (component.hasGodMode(player)) {
component.disableGodMode(player);
} else {
if (player == sender) {
player.sendMessage(ChatColor.RED + "You do not have god mode enabled!");
included = true;
} else {
sender.sendMessage(ChatColor.RED + player.getName() + " did not have god mode enabled!");
}
continue;
}

// Tell the user
if (player.equals(sender)) {
player.sendMessage(ChatColor.YELLOW + "God mode disabled!");

// Keep track of this
included = true;
} else {
if (!silent) {
player.sendMessage(ChatColor.YELLOW + "God disabled by "
+ ChatUtil.toColoredName(sender, ChatColor.YELLOW) + ".");
}
}
}

// The player didn't receive any items, then we need to send the
// user a message so s/he know that something is indeed working
if (!included) {
sender.sendMessage(ChatColor.YELLOW.toString() + "Players no longer have god mode.");
}
}
}
@@ -18,21 +18,12 @@

package com.sk89q.commandbook.component.god;

import com.google.common.collect.Lists;
import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.component.info.InfoComponent;
import com.sk89q.commandbook.util.ChatUtil;
import com.sk89q.commandbook.util.InputUtil;
import com.sk89q.commandbook.util.entity.player.PlayerUtil;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
import com.zachsthings.libcomponents.ComponentInformation;
import com.zachsthings.libcomponents.bukkit.BukkitComponent;
import com.zachsthings.libcomponents.config.ConfigurationBase;
import com.zachsthings.libcomponents.config.Setting;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@@ -59,7 +50,11 @@
@Override
public void enable() {
config = configure(new LocalConfiguration());
registerCommands(Commands.class);

CommandBook.registerComponentCommands((commandManager, registration) -> {
registration.register(commandManager, GodCommandsRegistration.builder(), new GodCommands(this));
});

// Check god mode for existing players, if any
for (Player player : CommandBook.server().getOnlinePlayers()) {
checkAutoEnable(player);
@@ -90,6 +85,7 @@ public void reload() {
public void enableGodMode(Player player) {
if (!hasGodMode(player)) {
player.setMetadata(METADATA_KEY, new FixedMetadataValue(CommandBook.inst(), true));
player.setFireTicks(0);
}
}

@@ -201,122 +197,4 @@ public void playerWhois(InfoComponent.PlayerWhoisEvent event) {
}
}
}

public class Commands {
@Command(aliases = {"god"}, usage = "[player]",
desc = "Enable godmode on a player", flags = "s", max = 1)
public void god(CommandContext args, CommandSender sender) throws CommandException {

Iterable<Player> targets = null;
boolean included = false;

// Detect arguments based on the number of arguments provided
if (args.argsLength() == 0) {
targets = Lists.newArrayList(PlayerUtil.checkPlayer(sender));
} else if (args.argsLength() == 1) {
targets = InputUtil.PlayerParser.matchPlayers(sender, args.getString(0));
}

// Check permissions!
for (Player player : targets) {
if (player == sender) {
CommandBook.inst().checkPermission(sender, "commandbook.god");
} else {
CommandBook.inst().checkPermission(sender, "commandbook.god.other");
break;
}
}

for (Player player : targets) {
if (!hasGodMode(player)) {
enableGodMode(player);
player.setFireTicks(0);
} else {
if (player == sender) {
player.sendMessage(ChatColor.RED + "You already have god mode!");
included = true;
} else {
sender.sendMessage(ChatColor.RED + player.getName() + " already has god mode!");
}
continue;
}

// Tell the user
if (player.equals(sender)) {
player.sendMessage(ChatColor.YELLOW + "God mode enabled! Use /ungod to disable.");

// Keep track of this
included = true;
} else {
if (!args.hasFlag('s'))
player.sendMessage(ChatColor.YELLOW + "God enabled by "
+ ChatUtil.toColoredName(sender, ChatColor.YELLOW) + ".");

}
}

// The player didn't receive any items, then we need to send the
// user a message so s/he know that something is indeed working
if (!included) {
sender.sendMessage(ChatColor.YELLOW.toString() + "Players now have god mode.");
}
}

@Command(aliases = {"ungod"}, usage = "[player]",
desc = "Disable godmode on a player", flags = "s", max = 1)
public void ungod(CommandContext args, CommandSender sender) throws CommandException {

Iterable<Player> targets = null;
boolean included = false;

// Detect arguments based on the number of arguments provided
if (args.argsLength() == 0) {
targets = Lists.newArrayList(PlayerUtil.checkPlayer(sender));
} else if (args.argsLength() == 1) {
targets = InputUtil.PlayerParser.matchPlayers(sender, args.getString(0));
}

// Check permissions!
for (Player player : targets) {
if (player == sender) {
CommandBook.inst().checkPermission(sender, "commandbook.god");
} else {
CommandBook.inst().checkPermission(sender, "commandbook.god.other");
break;
}
}

for (Player player : targets) {
if (hasGodMode(player)) {
disableGodMode(player);
} else {
if (player == sender) {
player.sendMessage(ChatColor.RED + "You do not have god mode enabled!");
included = true;
} else {
sender.sendMessage(ChatColor.RED + player.getName() + " did not have god mode enabled!");
}
continue;
}

// Tell the user
if (player.equals(sender)) {
player.sendMessage(ChatColor.YELLOW + "God mode disabled!");

// Keep track of this
included = true;
} else {
player.sendMessage(ChatColor.YELLOW + "God disabled by "
+ ChatUtil.toColoredName(sender, ChatColor.YELLOW) + ".");

}
}

// The player didn't receive any items, then we need to send the
// user a message so s/he know that something is indeed working
if (!included) {
sender.sendMessage(ChatColor.YELLOW.toString() + "Players no longer have god mode.");
}
}
}
}

0 comments on commit 3a00baa

Please sign in to comment.
You can’t perform that action at this time.