Giants/src/main/java/eu/m724/giants/GiantsCommand.java
2024-11-10 09:26:37 +01:00

147 lines
6 KiB
Java

package eu.m724.giants;
import eu.m724.giants.updater.UpdateCommand;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class GiantsCommand implements CommandExecutor {
private final GiantsPlugin plugin;
private final Configuration configuration;
private final UpdateCommand updateCommand;
public GiantsCommand(GiantsPlugin plugin, Configuration configuration, UpdateCommand updateCommand) {
this.plugin = plugin;
this.configuration = configuration;
this.updateCommand = updateCommand;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) {
sender.sendMessage("Giants " + plugin.getDescription().getVersion());
return true;
}
String action = args[0];
if (!sender.hasPermission("giants.command." + action)) {
sender.sendMessage("You don't have permission to use this command, or it doesn't exist.");
return true;
}
Player player = sender instanceof Player ? (Player) sender : null;
if (action.equals("spawn")) {
if (player != null) {
if (plugin.spawnGiantIfPossible(player.getLocation()) == null) {
sender.sendMessage("No space here for a Giant");
} else {
sender.sendMessage("Spawned a Giant");
}
} else {
sender.sendMessage("Only players can use this command.");
}
} else if (action.equals("serialize")) {
if (player != null) {
ItemStack itemStack = player.getInventory().getItemInMainHand();
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("chance", 1);
map.put("quantityMin", itemStack.getAmount());
map.put("quantityMax", itemStack.getAmount());
map.put("itemStack", itemStack);
list.add(map);
YamlConfiguration yamlConfiguration = new YamlConfiguration();
try {
Method method = yamlConfiguration.getClass().getMethod("setInlineComments", String.class, List.class);
yamlConfiguration.set("v", list);
method.invoke(yamlConfiguration, "v", List.of("Copy the below content to your config.yml"));
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
yamlConfiguration.set("v", null); // two latter exceptions happen after setting
yamlConfiguration.set("copy_everything_below_to_config_yml", list);
}
long now = System.currentTimeMillis();
String name = "item-" + now + ".yml";
File file = new File(plugin.getDataFolder(), name);
try {
yamlConfiguration.save(file);
sender.sendMessage("Saved to plugins/Giants/" + name + ". To add it as a drop, see instructions in the file.");
} catch (IOException e) {
sender.sendMessage("Error saving file. See console for details.");
throw new RuntimeException("Error saving file to " + file.getName(), e);
}
} else {
sender.sendMessage("Only players can use this command.");
}
} else if (action.equals("jm")) { // TODO remove
if (args.length > 1) {
int mode = Integer.parseInt(args[1]);
configuration.jumpMode = mode;
sender.sendMessage("Jump mode set to " + mode);
sender.sendMessage("This command doesn't check if it's valid, an invalid value turns off jumping.");
if (configuration.jumpHeight == -1) {
configuration.jumpHeight = configuration.defaultJumpHeight();
sender.sendMessage("Jump height set to " + configuration.jumpHeight + ". Modify it with /giants jumpheight");
}
} else {
sender.sendMessage("Jump mode: " + configuration.jumpMode);
}
} else if (action.equals("jh")) {
if (args.length > 1) {
double height = Double.parseDouble(args[1]);
configuration.jumpHeight = height;
sender.sendMessage("Jump height set to " + height);
} else {
sender.sendMessage("Jump height: " + configuration.jumpHeight);
}
} else if (action.equals("jc")) {
if (args.length > 1) {
int condition = Integer.parseInt(args[1]);
configuration.jumpCondition = condition;
sender.sendMessage("Jump condition set to " + condition);
} else {
sender.sendMessage("Jump condition: " + configuration.jumpCondition);
}
} else if (action.equals("jd")) {
if (args.length > 1) {
int delay = Integer.parseInt(args[1]);
configuration.jumpDelay = delay;
sender.sendMessage("Jump delay set to " + delay);
} else {
sender.sendMessage("Jump delay: " + configuration.jumpDelay);
}
} else if (action.equals("update")) {
if (updateCommand != null)
updateCommand.onCommand(sender, command, label, args);
else
sender.sendMessage("Updater is disabled");
}
return true;
}
}