Skip to content

Commit 8c1c970

Browse files
Merge pull request #66 from CodeMonkeysMods/Blacksmith-an-stuff
Blacksmith an stuff
2 parents 357b705 + 82cb8e3 commit 8c1c970

3 files changed

Lines changed: 31 additions & 25 deletions

File tree

src/main/java/com/tcm/MineTale/block/workbenches/AlchemistsWorkbench.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,31 @@ public class AlchemistsWorkbench extends AbstractWorkbench<AlchemistsWorkbenchEn
2525

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

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

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

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

61+
/**
62+
* Provides the MapCodec used to serialise and deserialise this workbench.
63+
*
64+
* @return the MapCodec for this AlchemistsWorkbench
65+
*/
5066
@Override
5167
protected MapCodec<? extends AlchemistsWorkbench> codec() {
5268
return CODEC;

src/main/java/com/tcm/MineTale/block/workbenches/entity/AbstractWorkbenchEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public void setItem(int slot, ItemStack stack) {
280280
/**
281281
* Indicates whether this workbench currently has fuel available to perform crafting.
282282
*
283-
* @return `true` if the workbench has fuel available, `false` otherwise.
283+
* @return {@code true} if the workbench has fuel available, {@code false} otherwise.
284284
*/
285285
protected abstract boolean hasFuel();
286286

src/main/java/com/tcm/MineTale/block/workbenches/entity/AlchemistsWorkbenchEntity.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.ArrayList;
2323
import java.util.List;
24+
import java.util.stream.IntStream;
2425

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

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

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

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

118115
// Read the inventory list back
119116
valueInput.read("Inventory", ItemStack.OPTIONAL_CODEC.listOf()).ifPresent(stacks -> {
117+
// Fix: Clear existing items to prevent stale data if the saved list is smaller
118+
inventory.clearContent();
119+
120120
for (int i = 0; i < stacks.size() && i < inventory.getContainerSize(); i++) {
121121
inventory.setItem(i, stacks.get(i));
122122
}
@@ -153,22 +153,12 @@ public RecipeType<WorkbenchRecipe> getWorkbenchRecipeType() {
153153
}
154154

155155
/**
156-
* Determines whether the workbench currently has fuel available.
157-
*
158-
* Checks that the entity is in a loaded level and that the configured fuel slot contains an item.
156+
* Report that the workbench has fuel available.
159157
*
160-
* @return `true` if the entity is in a loaded level and the fuel slot contains an item, `false` otherwise.
158+
* @return `true` always.
161159
*/
162160
@Override
163161
protected boolean hasFuel() {
164-
if (this.level == null) return false;
165-
166-
// Check if block is lit
167-
// BlockState state = this.level.getBlockState(this.worldPosition);
168-
// boolean isLit = state.hasProperty(BlockStateProperties.LIT) && state.getValue(BlockStateProperties.LIT);
169-
170-
boolean hasFuelItem = !this.getItem(Constants.FUEL_SLOT).isEmpty();
171-
172-
return hasFuelItem;
162+
return true;
173163
}
174164
}

0 commit comments

Comments
 (0)