CommandUtils.java 3.3 KB

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