22
33import org .jspecify .annotations .Nullable ;
44
5+ import com .mojang .serialization .Codec ;
6+ import com .tcm .MineTale .block .workbenches .menu .CampfireWorkbenchMenu ;
57import com .tcm .MineTale .registry .ModBlockEntities ;
68
79import net .minecraft .core .BlockPos ;
10+ import net .minecraft .world .SimpleContainer ;
811import net .minecraft .world .entity .player .Inventory ;
912import net .minecraft .world .entity .player .Player ;
1013import net .minecraft .world .inventory .AbstractContainerMenu ;
14+ import net .minecraft .world .inventory .ContainerData ;
15+ import net .minecraft .world .level .Level ;
1116import net .minecraft .world .level .block .state .BlockState ;
17+ import net .minecraft .world .level .storage .ValueInput ;
18+ import net .minecraft .world .level .storage .ValueOutput ;
1219
1320public class CampfireWorkbenchEntity extends AbstractWorkbenchEntity {
21+ private final SimpleContainer inventory = new SimpleContainer (7 );
22+
23+ private int cookTime ;
24+ private int cookTimeTotal = 200 ;
25+ private int fuelTime ;
26+
27+ protected final ContainerData data = new ContainerData () {
28+ /**
29+ * Retrieves an internal data value by index for UI synchronization.
30+ *
31+ * @param index the data index: 0 = remaining fuel time, 1 = fuel total (constant 100),
32+ * 2 = current cook progress, 3 = total cook time
33+ * @return the value associated with {@code index}, or 0 for any other index
34+ */
35+ @ Override
36+ public int get (int index ) {
37+ return switch (index ) {
38+ case 0 -> fuelTime ;
39+ case 1 -> 100 ; // Fuel total
40+ case 2 -> cookTime ;
41+ case 3 -> cookTimeTotal ;
42+ default -> 0 ;
43+ };
44+ }
45+
46+ /**
47+ * Sets an internal workbench data field identified by index.
48+ *
49+ * Supported indices:
50+ * <ul>
51+ * <li>0 — sets {@code fuelTime}</li>
52+ * <li>2 — sets {@code cookTime}</li>
53+ * </ul>
54+ * Other indices are ignored.
55+ *
56+ * @param index the data index to set
57+ * @param value the value to assign to the indexed field
58+ */
59+ @ Override
60+ public void set (int index , int value ) {
61+ switch (index ) {
62+ case 0 -> fuelTime = value ;
63+ case 2 -> cookTime = value ;
64+ }
65+ }
66+
67+ /**
68+ * The number of data values exposed by this ContainerData.
69+ *
70+ * @return the number of data entries (4)
71+ */
72+ @ Override
73+ public int getCount () {
74+ return 4 ;
75+ }
76+ };
77+
1478 /**
1579 * Creates a CampfireWorkbenchEntity at the given position with the provided block state.
1680 *
@@ -22,21 +86,65 @@ public class CampfireWorkbenchEntity extends AbstractWorkbenchEntity {
2286 public CampfireWorkbenchEntity (BlockPos blockPos , BlockState blockState ) {
2387 super (ModBlockEntities .CAMPFIRE_WORKBENCH_BE , blockPos , blockState );
2488
25- this .scanRadius = 6 .0 ;
89+ this .scanRadius = 0 .0 ;
2690 this .tier = 1 ;
2791 }
2892
93+ public void tick (Level level , BlockPos pos , BlockState state ) {
94+ if (level .isClientSide ()) return ;
95+
96+ // boolean changed = false;
97+ // ItemStack fuel = inventory.getItem(FUEL_SLOT);
98+ // List<ItemStack> inputs = List.of(inventory.getItem(INPUT_1), inventory.getItem(INPUT_2));
99+ }
100+
101+ /**
102+ * Determines whether the provided item stack is a supported food.
103+ *
104+ * @param stack the item stack to test
105+ */
106+ // private boolean isCookable(List<ItemStack> stacks) { return stack.is(ItemTags.MEAT); }
107+
108+ /**
109+ * Determines whether the given item stack represents a fuel item.
110+ *
111+ * @param stack the item stack to inspect
112+ */
113+ // private boolean isFuel(ItemStack stack) { return stack.is(ItemTags.LOGS_THAT_BURN) || stack.is(Items.STICK); }
114+
115+ /**
116+ * Persist entity-specific state into the provided ValueOutput.
117+ *
118+ * Stores the workbench's tier as "WorkbenchTier" and its scan radius as "ScanRadius"
119+ * using type-safe Codecs.
120+ *
121+ * @param valueOutput the output writer used to serialize this entity's fields
122+ */
123+ @ Override
124+ protected void saveAdditional (ValueOutput valueOutput ) {
125+ super .saveAdditional (valueOutput );
126+ // store() uses Codecs for type safety
127+ valueOutput .store ("WorkbenchTier" , Codec .INT , this .tier );
128+ valueOutput .store ("ScanRadius" , Codec .DOUBLE , this .scanRadius );
129+ }
130+
29131 /**
30- * Creates the server-side container menu for this workbench for the given player and window ID .
132+ * Restores workbench-specific state from persistent storage and applies defaults when keys are absent .
31133 *
32- * @param syncId the window synchronization ID provided by the client
33- * @param playerInventory the player's inventory
34- * @param player the player opening the menu
35- * @return the created {@link AbstractContainerMenu}, or `null` if no menu should be opened
134+ * Delegates to the superclass load logic, then reads:
135+ * - "WorkbenchTier" (int) into {@code tier}, defaulting to {@code 1} if missing.
136+ * - "ScanRadius" (double) into {@code scanRadius}, defaulting to {@code 5.0} if missing.
36137 */
138+ @ Override
139+ protected void loadAdditional (ValueInput valueInput ) {
140+ super .loadAdditional (valueInput );
141+ // read() returns an Optional
142+ this .tier = valueInput .read ("WorkbenchTier" , Codec .INT ).orElse (1 );
143+ this .scanRadius = valueInput .read ("ScanRadius" , Codec .DOUBLE ).orElse (0.0 );
144+ }
145+
37146 @ Override
38147 public @ Nullable AbstractContainerMenu createMenu (int syncId , Inventory playerInventory , Player player ) {
39- // TODO Auto-generated method stub
40- throw new UnsupportedOperationException ("Unimplemented method 'createMenu'" );
148+ return new CampfireWorkbenchMenu (syncId , playerInventory , this .inventory , this .data );
41149 }
42150}
0 commit comments