Skip to content

Commit

Permalink
offline compat for a variety of tags and mechs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 31, 2022
1 parent 4e0a653 commit 0c03f34
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 69 deletions.
Expand Up @@ -236,17 +236,6 @@ public void setFoodLevel(int input) {
}
}

public int getFoodTickTimer() {
return this.compound.getInt("foodTickTimer");
}

public void setFoodTickTimer(int input) {
this.compound = compound.createBuilder().putInt("foodTickTimer", input).build();
if (this.autosave) {
savePlayerData();
}
}

public GameMode getGameMode() {
return GameMode.getByValue(this.compound.getInt("playerGameType"));
}
Expand All @@ -259,28 +248,6 @@ public void setGameMode(GameMode input) {
}
}

public int getHealthInt() {
return this.compound.getShort("Health");
}

public void setHealthInt(int input) {
this.compound = compound.createBuilder().putShort("Health", (short) input).build();
if (this.autosave) {
savePlayerData();
}
}

public boolean getIsInvulnerable() {
return compound.getBoolean("Invulnerable");
}

public void setIsInvulnerable(boolean input) {
this.compound = compound.createBuilder().putBoolean("Invulnerable", input).build();
if (this.autosave) {
savePlayerData();
}
}

public boolean getIsOnGround() {
return compound.getBoolean("OnGround");
}
Expand All @@ -292,17 +259,6 @@ public void setIsOnGround(boolean input) {
}
}

public boolean getIsSleeping() {
return this.compound.getBoolean("Sleeping");
}

public void setIsSleeping(boolean input) {
this.compound = compound.createBuilder().putBoolean("Sleeping", input).build();
if (this.autosave) {
savePlayerData();
}
}

public int getItemInHand() {
return this.compound.getInt("SelectedItemSlot");
}
Expand All @@ -329,17 +285,6 @@ public UUID getUniqueId() {
return this.player;
}

public int getPortalCooldown() {
return this.compound.getInt("PortalCooldown");
}

public void setPortalCooldown(int input) {
this.compound = compound.createBuilder().putInt("PortalCooldown", input).build();
if (this.autosave) {
savePlayerData();
}
}

public int getRemainingAir() {
return this.compound.getShort("Air");
}
Expand Down Expand Up @@ -456,4 +401,17 @@ public void setWalkSpeed(float speed) {
savePlayerData();
}
}

public boolean getAllowFlight() {
return ((CompoundTag) this.compound.getValue().get("abilities")).getBoolean("mayfly");
}

