BlockCommands.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package me.km.snuviscript.commands;
  2. import com.mojang.brigadier.StringReader;
  3. import me.hammerle.snuviscript.code.ScriptManager;
  4. import me.hammerle.snuviscript.code.SnuviUtils;
  5. import me.km.inventory.InventoryUtils;
  6. import me.km.utils.Location;
  7. import me.km.utils.Mapper;
  8. import net.minecraft.block.*;
  9. import net.minecraft.command.arguments.BlockStateParser;
  10. import net.minecraft.entity.EntityType;
  11. import net.minecraft.entity.player.ServerPlayerEntity;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.nbt.CompoundNBT;
  14. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  15. import net.minecraft.state.IProperty;
  16. import net.minecraft.state.properties.ChestType;
  17. import net.minecraft.tags.BlockTags;
  18. import net.minecraft.tags.Tag;
  19. import net.minecraft.tileentity.*;
  20. import net.minecraft.util.Direction;
  21. import net.minecraft.util.ResourceLocation;
  22. import net.minecraft.util.math.BlockPos;
  23. import net.minecraft.util.text.StringTextComponent;
  24. import net.minecraft.world.IWorld;
  25. import net.minecraft.world.World;
  26. public class BlockCommands {
  27. public static void registerFunctions(ScriptManager sm) {
  28. sm.registerFunction("block.gettag", (sc, in) -> BlockTags.getCollection().get(new ResourceLocation(in[0].getString(sc))));
  29. sm.registerFunction("block.hastag", (sc, in) -> ((Tag<Block>) in[0].get(sc)).contains((Block) in[1].get(sc)));
  30. sm.registerFunction("block.gettype", (sc, in) -> {
  31. Location l = (Location) in[0].get(sc);
  32. return l.getWorld().getBlockState(l.getBlockPos()).getBlock().getRegistryName().toString();
  33. });
  34. sm.registerFunction("block.get", (sc, in) -> {
  35. Location l = (Location) in[0].get(sc);
  36. return l.getWorld().getBlockState(l.getBlockPos()).getBlock();
  37. });
  38. sm.registerFunction("block.getproperty", (sc, in) -> Mapper.getProperty(in[0].getString(sc)));
  39. sm.registerFunction("block.getstate", (sc, in) -> {
  40. Location l = (Location) in[0].get(sc);
  41. IProperty prop = (IProperty) in[1].get(sc);
  42. BlockState state = l.getWorld().getBlockState(l.getBlockPos());
  43. if(state.has(prop)) {
  44. Object o = l.getWorld().getBlockState(l.getBlockPos()).get(prop);
  45. if(o instanceof Number) {
  46. return ((Number) o).doubleValue();
  47. } else if(o instanceof Boolean) {
  48. return o;
  49. }
  50. return o.toString();
  51. }
  52. return null;
  53. });
  54. sm.registerConsumer("block.clone", (sc, in) -> {
  55. Location l0 = (Location) in[0].get(sc);
  56. Location l1 = (Location) in[1].get(sc);
  57. IWorld w0 = l0.getWorld();
  58. BlockPos pos0 = l0.getBlockPos();
  59. BlockState state = w0.getBlockState(pos0);
  60. TileEntity tileEnt0 = w0.getTileEntity(pos0);
  61. IWorld w1 = l1.getWorld();
  62. BlockPos pos1 = l1.getBlockPos();
  63. w1.setBlockState(pos1, state, 2);
  64. TileEntity tileEnt1 = w1.getTileEntity(pos1);
  65. if(tileEnt0 != null && tileEnt1 != null) {
  66. CompoundNBT nbt = tileEnt0.write(new CompoundNBT());
  67. nbt.putInt("x", pos1.getX());
  68. nbt.putInt("y", pos1.getY());
  69. nbt.putInt("z", pos1.getZ());
  70. tileEnt1.read(nbt);
  71. tileEnt1.markDirty();
  72. }
  73. });
  74. sm.registerConsumer("block.set", (sc, in) -> {
  75. Location l = (Location) in[0].get(sc);
  76. BlockStateParser parser = new BlockStateParser(new StringReader(in[1].getString(sc)), true);
  77. BlockState state = parser.parse(true).getState();
  78. int flag = 2;
  79. if(in.length >= 3 && in[2].getBoolean(sc)) {
  80. flag |= 16;
  81. }
  82. l.getWorld().setBlockState(l.getBlockPos(), state, flag);
  83. });
  84. sm.registerConsumer("block.setsign", (sc, in) -> {
  85. Location l = (Location) in[0].get(sc);
  86. SignTileEntity sign = (SignTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  87. sign.signText[in[1].getInt(sc)] = new StringTextComponent(SnuviUtils.connect(sc, in, 2));
  88. SUpdateTileEntityPacket packet = sign.getUpdatePacket();
  89. World w = sign.getWorld();
  90. if(w != null) {
  91. w.getPlayers().stream().filter(p -> p instanceof ServerPlayerEntity)
  92. .forEach(p -> ((ServerPlayerEntity) p).connection.sendPacket(packet));
  93. }
  94. });
  95. sm.registerFunction("block.getsign", (sc, in) -> {
  96. Location l = (Location) in[0].get(sc);
  97. SignTileEntity sign = (SignTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  98. return sign.signText[in[1].getInt(sc)].getString();
  99. });
  100. sm.registerConsumer("block.setdoorstatus", (sc, in) -> {
  101. Location l = (Location) in[0].get(sc);
  102. BlockPos pos = l.getBlockPos();
  103. ((DoorBlock) l.getWorld().getBlockState(pos).getBlock()).toggleDoor(l.getWorld().getWorld(), pos, in[1].getBoolean(sc));
  104. });
  105. sm.registerFunction("block.getdoorstatus", (sc, in) -> {
  106. Location l = (Location) in[0].get(sc);
  107. return l.getBlockState().get(DoorBlock.OPEN);
  108. });
  109. sm.registerFunction("block.isdoor", (sc, in) -> {
  110. Location l = (Location) in[0].get(sc);
  111. return l.getWorld().getBlockState(l.getBlockPos()).getBlock() instanceof DoorBlock;
  112. });
  113. sm.registerFunction("block.issolid", (sc, in) -> {
  114. return CommandUtils.getBlockState((Location) in[0].get(sc)).isSolid();
  115. });
  116. sm.registerFunction("block.tostack", (sc, in) -> {
  117. Location l = (Location) in[0].get(sc);
  118. return new ItemStack(l.getBlockState().getBlock().asItem());
  119. });
  120. sm.registerFunction("block.getitemamount", (sc, in) -> {
  121. Location l = (Location) in[0].get(sc);
  122. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  123. if(te == null || !(te instanceof ChestTileEntity)) {
  124. return 0.0d;
  125. }
  126. return (double) InventoryUtils.searchInventoryFor((ChestTileEntity) te, (ItemStack) in[2].get(sc), in[1].getBoolean(sc));
  127. });
  128. sm.registerFunction("block.getsecchest", (sc, in) -> {
  129. Location l = (Location) in[0].get(sc);
  130. BlockPos pos = l.getBlockPos();
  131. BlockState state = l.getWorld().getBlockState(pos);
  132. ChestType chesttype = state.get(ChestBlock.TYPE);
  133. if(chesttype == ChestType.SINGLE) {
  134. return null;
  135. }
  136. Direction dir = ChestBlock.getDirectionToAttached(state);
  137. return l.copyAdd(dir.getXOffset(), dir.getYOffset(), dir.getZOffset());
  138. });
  139. sm.registerFunction("block.additem", (sc, in) -> {
  140. Location l = (Location) in[0].get(sc);
  141. ItemStack stack = ((ItemStack) in[1].get(sc));
  142. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  143. if(te == null || !(te instanceof ChestTileEntity)) {
  144. return stack;
  145. }
  146. stack.setCount(InventoryUtils.addToInventory((ChestTileEntity) te, stack));
  147. return stack;
  148. });
  149. sm.registerFunction("block.subitem", (sc, in) -> {
  150. Location l = (Location) in[0].get(sc);
  151. ItemStack stack = ((ItemStack) in[1].get(sc));
  152. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  153. if(te == null || !(te instanceof ChestTileEntity)) {
  154. return stack;
  155. }
  156. stack.setCount(InventoryUtils.removeFromInventory((ChestTileEntity) te, stack));
  157. return stack;
  158. });
  159. sm.registerConsumer("block.grow", (sc, in) -> {
  160. Location l1 = (Location) in[0].get(sc);
  161. World w = l1.getWorld().getWorld();
  162. BlockPos pos1 = l1.getBlockPos();
  163. BlockPos pos2 = ((Location) in[1].get(sc)).getBlockPos();
  164. int x = Math.min(pos1.getX(), pos2.getX());
  165. int endX = Math.max(pos1.getX(), pos2.getX());
  166. int y = Math.min(pos1.getY(), pos2.getY());
  167. int endY = Math.max(pos1.getY(), pos2.getY());
  168. int z = Math.min(pos1.getZ(), pos2.getZ());
  169. int endZ = Math.max(pos1.getZ(), pos2.getZ());
  170. if(endX - x > 50 || endY - y > 50 || endZ - z > 50) {
  171. throw new IllegalArgumentException("uhh, that area seems way to big for growing plants");
  172. }
  173. BlockPos relative;
  174. BlockState state;
  175. for(; x <= endX; x++) {
  176. for(; y <= endY; y++) {
  177. for(; z <= endZ; z++) {
  178. relative = new BlockPos(x, y, z);
  179. state = w.getBlockState(relative);
  180. if(state.getBlock() instanceof CropsBlock) {
  181. w.setBlockState(relative, state.with(CropsBlock.AGE, 7));
  182. }
  183. }
  184. }
  185. }
  186. });
  187. sm.registerConsumer("block.setspawnertype", (sc, in) -> {
  188. Location l = (Location) in[0].get(sc);
  189. MobSpawnerTileEntity spawner = (MobSpawnerTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  190. spawner.getSpawnerBaseLogic().setEntityType(EntityType.byKey(in[1].getString(sc)).get());
  191. });
  192. }
  193. }