Guest User

Untitled

a guest
Apr 19th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. public class PlatingPressBlockEntity extends InventoryBlockEntity {
  2. public static final Component TITLE = new TranslatableComponent(
  3. "container." + TankMod.MOD_ID + ".plating_press");
  4.  
  5. public PlatingPressBlockEntity(BlockPos pos, BlockState state) {
  6. super(TankModBlockEntities.PLATING_PRESS.get(), pos, state, 5);
  7. }
  8.  
  9. public static void tick(Level world, PlatingPressBlockEntity entity) {
  10. if (hasRecipe(entity)) {
  11. craftItem(entity);
  12. }
  13. }
  14. private static boolean hasRecipe(PlatingPressBlockEntity entity) {
  15. Level world = entity.level;
  16. SimpleContainer inventory = new SimpleContainer(entity.inventory.getSlots());
  17. for (int i = 0; i < entity.inventory.getSlots(); i++) {
  18. inventory.setItem(i, entity.getItemInSlot(i));
  19. }
  20.  
  21. Optional<PlatingPressRecipe> match = world.getRecipeManager()
  22. .getRecipeFor(PlatingPressRecipe.Type.INSTANCE, inventory, world);
  23.  
  24. return match.isPresent()
  25. && canInsertAmountIntoOutputSlot(inventory)
  26. && canInsertItemIntoOutputSlot(inventory, match.get().getResultItem());
  27.  
  28. }
  29.  
  30.  
  31. private static void craftItem(PlatingPressBlockEntity entity) {
  32. Level world = entity.level;
  33. SimpleContainer inventory = new SimpleContainer(entity.inventory.getSlots());
  34. for (int i = 0; i < entity.inventory.getSlots(); i++) {
  35. inventory.setItem(i, entity.inventory.getStackInSlot(i));
  36. }
  37.  
  38. Optional<PlatingPressRecipe> match = world.getRecipeManager()
  39. .getRecipeFor(PlatingPressRecipe.Type.INSTANCE, inventory, world);
  40.  
  41. if (match.isPresent()) {
  42. entity.extractItem(0);
  43. entity.extractItem(1);
  44. entity.extractItem(2);
  45. entity.extractItem(3);
  46.  
  47. entity.insertItem(4, new ItemStack(match.get().getResultItem().getItem(),
  48. 1));
  49. }
  50. }
  51.  
  52. private static boolean canInsertItemIntoOutputSlot(SimpleContainer inventory, ItemStack output) {
  53. return inventory.getItem(4).getItem() == output.getItem() || inventory.getItem(4).isEmpty();
  54. }
  55.  
  56. private static boolean canInsertAmountIntoOutputSlot(SimpleContainer inventory) {
  57. return inventory.getItem(4).getMaxStackSize() > inventory.getItem(4).getCount();
  58. }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment