Skip to content

Commit

Permalink
Hey, it actually crafts now
Browse files Browse the repository at this point in the history
- Fixed lastRecipe caching not really working
- Changed the name inside of the autocrafter to be "autocrafter"
- Removed some Stream API nonsense
  • Loading branch information
QPCrummer committed May 27, 2024
1 parent fd94527 commit 4ee804d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -78,17 +78,17 @@ 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());
}
}
}

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CraftingTableBlockEntity> TYPE = BlockEntityType.Builder.create(CraftingTableBlockEntity::new, BLOCK).build(null);
public static final BlockEntityType<AutoCraftingTableBlockEntity> TYPE = BlockEntityType.Builder.create(AutoCraftingTableBlockEntity::new, BLOCK).build(null);

@Override
public void onInitialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,33 @@
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};

private final List<AutoCraftingTableContainer> openContainers = new ArrayList<>();
private final CraftingInventory craftingInventory = new CraftingInventory(null, 3, 3);
public DefaultedList<ItemStack> 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;
}
Expand All @@ -69,14 +63,18 @@ protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLo

@Override
protected Text getContainerName() {
return Text.translatable("container.crafting");
return Text.translatable("block.autocrafter.autocrafter");
}

@Override
protected DefaultedList<ItemStack> getHeldStacks() {
return this.inventory;
}

public ItemStack getOutput() {
return this.output;
}

@Override
protected void setHeldStacks(DefaultedList<ItemStack> inventory) {
this.inventory = inventory;
Expand All @@ -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
Expand Down Expand Up @@ -201,14 +199,16 @@ private Optional<CraftingRecipe> getCurrentRecipe() {
var getLastRecipe = getLastRecipe();

if (getLastRecipe != null) {
CraftingRecipe recipe = (CraftingRecipe) getLastRecipe.value();
Collection<RecipeEntry<CraftingRecipe>> 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<CraftingRecipe> 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<RecipeEntry<CraftingRecipe>> recipe = manager.getFirstMatch(RecipeType.CRAFTING, craftingInventory, world);
Expand All @@ -220,6 +220,7 @@ private Optional<CraftingRecipe> getCurrentRecipe() {
private ItemStack craft() {
if (this.world == null) return ItemStack.EMPTY;
final Optional<CraftingRecipe> optionalRecipe = getCurrentRecipe();
System.out.println(optionalRecipe.isEmpty());
if (optionalRecipe.isEmpty()) return ItemStack.EMPTY;

final CraftingRecipe recipe = optionalRecipe.get();
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4ee804d

Please sign in to comment.