Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx4G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.21.5
yarn_mappings=1.21.5+build.1
loader_version=0.16.10
#Fabric api
fabric_version=0.119.1+1.21.5
minecraft_version=1.21.6
yarn_mappings=1.21.6+build.1
loader_version=0.16.14
# Fabric API
fabric_version=0.127.0+1.21.6

elmendorf_version = 0.15.0

# Mod Properties
mod_version = 1.13.0
mod_version = 1.14.0
maven_group = io.github.ladysnake
archives_base_name = pal

Expand All @@ -22,7 +22,7 @@ display_name = PlayerAbilityLib
license_header = LGPL
gpl_version = 3
curseforge_id = 359522
curseforge_versions = 1.21.5
curseforge_versions = 1.21.6
cf_requirements = fabric-api
modrinth_id = DHQA06r4
release_type = release
11 changes: 6 additions & 5 deletions src/main/java/io/github/ladysnake/pal/AbilityTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
package io.github.ladysnake.pal;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.storage.ReadView;
import net.minecraft.storage.WriteView;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -104,16 +105,16 @@ public interface AbilityTracker {
/**
* Saves this {@code AbilityTracker} in a serialized form to {@code tag}.
*
* @param tag the tag to write to
* @param view the view to read from
*/
@Contract(mutates = "param")
void save(NbtCompound tag);
void save(WriteView view);

/**
* Loads a serialized form of an {@code AbilityTracker} from {@code tag} into this object.
*
* @param tag the tag to read from
* @param view the view to write to
*/
@Contract(mutates = "this")
void load(NbtCompound tag);
void load(ReadView view);
}
23 changes: 11 additions & 12 deletions src/main/java/io/github/ladysnake/pal/SimpleAbilityTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
*/
package io.github.ladysnake.pal;

import com.mojang.serialization.Codec;
import io.github.ladysnake.pal.impl.PalInternals;
import io.github.ladysnake.pal.impl.VanillaAbilityTracker;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.storage.ReadView;
import net.minecraft.storage.WriteView;
import net.minecraft.util.Identifier;

import java.util.SortedSet;
Expand Down Expand Up @@ -104,23 +104,22 @@ protected boolean shouldBeEnabled() {
}

@Override
public void save(NbtCompound tag) {
NbtList list = new NbtList();
public void save(WriteView view) {
var list = view.getListAppender("ability_sources", Codec.STRING);
for (AbilitySource abilitySource : this.abilitySources) {
list.add(NbtString.of(abilitySource.getId().toString()));
list.add(abilitySource.getId().toString());
}
tag.put("ability_sources", list);
}

@Override
public void load(NbtCompound tag) {
NbtList list = tag.getListOrEmpty("ability_sources");
for (int i = 0; i < list.size(); i++) {
AbilitySource source = list.getString(i).map(x -> PalInternals.getSource(Identifier.tryParse(x))).orElse(null);
public void load(ReadView view) {
var list = view.getTypedListView("ability_sources", Codec.STRING);
for (var string : list) {
AbilitySource source = PalInternals.getSource(Identifier.tryParse(string));
if (source != null) {
this.addSource(source);
} else {
PalInternals.LOGGER.warn("Unknown ability source {} attached to {} for {}", list.getString(i), this.player, this.ability);
PalInternals.LOGGER.warn("Unknown ability source {} attached to {} for {}", string, this.player, this.ability);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.storage.NbtReadView;
import net.minecraft.storage.NbtWriteView;
import net.minecraft.storage.ReadView;
import net.minecraft.storage.WriteView;
import net.minecraft.util.ErrorReporter;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -50,8 +54,8 @@ public abstract class ServerPlayerEntityMixin extends PlayerEntity implements Pl
@Unique
private final Map<PlayerAbility, AbilityTracker> palAbilities = new LinkedHashMap<>();

public ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile gameProfile) {
super(world, pos, yaw, gameProfile);
public ServerPlayerEntityMixin(World world, GameProfile gameProfile) {
super(world, gameProfile);
}


Expand Down Expand Up @@ -85,10 +89,10 @@ public void refreshAllPalAbilities(boolean syncVanilla) {
@Inject(method = "copyFrom", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerInteractionManager;setGameMode(Lnet/minecraft/world/GameMode;Lnet/minecraft/world/GameMode;)V", shift = At.Shift.AFTER))
private void copyAbilitiesAfterRespawn(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo ci) {
if (alive) {
NbtCompound nbt = new NbtCompound();
var writeView = NbtWriteView.create(ErrorReporter.EMPTY, oldPlayer.getRegistryManager());
//noinspection ConstantConditions
((ServerPlayerEntityMixin) (Object) oldPlayer).writeAbilitiesToTag(nbt, null);
this.readAbilitiesFromTag(nbt, null);
((ServerPlayerEntityMixin) (Object) oldPlayer).writeAbilitiesToData(writeView, null);
this.readAbilitiesFromData(NbtReadView.create(ErrorReporter.EMPTY, oldPlayer.getRegistryManager(), writeView.getNbt()), null);
}
}

Expand All @@ -102,33 +106,29 @@ private void checkAbilityConsistency(CallbackInfo ci) {
}
}

@Inject(method = "writeCustomDataToNbt", at = @At("RETURN"))
private void writeAbilitiesToTag(NbtCompound tag, CallbackInfo ci) {
NbtList list = new NbtList();
@Inject(method = "writeCustomData", at = @At("RETURN"))
private void writeAbilitiesToData(WriteView view, CallbackInfo ci) {
var list = view.getList("playerabilitylib:abilities");
for (Map.Entry<PlayerAbility, AbilityTracker> entry : this.palAbilities.entrySet()) {
NbtCompound abilityTag = new NbtCompound();
var abilityTag = list.add();
abilityTag.putString("ability_id", entry.getKey().getId().toString());
entry.getValue().save(abilityTag);
list.add(abilityTag);
}
tag.put("playerabilitylib:abilities", list);
}

@Inject(
method = "readCustomDataFromNbt",
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;readCustomDataFromNbt(Lnet/minecraft/nbt/NbtCompound;)V", shift = At.Shift.AFTER)
method = "readCustomData",
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;readCustomData(Lnet/minecraft/storage/ReadView;)V", shift = At.Shift.AFTER)
)
private void readAbilitiesFromTag(NbtCompound tag, CallbackInfo ci) {
for (NbtElement t : tag.getListOrEmpty("playerabilitylib:abilities")) {
if (t instanceof NbtCompound abilityTag) {
if (abilityTag.contains("ability_id")) {
String abilityId = abilityTag.getString("ability_id", "");
AbilityTracker tracker = this.palAbilities.get(PalInternals.getAbility(Identifier.tryParse(abilityId)));
if (tracker != null) {
tracker.load(abilityTag);
} else {
PalInternals.LOGGER.warn("Encountered unknown ability {} while deserializing data for {}", abilityId, this);
}
private void readAbilitiesFromData(ReadView view, CallbackInfo ci) {
for (var abilityTag : view.getListReadView("playerabilitylib:abilities")) {
String abilityId = abilityTag.getString("ability_id", "");
if (!abilityId.isEmpty()) {
AbilityTracker tracker = this.palAbilities.get(PalInternals.getAbility(Identifier.tryParse(abilityId)));
if (tracker != null) {
tracker.load(abilityTag);
} else {
PalInternals.LOGGER.warn("Encountered unknown ability {} while deserializing data for {}", abilityId, this);
}
}
}
Expand Down