Skip to content

Commit 13df396

Browse files
fix: abstracted furnace logic to an abstract
1 parent 6745608 commit 13df396

11 files changed

Lines changed: 424 additions & 315 deletions

src/main/java/com/tcm/MineTale/MineTale.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,21 @@ public class MineTale implements ModInitializer {
3434
*/
3535
@Override
3636
public void onInitialize() {
37+
// 1. Blocks first - They are the foundation
3738
ModBlocks.initialize();
38-
ModRecipes.initialize();
39+
40+
// 2. Items second - Many blocks have associated BlockItems
41+
ModItems.initialize();
42+
43+
// 3. Block Entities third - They now have non-null Blocks to reference
3944
ModBlockEntities.initialize();
40-
ModMenuTypes.initialize();
45+
46+
// 4. Entities & Menus - These depend on the objects above
4147
ModEntities.initialize();
42-
ModItems.initialize();
48+
ModMenuTypes.initialize();
49+
50+
// 5. Recipes last - These depend on Items, Blocks, and Entities existing
51+
ModRecipes.initialize();
4352

4453
Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, MINETALE_CREATIVE_TAB_KEY, MINETALE_CREATIVE_TAB);
4554

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,24 @@ public abstract class AbstractWorkbench<E extends AbstractWorkbenchEntity> exten
3737
protected final Supplier<BlockEntityType<? extends E>> blockEntityType;
3838
protected final boolean isWide;
3939
protected final boolean isTall;
40+
protected int tier;
4041

41-
protected AbstractWorkbench(Properties properties, Supplier<BlockEntityType<? extends E>> supplier, boolean isWide, boolean isTall) {
42+
protected AbstractWorkbench(Properties properties, Supplier<BlockEntityType<? extends E>> supplier, boolean isWide, boolean isTall, int tier) {
4243
super(properties);
4344
this.blockEntityType = supplier;
4445
this.isWide = isWide;
4546
this.isTall = isTall;
47+
this.tier = tier;
4648
this.registerDefaultState(this.stateDefinition.any()
4749
.setValue(FACING, Direction.NORTH)
4850
.setValue(HALF, DoubleBlockHalf.LOWER)
4951
.setValue(TYPE, ChestType.SINGLE));
5052
}
5153

54+
public int getTier() {
55+
return this.tier;
56+
}
57+
5258
@Override
5359
@Nullable
5460
public BlockState getStateForPlacement(BlockPlaceContext context) {
@@ -144,29 +150,17 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt
144150
builder.add(FACING, HALF, TYPE);
145151
}
146152

147-
/**
148-
* Create the block entity for the primary anchor of a multipart workbench.
149-
*
150-
* @param pos the position where the block entity would be created
151-
* @param state the block state at the position
152-
* @return the new block entity when this block is the primary anchor (lower half with TYPE `LEFT` or `SINGLE`), or `null` if this block is a secondary part
153-
*/
154153
@Nullable
155154
@Override
156155
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
157-
// We only want a Block Entity at the 'primary' anchor point of the structure.
158-
// For 1x1: HALF=LOWER, TYPE=SINGLE
159-
// For 2x1: HALF=LOWER, TYPE=LEFT
160-
// For 2x2: HALF=LOWER, TYPE=LEFT
161-
162156
boolean isLower = state.getValue(HALF) == DoubleBlockHalf.LOWER;
163157
ChestType type = state.getValue(TYPE);
164158

165-
// If it's the RIGHT side of a wide block, or the UPPER half of a tall block, return null.
159+
// Only the Master block gets the entity
166160
if (isLower && (type == ChestType.LEFT || type == ChestType.SINGLE)) {
161+
// We call create() on the supplier we were given in the constructor
167162
return this.blockEntityType.get().create(pos, state);
168163
}
169-
170164
return null;
171165
}
172166

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class CampfireWorkbench extends AbstractWorkbench<CampfireWorkbenchEntity
3939
*/
4040
public CampfireWorkbench(Properties properties) {
4141
// Hardcode the supplier and sounds here if they never change
42-
super(properties, () -> ModBlockEntities.CAMPFIRE_WORKBENCH_BE, IS_WIDE, IS_TALL);
42+
super(properties, () -> ModBlockEntities.CAMPFIRE_WORKBENCH_BE, IS_WIDE, IS_TALL, 1);
4343
}
4444

4545
/**
@@ -50,7 +50,7 @@ public CampfireWorkbench(Properties properties) {
5050
*/
5151
public CampfireWorkbench(Properties properties, Supplier<BlockEntityType<? extends CampfireWorkbenchEntity>> supplier) {
5252
// isWide = false, isTall = false (1x1 footprint)
53-
super(properties, supplier, IS_WIDE, IS_TALL);
53+
super(properties, supplier, IS_WIDE, IS_TALL, 1);
5454
}
5555

5656
/**

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

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.tcm.MineTale.block.workbenches;
22

3-
import java.util.function.Supplier;
4-
53
import org.jetbrains.annotations.Nullable;
64

5+
import com.mojang.serialization.Codec;
76
import com.mojang.serialization.MapCodec;
7+
import com.mojang.serialization.codecs.RecordCodecBuilder;
88
import com.tcm.MineTale.block.workbenches.entity.FurnaceWorkbenchEntity;
9-
import com.tcm.MineTale.registry.ModBlockEntities;
9+
import com.tcm.MineTale.registry.ModTiers;
10+
import com.tcm.MineTale.registry.ModTiers.FurnaceTier;
1011

1112
import net.minecraft.core.BlockPos;
1213
import net.minecraft.world.level.Level;
@@ -21,26 +22,33 @@ public class FurnaceWorkbench extends AbstractWorkbench<FurnaceWorkbenchEntity>
2122
private static final boolean IS_WIDE = true;
2223
private static final boolean IS_TALL = true;
2324

24-
public static final MapCodec<FurnaceWorkbench> CODEC = simpleCodec(FurnaceWorkbench::new);
25+
public static final MapCodec<FurnaceWorkbench> CODEC = RecordCodecBuilder.mapCodec(instance ->
26+
instance.group(
27+
// This handles the standard block properties
28+
propertiesCodec(),
29+
// This handles the tier (assuming FurnaceTier is a record/enum with its own codec)
30+
Codec.INT.fieldOf("tier").forGetter(block -> block.getTier())
31+
).apply(instance, (props, id) -> new FurnaceWorkbench(props, ModTiers.getTierFromInt(id)))
32+
);
2533

2634
/**
2735
* Creates a FurnaceWorkbench using the default furnace workbench block entity type and a 2×2 footprint.
2836
*
2937
* @param properties block properties for this workbench
3038
*/
31-
public FurnaceWorkbench(Properties properties) {
32-
super(properties, () -> ModBlockEntities.FURNACE_WORKBENCH_BE, IS_WIDE, IS_TALL);
39+
public FurnaceWorkbench(Properties properties, FurnaceTier tier) {
40+
super(properties, () -> ModTiers.TIER_MAP.get(tier), IS_WIDE, IS_TALL, tier.id());
3341
}
3442

35-
/**
36-
* Creates a FurnaceWorkbench that uses a custom BlockEntityType supplier and a 2x2 footprint.
37-
*
38-
* @param properties block properties for this workbench
39-
* @param supplier supplies the BlockEntityType to use for the workbench's master block entity
40-
*/
41-
public FurnaceWorkbench(Properties properties, Supplier<BlockEntityType<? extends FurnaceWorkbenchEntity>> supplier) {
42-
super(properties, supplier, IS_WIDE, IS_TALL);
43-
}
43+
// /**
44+
// * Creates a FurnaceWorkbench that uses a custom BlockEntityType supplier and a 2x2 footprint.
45+
// *
46+
// * @param properties block properties for this workbench
47+
// * @param supplier supplies the BlockEntityType to use for the workbench's master block entity
48+
// */
49+
// public FurnaceWorkbench(Properties properties, Supplier<BlockEntityType<? extends FurnaceWorkbenchEntity>> supplier) {
50+
// super(properties, supplier, IS_WIDE, IS_TALL, 1);
51+
// }
4452

4553
/**
4654
* Ensures the block is rendered using its model so the 2x2 workbench model is visible.
@@ -72,26 +80,13 @@ public RenderShape getRenderShape(BlockState state) {
7280
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
7381
// Only the Master block (Lower-Left) should tick to process smelting
7482
// This helper ensures the logic only runs on the Server side for our specific BE
75-
return createTickerHelper(type, ModBlockEntities.FURNACE_WORKBENCH_BE, (lvl, pos, st, be) -> {
83+
return createTickerHelper(type, ModTiers.TIER_MAP.get(ModTiers.getTierFromInt(this.tier)), (lvl, pos, st, be) -> {
7684
if (be instanceof FurnaceWorkbenchEntity furnace) {
7785
furnace.tick(lvl, pos, st);
7886
}
7987
});
8088
}
8189

82-
/**
83-
* Create the block entity for this block; only the master block of the multi-block workbench receives an entity.
84-
*
85-
* @return the created {@link BlockEntity} for the master block, or `null` if this position does not host an entity
86-
*/
87-
@Nullable
88-
@Override
89-
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
90-
// AbstractWorkbench logic ensures only the Master block gets the entity.
91-
// We override it here to point specifically to our Furnace entity.
92-
return super.newBlockEntity(pos, state);
93-
}
94-
9590
/**
9691
* Supply the codec used to serialize and deserialize this FurnaceWorkbench.
9792
*

0 commit comments

Comments
 (0)