diff --git a/gradle.properties b/gradle.properties index 908c8d8..9b5a98d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ minecraft_version=1.20.6 yarn_mappings=1.20.6+build.1 loader_version=0.15.11 fabric_version=0.97.8+1.20.6 -mod_version=1.0.15 +mod_version=1.0.16 maven_group=com.github.tatercertified archives_base_name=fabricautocrafter modrinth_id=wbqioEpc diff --git a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafter.java b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafter.java index dbff684..48a4152 100644 --- a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafter.java +++ b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafter.java @@ -29,7 +29,7 @@ protected AutoCrafter(AbstractBlock.Settings blockSettings) { protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { if (world.isClient) { return ActionResult.SUCCESS; - } else if (world.getBlockEntity(pos) instanceof CraftingTableBlockEntity entity) { + } else if (world.getBlockEntity(pos) instanceof AutoCraftingTableBlockEntity entity) { player.openHandledScreen(entity); player.incrementStat(Stats.INTERACT_WITH_CRAFTING_TABLE); } @@ -49,9 +49,9 @@ public boolean hasComparatorOutput(BlockState state) { @Override public int getComparatorOutput(BlockState state, World world, BlockPos pos) { if (!state.hasBlockEntity()) return 0; - if (world.getBlockEntity(pos) instanceof CraftingTableBlockEntity craftingTableBlockEntity) { + if (world.getBlockEntity(pos) instanceof AutoCraftingTableBlockEntity craftingTableBlockEntity) { int filled = 0; - for (ItemStack stack : craftingTableBlockEntity.inventory) { + for (ItemStack stack : craftingTableBlockEntity.getHeldStacks()) { if (!stack.isEmpty()) filled++; } return (filled * 15) / 9; @@ -63,10 +63,10 @@ public int getComparatorOutput(BlockState state, World world, BlockPos pos) { @Override public void onStateReplaced(BlockState oldState, World world, BlockPos pos, BlockState newState, boolean moved) { if (oldState.getBlock() != newState.getBlock()) { - if (world.getBlockEntity(pos) instanceof CraftingTableBlockEntity entity) { - ItemScatterer.spawn(world, pos, entity.inventory); - if (!entity.output.isEmpty()) { - ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), entity.output); + if (world.getBlockEntity(pos) instanceof AutoCraftingTableBlockEntity entity) { + ItemScatterer.spawn(world, pos, entity.getHeldStacks()); + if (!entity.getOutput().isEmpty()) { + ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), entity.getOutput()); } world.updateNeighborsAlways(pos, this); } @@ -78,10 +78,10 @@ public void onStateReplaced(BlockState oldState, World world, BlockPos pos, Bloc @Override public void onDestroyedByExplosion(World world, BlockPos pos, Explosion explosion) { - if (world.getBlockEntity(pos) instanceof CraftingTableBlockEntity entity) { - ItemScatterer.spawn(world, pos, entity.inventory); - if (!entity.output.isEmpty()) { - ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), entity.output); + if (world.getBlockEntity(pos) instanceof AutoCraftingTableBlockEntity entity) { + ItemScatterer.spawn(world, pos, entity.getHeldStacks()); + if (!entity.getOutput().isEmpty()) { + ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), entity.getOutput()); } } } @@ -89,6 +89,6 @@ public void onDestroyedByExplosion(World world, BlockPos pos, Explosion explosio @Nullable @Override public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { - return state.isOf(AutoCrafterMod.BLOCK) ? new CraftingTableBlockEntity(pos, state) : null; + return state.isOf(AutoCrafterMod.BLOCK) ? new AutoCraftingTableBlockEntity(pos, state) : null; } } diff --git a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafterMod.java b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafterMod.java index fd1c537..6c2e014 100644 --- a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafterMod.java +++ b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCrafterMod.java @@ -22,7 +22,7 @@ public class AutoCrafterMod implements ModInitializer { public static final Identifier IDENTIFIER = new Identifier("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); - public static final BlockEntityType TYPE = BlockEntityType.Builder.create(CraftingTableBlockEntity::new, BLOCK).build(null); + public static final BlockEntityType TYPE = BlockEntityType.Builder.create(AutoCraftingTableBlockEntity::new, BLOCK).build(null); @Override public void onInitialize() { diff --git a/src/main/java/com/github/tatercertified/fabricautocrafter/CraftingTableBlockEntity.java b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java similarity index 84% rename from src/main/java/com/github/tatercertified/fabricautocrafter/CraftingTableBlockEntity.java rename to src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java index e3991ba..5e93bd6 100644 --- a/src/main/java/com/github/tatercertified/fabricautocrafter/CraftingTableBlockEntity.java +++ b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java @@ -14,22 +14,16 @@ import net.minecraft.registry.RegistryWrapper; import net.minecraft.screen.ScreenHandler; import net.minecraft.text.Text; +import net.minecraft.util.Identifier; import net.minecraft.util.ItemScatterer; import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Optional; +import java.util.*; -import static com.github.tatercertified.fabricautocrafter.AutoCrafterMod.TYPE; -import static net.minecraft.util.math.Direction.DOWN; - - -public class CraftingTableBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider { +public class AutoCraftingTableBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider { 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}; @@ -37,16 +31,16 @@ public class CraftingTableBlockEntity extends LockableContainerBlockEntity imple private final List openContainers = new ArrayList<>(); private final CraftingInventory craftingInventory = new CraftingInventory(null, 3, 3); public DefaultedList inventory; - public ItemStack output = ItemStack.EMPTY; + private ItemStack output = ItemStack.EMPTY; private RecipeEntry lastRecipe; - public CraftingTableBlockEntity(BlockPos pos, BlockState state) { - super(TYPE, pos, state); - this.inventory = DefaultedList.ofSize(10, ItemStack.EMPTY); + public AutoCraftingTableBlockEntity(BlockPos pos, BlockState state) { + super(AutoCrafterMod.TYPE, pos, state); + this.inventory = DefaultedList.ofSize(9, ItemStack.EMPTY); ((CraftingInventoryMixin) craftingInventory).setInventory(this.inventory); } - public CraftingInventory boundCraftingInventory(ScreenHandler handler) { + public CraftingInventory bindInventory(ScreenHandler handler) { ((CraftingInventoryMixin) craftingInventory).setHandler(handler); return craftingInventory; } @@ -69,7 +63,7 @@ protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLo @Override protected Text getContainerName() { - return Text.translatable("container.crafting"); + return Text.translatable("block.autocrafter.autocrafter"); } @Override @@ -77,6 +71,10 @@ protected DefaultedList getHeldStacks() { return this.inventory; } + public ItemStack getOutput() { + return this.output; + } + @Override protected void setHeldStacks(DefaultedList inventory) { this.inventory = inventory; @@ -91,7 +89,7 @@ protected ScreenHandler createScreenHandler(int id, PlayerInventory playerInvent @Override public int[] getAvailableSlots(Direction dir) { - return (dir == DOWN && (!output.isEmpty() || getCurrentRecipe().isPresent())) ? OUTPUT_SLOTS : INPUT_SLOTS; + return (dir == Direction.DOWN && (!output.isEmpty() || getCurrentRecipe().isPresent())) ? OUTPUT_SLOTS : INPUT_SLOTS; } @Override @@ -201,14 +199,16 @@ private Optional getCurrentRecipe() { var getLastRecipe = getLastRecipe(); if (getLastRecipe != null) { - CraftingRecipe recipe = (CraftingRecipe) getLastRecipe.value(); - Collection> craftingRecipes = manager.getAllOfType(RecipeType.CRAFTING); - - return craftingRecipes.stream() - .filter(entry -> entry.value().equals(recipe)) - .map(RecipeEntry::value) - .filter(mapRecipe1 -> mapRecipe1.matches(craftingInventory, world)) - .findFirst(); + 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, world)) { + return Optional.of(mapRecipe); + } + } + } } Optional> recipe = manager.getFirstMatch(RecipeType.CRAFTING, craftingInventory, world); @@ -220,6 +220,7 @@ private Optional getCurrentRecipe() { private ItemStack craft() { if (this.world == null) return ItemStack.EMPTY; final Optional optionalRecipe = getCurrentRecipe(); + System.out.println(optionalRecipe.isEmpty()); if (optionalRecipe.isEmpty()) return ItemStack.EMPTY; final CraftingRecipe recipe = optionalRecipe.get(); @@ -232,9 +233,7 @@ private ItemStack craft() { current.decrement(1); } if (!remainingStack.isEmpty()) { - System.out.println("TEST1"); if (current.isEmpty()) { - System.out.println("TEST2"); inventory.set(i, remainingStack); } else if (ItemStack.areItemsAndComponentsEqual(current, remainingStack)) { current.increment(remainingStack.getCount()); diff --git a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableContainer.java b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableContainer.java index 9153b33..a5b80f8 100644 --- a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableContainer.java +++ b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableContainer.java @@ -20,16 +20,16 @@ import java.util.List; public class AutoCraftingTableContainer extends CraftingScreenHandler { - private final CraftingTableBlockEntity blockEntity; + private final AutoCraftingTableBlockEntity blockEntity; private final PlayerEntity player; private CraftingInventory crafting_inv; - AutoCraftingTableContainer(int id, PlayerInventory playerInventory, CraftingTableBlockEntity blockEntity) { + AutoCraftingTableContainer(int id, PlayerInventory playerInventory, AutoCraftingTableBlockEntity blockEntity) { super(id, playerInventory); this.blockEntity = blockEntity; this.player = playerInventory.player; - this.crafting_inv = blockEntity.boundCraftingInventory(this); + this.crafting_inv = blockEntity.bindInventory(this); var self = (AccessorScreenHandler) this; slots.clear(); @@ -70,7 +70,7 @@ public ItemStack quickMove(PlayerEntity player, int slot) { ItemStack current = before.copy(); if (!this.insertItem(current, 10, 46, true)) return ItemStack.EMPTY; this.blockEntity.removeStack(0, before.getCount() - current.getCount()); - slots.get(0).onQuickTransfer(current, before); // calls onCrafted if different + slots.getFirst().onQuickTransfer(current, before); // calls onCrafted if different return this.blockEntity.getStack(0); } return super.quickMove(player, slot);