Skip to content

Commit

Permalink
feat: store cooldowns and calculate time passed offline
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Feb 1, 2022
1 parent 5802cd6 commit 4270658
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/sekwah/narutomod/capabilities/NinjaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public boolean canRegen() {

private static final String CHAKRA_TAG = "chakra";
private static final String STAMINA_TAG = "stamina";
private static final String SAVE_TIME = "save_time";
private static final String COOLDOWN_TAG = "cooldowns";

private final LazyOptional<INinjaData> holder = LazyOptional.of(() -> this);

Expand Down Expand Up @@ -283,14 +285,29 @@ public Tag serializeNBT() {
final CompoundTag nbt = new CompoundTag();
nbt.putFloat(CHAKRA_TAG, this.chakra);
nbt.putFloat(STAMINA_TAG, this.stamina);
long currentTime = System.currentTimeMillis();
nbt.putLong(SAVE_TIME, currentTime);
final CompoundTag cooldownData = new CompoundTag();
for (String key : this.cooldownTickEvents.keySet()) {
CooldownTickEvent event = this.cooldownTickEvents.get(key);
cooldownData.putInt(key, event.ticks);
}
nbt.put(COOLDOWN_TAG, cooldownData);
return nbt;
}

@Override
public void deserializeNBT(Tag tag) {
if (tag instanceof CompoundTag compoundTag) {
long currentTime = System.currentTimeMillis();
long saveTime = compoundTag.getLong(SAVE_TIME);
int ticksPassed = Math.max((int) ((currentTime - saveTime) / 1000 * 20), 0);
this.chakra = compoundTag.getFloat(CHAKRA_TAG);
this.stamina = compoundTag.getFloat(STAMINA_TAG);
CompoundTag cooldownData = compoundTag.getCompound(COOLDOWN_TAG);
for (String key : cooldownData.getAllKeys()) {
this.cooldownTickEvents.put(key, new CooldownTickEvent(cooldownData.getInt(key) - ticksPassed));
}
}
}

Expand Down

0 comments on commit 4270658

Please sign in to comment.