Skip to content

Commit

Permalink
Port God Component commands to Piston
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkArc committed Jan 2, 2020
1 parent 0b03a10 commit a264217
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 130 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/sk89q/commandbook/CommandBook.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sk89q.commandbook.config.LegacyCommandBookConfigurationMigrator; import com.sk89q.commandbook.config.LegacyCommandBookConfigurationMigrator;
import com.sk89q.util.yaml.YAMLFormat; import com.sk89q.util.yaml.YAMLFormat;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
import com.sk89q.worldedit.internal.command.CommandUtil; import com.sk89q.worldedit.internal.command.CommandUtil;
import com.zachsthings.libcomponents.InjectComponent; import com.zachsthings.libcomponents.InjectComponent;
import com.zachsthings.libcomponents.InjectComponentAnnotationHandler; import com.zachsthings.libcomponents.InjectComponentAnnotationHandler;
Expand All @@ -39,11 +40,13 @@
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.enginehub.piston.CommandManager;
import org.yaml.snakeyaml.error.YAMLException; import org.yaml.snakeyaml.error.YAMLException;


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


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


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

private void publishPistonCommands() { private void publishPistonCommands() {
commandManager.registerCommandsWith(this); commandManager.registerCommandsWith(this);
} }
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/sk89q/commandbook/PlatformCommandManager.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@


import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.BiConsumer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
Expand Down Expand Up @@ -90,6 +91,12 @@ private void registerCoreCommands() {
CommandBookCommands.register(commandManagerService, commandManager, registration); 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) { public void registerCommandsWith(CommandBook commandBook) {
BukkitCommandInspector inspector = new BukkitCommandInspector(commandBook, commandManager); BukkitCommandInspector inspector = new BukkitCommandInspector(commandBook, commandManager);


Expand Down Expand Up @@ -144,7 +151,7 @@ private void handleUnknownException(Actor actor, Throwable t) {
log.error("An unexpected error while handling a CommandBook command", 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); Actor actor = WorldEditAdapter.adapt(sender);
String[] split = parseArgs(arguments).map(Substring::getSubstring).toArray(String[]::new); String[] split = parseArgs(arguments).map(Substring::getSubstring).toArray(String[]::new);


Expand Down Expand Up @@ -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 { try {
List<Substring> split = parseArgs(arguments).collect(Collectors.toList()); List<Substring> split = parseArgs(arguments).collect(Collectors.toList());
List<String> argStrings = split.stream() List<String> argStrings = split.stream()
Expand Down
135 changes: 135 additions & 0 deletions src/main/java/com/sk89q/commandbook/component/god/GodCommands.java
Original file line number Original file line Diff line number Diff line change
@@ -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.");
}
}
}
134 changes: 6 additions & 128 deletions src/main/java/com/sk89q/commandbook/component/god/GodComponent.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,21 +18,12 @@


package com.sk89q.commandbook.component.god; package com.sk89q.commandbook.component.god;


import com.google.common.collect.Lists;
import com.sk89q.commandbook.CommandBook; import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.component.info.InfoComponent; 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.ComponentInformation;
import com.zachsthings.libcomponents.bukkit.BukkitComponent; import com.zachsthings.libcomponents.bukkit.BukkitComponent;
import com.zachsthings.libcomponents.config.ConfigurationBase; import com.zachsthings.libcomponents.config.ConfigurationBase;
import com.zachsthings.libcomponents.config.Setting; import com.zachsthings.libcomponents.config.Setting;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
Expand All @@ -59,7 +50,11 @@ public class GodComponent extends BukkitComponent implements Listener {
@Override @Override
public void enable() { public void enable() {
config = configure(new LocalConfiguration()); 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 // Check god mode for existing players, if any
for (Player player : CommandBook.server().getOnlinePlayers()) { for (Player player : CommandBook.server().getOnlinePlayers()) {
checkAutoEnable(player); checkAutoEnable(player);
Expand Down Expand Up @@ -90,6 +85,7 @@ private static class LocalConfiguration extends ConfigurationBase {
public void enableGodMode(Player player) { public void enableGodMode(Player player) {
if (!hasGodMode(player)) { if (!hasGodMode(player)) {
player.setMetadata(METADATA_KEY, new FixedMetadataValue(CommandBook.inst(), true)); player.setMetadata(METADATA_KEY, new FixedMetadataValue(CommandBook.inst(), true));
player.setFireTicks(0);
} }
} }


Expand Down Expand Up @@ -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 a264217

Please sign in to comment.