CommandUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package me.km.snuviscript.commands;
  2. import java.util.UUID;
  3. import java.util.function.Consumer;
  4. import me.hammerle.snuviscript.code.Script;
  5. import me.hammerle.snuviscript.inputprovider.InputProvider;
  6. import me.km.overrides.ModEntityPlayerMP;
  7. import me.km.permissions.Permissions;
  8. import me.km.playerbank.IPlayerBank;
  9. import me.km.snuviscript.Scripts;
  10. import me.km.utils.Location;
  11. import net.minecraft.block.BlockState;
  12. import net.minecraft.command.ICommandSource;
  13. import net.minecraft.entity.player.PlayerEntity;
  14. import net.minecraft.server.MinecraftServer;
  15. import net.minecraft.util.Util;
  16. import net.minecraft.util.text.ITextComponent;
  17. import net.minecraft.util.text.StringTextComponent;
  18. public class CommandUtils {
  19. private final static UUID SERVER_UUID = new UUID(0, 0);
  20. public static UUID getUUID(Object o) {
  21. if(o instanceof PlayerEntity) {
  22. return ((PlayerEntity) o).getUniqueID();
  23. } else if(o instanceof UUID) {
  24. return (UUID) o;
  25. } else if("SERVER".equals(o)) {
  26. return SERVER_UUID;
  27. }
  28. return UUID.fromString(o.toString());
  29. }
  30. public static ITextComponent concat(Script sc, int start, String pre, InputProvider... ob) throws Exception {
  31. StringTextComponent text = new StringTextComponent(pre);
  32. Object o;
  33. for(int i = start; i < ob.length; i++) {
  34. o = ob[i].get(sc);
  35. if(o instanceof ITextComponent) {
  36. text.append((ITextComponent) o);
  37. } else {
  38. text.appendString(String.valueOf(o));
  39. }
  40. }
  41. return text;
  42. }
  43. public static void sendMessageToGroup(MinecraftServer server, Scripts scripts, Permissions perms, Object group, Script sc, ITextComponent text) {
  44. doForGroup(server, scripts, perms, group, sc, p -> p.sendMessage(text, Util.DUMMY_UUID));
  45. }
  46. public static void doForGroup(MinecraftServer server, Scripts scripts, Permissions perms, Object group, Script sc, Consumer<ICommandSource> c) {
  47. if(group instanceof String) {
  48. switch(group.toString().toLowerCase()) {
  49. case "online":
  50. if(server.getPlayerList() != null) {
  51. server.getPlayerList().getPlayers().forEach(p -> c.accept(p));
  52. }
  53. return;
  54. case "dev":
  55. if(server.getPlayerList() != null) {
  56. server.getPlayerList().getPlayers().stream().filter(p -> perms.has(p, "script.error")).forEach(c);
  57. }
  58. return;
  59. case "server":
  60. c.accept(server);
  61. return;
  62. }
  63. }
  64. c.accept((PlayerEntity) group);
  65. }
  66. public static int getId(IPlayerBank bank, Object o) {
  67. if(o instanceof ModEntityPlayerMP) {
  68. return ((ModEntityPlayerMP) o).getId();
  69. } else if(o instanceof Double) {
  70. return ((Double) o).intValue();
  71. }
  72. UUID uuid = getUUID(o);
  73. return bank.getId(uuid);
  74. }
  75. public static Class getNamedClass(String s) throws ClassNotFoundException {
  76. return Class.forName(s);
  77. }
  78. public static BlockState getBlockState(Location l) {
  79. return l.getWorld().getBlockState(l.getBlockPos());
  80. }
  81. }