Advertisement
Guest User

Untitled

a guest
Apr 28th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.17 KB | None | 0 0
  1. public class BoilerBlockEntity extends BlockEntity implements MenuProvider {
  2.  
  3. private final ItemStackHandler itemHandler = new ItemStackHandler(Constants.BOILER_SLOTS) {
  4. @Override
  5. protected void onContentsChanged(int slot) {
  6. setChanged();
  7. if(!level.isClientSide()) {
  8. level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
  9. }
  10. }
  11. @Override
  12. public boolean isItemValid(int slot, @NotNull ItemStack stack) {
  13. return switch (slot) {
  14. case 0 -> stack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).isPresent();
  15. default -> super.isItemValid(slot,stack);
  16. };
  17. }
  18. };
  19.  
  20. private final FluidTank WATER_TANK = new FluidTank(5000) {
  21. @Override
  22. protected void onContentsChanged() {
  23. setChanged();
  24. if(!level.isClientSide()) {
  25. level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
  26. }
  27. }
  28. @Override
  29. public boolean isFluidValid(FluidStack stack) {
  30. return stack.getFluid() == Fluids.WATER;
  31. }
  32. };
  33. public void setWaterFluid(FluidStack stack) {
  34. this.WATER_TANK.setFluid(stack);
  35. }
  36. public FluidStack getWaterFluid() {
  37. return this.WATER_TANK.getFluid();
  38. }
  39.  
  40.  
  41. private final FluidTank PP_TANK = new FluidTank(5000) {
  42. @Override
  43. protected void onContentsChanged() {
  44. setChanged();
  45. if(!level.isClientSide()) {
  46. level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
  47. }
  48. }
  49. @Override
  50. public boolean isFluidValid(FluidStack stack) {
  51. return stack.getFluid() == ModFluids.SOURCE_GROWPYMPARTICLES.get() ||
  52. stack.getFluid() == ModFluids.SOURCE_SHRINKPYMPARTICLES.get();
  53. }
  54. };
  55. public void setPPFluid(FluidStack stack) {
  56. this.PP_TANK.setFluid(stack);
  57. }
  58. public FluidStack getPPFluid() {
  59. return this.PP_TANK.getFluid();
  60. }
  61.  
  62.  
  63.  
  64. private LazyOptional<IFluidHandler> lazyPPFluidHandler = LazyOptional.empty();
  65. private LazyOptional<IFluidHandler> lazyWaterFluidHandler = LazyOptional.empty();
  66. private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty();
  67.  
  68. protected final ContainerData data;
  69. private int progress = 0;
  70. private int maxProgress = 100;
  71.  
  72.  
  73. public BoilerBlockEntity(BlockPos pos, BlockState state) {
  74. super(ModBlockEntities.BOILER.get(), pos, state);
  75. this.data = new ContainerData() {
  76. @Override
  77. public int get(int i) {
  78. return switch (i) {
  79. case 0 -> BoilerBlockEntity.this.progress;
  80. case 1 -> BoilerBlockEntity.this.maxProgress;
  81. default -> 0;
  82. };
  83. }
  84. @Override
  85. public void set(int i, int value) {
  86. switch (i) {
  87. case 0 -> BoilerBlockEntity.this.progress = value;
  88. case 1 -> BoilerBlockEntity.this.maxProgress = value;
  89. };
  90. }
  91. @Override
  92. public int getCount() {
  93. return 2;
  94. }
  95. };
  96. }
  97. @Override
  98. public Component getDisplayName() {
  99. return Component.empty();
  100. }
  101.  
  102. @Nullable
  103. @Override
  104. public AbstractContainerMenu createMenu(int id, Inventory inventory, Player player) {
  105. return new BoilerMenu(id, inventory, this, this.data);
  106. }
  107.  
  108. @Override
  109. public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
  110. if(cap == ForgeCapabilities.ITEM_HANDLER) {
  111. return lazyItemHandler.cast();
  112. }
  113.  
  114. // Dont know if this would be a problem
  115. if(cap == ForgeCapabilities.FLUID_HANDLER) {
  116. return lazyPPFluidHandler.cast();
  117. }
  118. if(cap == ForgeCapabilities.FLUID_HANDLER) {
  119. return lazyWaterFluidHandler.cast();
  120. }
  121.  
  122. return super.getCapability(cap, side);
  123. }
  124.  
  125. @Override
  126. public void onLoad() {
  127. super.onLoad();
  128. lazyItemHandler = LazyOptional.of(() -> itemHandler);
  129. lazyWaterFluidHandler = LazyOptional.of(() -> WATER_TANK);
  130. lazyPPFluidHandler = LazyOptional.of(() -> PP_TANK);
  131. }
  132. @Override
  133. public void invalidateCaps() {
  134. super.invalidateCaps();
  135. lazyItemHandler.invalidate();
  136. lazyWaterFluidHandler.invalidate();
  137. lazyPPFluidHandler.invalidate();
  138. }
  139.  
  140. // Not working
  141. @Override
  142. protected void saveAdditional(CompoundTag tag) {
  143. tag.put("inventory", itemHandler.serializeNBT());
  144. tag = WATER_TANK.writeToNBT(tag);
  145. tag = PP_TANK.writeToNBT(tag);
  146. super.saveAdditional(tag);
  147. }
  148. // Not working
  149. @Override
  150. public void load(CompoundTag tag) {
  151. super.load(tag);
  152. itemHandler.deserializeNBT(tag.getCompound("inventory"));
  153. WATER_TANK.readFromNBT(tag);
  154. PP_TANK.readFromNBT(tag);
  155. }
  156.  
  157. public void drops() {
  158. SimpleContainer inventory = new SimpleContainer(itemHandler.getSlots());
  159. for (int i = 0; i < itemHandler.getSlots(); i++) {
  160. inventory.setItem(i, itemHandler.getStackInSlot(i));
  161. }
  162. Containers.dropContents(this.level, this.worldPosition, inventory);
  163. }
  164.  
  165. public static void tick(Level level, BlockPos pos, BlockState state, BoilerBlockEntity entity) {
  166. if(level.isClientSide) return;
  167.  
  168. if(hasSPPRecipe(entity) && hasEnoughWaterFluid(entity)) {
  169. entity.progress++;
  170. setChanged(level, pos, state);
  171.  
  172. if (entity.progress >= entity.maxProgress) {
  173. craftPP(entity);
  174. }
  175. } else if(hasGPPRecipe(entity) && hasEnoughWaterFluid(entity)) {
  176. entity.progress++;
  177. setChanged(level, pos, state);
  178.  
  179. if (entity.progress >= entity.maxProgress) {
  180. craftPP(entity);
  181. }
  182. } else {
  183. entity.resetProgress();
  184. setChanged(level, pos, state);
  185. }
  186.  
  187. if(hasWaterFluidInSlot0(entity)) {
  188. transferWaterFluidToFluidTank(entity);
  189. }
  190. }
  191.  
  192. private static void craftPP(BoilerBlockEntity entity) {
  193. // Remove water
  194. // Remove items
  195. // Add pp
  196. int drainAmount = Math.min(entity.WATER_TANK.getSpace(), 1000);
  197. entity.WATER_TANK.drain(drainAmount, IFluidHandler.FluidAction.EXECUTE);
  198.  
  199. entity.PP_TANK.fill(
  200. new FluidStack(ModFluids.SOURCE_PP.get(), 500),
  201. IFluidHandler.FluidAction.EXECUTE);
  202.  
  203. entity.resetProgress();
  204. }
  205.  
  206.  
  207. private static boolean hasEnoughWaterFluid(BoilerBlockEntity entity) {
  208. return entity.WATER_TANK.getFluidAmount() > 500;
  209. }
  210.  
  211. private static void transferWaterFluidToFluidTank(BoilerBlockEntity entity) {
  212. entity.itemHandler.getStackInSlot(0).getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).ifPresent(handler -> {
  213. int drainAmount = Math.min(entity.WATER_TANK.getSpace(), 1000);
  214.  
  215. FluidStack stack = handler.drain(drainAmount, IFluidHandler.FluidAction.SIMULATE);
  216. if(entity.WATER_TANK.isFluidValid(stack)) {
  217. stack = handler.drain(drainAmount, IFluidHandler.FluidAction.EXECUTE);
  218. fillTankWaterFluid(entity, stack, handler.getContainer());
  219. }
  220. });
  221. }
  222. private static void fillTankWaterFluid(BoilerBlockEntity entity, FluidStack stack, ItemStack container) {
  223. entity.WATER_TANK.fill(stack, IFluidHandler.FluidAction.EXECUTE);
  224. entity.itemHandler.extractItem(0, 1, false);
  225. entity.itemHandler.insertItem(0, container, false);
  226. //entity.level.sendBlockUpdated(entity.worldPosition, entity.getBlockState(),entity.getBlockState(), Block.UPDATE_ALL);
  227. System.out.println("filled with water");
  228. }
  229. private static boolean hasWaterFluidInSlot0(BoilerBlockEntity entity) {
  230. return entity.itemHandler.getStackInSlot(0).getCount() > 0;
  231. }
  232.  
  233.  
  234. private void resetProgress() {
  235. this.progress = 0;
  236. setChanged();
  237. level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
  238. }
  239. private static boolean hasSPPRecipe(BoilerBlockEntity entity) {
  240. SimpleContainer inv = new SimpleContainer(entity.itemHandler.getSlots());
  241. for(int i = 0; i < entity.itemHandler.getSlots(); i++) {
  242. inv.setItem(i, entity.itemHandler.getStackInSlot(i));
  243. }
  244.  
  245. boolean[] materialsFound = {false, false, false};
  246. int additional = 0;
  247.  
  248. for (int i = 4; i <= 12; i++) {
  249. ItemStack stack = entity.itemHandler.getStackInSlot(i);
  250. if (!stack.isEmpty()) {
  251. Item item = stack.getItem();
  252. if (item == Items.EMMERALD)) {
  253. materialsFound[0] = true;
  254. } else if (item == Items.DIAMOND) {
  255. materialsFound[1] = true;
  256. } else if (item == Items.REDSTONE) {
  257. materialsFound[2] = true;
  258. } else {
  259. additional++;
  260. }
  261. }
  262. }
  263. return materialsFound[0] && materialsFound[1] && materialsFound[2] && additional == 0;
  264. }
  265.  
  266. private static boolean hasGPPRecipe(BoilerBlockEntity entity) {
  267. SimpleContainer inv = new SimpleContainer(entity.itemHandler.getSlots());
  268. for(int i = 0; i < entity.itemHandler.getSlots(); i++) {
  269. inv.setItem(i, entity.itemHandler.getStackInSlot(i));
  270. }
  271.  
  272. boolean[] materialsFound = {false, false, false};
  273. int additional = 0;
  274.  
  275. for (int i = 4; i <= 12; i++) {
  276. ItemStack stack = entity.itemHandler.getStackInSlot(i);
  277. if (!stack.isEmpty()) {
  278. Item item = stack.getItem();
  279. if (item == Items.EMERALD) {
  280. materialsFound[0] = true;
  281. } else if (item == Items.DIAMOND) {
  282. materialsFound[1] = true;
  283. } else if (item == Items.BONE_MEAL) {
  284. materialsFound[2] = true;
  285. } else {
  286. additional++;
  287. }
  288. }
  289. }
  290. return materialsFound[0] && materialsFound[1] && materialsFound[2] && additional == 0;
  291. }
  292.  
  293.  
  294. /*Synchronization to the client*/
  295. @Nullable
  296. @Override
  297. public Packet<ClientGamePacketListener> getUpdatePacket() {
  298. return ClientboundBlockEntityDataPacket.create(this);
  299. }
  300. @Override
  301. public CompoundTag getUpdateTag() {
  302. return saveWithoutMetadata();
  303. }
  304. @Override
  305. public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
  306. super.onDataPacket(net, pkt);
  307. if (Minecraft.getInstance().player.containerMenu instanceof BoilerMenu menu && menu.blockEntity == this) {
  308. menu.setWaterFluid(getWaterFluid());
  309. menu.setPPFluid(getPPFluid());
  310. }
  311. }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement