Teleblock now persists across login sessions

This commit is contained in:
Ceikry 2022-06-23 23:29:05 +00:00 committed by Ryan
parent 419533e6c8
commit 67e54d5429

View file

@ -1,8 +1,12 @@
package core.game.node.entity.state.impl;
import api.PersistPlayer;
import core.game.node.entity.Entity;
import core.game.node.entity.player.Player;
import core.game.node.entity.state.EntityState;
import core.game.node.entity.state.StatePulse;
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONObject;
import java.nio.ByteBuffer;
@ -10,17 +14,17 @@ import java.nio.ByteBuffer;
* Handles the teleblock state pulse.
* @author Vexia
*/
public final class TeleblockStatePulse extends StatePulse {
public final class TeleblockStatePulse extends StatePulse implements PersistPlayer {
/**
* The ticks needed to pass.
*/
private final int ticks;
public int ticks;
/**
* The current tick.
*/
private int currentTick;
public int currentTick;
/**
* Constructs a new {@Code TeleblockStatePulse} {@Code Object}
@ -34,6 +38,33 @@ public final class TeleblockStatePulse extends StatePulse {
this.currentTick = currentTick;
}
//Required to be instantiated as a ContentListener for PersistPlayer
public TeleblockStatePulse() {
super(null, 0);
}
@Override
public void savePlayer(@NotNull Player player, @NotNull JSONObject save) {
TeleblockStatePulse tbPulse = (TeleblockStatePulse) player.getStateManager().get(EntityState.TELEBLOCK);
if (tbPulse != null && tbPulse.isSaveRequired()) {
JSONObject tbObj = new JSONObject();
tbObj.put("tb-state-current", "" + tbPulse.currentTick);
tbObj.put("tb-state-total", "" + tbPulse.ticks);
save.put("teleblock-state", tbObj);
}
}
@Override
public void parsePlayer(@NotNull Player player, @NotNull JSONObject data) {
if (data.containsKey("teleblock-state")) {
JSONObject tbData = (JSONObject) data.get("teleblock-state");
int currentTick = Integer.parseInt(tbData.get("tb-state-current").toString());
int totalTicks = Integer.parseInt(tbData.get("tb-state-total").toString());
player.getStateManager().set(EntityState.TELEBLOCK, totalTicks, currentTick);
}
}
@Override
public boolean isSaveRequired() {
return currentTick < ticks;
@ -68,7 +99,7 @@ public final class TeleblockStatePulse extends StatePulse {
@Override
public StatePulse create(Entity entity, Object... args) {
return new TeleblockStatePulse(entity, (int) args[0], 0);
return new TeleblockStatePulse(entity, (int) args[0], args.length > 1 ? (int) args[1] : 0);
}
}