diff --git a/res/plugin.yml b/res/plugin.yml index 4d62d58..b197d4a 100644 --- a/res/plugin.yml +++ b/res/plugin.yml @@ -29,4 +29,12 @@ commands: chestname: description: Add a floating nametag to a chest usage: / {name} - permission: invtweaks.spawnfake \ No newline at end of file + permission: invtweaks.spawnfake + growup: + description: Age baby animals + usage: / [radius] + permission: invtweaks.growup + rewool: + description: Put the wool back on sheep + usage: / [radius] + permission: invtweaks.rewool \ No newline at end of file diff --git a/src/dev/w1zzrd/invtweaks/command/Commands.java b/src/dev/w1zzrd/invtweaks/command/Commands.java new file mode 100644 index 0000000..cf744a8 --- /dev/null +++ b/src/dev/w1zzrd/invtweaks/command/Commands.java @@ -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 Stream getCommandEntityRadius( + final CommandSender sender, + final String[] args, + final int radiusIndex, + final Class 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); + } +} diff --git a/src/dev/w1zzrd/invtweaks/command/GrowUpCommand.java b/src/dev/w1zzrd/invtweaks/command/GrowUpCommand.java new file mode 100644 index 0000000..74006c0 --- /dev/null +++ b/src/dev/w1zzrd/invtweaks/command/GrowUpCommand.java @@ -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 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; + } +} diff --git a/src/dev/w1zzrd/invtweaks/command/ReWoolCommand.java b/src/dev/w1zzrd/invtweaks/command/ReWoolCommand.java new file mode 100644 index 0000000..56b712e --- /dev/null +++ b/src/dev/w1zzrd/invtweaks/command/ReWoolCommand.java @@ -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 = Commands.getCommandEntityRadius(sender, args, 0, Sheep.class); + if (sheep == null) + return true; + + sheep.forEach(it -> it.setSheared(false)); + + return true; + } +}