-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResearchTableBlockEntity.java
More file actions
754 lines (666 loc) · 30.7 KB
/
ResearchTableBlockEntity.java
File metadata and controls
754 lines (666 loc) · 30.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
package com.researchcube.block;
import com.researchcube.ResearchCubeMod;
import com.researchcube.item.CubeItem;
import com.researchcube.item.DriveItem;
import com.researchcube.registry.ModBlockEntities;
import com.researchcube.registry.ModConfig;
import com.researchcube.registry.ModFluids;
import com.researchcube.registry.ModItems;
import com.researchcube.research.*;
import com.researchcube.research.criterion.CompleteResearchTrigger;
import com.researchcube.util.NbtUtil;
import com.researchcube.util.TierUtil;
import com.researchcube.util.IdeaChipMatcher;
import com.researchcube.network.SyncResearchProgressPacket;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.Containers;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.fluids.capability.templates.FluidTank;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.Nullable;
import software.bernie.geckolib.animatable.GeoBlockEntity;
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.*;
import software.bernie.geckolib.util.GeckoLibUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* BlockEntity for the Research Table.
* Manages active research state, item storage slots, and tick-based progress.
*
* Slots:
* 0 = Drive
* 1 = Cube
* 2-7 = Item cost inputs
* 8 = Bucket input
* 9 = Bucket output
* 10 = Idea chip
*
* NBT:
* ActiveResearch: ResourceLocation string of current research
* StartTime: game tick when research started
* Inventory: item handler contents
*/
public class ResearchTableBlockEntity extends BlockEntity implements GeoBlockEntity {
public static final int SLOT_DRIVE = 0;
public static final int SLOT_CUBE = 1;
public static final int COST_SLOT_START = 2;
public static final int SLOT_BUCKET_IN = 8;
public static final int SLOT_BUCKET_OUT = 9;
public static final int SLOT_IDEA_CHIP = 10;
public static final int TOTAL_SLOTS = 11; // 2 fixed + 6 cost + 2 bucket + 1 idea chip
public static final int TANK_CAPACITY = 8000; // 8 buckets (in mB)
// GeckoLib animation
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private static final RawAnimation IDLE_ANIM = RawAnimation.begin().thenLoop("animation.researchstation.idle");
private final ItemStackHandler inventory = new ItemStackHandler(TOTAL_SLOTS) {
@Override
protected void onContentsChanged(int slot) {
setChanged();
}
@Override
public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) {
// Override to prevent resizing when loading from older saves with fewer slots.
// Always maintain TOTAL_SLOTS regardless of the saved "Size" value.
ListTag tagList = nbt.getList("Items", Tag.TAG_COMPOUND);
for (int i = 0; i < getSlots(); i++) {
stacks.set(i, ItemStack.EMPTY);
}
for (int i = 0; i < tagList.size(); i++) {
CompoundTag itemTag = tagList.getCompound(i);
int slot = itemTag.getInt("Slot");
if (slot >= 0 && slot < getSlots()) {
stacks.set(slot, ItemStack.parseOptional(provider, itemTag));
}
}
onLoad();
}
};
/** Internal fluid tank for research fluids. Accepts only the 4 research fluids. */
private final FluidTank fluidTank = new FluidTank(TANK_CAPACITY, stack -> {
Fluid fluid = stack.getFluid();
return fluid == ModFluids.THINKING_FLUID.get() ||
fluid == ModFluids.PONDERING_FLUID.get() ||
fluid == ModFluids.REASONING_FLUID.get() ||
fluid == ModFluids.IMAGINATION_FLUID.get();
});
@Nullable
private String activeResearchId = null;
private long startTime = -1;
@Nullable
private String researchKey = null; // team name or UUID string of the researcher
// Snapshot of item costs consumed when research started (for refund on cancel)
@Nullable
private List<ItemCost> consumedCosts = null;
// Snapshot of fluid cost consumed when research started (for refund on cancel)
@Nullable
private FluidCost consumedFluidCost = null;
public ResearchTableBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.RESEARCH_STATION.get(), pos, state);
}
public ItemStackHandler getInventory() {
return inventory;
}
@Nullable
public String getActiveResearchId() {
return activeResearchId;
}
/**
* Returns the ResearchTier of the cube currently in the cube slot, or null if empty/invalid.
* Works on both client and server since inventory is synced via getUpdateTag.
*/
@Nullable
public ResearchTier getCubeTier() {
ItemStack cubeStack = inventory.getStackInSlot(SLOT_CUBE);
if (!cubeStack.isEmpty() && cubeStack.getItem() instanceof CubeItem cube) {
return cube.getTier();
}
return null;
}
public long getStartTime() {
return startTime;
}
public boolean isResearching() {
return activeResearchId != null && startTime >= 0;
}
/** Expose the fluid tank for capability registration and menu sync. */
public FluidTank getFluidTank() {
return fluidTank;
}
/**
* Void all fluid in the tank. Called from WipeTankPacket handler.
*/
public void wipeTank() {
fluidTank.drain(fluidTank.getFluidAmount(), IFluidHandler.FluidAction.EXECUTE);
setChanged();
}
/**
* Map a bucket item to its corresponding research fluid, or null if not a research fluid bucket.
*/
@Nullable
private Fluid getFluidFromBucket(ItemStack stack) {
Item item = stack.getItem();
if (item == ModItems.THINKING_FLUID_BUCKET.get()) return ModFluids.THINKING_FLUID.get();
if (item == ModItems.PONDERING_FLUID_BUCKET.get()) return ModFluids.PONDERING_FLUID.get();
if (item == ModItems.REASONING_FLUID_BUCKET.get()) return ModFluids.REASONING_FLUID.get();
if (item == ModItems.IMAGINATION_FLUID_BUCKET.get()) return ModFluids.IMAGINATION_FLUID.get();
return null;
}
/**
* Process the bucket input slot: if it holds a research fluid bucket and the tank
* has capacity, drain the bucket into the tank and output an empty bucket.
*/
private void processBucketSlot() {
ItemStack bucketIn = inventory.getStackInSlot(SLOT_BUCKET_IN);
ItemStack bucketOut = inventory.getStackInSlot(SLOT_BUCKET_OUT);
if (bucketIn.isEmpty()) return;
Fluid fluid = getFluidFromBucket(bucketIn);
if (fluid == null) return;
// Check output slot can receive an empty bucket
if (!bucketOut.isEmpty() && (!bucketOut.is(Items.BUCKET) || bucketOut.getCount() >= Items.BUCKET.getDefaultMaxStackSize())) {
return;
}
// Try filling the tank
FluidStack toFill = new FluidStack(fluid, 1000);
int filled = fluidTank.fill(toFill, IFluidHandler.FluidAction.SIMULATE);
if (filled < 1000) return; // Not enough tank capacity
// Execute the fill
fluidTank.fill(toFill, IFluidHandler.FluidAction.EXECUTE);
bucketIn.shrink(1);
if (bucketOut.isEmpty()) {
inventory.setStackInSlot(SLOT_BUCKET_OUT, new ItemStack(Items.BUCKET));
} else {
bucketOut.grow(1);
}
setChanged();
}
// ── Research Start ──
/**
* Attempt to start a research. Validates all rules before starting.
* Called from the network packet handler.
*
* @param researchId the research definition ID to start
* @param completedResearch set of research IDs the player has already completed (for prereqs)
* @param researchKey the research key (team name or UUID string) for progress tracking
* @return true if research was started, false if validation failed
*/
public boolean tryStartResearch(String researchId, Set<String> completedResearch, String researchKey) {
if (level == null || level.isClientSide()) return false;
if (isResearching()) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start '{}': already researching '{}'", researchId, activeResearchId);
return false;
}
ResearchDefinition definition = ResearchRegistry.get(researchId);
if (definition == null) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start unknown research: {}", researchId);
return false;
}
// Validate drive & cube
ItemStack driveStack = inventory.getStackInSlot(SLOT_DRIVE);
ItemStack cubeStack = inventory.getStackInSlot(SLOT_CUBE);
if (driveStack.isEmpty() || !(driveStack.getItem() instanceof DriveItem drive)) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start '{}': no drive in drive slot", researchId);
return false;
}
if (cubeStack.isEmpty() || !(cubeStack.getItem() instanceof CubeItem cube)) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start '{}': no cube in cube slot", researchId);
return false;
}
// Tier rules: cube.tier >= research.tier && drive.tier == research.tier
if (!TierUtil.canResearch(cube.getTier(), drive.getTier(), definition.getTier())) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start '{}': tier mismatch. cube={}, drive={}, required={}",
researchId, cube.getTier(), drive.getTier(), definition.getTier());
return false;
}
// Check drive capacity
if (drive.isFull(driveStack)) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start '{}': drive is full", researchId);
return false;
}
// Prerequisites
if (!definition.getPrerequisites().isSatisfied(completedResearch)) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start '{}': prerequisites not met. completed={}", researchId, completedResearch);
return false;
}
// Validate item costs (check availability before consuming)
if (!validateItemCosts(definition.getItemCosts())) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start '{}': item costs not met. required={}", researchId, definition.getItemCosts());
return false;
}
// Validate fluid cost (if defined)
FluidCost fluidCost = definition.getFluidCost();
if (fluidCost != null) {
FluidStack tankContents = fluidTank.getFluid();
if (tankContents.isEmpty() ||
!BuiltInRegistries.FLUID.getKey(tankContents.getFluid()).equals(fluidCost.fluidId()) ||
tankContents.getAmount() < fluidCost.amount()) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start '{}': fluid cost not met. required={} {} mB, tank has {} {} mB",
researchId, fluidCost.fluidId(), fluidCost.amount(),
tankContents.isEmpty() ? "empty" : BuiltInRegistries.FLUID.getKey(tankContents.getFluid()),
tankContents.getAmount());
return false;
}
}
// Validate idea chip (if defined)
if (definition.getIdeaChip().isPresent()) {
ItemStack required = definition.getIdeaChip().get();
ItemStack candidate = inventory.getStackInSlot(SLOT_IDEA_CHIP);
if (!IdeaChipMatcher.matches(required, candidate)) {
ResearchCubeMod.LOGGER.warn("[ResearchCube] Cannot start '{}': missing idea chip: {}",
researchId, required.getHoverName().getString());
return false;
}
}
// All checks passed — snapshot costs for potential refund, then consume
this.consumedCosts = definition.getItemCosts();
this.consumedFluidCost = fluidCost;
consumeItemCosts(definition.getItemCosts());
// Consume fluid cost
if (fluidCost != null) {
FluidStack toDrain = new FluidStack(fluidCost.getFluid(), fluidCost.amount());
fluidTank.drain(toDrain, IFluidHandler.FluidAction.EXECUTE);
}
// Consume idea chip (not refunded on cancel — it is the entry price)
if (definition.getIdeaChip().isPresent()) {
inventory.getStackInSlot(SLOT_IDEA_CHIP).shrink(1);
}
// Start research
this.activeResearchId = researchId;
this.startTime = level.getGameTime();
this.researchKey = researchKey;
setChanged();
// Sync block entity state to client so GUI can display progress view
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
// Play start sound (subtle click/buzz)
level.playSound(null, worldPosition, SoundEvents.UI_BUTTON_CLICK.value(), SoundSource.BLOCKS, 0.6f, 1.2f);
ResearchCubeMod.LOGGER.info("[ResearchCube] Started research '{}' for key {}", researchId, researchKey);
return true;
}
// ── Research Ticking ──
/**
* Server-side tick. Checks research completion against duration.
* Broadcasts HUD progress to the researching player every 20 ticks.
*/
public static void serverTick(Level level, BlockPos pos, BlockState state, ResearchTableBlockEntity be) {
// Process bucket input → tank fill every tick
be.processBucketSlot();
if (!be.isResearching()) {
return;
}
ResearchDefinition definition = ResearchRegistry.get(be.activeResearchId);
if (definition == null) {
// Definition was removed (datapack reload?) — cancel research
ResearchCubeMod.LOGGER.warn("Active research '{}' no longer exists, cancelling.", be.activeResearchId);
be.clearResearchAndNotify(level);
return;
}
// Check if the drive is still present (player might have removed it)
ItemStack driveStack = be.inventory.getStackInSlot(SLOT_DRIVE);
if (driveStack.isEmpty() || !(driveStack.getItem() instanceof DriveItem)) {
ResearchCubeMod.LOGGER.debug("Drive removed during research, cancelling.");
be.clearResearchAndNotify(level);
return;
}
long elapsed = level.getGameTime() - be.startTime;
// Apply config duration multiplier
int adjustedDuration = (int) (definition.getDuration() * ModConfig.getResearchDurationMultiplier());
if (elapsed >= adjustedDuration) {
be.completeResearch(definition);
} else if (level.getGameTime() % 20 == 0) {
// Send HUD progress packet every second
be.sendProgressPacket(level, definition, elapsed, adjustedDuration);
}
}
/**
* Complete the research: randomly select a recipe from the pool, imprint it on the drive.
* Also records the research as completed in per-player SavedData.
*/
private void completeResearch(ResearchDefinition definition) {
if (level == null) return;
ItemStack driveStack = inventory.getStackInSlot(SLOT_DRIVE);
if (driveStack.isEmpty() || !(driveStack.getItem() instanceof DriveItem drive)) {
ResearchCubeMod.LOGGER.error("Cannot complete research — drive missing!");
clearResearch();
return;
}
// Double-check capacity (edge case: another research completed in the meantime)
if (drive.isFull(driveStack)) {
ResearchCubeMod.LOGGER.warn("Drive is full at completion time for research '{}'. Recipe lost.", definition.getId());
clearResearch();
return;
}
if (definition.hasRecipePool()) {
// Weighted random selection from recipe pool
ResourceLocation selectedRecipe = definition.pickWeightedRecipe(level.random);
if (selectedRecipe != null) {
// Imprint recipe into drive
NbtUtil.addRecipe(driveStack, selectedRecipe.toString());
ResearchCubeMod.LOGGER.debug("Research '{}' complete. Imprinted recipe: {}",
definition.getId(), selectedRecipe);
} else {
ResearchCubeMod.LOGGER.warn("Research '{}' weighted pick returned null.", definition.getId());
}
} else {
ResearchCubeMod.LOGGER.warn("Research '{}' completed but has no recipe pool.", definition.getId());
}
// Record completed research in SavedData (keyed by team or player UUID)
if (researchKey != null && level instanceof ServerLevel serverLevel) {
ResearchSavedData savedData = ResearchSavedData.get(serverLevel);
savedData.addCompleted(researchKey, definition.getId());
ResearchCubeMod.LOGGER.debug("Recorded research '{}' as completed for key {}",
definition.getId(), researchKey);
// Fire advancement criterion for player who completed the research
for (ServerPlayer player : serverLevel.players()) {
if (researchKey.equals(ResearchSavedData.getResearchKey(player))) {
CompleteResearchTrigger.INSTANCE.trigger(player, definition.getId());
}
}
}
// Play completion sound and spawn particles
BlockPos pos = worldPosition;
level.playSound(null, pos, SoundEvents.PLAYER_LEVELUP, SoundSource.BLOCKS, 1.0f, 1.0f);
if (level instanceof ServerLevel sl) {
sl.sendParticles(ParticleTypes.HAPPY_VILLAGER,
pos.getX() + 0.5, pos.getY() + 1.2, pos.getZ() + 0.5,
20, 0.5, 0.5, 0.5, 0.02);
sl.sendParticles(ParticleTypes.COMPOSTER,
pos.getX() + 0.5, pos.getY() + 1.0, pos.getZ() + 0.5,
10, 0.3, 0.3, 0.3, 0.05);
}
clearResearchAndNotify(level);
}
// ── Item Cost Validation & Consumption ──
/**
* Check that all item costs can be satisfied from the cost input slots (slots 2-7).
* Applies the config cost multiplier to determine the actual required amount.
*/
private boolean validateItemCosts(List<ItemCost> costs) {
for (ItemCost cost : costs) {
int remaining = ModConfig.applyResearchCostMultiplier(cost.count());
for (int i = COST_SLOT_START; i < SLOT_BUCKET_IN; i++) {
ItemStack stack = inventory.getStackInSlot(i);
if (!stack.isEmpty() && stack.getItem() == cost.getItem()) {
remaining -= stack.getCount();
if (remaining <= 0) break;
}
}
if (remaining > 0) return false;
}
return true;
}
/**
* Consume item costs from the cost input slots.
* Must only be called after validateItemCosts returns true.
* Applies the config cost multiplier to determine the actual consumed amount.
*/
private void consumeItemCosts(List<ItemCost> costs) {
for (ItemCost cost : costs) {
int remaining = ModConfig.applyResearchCostMultiplier(cost.count());
for (int i = COST_SLOT_START; i < SLOT_BUCKET_IN && remaining > 0; i++) {
ItemStack stack = inventory.getStackInSlot(i);
if (!stack.isEmpty() && stack.getItem() == cost.getItem()) {
int take = Math.min(remaining, stack.getCount());
stack.shrink(take);
remaining -= take;
if (stack.isEmpty()) {
inventory.setStackInSlot(i, ItemStack.EMPTY);
}
}
}
}
}
// ── Progress Query (for UI) ──
/**
* Returns the research progress as a float 0.0 to 1.0.
* Returns 0 if no research is active.
* Applies the config duration multiplier to the total duration.
*/
public float getProgress() {
if (!isResearching() || level == null) return 0f;
ResearchDefinition definition = ResearchRegistry.get(activeResearchId);
if (definition == null) return 0f;
long elapsed = level.getGameTime() - startTime;
int adjustedDuration = (int) (definition.getDuration() * ModConfig.getResearchDurationMultiplier());
return Math.min(1.0f, (float) elapsed / adjustedDuration);
}
/**
* Returns the active research definition, or null if none.
*/
@Nullable
public ResearchDefinition getActiveDefinition() {
if (activeResearchId == null) return null;
return ResearchRegistry.get(activeResearchId);
}
// ── State Management ──
/**
* Clears the active research (used on completion or cancellation).
*/
public void clearResearch() {
this.activeResearchId = null;
this.startTime = -1;
this.researchKey = null;
this.consumedCosts = null;
this.consumedFluidCost = null;
setChanged();
}
/**
* Clears research and sends a "not active" HUD packet to the researching player.
*/
private void clearResearchAndNotify(Level level) {
String key = this.researchKey;
clearResearch();
// Sync block entity state to client so GUI can switch back to list view
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
if (key != null && level instanceof ServerLevel serverLevel) {
sendClearPacket(serverLevel, key);
}
}
/**
* Send a progress update packet to the player who started this research.
*/
private void sendProgressPacket(Level level, ResearchDefinition definition, long elapsed, int adjustedDuration) {
if (researchKey == null || !(level instanceof ServerLevel serverLevel)) return;
float progress = Math.min(1.0f, (float) elapsed / adjustedDuration);
int remainingTicks = (int) (adjustedDuration - elapsed);
int remainingSeconds = Math.max(0, remainingTicks / 20);
SyncResearchProgressPacket packet = new SyncResearchProgressPacket(
definition.getDisplayName(),
progress,
remainingSeconds,
definition.getTier().getColor(),
true
);
for (ServerPlayer player : serverLevel.players()) {
if (researchKey.equals(ResearchSavedData.getResearchKey(player))) {
PacketDistributor.sendToPlayer(player, packet);
}
}
}
/**
* Send a "research inactive" packet to clear the HUD for the researching player.
*/
private void sendClearPacket(ServerLevel serverLevel, String key) {
SyncResearchProgressPacket packet = new SyncResearchProgressPacket("", 0f, 0, 0xFFFFFF, false);
for (ServerPlayer player : serverLevel.players()) {
if (key.equals(ResearchSavedData.getResearchKey(player))) {
PacketDistributor.sendToPlayer(player, packet);
}
}
}
/**
* Cancel active research and refund item costs back into cost slots.
* Called from CancelResearchPacket handler.
* Note: the idea chip is NOT refunded — it was consumed as the entry price.
*/
public void cancelResearchWithRefund() {
if (!isResearching()) return;
// Refund consumed costs into the cost slots
if (consumedCosts != null) {
for (ItemCost cost : consumedCosts) {
int remaining = cost.count();
ItemStack refundStack = new ItemStack(cost.getItem(), remaining);
for (int i = COST_SLOT_START; i < SLOT_BUCKET_IN && !refundStack.isEmpty(); i++) {
refundStack = inventory.insertItem(i, refundStack, false);
}
// If slots are full, drop excess items into the world
if (!refundStack.isEmpty() && level != null) {
BlockPos pos = getBlockPos();
Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), refundStack);
} else if (!refundStack.isEmpty()) {
ResearchCubeMod.LOGGER.warn("Could not fully refund {} x{} during cancel — {} lost (no level)",
cost.itemId(), cost.count(), refundStack.getCount());
}
}
}
// Refund consumed fluid cost back into the tank
if (consumedFluidCost != null) {
FluidStack refund = new FluidStack(consumedFluidCost.getFluid(), consumedFluidCost.amount());
int refunded = fluidTank.fill(refund, IFluidHandler.FluidAction.EXECUTE);
if (refunded < consumedFluidCost.amount()) {
ResearchCubeMod.LOGGER.warn("Could not fully refund fluid {} — {} mB lost",
consumedFluidCost.fluidId(), consumedFluidCost.amount() - refunded);
}
}
ResearchCubeMod.LOGGER.debug("Research '{}' cancelled with refund.", activeResearchId);
if (level != null) {
clearResearchAndNotify(level);
} else {
clearResearch();
}
}
/**
* Drop all inventory contents when block is broken.
*/
public void dropContents(Level level, BlockPos pos) {
for (int i = 0; i < inventory.getSlots(); i++) {
Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), inventory.getStackInSlot(i));
}
}
// ── NBT Persistence ──
@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.put("Inventory", inventory.serializeNBT(registries));
tag.put("FluidTank", fluidTank.writeToNBT(registries, new CompoundTag()));
if (activeResearchId != null) {
tag.putString("ActiveResearch", activeResearchId);
tag.putLong("StartTime", startTime);
if (researchKey != null) {
tag.putString("ResearchKey", researchKey);
}
// Persist consumed costs so refunds survive server restarts
if (consumedCosts != null) {
ListTag costList = new ListTag();
for (ItemCost cost : consumedCosts) {
CompoundTag costTag = new CompoundTag();
costTag.putString("Item", cost.itemId().toString());
costTag.putInt("Count", cost.count());
costList.add(costTag);
}
tag.put("ConsumedCosts", costList);
}
if (consumedFluidCost != null) {
CompoundTag fluidCostTag = new CompoundTag();
fluidCostTag.putString("Fluid", consumedFluidCost.fluidId().toString());
fluidCostTag.putInt("Amount", consumedFluidCost.amount());
tag.put("ConsumedFluidCost", fluidCostTag);
}
}
}
@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
inventory.deserializeNBT(registries, tag.getCompound("Inventory"));
if (tag.contains("FluidTank")) {
fluidTank.readFromNBT(registries, tag.getCompound("FluidTank"));
}
if (tag.contains("ActiveResearch")) {
activeResearchId = tag.getString("ActiveResearch");
startTime = tag.getLong("StartTime");
// Support both new "ResearchKey" and legacy "ResearcherUUID" fields
if (tag.contains("ResearchKey")) {
researchKey = tag.getString("ResearchKey");
} else if (tag.hasUUID("ResearcherUUID")) {
researchKey = tag.getUUID("ResearcherUUID").toString();
} else {
researchKey = null;
}
} else {
activeResearchId = null;
startTime = -1;
researchKey = null;
}
// Restore consumed costs for refund support across restarts
if (tag.contains("ConsumedCosts", Tag.TAG_LIST)) {
ListTag costList = tag.getList("ConsumedCosts", Tag.TAG_COMPOUND);
List<ItemCost> costs = new ArrayList<>();
for (int i = 0; i < costList.size(); i++) {
CompoundTag costTag = costList.getCompound(i);
costs.add(new ItemCost(
ResourceLocation.parse(costTag.getString("Item")),
costTag.getInt("Count")
));
}
consumedCosts = costs;
} else {
consumedCosts = null;
}
if (tag.contains("ConsumedFluidCost", Tag.TAG_COMPOUND)) {
CompoundTag fluidCostTag = tag.getCompound("ConsumedFluidCost");
consumedFluidCost = new FluidCost(
ResourceLocation.parse(fluidCostTag.getString("Fluid")),
fluidCostTag.getInt("Amount")
);
} else {
consumedFluidCost = null;
}
}
// ── Client Sync ──
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
CompoundTag tag = super.getUpdateTag(registries);
saveAdditional(tag, registries);
return tag;
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
// ── GeckoLib Animation ──
@Override
public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
controllers.add(new AnimationController<>(this, "idle", 0, state -> {
state.setAnimation(IDLE_ANIM);
return PlayState.CONTINUE;
}));
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return cache;
}
}