Implement new Quality-of-Life commands

This commit is contained in:
Gabriel Tofvesson 2021-07-23 23:55:34 +02:00
parent 5701a20cc8
commit 3fde89e1ca
4 changed files with 107 additions and 1 deletions

View File

@ -30,3 +30,11 @@ commands:
description: Add a floating nametag to a chest
usage: /<command> {name}
permission: invtweaks.spawnfake
growup:
description: Age baby animals
usage: /<command> [radius]
permission: invtweaks.growup
rewool:
description: Put the wool back on sheep
usage: /<command> [radius]
permission: invtweaks.rewool

View File

@ -0,0 +1,43 @@
package dev.w1zzrd.invtweaks.command;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.stream.Stream;
import static dev.w1zzrd.spigot.wizcompat.command.CommandUtils.errorMessage;
public final class Commands {
private Commands() { throw new UnsupportedOperationException("Functional class"); }
public static double getCommandRadius(final CommandSender sender, final String[] args, final int radiusIndex) {
try {
if (args.length > radiusIndex)
return Double.parseDouble(args[radiusIndex]);
} catch (NumberFormatException e) {
sender.spigot().sendMessage(errorMessage(String.format("\"%s\" is not a number", args[radiusIndex])));
}
return Double.NaN;
}
public static <T extends Entity> Stream<T> getCommandEntityRadius(
final CommandSender sender,
final String[] args,
final int radiusIndex,
final Class<T> entityType
) {
final double radius = Commands.getCommandRadius(sender, args, 0);
if (Double.compare(radius, Double.NaN) == 0)
return null;
return
(radius >= 0 ?
((Player) sender).getNearbyEntities(radius, radius, radius).stream() :
Bukkit.getWorlds().stream().flatMap(it -> it.getEntities().stream()))
.filter(it -> entityType.isAssignableFrom(it.getClass()))
.map(it -> (T) it);
}
}

View File

@ -0,0 +1,28 @@
package dev.w1zzrd.invtweaks.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Breedable;
import org.bukkit.entity.Player;
import java.util.stream.Stream;
import static dev.w1zzrd.spigot.wizcompat.command.CommandUtils.assertTrue;
public class GrowUpCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (assertTrue(args.length == 0 || sender instanceof Player, "Only players can run this command", sender))
return true;
final Stream<Breedable> breedables = Commands.getCommandEntityRadius(sender, args, 0, Breedable.class);
if (breedables == null)
return true;
breedables.filter(it -> !it.getAgeLock() && !it.isAdult()).forEach(Ageable::setAdult);
return true;
}
}

View File

@ -0,0 +1,27 @@
package dev.w1zzrd.invtweaks.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep;
import java.util.stream.Stream;
import static dev.w1zzrd.spigot.wizcompat.command.CommandUtils.assertTrue;
public class ReWoolCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (assertTrue(args.length == 0 || sender instanceof Player, "Only players can run this command", sender))
return true;
final Stream<Sheep> sheep = Commands.getCommandEntityRadius(sender, args, 0, Sheep.class);
if (sheep == null)
return true;
sheep.forEach(it -> it.setSheared(false));
return true;
}
}