BlockCommands.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.inventory.IInventory;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.CompoundNBT;
  15. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  16. import net.minecraft.state.IProperty;
  17. import net.minecraft.state.properties.ChestType;
  18. import net.minecraft.tags.BlockTags;
  19. import net.minecraft.tags.Tag;
  20. import net.minecraft.tileentity.*;
  21. import net.minecraft.util.Direction;
  22. import net.minecraft.util.ResourceLocation;
  23. import net.minecraft.util.math.BlockPos;
  24. import net.minecraft.util.text.StringTextComponent;
  25. import net.minecraft.world.IWorld;
  26. public class BlockCommands {
  27. private static class Offset {
  28. private final int x;
  29. private final int y;
  30. private final int z;
  31. public Offset(int x, int y, int z) {
  32. this.x = x;
  33. this.y = y;
  34. this.z = z;
  35. }
  36. }
  37. private static final Offset[] OFFSETS = new Offset[]{
  38. //new Offset(-1, -1, -1),
  39. new Offset(0, -1, -1),
  40. //new Offset(1, -1, -1),
  41. new Offset(-1, 0, -1),
  42. new Offset(0, 0, -1),
  43. new Offset(1, 0, -1),
  44. //new Offset(-1, 1, -1),
  45. new Offset(0, 1, -1),
  46. //new Offset(1, 1, -1),
  47. new Offset(-1, -1, 0),
  48. new Offset(0, -1, 0),
  49. new Offset(1, -1, 0),
  50. new Offset(-1, 0, 0),
  51. new Offset(0, 0, 0),
  52. new Offset(1, 0, 0),
  53. new Offset(-1, 1, 0),
  54. 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),
  60. new Offset(0, 0, 1),
  61. new Offset(1, 0, 1),
  62. //new Offset(-1, 1, 1),
  63. new Offset(0, 1, 1), //new Offset(1, 1, 1),
  64. };
  65. public static void registerFunctions(ScriptManager sm) {
  66. sm.registerFunction("block.gettag", (sc, in) -> BlockTags.getCollection().get(new ResourceLocation(in[0].getString(sc))));
  67. sm.registerFunction("block.hastag", (sc, in) -> ((Tag<Block>) in[0].get(sc)).contains((Block) in[1].get(sc)));
  68. sm.registerFunction("block.gettype", (sc, in) -> {
  69. Location l = (Location) in[0].get(sc);
  70. return l.getWorld().getBlockState(l.getBlockPos()).getBlock().getRegistryName().toString();
  71. });
  72. sm.registerFunction("block.isair", (sc, in) -> {
  73. Location l = (Location) in[0].get(sc);
  74. return l.getWorld().isAirBlock(l.getBlockPos());
  75. });
  76. sm.registerFunction("block.countair", (sc, in) -> {
  77. Location l = (Location) in[0].get(sc);
  78. IWorld w = l.getWorld();
  79. BlockPos.Mutable pos = new BlockPos.Mutable(l.getBlockPos());
  80. int ox = pos.getX();
  81. int oy = pos.getY();
  82. int oz = pos.getZ();
  83. double counter = 0;
  84. for(Offset off : OFFSETS) {
  85. pos.setPos(ox + off.x, oy + off.y, oz + off.z);
  86. if(w.isAirBlock(pos)) {
  87. counter++;
  88. }
  89. }
  90. return counter;
  91. });
  92. sm.registerFunction("block.get", (sc, in) -> {
  93. Location l = (Location) in[0].get(sc);
  94. return l.getWorld().getBlockState(l.getBlockPos()).getBlock();
  95. });
  96. sm.registerFunction("block.getproperty", (sc, in) -> Mapper.getProperty(in[0].getString(sc)));
  97. sm.registerFunction("block.getstate", (sc, in) -> {
  98. Location l = (Location) in[0].get(sc);
  99. IProperty prop = (IProperty) in[1].get(sc);
  100. BlockState state = l.getWorld().getBlockState(l.getBlockPos());
  101. if(state.has(prop)) {
  102. Object o = l.getWorld().getBlockState(l.getBlockPos()).get(prop);
  103. if(o instanceof Number) {
  104. return ((Number) o).doubleValue();
  105. } else if(o instanceof Boolean) {
  106. return o;
  107. }
  108. return o.toString();
  109. }
  110. return null;
  111. });
  112. sm.registerConsumer("block.clone", (sc, in) -> {
  113. Location l0 = (Location) in[0].get(sc);
  114. Location l1 = (Location) in[1].get(sc);
  115. IWorld w0 = l0.getWorld();
  116. BlockPos pos0 = l0.getBlockPos();
  117. BlockState state = w0.getBlockState(pos0);
  118. TileEntity tileEnt0 = w0.getTileEntity(pos0);
  119. IWorld w1 = l1.getWorld();
  120. BlockPos pos1 = l1.getBlockPos();
  121. w1.setBlockState(pos1, state, 2);
  122. TileEntity tileEnt1 = w1.getTileEntity(pos1);
  123. if(tileEnt0 != null && tileEnt1 != null) {
  124. CompoundNBT nbt = tileEnt0.write(new CompoundNBT());
  125. nbt.putInt("x", pos1.getX());
  126. nbt.putInt("y", pos1.getY());
  127. nbt.putInt("z", pos1.getZ());
  128. tileEnt1.read(nbt);
  129. tileEnt1.markDirty();
  130. }
  131. });
  132. sm.registerConsumer("block.set", (sc, in) -> {
  133. Location l = (Location) in[0].get(sc);
  134. BlockStateParser parser = new BlockStateParser(new StringReader(in[1].getString(sc)), true);
  135. BlockState state = parser.parse(true).getState();
  136. int flag = 2;
  137. if(in.length >= 3 && in[2].getBoolean(sc)) {
  138. flag |= 16;
  139. }
  140. l.getWorld().setBlockState(l.getBlockPos(), state, flag);
  141. });
  142. sm.registerFunction("block.newstate", (sc, in) -> {
  143. BlockStateParser parser = new BlockStateParser(new StringReader(in[0].getString(sc)), true);
  144. return parser.parse(true).getState();
  145. });
  146. sm.registerConsumer("block.setstate", (sc, in) -> {
  147. Location l = (Location) in[0].get(sc);
  148. int flag = 2;
  149. if(in.length >= 3 && in[2].getBoolean(sc)) {
  150. flag |= 16;
  151. }
  152. l.getWorld().setBlockState(l.getBlockPos(), (BlockState) in[1].get(sc), flag);
  153. });
  154. sm.registerFunction("block.setsign", (sc, in) -> {
  155. Location l = (Location) in[0].get(sc);
  156. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  157. if(te == null || !(te instanceof SignTileEntity)) {
  158. return false;
  159. }
  160. SignTileEntity sign = (SignTileEntity) te;
  161. sign.signText[in[1].getInt(sc)] = new StringTextComponent(SnuviUtils.connect(sc, in, 2));
  162. SUpdateTileEntityPacket packet = sign.getUpdatePacket();
  163. l.getWorld().getPlayers().forEach(p -> ((ServerPlayerEntity) p).connection.sendPacket(packet));
  164. return true;
  165. });
  166. sm.registerFunction("block.getsign", (sc, in) -> {
  167. Location l = (Location) in[0].get(sc);
  168. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  169. if(te == null || !(te instanceof SignTileEntity)) {
  170. return null;
  171. }
  172. return ((SignTileEntity) te).signText[in[1].getInt(sc)].getString();
  173. });
  174. sm.registerConsumer("block.setdoorstatus", (sc, in) -> {
  175. Location l = (Location) in[0].get(sc);
  176. BlockPos pos = l.getBlockPos();
  177. ((DoorBlock) l.getWorld().getBlockState(pos).getBlock()).toggleDoor(l.getWorld().getWorld(), pos, in[1].getBoolean(sc));
  178. });
  179. sm.registerFunction("block.getdoorstatus", (sc, in) -> {
  180. Location l = (Location) in[0].get(sc);
  181. return l.getBlockState().get(DoorBlock.OPEN);
  182. });
  183. sm.registerFunction("block.isdoor", (sc, in) -> {
  184. Location l = (Location) in[0].get(sc);
  185. return l.getWorld().getBlockState(l.getBlockPos()).getBlock() instanceof DoorBlock;
  186. });
  187. sm.registerFunction("block.issolid", (sc, in) -> {
  188. return CommandUtils.getBlockState((Location) in[0].get(sc)).isSolid();
  189. });
  190. sm.registerFunction("block.tostack", (sc, in) -> {
  191. Location l = (Location) in[0].get(sc);
  192. return new ItemStack(l.getBlockState().getBlock().asItem());
  193. });
  194. sm.registerFunction("block.getitemamount", (sc, in) -> {
  195. Location l = (Location) in[0].get(sc);
  196. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  197. if(te == null || !(te instanceof ChestTileEntity)) {
  198. return 0.0d;
  199. }
  200. return (double) InventoryUtils.searchInventoryFor((ChestTileEntity) te, (ItemStack) in[2].get(sc), in[1].getBoolean(sc));
  201. });
  202. sm.registerFunction("block.getsecchest", (sc, in) -> {
  203. Location l = (Location) in[0].get(sc);
  204. BlockPos pos = l.getBlockPos();
  205. BlockState state = l.getWorld().getBlockState(pos);
  206. ChestType chesttype = state.get(ChestBlock.TYPE);
  207. if(chesttype == ChestType.SINGLE) {
  208. return null;
  209. }
  210. Direction dir = ChestBlock.getDirectionToAttached(state);
  211. return l.copyAdd(dir.getXOffset(), dir.getYOffset(), dir.getZOffset());
  212. });
  213. sm.registerFunction("block.additem", (sc, in) -> {
  214. Location l = (Location) in[0].get(sc);
  215. ItemStack stack = ((ItemStack) in[1].get(sc));
  216. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  217. if(te == null || !(te instanceof ChestTileEntity)) {
  218. return stack;
  219. }
  220. stack.setCount(InventoryUtils.addToInventory((ChestTileEntity) te, stack));
  221. return stack;
  222. });
  223. sm.registerFunction("block.subitem", (sc, in) -> {
  224. Location l = (Location) in[0].get(sc);
  225. ItemStack stack = ((ItemStack) in[1].get(sc));
  226. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  227. if(te == null || !(te instanceof ChestTileEntity)) {
  228. return stack;
  229. }
  230. stack.setCount(InventoryUtils.removeFromInventory((ChestTileEntity) te, stack));
  231. return stack;
  232. });
  233. sm.registerConsumer("block.setspawnertype", (sc, in) -> {
  234. Location l = (Location) in[0].get(sc);
  235. MobSpawnerTileEntity spawner = (MobSpawnerTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  236. spawner.getSpawnerBaseLogic().setEntityType(EntityType.byKey(in[1].getString(sc)).get());
  237. });
  238. sm.registerFunction("block.getinv", (sc, in) -> {
  239. Location l = (Location) in[0].get(sc);
  240. return (IInventory) l.getWorld().getTileEntity(l.getBlockPos());
  241. });
  242. }
  243. }