BlockCommands.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 me.km.utils.ReflectionUtils;
  9. import net.minecraft.block.*;
  10. import net.minecraft.block.material.Material;
  11. import net.minecraft.command.arguments.BlockStateParser;
  12. import net.minecraft.entity.Entity;
  13. import net.minecraft.entity.EntityType;
  14. import net.minecraft.entity.player.ServerPlayerEntity;
  15. import net.minecraft.fluid.Fluids;
  16. import net.minecraft.inventory.IInventory;
  17. import net.minecraft.item.ItemStack;
  18. import net.minecraft.nbt.CompoundNBT;
  19. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  20. import net.minecraft.state.BooleanProperty;
  21. import net.minecraft.state.EnumProperty;
  22. import net.minecraft.state.IntegerProperty;
  23. import net.minecraft.state.Property;
  24. import net.minecraft.state.properties.ChestType;
  25. import net.minecraft.tags.BlockTags;
  26. import net.minecraft.tags.Tag;
  27. import net.minecraft.tileentity.*;
  28. import net.minecraft.util.Direction;
  29. import net.minecraft.util.ResourceLocation;
  30. import net.minecraft.util.math.BlockPos;
  31. import net.minecraft.util.text.StringTextComponent;
  32. import net.minecraft.world.IWorld;
  33. import net.minecraft.world.World;
  34. public class BlockCommands {
  35. private static class Offset {
  36. private final int x;
  37. private final int y;
  38. private final int z;
  39. public Offset(int x, int y, int z) {
  40. this.x = x;
  41. this.y = y;
  42. this.z = z;
  43. }
  44. }
  45. private static final Offset[] OFFSETS = new Offset[] {
  46. // new Offset(-1, -1, -1),
  47. new Offset(0, -1, -1),
  48. // new Offset(1, -1, -1),
  49. new Offset(-1, 0, -1), new Offset(0, 0, -1), new Offset(1, 0, -1),
  50. // new Offset(-1, 1, -1),
  51. new Offset(0, 1, -1),
  52. // new Offset(1, 1, -1),
  53. new Offset(-1, -1, 0), new Offset(0, -1, 0), new Offset(1, -1, 0), new Offset(-1, 0, 0),
  54. new Offset(0, 0, 0), new Offset(1, 0, 0), new Offset(-1, 1, 0), new Offset(0, 1, 0),
  55. new Offset(1, 1, 0),
  56. // new Offset(-1, -1, 1),
  57. new Offset(0, -1, 1),
  58. // new Offset(1, -1, 1),
  59. new Offset(-1, 0, 1), new Offset(0, 0, 1), new Offset(1, 0, 1),
  60. // new Offset(-1, 1, 1),
  61. new Offset(0, 1, 1), // new Offset(1, 1, 1),
  62. };
  63. @SuppressWarnings({"unchecked", "rawtypes"})
  64. public static void registerFunctions(ScriptManager sm) {
  65. sm.registerFunction("block.gettag", (sc, in) -> BlockTags.getCollection()
  66. .get(new ResourceLocation(in[0].getString(sc))));
  67. sm.registerFunction("block.hastag",
  68. (sc, in) -> ((Tag<Block>) in[0].get(sc)).contains((Block) in[1].get(sc)));
  69. sm.registerFunction("block.gettype", (sc, in) -> {
  70. Location l = (Location) in[0].get(sc);
  71. return l.getWorld().getBlockState(l.getBlockPos()).getBlock().getRegistryName()
  72. .toString();
  73. });
  74. sm.registerFunction("block.isair", (sc, in) -> {
  75. Location l = (Location) in[0].get(sc);
  76. return l.getWorld().isAirBlock(l.getBlockPos());
  77. });
  78. sm.registerFunction("block.countair", (sc, in) -> {
  79. Location l = (Location) in[0].get(sc);
  80. IWorld w = l.getWorld();
  81. BlockPos oldPos = l.getBlockPos();
  82. BlockPos.Mutable pos =
  83. new BlockPos.Mutable(oldPos.getX(), oldPos.getY(), oldPos.getZ());
  84. int ox = pos.getX();
  85. int oy = pos.getY();
  86. int oz = pos.getZ();
  87. double counter = 0;
  88. for(Offset off : OFFSETS) {
  89. pos.setPos(ox + off.x, oy + off.y, oz + off.z);
  90. if(w.isAirBlock(pos)) {
  91. counter++;
  92. }
  93. }
  94. return counter;
  95. });
  96. sm.registerFunction("block.get", (sc, in) -> {
  97. Location l = (Location) in[0].get(sc);
  98. return l.getWorld().getBlockState(l.getBlockPos()).getBlock();
  99. });
  100. sm.registerFunction("block.getproperties", (sc, in) -> {
  101. Location l = (Location) in[0].get(sc);
  102. return l.getWorld().getBlockState(l.getBlockPos()).getBlock().getStateContainer()
  103. .getProperties();
  104. });
  105. sm.registerFunction("block.getproperty",
  106. (sc, in) -> Mapper.getProperty(in[0].getString(sc)));
  107. sm.registerFunction("block.property.getvalue", (sc, in) -> {
  108. Location l = (Location) in[0].get(sc);
  109. Property<?> prop = (Property) in[1].get(sc);
  110. BlockState state = l.getWorld().getBlockState(l.getBlockPos());
  111. if(state.hasProperty(prop)) {
  112. Object o = l.getWorld().getBlockState(l.getBlockPos()).get(prop);
  113. if(o instanceof Number) {
  114. return ((Number) o).doubleValue();
  115. } else if(o instanceof Boolean) {
  116. return o;
  117. }
  118. return o.toString();
  119. }
  120. return null;
  121. });
  122. sm.registerConsumer("block.property.setint", (sc, in) -> {
  123. Location l = (Location) in[0].get(sc);
  124. World w = l.getWorld();
  125. BlockPos pos = l.getBlockPos();
  126. IntegerProperty prop = (IntegerProperty) in[1].get(sc);
  127. BlockState state = w.getBlockState(pos);
  128. w.setBlockState(pos, state.with(prop, in[2].getInt(sc)), 18);
  129. });
  130. sm.registerConsumer("block.property.setbool", (sc, in) -> {
  131. Location l = (Location) in[0].get(sc);
  132. World w = l.getWorld();
  133. BlockPos pos = l.getBlockPos();
  134. BooleanProperty prop = (BooleanProperty) in[1].get(sc);
  135. BlockState state = w.getBlockState(pos);
  136. w.setBlockState(pos, state.with(prop, in[2].getBoolean(sc)), 18);
  137. });
  138. sm.registerConsumer("block.property.setenum", (sc, in) -> {
  139. Location l = (Location) in[0].get(sc);
  140. World w = l.getWorld();
  141. BlockPos pos = l.getBlockPos();
  142. EnumProperty prop = (EnumProperty) in[1].get(sc);
  143. Enum e = (Enum) prop.parseValue(in[2].getString(sc)).get();
  144. BlockState state = w.getBlockState(pos);
  145. w.setBlockState(pos, state.with(prop, e), 18);
  146. });
  147. sm.registerConsumer("block.clone", (sc, in) -> {
  148. Location l0 = (Location) in[0].get(sc);
  149. Location l1 = (Location) in[1].get(sc);
  150. IWorld w0 = l0.getWorld();
  151. BlockPos pos0 = l0.getBlockPos();
  152. BlockState state = w0.getBlockState(pos0);
  153. TileEntity tileEnt0 = w0.getTileEntity(pos0);
  154. IWorld w1 = l1.getWorld();
  155. BlockPos pos1 = l1.getBlockPos();
  156. w1.setBlockState(pos1, state, 2);
  157. TileEntity tileEnt1 = w1.getTileEntity(pos1);
  158. if(tileEnt0 != null && tileEnt1 != null) {
  159. CompoundNBT nbt = tileEnt0.write(new CompoundNBT());
  160. nbt.putInt("x", pos1.getX());
  161. nbt.putInt("y", pos1.getY());
  162. nbt.putInt("z", pos1.getZ());
  163. tileEnt1.read(w1.getBlockState(pos1), nbt);
  164. tileEnt1.markDirty();
  165. }
  166. });
  167. sm.registerConsumer("block.break", (sc, in) -> {
  168. Location l = (Location) in[0].get(sc);
  169. Entity ent = (Entity) in[1].get(sc);
  170. l.getWorld().destroyBlock(l.getBlockPos(), true, ent);
  171. });
  172. sm.registerConsumer("block.set", (sc, in) -> {
  173. Location l = (Location) in[0].get(sc);
  174. BlockStateParser parser =
  175. new BlockStateParser(new StringReader(in[1].getString(sc)), true);
  176. BlockState state = parser.parse(true).getState();
  177. int flag = 2;
  178. if(in.length >= 3 && in[2].getBoolean(sc)) {
  179. flag |= 16;
  180. }
  181. l.getWorld().setBlockState(l.getBlockPos(), state, flag);
  182. });
  183. sm.registerFunction("block.newstate", (sc, in) -> {
  184. try {
  185. BlockStateParser parser =
  186. new BlockStateParser(new StringReader(in[0].getString(sc)), true);
  187. return parser.parse(true).getState();
  188. } catch(Exception e) {
  189. return null;
  190. }
  191. });
  192. sm.registerConsumer("block.setstate", (sc, in) -> {
  193. Location l = (Location) in[0].get(sc);
  194. int flag = 2;
  195. if(in.length >= 3 && in[2].getBoolean(sc)) {
  196. flag |= 16;
  197. }
  198. l.getWorld().setBlockState(l.getBlockPos(), (BlockState) in[1].get(sc), flag);
  199. });
  200. sm.registerFunction("block.setsign", (sc, in) -> {
  201. Location l = (Location) in[0].get(sc);
  202. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  203. if(te == null || !(te instanceof SignTileEntity)) {
  204. return false;
  205. }
  206. SignTileEntity sign = (SignTileEntity) te;
  207. sign.setText(in[1].getInt(sc), new StringTextComponent(SnuviUtils.connect(sc, in, 2)));
  208. SUpdateTileEntityPacket packet = sign.getUpdatePacket();
  209. l.getWorld().getPlayers()
  210. .forEach(p -> ((ServerPlayerEntity) p).connection.sendPacket(packet));
  211. return true;
  212. });
  213. sm.registerFunction("block.getsign", (sc, in) -> {
  214. Location l = (Location) in[0].get(sc);
  215. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  216. if(te == null || !(te instanceof SignTileEntity)) {
  217. return null;
  218. }
  219. return ReflectionUtils.getSignText((SignTileEntity) te, in[1].getInt(sc)).getString();
  220. });
  221. sm.registerConsumer("block.settrapdoorstatus", (sc, in) -> {
  222. Location l = (Location) in[0].get(sc);
  223. BlockPos pos = l.getBlockPos();
  224. BlockState state = l.getWorld().getBlockState(pos);
  225. World w = l.getWorld();
  226. state = state.with(TrapDoorBlock.OPEN, in[1].getBoolean(sc));
  227. w.setBlockState(pos, state, 2);
  228. if(state.get(TrapDoorBlock.WATERLOGGED)) {
  229. w.getPendingFluidTicks().scheduleTick(pos, Fluids.WATER,
  230. Fluids.WATER.getTickRate(w));
  231. }
  232. Material m = state.getMaterial();
  233. if(state.get(TrapDoorBlock.OPEN)) {
  234. int i = m == Material.IRON ? 1037 : 1007;
  235. w.playEvent(null, i, pos, 0);
  236. } else {
  237. int j = m == Material.IRON ? 1036 : 1013;
  238. w.playEvent(null, j, pos, 0);
  239. }
  240. });
  241. sm.registerFunction("block.gettrapdoorstatus", (sc, in) -> {
  242. Location l = (Location) in[0].get(sc);
  243. return l.getBlockState().get(TrapDoorBlock.OPEN);
  244. });
  245. sm.registerConsumer("block.setdoorstatus", (sc, in) -> {
  246. Location l = (Location) in[0].get(sc);
  247. BlockPos pos = l.getBlockPos();
  248. BlockState state = l.getWorld().getBlockState(pos);
  249. ((DoorBlock) state.getBlock()).openDoor((World) l.getWorld(), state, pos,
  250. in[1].getBoolean(sc));
  251. });
  252. sm.registerFunction("block.getdoorstatus", (sc, in) -> {
  253. Location l = (Location) in[0].get(sc);
  254. return l.getBlockState().get(DoorBlock.OPEN);
  255. });
  256. sm.registerFunction("block.issolid", (sc, in) -> {
  257. return CommandUtils.getBlockState((Location) in[0].get(sc)).isSolid();
  258. });
  259. sm.registerFunction("block.tostack", (sc, in) -> {
  260. Location l = (Location) in[0].get(sc);
  261. return new ItemStack(l.getBlockState().getBlock().asItem());
  262. });
  263. sm.registerFunction("block.getitemamount", (sc, in) -> {
  264. Location l = (Location) in[0].get(sc);
  265. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  266. if(te == null || !(te instanceof ChestTileEntity)) {
  267. return 0.0d;
  268. }
  269. return (double) InventoryUtils.searchInventoryFor((ChestTileEntity) te,
  270. (ItemStack) in[2].get(sc), in[1].getBoolean(sc));
  271. });
  272. sm.registerFunction("block.getsecchest", (sc, in) -> {
  273. Location l = (Location) in[0].get(sc);
  274. BlockPos pos = l.getBlockPos();
  275. BlockState state = l.getWorld().getBlockState(pos);
  276. ChestType chesttype = state.get(ChestBlock.TYPE);
  277. if(chesttype == ChestType.SINGLE) {
  278. return null;
  279. }
  280. Direction dir = ChestBlock.getDirectionToAttached(state);
  281. return l.copyAdd(dir.getXOffset(), dir.getYOffset(), dir.getZOffset());
  282. });
  283. sm.registerFunction("block.additem", (sc, in) -> {
  284. Location l = (Location) in[0].get(sc);
  285. ItemStack stack = ((ItemStack) in[1].get(sc));
  286. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  287. if(te == null || !(te instanceof ChestTileEntity)) {
  288. return stack;
  289. }
  290. stack.setCount(InventoryUtils.addToInventory((ChestTileEntity) te, stack));
  291. return stack;
  292. });
  293. sm.registerFunction("block.subitem", (sc, in) -> {
  294. Location l = (Location) in[0].get(sc);
  295. ItemStack stack = ((ItemStack) in[1].get(sc));
  296. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  297. if(te == null || !(te instanceof ChestTileEntity)) {
  298. return stack;
  299. }
  300. stack.setCount(InventoryUtils.removeFromInventory((ChestTileEntity) te, stack));
  301. return stack;
  302. });
  303. sm.registerConsumer("block.setspawnertype", (sc, in) -> {
  304. Location l = (Location) in[0].get(sc);
  305. MobSpawnerTileEntity spawner =
  306. (MobSpawnerTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  307. spawner.getSpawnerBaseLogic()
  308. .setEntityType(EntityType.byKey(in[1].getString(sc)).get());
  309. });
  310. sm.registerFunction("block.getinv", (sc, in) -> {
  311. Location l = (Location) in[0].get(sc);
  312. return (IInventory) l.getWorld().getTileEntity(l.getBlockPos());
  313. });
  314. }
  315. }