Add /easter spawneggs subcommand for spawning debug eggs
This commit is contained in:
parent
d3c4f77931
commit
7da09d7601
|
|
@ -3,6 +3,7 @@ package com.alttd.easter.commands;
|
|||
import com.alttd.easter.Easter;
|
||||
import com.alttd.easter.commands.subcommands.Reload;
|
||||
import com.alttd.easter.commands.subcommands.SetPrize;
|
||||
import com.alttd.easter.commands.subcommands.SpawnEggs;
|
||||
import com.alttd.easter.config.Messages;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
|
@ -33,11 +34,11 @@ public class Command implements CommandExecutor, TabExecutor {
|
|||
}
|
||||
command.setExecutor(this);
|
||||
command.setTabCompleter(this);
|
||||
command.setAliases(List.of("pu"));
|
||||
|
||||
subCommands = List.of(
|
||||
new Reload(easter),
|
||||
new SetPrize()
|
||||
new SetPrize(),
|
||||
new SpawnEggs()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,110 @@
|
|||
package com.alttd.easter.commands.subcommands;
|
||||
|
||||
import com.alttd.easter.commands.SubCommand;
|
||||
import com.alttd.easter.config.Messages;
|
||||
import com.alttd.easter.egg.EggType;
|
||||
import com.alttd.easter.util.ItemUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SpawnEggs extends SubCommand {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
commandSender.sendRichMessage(Messages.GENERIC.PLAYER_ONLY);
|
||||
return true;
|
||||
}
|
||||
|
||||
int amount = 1;
|
||||
if (args.length >= 2) {
|
||||
try {
|
||||
amount = Math.max(1, Integer.parseInt(args[1]));
|
||||
} catch (NumberFormatException ignored) {
|
||||
// keep default
|
||||
}
|
||||
}
|
||||
|
||||
// Optional: allow spawning a single type via argument
|
||||
if (args.length >= 2) {
|
||||
EggType specific = parseEggType(args[1]);
|
||||
if (specific != null) {
|
||||
giveEgg(player, specific, amount);
|
||||
player.sendRichMessage("<green>Spawned </green><gold>" + amount + "</gold> <green>of </green><gold>" + ItemUtils.prettify(specific.name()) + "</gold> <green>egg(s).</green>");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (args.length >= 3) {
|
||||
EggType specific = parseEggType(args[2]);
|
||||
if (specific != null) {
|
||||
giveEgg(player, specific, amount);
|
||||
player.sendRichMessage("<green>Spawned </green><gold>" + amount + "</gold> <green>of </green><gold>" + ItemUtils.prettify(specific.name()) + "</gold> <green>egg(s).</green>");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (EggType type : EggType.values()) {
|
||||
giveEgg(player, type, amount);
|
||||
}
|
||||
player.sendRichMessage("<green>Spawned </green><gold>" + amount + "</gold> <green>of every easter egg in your inventory for debugging.</green>");
|
||||
return true;
|
||||
}
|
||||
|
||||
private void giveEgg(Player player, EggType type, int amount) {
|
||||
ItemStack item = ItemUtils.createEggItem(type);
|
||||
item.setAmount(Math.max(1, Math.min(64, amount)));
|
||||
var left = player.getInventory().addItem(item);
|
||||
if (!left.isEmpty()) {
|
||||
// drop leftovers at the player's location
|
||||
left.values().forEach(stack -> player.getWorld().dropItemNaturally(player.getLocation(), stack));
|
||||
}
|
||||
}
|
||||
|
||||
private EggType parseEggType(String arg) {
|
||||
String key = arg.trim().toUpperCase().replace('-', '_').replace(' ', '_');
|
||||
// try direct enum name
|
||||
try {
|
||||
return EggType.valueOf(key);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
// try match by pretty color words contained
|
||||
for (EggType type : EggType.values()) {
|
||||
if (ItemUtils.prettify(type.name()).replace(" ", "").equalsIgnoreCase(arg.replace(" ", ""))) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "spawneggs";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
List<String> res = new ArrayList<>();
|
||||
if (args.length == 2) {
|
||||
// amount suggestions
|
||||
res.addAll(List.of("1", "4", "16", "64"));
|
||||
// also allow type in 2nd arg for convenience
|
||||
for (EggType type : EggType.values()) {
|
||||
res.add(type.name().toLowerCase());
|
||||
}
|
||||
} else if (args.length == 3) {
|
||||
for (EggType type : EggType.values()) {
|
||||
res.add(type.name().toLowerCase());
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Messages.HELP.SPAWN_EGGS;
|
||||
}
|
||||
}
|
||||
|
|
@ -26,6 +26,7 @@ public class Messages extends AbstractConfig {
|
|||
public static String HELP_MESSAGE = "<green>Show this menu: <gold>/pu help</gold></green>";
|
||||
public static String RELOAD = "<green>Reload the configs for Easter: <gold>/pu reload</gold></green>";
|
||||
public static String SET_PRIZE = "<green>Add the item in your hand as a prize: <gold>/pu setprize</gold></green>";
|
||||
public static String SPAWN_EGGS = "<green>Give yourself debug eggs: <gold>/pu spawneggs [amount|type] [type]</gold></green>";
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void load() {
|
||||
|
|
@ -33,6 +34,7 @@ public class Messages extends AbstractConfig {
|
|||
HELP_MESSAGE = config.getString(prefix, "help", HELP_MESSAGE);
|
||||
RELOAD = config.getString(prefix, "reload", RELOAD);
|
||||
SET_PRIZE = config.getString(prefix, "set-prize", SET_PRIZE);
|
||||
SPAWN_EGGS = config.getString(prefix, "spawn-eggs", SPAWN_EGGS);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,3 +17,6 @@ permissions:
|
|||
easter.setprize:
|
||||
description: Set the held item as a prize
|
||||
default: op
|
||||
easter.spawneggs:
|
||||
description: Give yourself debug eggs
|
||||
default: op
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user