Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,31 @@ public class AlchemistsWorkbench extends AbstractWorkbench<AlchemistsWorkbenchEn

public static final MapCodec<AlchemistsWorkbench> CODEC = simpleCodec(AlchemistsWorkbench::new);

/**
* Constructs a new AlchemistsWorkbench configured to use the mod's Alchemists Workbench block-entity type.
*
* @param properties block properties for this workbench
*/
public AlchemistsWorkbench(Properties properties) {
// Hardcode the supplier and sounds here if they never change
super(properties, () -> ModBlockEntities.ALCHEMISTS_WORKBENCH_BE, IS_WIDE, IS_TALL, 1);
}

/**
* Constructs an AlchemistsWorkbench configured with the provided block properties and block-entity type supplier.
*
* @param properties the block properties to apply to this workbench
* @param supplier supplier that provides the BlockEntityType for the AlchemistsWorkbenchEntity
*/
public AlchemistsWorkbench(Properties properties, Supplier<BlockEntityType<? extends AlchemistsWorkbenchEntity>> supplier) {
super(properties, supplier, IS_WIDE, IS_TALL, 1);
}

/**
* Provides a ticker for workbench block entities when the supplied block entity type matches this block's entity type.
* Supplies the ticker used to update this workbench's block entity instances when appropriate.
*
* @param type the block entity type to match against this block's workbench entity type
* @return a BlockEntityTicker that updates matching workbench block entities, or {@code null} if the types do not match
* @param type the block entity type to check for compatibility with this workbench
* @return the BlockEntityTicker that updates matching workbench block entities, or {@code null} if the provided type is not compatible
*/
@Nullable
@Override
Expand All @@ -47,6 +58,11 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, Block
return createTickerHelper(type, ModBlockEntities.ALCHEMISTS_WORKBENCH_BE, AbstractWorkbenchEntity::tick);
}

/**
* Provides the MapCodec used to serialise and deserialise this workbench.
*
* @return the MapCodec for this AlchemistsWorkbench
*/
@Override
protected MapCodec<? extends AlchemistsWorkbench> codec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void setItem(int slot, ItemStack stack) {
/**
* Indicates whether this workbench currently has fuel available to perform crafting.
*
* @return `true` if the workbench has fuel available, `false` otherwise.
* @return {@code true} if the workbench has fuel available, {@code false} otherwise.
*/
protected abstract boolean hasFuel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class AlchemistsWorkbenchEntity extends AbstractWorkbenchEntity {
protected final ContainerData data = new ContainerData() {
Expand Down Expand Up @@ -85,18 +86,14 @@ public AlchemistsWorkbenchEntity(BlockPos blockPos, BlockState blockState) {
@Override
protected void saveAdditional(ValueOutput valueOutput) {
super.saveAdditional(valueOutput);
// store() uses Codecs for type safety
valueOutput.store("WorkbenchTier", Codec.INT, this.tier);
valueOutput.store("ScanRadius", Codec.DOUBLE, this.scanRadius);

// Convert the SimpleContainer to a List of ItemStacks for the Codec
// Or use the built-in NBT helper if your framework supports it
List<ItemStack> stacks = new ArrayList<>();
for (int i = 0; i < inventory.getContainerSize(); i++) {
stacks.add(inventory.getItem(i));
}
// Optimized: Stream the inventory slots directly into a list
List<ItemStack> stacks = IntStream.range(0, inventory.getContainerSize())
.mapToObj(inventory::getItem)
.toList();

// CHANGE: Use OPTIONAL_CODEC instead of CODEC
valueOutput.store("Inventory", ItemStack.OPTIONAL_CODEC.listOf(), stacks);
}

Expand All @@ -117,6 +114,9 @@ protected void loadAdditional(ValueInput valueInput) {

// Read the inventory list back
valueInput.read("Inventory", ItemStack.OPTIONAL_CODEC.listOf()).ifPresent(stacks -> {
// Fix: Clear existing items to prevent stale data if the saved list is smaller
inventory.clearContent();

for (int i = 0; i < stacks.size() && i < inventory.getContainerSize(); i++) {
inventory.setItem(i, stacks.get(i));
}
Expand Down Expand Up @@ -153,22 +153,12 @@ public RecipeType<WorkbenchRecipe> getWorkbenchRecipeType() {
}

/**
* Determines whether the workbench currently has fuel available.
*
* Checks that the entity is in a loaded level and that the configured fuel slot contains an item.
* Report that the workbench has fuel available.
*
* @return `true` if the entity is in a loaded level and the fuel slot contains an item, `false` otherwise.
* @return `true` always.
*/
@Override
protected boolean hasFuel() {
if (this.level == null) return false;

// Check if block is lit
// BlockState state = this.level.getBlockState(this.worldPosition);
// boolean isLit = state.hasProperty(BlockStateProperties.LIT) && state.getValue(BlockStateProperties.LIT);

boolean hasFuelItem = !this.getItem(Constants.FUEL_SLOT).isEmpty();

return hasFuelItem;
return true;
}
}