Guest User

Untitled

a guest
Apr 18th, 2022
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. public class PlatingPressBlockEntity extends InventoryBlockEntity implements MenuProvider {
  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.CRAFTER.get(), pos, state, 5);
  7. }
  8.  
  9. @Override
  10. public Component getDisplayName() {
  11. return new TextComponent("Plating Press");
  12. }
  13.  
  14. @Nullable
  15. @Override
  16. public AbstractContainerMenu createMenu(int id, Inventory inv, Player p_39956_) {
  17. return null;
  18. }
  19.  
  20. public static void tick(Level world, PlatingPressBlockEntity entity) {
  21. if (hasRecipe(entity)) {
  22. craftItem(entity);
  23. }
  24. }
  25.  
  26.  
  27. private static boolean hasRecipe(PlatingPressBlockEntity entity) {
  28. Level world = entity.level;
  29. SimpleContainer inventory = new SimpleContainer(entity.inventory.getSlots());
  30. for (int i = 0; i < entity.inventory.getSlots(); i++) {
  31. inventory.setItem(i, entity.getItemInSlot(i));
  32. }
  33.  
  34. Optional<PlatingPressRecipe> match = world.getRecipeManager()
  35. .getRecipeFor(PlatingPressRecipe.Type.INSTANCE, inventory, world);
  36.  
  37. return match.isPresent()
  38. && canInsertAmountIntoOutputSlot(inventory)
  39. && canInsertItemIntoOutputSlot(inventory, match.get().getResultItem());
  40.  
  41. }
  42.  
  43.  
  44. private static void craftItem(PlatingPressBlockEntity entity) {
  45. Level world = entity.level;
  46. SimpleContainer inventory = new SimpleContainer(entity.inventory.getSlots());
  47. for (int i = 0; i < entity.inventory.getSlots(); i++) {
  48. inventory.setItem(i, entity.inventory.getStackInSlot(i));
  49. }
  50.  
  51. Optional<PlatingPressRecipe> match = world.getRecipeManager()
  52. .getRecipeFor(PlatingPressRecipe.Type.INSTANCE, inventory, world);
  53.  
  54. if (match.isPresent()) {
  55. entity.extractItem(0);
  56. entity.extractItem(1);
  57. entity.extractItem(2);
  58. entity.extractItem(3);
  59.  
  60. entity.insertItem(4, new ItemStack(match.get().getResultItem().getItem(),
  61. 1));
  62. }
  63. }
  64.  
  65. private static boolean canInsertItemIntoOutputSlot(SimpleContainer inventory, ItemStack output) {
  66. return inventory.getItem(4).getItem() == output.getItem() || inventory.getItem(4).isEmpty();
  67. }
  68.  
  69. private static boolean canInsertAmountIntoOutputSlot(SimpleContainer inventory) {
  70. return inventory.getItem(4).getMaxStackSize() > inventory.getItem(4).getCount();
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment