public class FossilCleanerBlockEntity extends BlockEntity implements MenuProvider {
private final ItemStackHandler itemHandler = new ItemStackHandler(5);
text
private static final int INPUT_SLOT = 0;
private static final int INPUT_SLOT2 = 4;
private static final int OUTPUT_SLOT = 1;
private static final int OUTPUT_SLOT2 = 2;
private static final int OUTPUT_SLOT3 = 3;
public class FossilCleaningRecipe implements Recipe {
private final NonNullList inputItems;
private final ItemStack output;
private final ResourceLocation id;
@Override
public boolean canCraftInDimensions(int pWidth, int pHeight) {
return true;
}
@Override
public ItemStack getResultItem(RegistryAccess pRegistryAccess) {
return output.copy();
}
@Override
public ResourceLocation getId() {
return id;
}
@Override
public RecipeSerializer<?> getSerializer() {
return Serializer.INSTANCE;
}
@Override
public RecipeType<?> getType() {
return Type.INSTANCE;
}
public static class Type implements RecipeType {
public static final Type INSTANCE = new Type();
public static final String ID = "fossil_cleaning";
}
public static class Serializer implements RecipeSerializer {
public static final Serializer INSTANCE = new Serializer();
public static final ResourceLocation ID = new ResourceLocation(PrehistoricAscension.MOD_ID, "fossil_cleaning");
These will be the random outputs all with different chances of being these items
Bones, bonemeal, coal, deepslate, cobblestone, sand, or the random fossil
For the random fossil make it so it will choose out of a tag
Please send all of the classes back fully so I can easily copy paste them with the fixes
I'll fix all the issues and provide you with the complete corrected classes. Here are the fully corrected versions:
1. FossilCleanerBlockEntity.java
java
package net.newt.prehistoricascension.block.entity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.Containers;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.newt.prehistoricascension.recipe.FossilCleaningRecipe;
import net.newt.prehistoricascension.screen.FossilCleanerMenu;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
public class FossilCleanerBlockEntity extends BlockEntity implements MenuProvider {
private final ItemStackHandler itemHandler = new ItemStackHandler(5);
private static final int INPUT_SLOT = 0;
private static final int INPUT_SLOT2 = 4;
private static final int OUTPUT_SLOT = 1;
private static final int OUTPUT_SLOT2 = 2;
private static final int OUTPUT_SLOT3 = 3;
private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty();
protected final ContainerData data;
private int progress = 0;
private int maxProgress = 78;
public FossilCleanerBlockEntity(BlockPos pPos, BlockState pBlockState) {
super(net.newt.prehistoricascension.block.entity.ModBlockEntities.FOSSIL_CLEANER.get(), pPos, pBlockState);
this.data = new ContainerData() {
@Override
public int get(int pIndex) {
return switch (pIndex) {
case 0 -> FossilCleanerBlockEntity.this.progress;
case 1 -> FossilCleanerBlockEntity.this.maxProgress;
default -> 0;
};
}
@Override
public void set(int pIndex, int pValue) {
switch (pIndex) {
case 0 -> FossilCleanerBlockEntity.this.progress = pValue;
case 1 -> FossilCleanerBlockEntity.this.maxProgress = pValue;
}
}
@Override
public int getCount() {
return 2;
}
};
}
public void drops() {
SimpleContainer inventory = new SimpleContainer(itemHandler.getSlots());
for(int i = 0; i < itemHandler.getSlots(); i++) {
inventory.setItem(i, itemHandler.getStackInSlot(i));
}
Containers.dropContents(this.level, this.worldPosition, inventory);
}
@Override
public Component getDisplayName() {
return Component.translatable("block.prehistoricascension.fossil_cleaner");
}
@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
if(cap == ForgeCapabilities.ITEM_HANDLER) {
return lazyItemHandler.cast();
}
return super.getCapability(cap, side);
}
@Override
public void onLoad() {
super.onLoad();
lazyItemHandler = LazyOptional.of(() -> itemHandler);
}
@Override
public void invalidateCaps() {
super.invalidateCaps();
lazyItemHandler.invalidate();
}
@Override
public @Nullable AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) {
return new FossilCleanerMenu(pContainerId, pPlayerInventory, this, this.data);
}
@Override
protected void saveAdditional(CompoundTag pTag) {
pTag.put("inventory", itemHandler.serializeNBT());
pTag.putInt("fossil_cleaner.progress", progress);
super.saveAdditional(pTag);
}
@Override
public void load(CompoundTag pTag) {
super.load(pTag);
itemHandler.deserializeNBT(pTag.getCompound("inventory"));
progress = pTag.getInt("fossil_cleaner.progress");
}
public void tick(Level pLevel, BlockPos pPos, BlockState pState) {
if(hasRecipe()) {
increaseCraftingProgress();
setChanged(pLevel, pPos, pState);
if(hasProgressFinished()) {
craftItem();
resetProgress();
}
} else {
resetProgress();
}
}
private void resetProgress() {
progress = 0;
}
private void craftItem() {
Optional<FossilCleaningRecipe> recipe = getCurrentRecipe();
if (recipe.isEmpty()) return;
// Extract inputs
this.itemHandler.extractItem(INPUT_SLOT, 1, false);
this.itemHandler.extractItem(INPUT_SLOT2, 1, false);
// Generate 3 random outputs for each output slot
for (int i = 0; i < 3; i++) {
ItemStack result = recipe.get().assemble(new SimpleContainer(0), level.registryAccess());
int outputSlot = OUTPUT_SLOT + i;
if (result.isEmpty()) continue;
if (this.itemHandler.getStackInSlot(outputSlot).isEmpty()) {
this.itemHandler.setStackInSlot(outputSlot, result.copy());
} else if (this.itemHandler.getStackInSlot(outputSlot).is(result.getItem())) {
// Merge if same item
ItemStack currentStack = this.itemHandler.getStackInSlot(outputSlot);
int newCount = Math.min(
currentStack.getCount() + result.getCount(),
currentStack.getMaxStackSize()
);
currentStack.setCount(newCount);
}
// If different item and slot not empty, skip this slot
}
}
private boolean hasProgressFinished() {
return progress >= maxProgress;
}
private void increaseCraftingProgress() {
progress++;
}
private boolean hasRecipe() {
Optional<FossilCleaningRecipe> recipe = getCurrentRecipe();
if(recipe.isEmpty()) {
return false;
}
// Check if inputs are present
if (!inputItemsMatch(recipe.get())) {
return false;
}
// Check if we have at least one empty or partially filled output slot
return hasEmptyOrCompatibleOutputSlots();
}
private boolean inputItemsMatch(FossilCleaningRecipe recipe) {
SimpleContainer inventory = new SimpleContainer(this.itemHandler.getSlots());
for(int i = 0; i < itemHandler.getSlots(); i++) {
inventory.setItem(i, this.itemHandler.getStackInSlot(i));
}
return recipe.matches(inventory, level);
}
private boolean hasEmptyOrCompatibleOutputSlots() {
for (int i = 0; i < 3; i++) {
ItemStack stack = this.itemHandler.getStackInSlot(OUTPUT_SLOT + i);
if (stack.isEmpty() || stack.getCount() < stack.getMaxStackSize()) {
return true;
}
}
return false;
}
private Optional<FossilCleaningRecipe> getCurrentRecipe() {
SimpleContainer inventory = new SimpleContainer(this.itemHandler.getSlots());
for(int i = 0; i < itemHandler.getSlots(); i++) {
inventory.setItem(i, this.itemHandler.getStackInSlot(i));
}
return this.level.getRecipeManager().getRecipeFor(FossilCleaningRecipe.Type.INSTANCE, inventory, level);
}
}