public void setAllowFlight(boolean allow) {
CompoundTag compoundTag = (CompoundTag) this.compound.getValue().get("abilities");
compoundTag = compoundTag.createBuilder().putBoolean("mayfly", allow).build();
this.compound = compound.createBuilder().put("abilities", compoundTag).build();
if (this.autosave) {
savePlayerData();
}
}
}
Expand Up @@ -827,7 +827,7 @@ public static void registerTags() {
// @description
// Returns the cooldown duration remaining on player's material.
// -->
tagProcessor.registerTag(DurationTag.class, "item_cooldown", (attribute, object) -> {
registerOnlineOnlyTag(DurationTag.class, "item_cooldown", (attribute, object) -> {
MaterialTag mat = new ElementTag(attribute.getParam()).asType(MaterialTag.class, attribute.context);
if (mat != null) {
return new DurationTag((long) object.getPlayerEntity().getCooldown(mat.getMaterial()));
Expand Down Expand Up @@ -869,9 +869,15 @@ public static void registerTags() {
// @description
// Returns the player's exhaustion value. Exhaustion is increased in vanilla when a player sprints or jumps, and is used to reduce food saturation over time.
// This can reach a maximum value of 40, and decreases by 4 every tick.
// Works with offline players.
// -->
tagProcessor.registerTag(ElementTag.class, "exhaustion", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().getExhaustion());
if (object.isOnline()) {
return new ElementTag(object.getPlayerEntity().getExhaustion());
}
else {
return new ElementTag(object.getNBTEditor().getExhaustion());
}
});

// Handle EntityTag oxygen tags here to allow getting them when the player is offline
Expand Down Expand Up @@ -1835,9 +1841,15 @@ else if (attribute.startsWith("percent", 2)) {
// @mechanism PlayerTag.can_fly
// @description
// Returns whether the player is allowed to fly.
// Works with offline players.
// -->
registerOnlineOnlyTag(ElementTag.class, "can_fly", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().getAllowFlight());
tagProcessor.registerTag(ElementTag.class, "can_fly", (attribute, object) -> {
if (object.isOnline()) {
return new ElementTag(object.getPlayerEntity().getAllowFlight());
}
else {
return new ElementTag(object.getNBTEditor().getAllowFlight());
}
}, "allowed_flight");

// <--[tag]
Expand All @@ -1847,9 +1859,15 @@ else if (attribute.startsWith("percent", 2)) {
// @description
// Returns the speed the player can fly at.
// Default value is '0.2'.
// Works with offline players.
// -->
registerOnlineOnlyTag(ElementTag.class, "fly_speed", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().getFlySpeed());
tagProcessor.registerTag(ElementTag.class, "fly_speed", (attribute, object) -> {
if (object.isOnline()) {
return new ElementTag(object.getPlayerEntity().getFlySpeed());
}
else {
return new ElementTag(object.getNBTEditor().getFlySpeed());
}
});

// <--[tag]
Expand All @@ -1858,9 +1876,15 @@ else if (attribute.startsWith("percent", 2)) {
// @mechanism PlayerTag.walk_speed
// @description
// Returns the speed the player can walk at.
// Works with offline players.
// -->
registerOnlineOnlyTag(ElementTag.class, "walk_speed", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().getWalkSpeed());
tagProcessor.registerTag(ElementTag.class, "walk_speed", (attribute, object) -> {
if (object.isOnline()) {
return new ElementTag(object.getPlayerEntity().getWalkSpeed());
}
else {
return new ElementTag(object.getNBTEditor().getWalkSpeed());
}
});

// <--[tag]
Expand All @@ -1869,9 +1893,15 @@ else if (attribute.startsWith("percent", 2)) {
// @mechanism PlayerTag.saturation
// @description
// Returns the current food saturation of the player.
// Works with offline players.
// -->
registerOnlineOnlyTag(ElementTag.class, "saturation", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().getSaturation());
tagProcessor.registerTag(ElementTag.class, "saturation", (attribute, object) -> {
if (object.isOnline()) {
return new ElementTag(object.getPlayerEntity().getSaturation());
}
else {
return new ElementTag(object.getNBTEditor().getSaturation());
}
});

// <--[tag]
Expand Down Expand Up @@ -2733,11 +2763,17 @@ public void adjust(Mechanism mechanism) {
// @input ElementTag(Decimal)
// @description
// Sets the current food saturation level of a player.
// Works with offline players.
// @tags
// <PlayerTag.saturation>
// -->
if (mechanism.matches("saturation") && mechanism.requireFloat()) {
getPlayerEntity().setSaturation(mechanism.getValue().asFloat());
if (isOnline()) {
getPlayerEntity().setSaturation(mechanism.getValue().asFloat());
}
else {
getNBTEditor().setSaturation(mechanism.getValue().asFloat());
}
}

// <--[mechanism]
Expand Down Expand Up @@ -2809,11 +2845,17 @@ public void adjust(Mechanism mechanism) {
// @input ElementTag(Boolean)
// @description
// Sets whether the player is allowed to fly.
// Works with offline players.
// @tags
// <PlayerTag.can_fly>
// -->
if (mechanism.matches("can_fly") && mechanism.requireBoolean()) {
getPlayerEntity().setAllowFlight(mechanism.getValue().asBoolean());
if (isOnline()) {
getPlayerEntity().setAllowFlight(mechanism.getValue().asBoolean());
}
else {
getNBTEditor().setAllowFlight(mechanism.getValue().asBoolean());
}
}

// <--[mechanism]
Expand All @@ -2822,6 +2864,7 @@ public void adjust(Mechanism mechanism) {
// @input ElementTag(Decimal)
// @description
// Sets the fly speed of the player. Valid range is 0.0 to 1.0
// Works with offline players.
// @tags
// <PlayerTag.fly_speed>
// -->
Expand Down Expand Up @@ -2965,11 +3008,17 @@ public void adjust(Mechanism mechanism) {
// @input ElementTag(Decimal)
// @description
// Sets the walk speed of the player. The standard value is '0.2'. Valid range is 0.0 to 1.0
// Works with offline players.
// @tags
// <PlayerTag.walk_speed>
// -->
if (mechanism.matches("walk_speed") && mechanism.requireFloat()) {
getPlayerEntity().setWalkSpeed(mechanism.getValue().asFloat());
if (isOnline()) {
getPlayerEntity().setWalkSpeed(mechanism.getValue().asFloat());
}
else {
getNBTEditor().setWalkSpeed(mechanism.getValue().asFloat());
}
}

// <--[mechanism]
Expand All @@ -2978,11 +3027,17 @@ public void adjust(Mechanism mechanism) {
// @input ElementTag(Decimal)
// @description
// Sets the exhaustion level of a player.
// Works with offline players.
// @tags
// <PlayerTag.exhaustion>
// -->
if (mechanism.matches("exhaustion") && mechanism.requireFloat()) {
getPlayerEntity().setExhaustion(mechanism.getValue().asFloat());
if (isOnline()) {
getPlayerEntity().setExhaustion(mechanism.getValue().asFloat());
}
else {
getNBTEditor().setExhaustion(mechanism.getValue().asFloat());
}
}

// <--[mechanism]
Expand Down

0 comments on commit 0c03f34

Please sign in to comment.