From b74378d1082e195b30b1b34c645fa841ec40d5c6 Mon Sep 17 00:00:00 2001 From: QPCrummer <66036033+QPCrummer@users.noreply.github.com> Date: Thu, 26 Dec 2024 00:29:22 -0500 Subject: [PATCH] Rewrite for 1.21.4 --- gradle.properties | 2 +- .../fabricautocrafter/AutoCrafterMod.java | 8 +- .../AutoCraftingTableBlockEntity.java | 107 +++++++++--------- 3 files changed, 60 insertions(+), 57 deletions(-) diff --git a/gradle.properties b/gradle.properties index 66ad3be..1f09a64 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ minecraft_version=1.21.4 yarn_mappings=1.21.4+build.2 loader_version=0.16.9 fabric_version=0.112.2+1.21.4 -mod_version=1.0.19 +mod_version=1.1.0 maven_group=com.github.tatercertified archives_base_name=fabricautocrafter modrinth_id=wbqioEpc diff --git a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafterMod.java b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafterMod.java index 7fe1c2f..add8df5 100644 --- a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafterMod.java +++ b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafterMod.java @@ -14,6 +14,8 @@ import net.minecraft.item.Items; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; import net.minecraft.util.Identifier; import static net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents.modifyEntriesEvent; @@ -21,8 +23,10 @@ public class AutoCrafterMod implements ModInitializer { public static final Identifier IDENTIFIER = Identifier.of("autocrafter", "autocrafter"); - public static final Block BLOCK = new AutoCrafter(AbstractBlock.Settings.copy(Blocks.CRAFTING_TABLE).strength(2.5f, 2.5f)); - public static final BlockItem ITEM = new PolymerBlockItem(BLOCK, new Item.Settings(), Items.CRAFTING_TABLE); + private static final RegistryKey key_block = RegistryKey.of(RegistryKeys.BLOCK, IDENTIFIER); + private static final RegistryKey key_item = RegistryKey.of(RegistryKeys.ITEM, IDENTIFIER); + public static final Block BLOCK = new AutoCrafter(AbstractBlock.Settings.copy(Blocks.CRAFTING_TABLE).strength(2.5f, 2.5f).registryKey(key_block)); + public static final BlockItem ITEM = new PolymerBlockItem(BLOCK, new Item.Settings().registryKey(key_item), Items.CRAFTING_TABLE); public static final BlockEntityType TYPE = FabricBlockEntityTypeBuilder.create(AutoCraftingTableBlockEntity::new, BLOCK).build(); @Override diff --git a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java index fed032f..f99c48f 100644 --- a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java +++ b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java @@ -2,20 +2,23 @@ import com.github.tatercertified.fabricautocrafter.mixin.CraftingInventoryMixin; import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.LockableContainerBlockEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.CraftingInventory; import net.minecraft.inventory.Inventories; +import net.minecraft.inventory.RecipeInputInventory; import net.minecraft.inventory.SidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.recipe.*; import net.minecraft.recipe.input.CraftingRecipeInput; +import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.RegistryWrapper; import net.minecraft.screen.ScreenHandler; +import net.minecraft.server.world.ServerWorld; import net.minecraft.text.Text; -import net.minecraft.util.ItemScatterer; import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; @@ -23,16 +26,19 @@ import java.util.*; -public class AutoCraftingTableBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider { +public class AutoCraftingTableBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputInventory { private static final int[] OUTPUT_SLOTS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; private static final int[] INPUT_SLOTS = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + private static final int GRID_WIDTH = 3; + private static final int GRID_HEIGHT = 3; private final List openContainers = new ArrayList<>(); private final CraftingInventory craftingInventory = new CraftingInventory(null, 3, 3); public DefaultedList inventory; private ItemStack output = ItemStack.EMPTY; private RecipeEntry lastRecipe; + private static final RecipeCache recipeCache = new RecipeCache(10); public AutoCraftingTableBlockEntity(BlockPos pos, BlockState state) { super(AutoCrafterMod.TYPE, pos, state); @@ -67,7 +73,17 @@ protected Text getContainerName() { } @Override - protected DefaultedList getHeldStacks() { + public int getWidth() { + return GRID_WIDTH; + } + + @Override + public int getHeight() { + return GRID_HEIGHT; + } + + @Override + public DefaultedList getHeldStacks() { return this.inventory; } @@ -89,7 +105,7 @@ protected ScreenHandler createScreenHandler(int id, PlayerInventory playerInvent @Override public int[] getAvailableSlots(Direction dir) { - return (dir == Direction.DOWN && (!output.isEmpty() || getCurrentRecipe().isPresent())) ? OUTPUT_SLOTS : INPUT_SLOTS; + return (dir == Direction.DOWN && (!output.isEmpty() || (!quickEscape() && !getCurrentRecipe().isEmpty()))) ? OUTPUT_SLOTS : INPUT_SLOTS; } @Override @@ -99,7 +115,7 @@ public boolean canInsert(int slot, ItemStack stack, Direction dir) { @Override public boolean canExtract(int slot, ItemStack stack, Direction dir) { - return slot != 0 || !output.isEmpty() || getCurrentRecipe().isPresent(); + return slot != 0 || !output.isEmpty() || (!quickEscape() && !getCurrentRecipe().isEmpty()); } @Override @@ -124,8 +140,7 @@ public boolean isEmpty() { public ItemStack getStack(int slot) { if (slot > 0) return this.inventory.get(slot - 1); if (!output.isEmpty()) return output; - Optional recipe = getCurrentRecipe(); - return recipe.map(craftingRecipe -> craftingRecipe.craft(craftingInventory.createRecipeInput(), this.getWorld().getRegistryManager())).orElse(ItemStack.EMPTY); + return quickEscape()? ItemStack.EMPTY : getCurrentRecipe(); } @Override @@ -188,60 +203,44 @@ public void clear() { this.inventory.clear(); } - private Optional getCurrentRecipe() { - // Optimization Code from Crec0 - if (this.world == null || this.isEmpty()) return Optional.empty(); - - RecipeManager manager = this.world.getRecipeManager(); - - var getLastRecipe = getLastRecipe(); - - if (getLastRecipe != null) { - CraftingRecipe recipe = (CraftingRecipe) getLastRecipe.value(); - - for (RecipeEntry entry : manager.getAllOfType(RecipeType.CRAFTING)) { - if (entry.value().equals(recipe)) { - CraftingRecipe mapRecipe = entry.value(); - if (mapRecipe.matches(this.craftingInventory.createRecipeInput(), world)) { - return Optional.of(mapRecipe); - } - } - } + private ItemStack getCurrentRecipe() { + BlockEntity craftingRecipeInput = this.world.getBlockEntity(pos); + if (craftingRecipeInput instanceof AutoCraftingTableBlockEntity autoCraftingTableBlockEntity) { + CraftingRecipeInput var11 = autoCraftingTableBlockEntity.createRecipeInput(); + Optional> optional = getCraftingRecipe((ServerWorld) this.world, var11); + if (optional.isPresent()) { + RecipeEntry recipeEntry = optional.get(); + return recipeEntry.value().craft(var11, this.world.getRegistryManager()); + } } + return ItemStack.EMPTY; + } - Optional> recipe = manager.getFirstMatch(RecipeType.CRAFTING, craftingInventory.createRecipeInput(), world); - recipe.ifPresent(this::setLastRecipe); - - return recipe.map(RecipeEntry::value); + private boolean quickEscape() { + return this.world == null || this.isEmpty(); } private ItemStack craft() { - if (this.world == null) return ItemStack.EMPTY; - final Optional optionalRecipe = getCurrentRecipe(); - if (optionalRecipe.isEmpty()) return ItemStack.EMPTY; - - final CraftingRecipe recipe = optionalRecipe.get(); - final CraftingRecipeInput input = craftingInventory.createRecipeInput(); - final ItemStack result = recipe.craft(input, this.getWorld().getRegistryManager()); - final DefaultedList remaining = world.getRecipeManager().getRemainingStacks(RecipeType.CRAFTING, input, world); - for (int i = 0; i < 9; i++) { - ItemStack current = inventory.get(i); - ItemStack remainingStack = remaining.get(i); - if (!current.isEmpty()) { - current.decrement(1); - } - if (!remainingStack.isEmpty()) { - if (current.isEmpty()) { - inventory.set(i, remainingStack); - } else if (ItemStack.areItemsAndComponentsEqual(current, remainingStack)) { - current.increment(remainingStack.getCount()); - } else { - ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), remainingStack); - } + if (quickEscape()) return ItemStack.EMPTY; + BlockEntity craftingRecipeInput = this.world.getBlockEntity(pos); + if (craftingRecipeInput instanceof AutoCraftingTableBlockEntity autoCraftingTableBlockEntity) { + ItemStack itemStack = getCurrentRecipe(); + if (!itemStack.isEmpty()) { + itemStack.onCraftByCrafter(this.world); + autoCraftingTableBlockEntity.getHeldStacks().forEach((stack) -> { + if (!stack.isEmpty()) { + stack.decrement(1); + } + }); + autoCraftingTableBlockEntity.markDirty(); + return itemStack; } } - markDirty(); - return result; + return ItemStack.EMPTY; + } + + private static Optional> getCraftingRecipe(ServerWorld world, CraftingRecipeInput input) { + return recipeCache.getRecipe(world, input); } public CraftingInventory unsetHandler() {