Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified data tracker access and improved compatibility #11

Merged
merged 1 commit into from
May 14, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package net.spell_engine.mixin.effect;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import net.spell_engine.api.effect.Synchronized;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -20,27 +23,29 @@
import java.util.Map;

@Mixin(LivingEntity.class)
public abstract class LivingEntityStatusEffectSync implements Synchronized.Provider {
public abstract class LivingEntityStatusEffectSync extends Entity implements Synchronized.Provider {
@Shadow
@Final
private Map<StatusEffect, StatusEffectInstance> activeStatusEffects;

private final ArrayList<Synchronized.Effect> SpellEngine_syncedStatusEffects = new ArrayList();
private static final TrackedData<String> SPELL_ENGINE_SYNCED_EFFECTS = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.STRING);

public LivingEntityStatusEffectSync(EntityType<?> type, World world) {
super(type, world);
}

@Inject(method = "initDataTracker", at = @At("TAIL"))
private void initDataTracker_TAIL_SpellEngine_SyncEffects(CallbackInfo ci) {
var entity = (LivingEntity) ((Object) this);
entity.getDataTracker().startTracking(SPELL_ENGINE_SYNCED_EFFECTS, "");
dataTracker.startTracking(SPELL_ENGINE_SYNCED_EFFECTS, "");
}

@Inject(method = "updatePotionVisibility", at = @At("HEAD"))
private void updatePotionVisibility_HEAD_SpellEngine_SyncEffects(CallbackInfo ci) {
var entity = (LivingEntity) ((Object) this);
if (activeStatusEffects.isEmpty()) {
entity.getDataTracker().set(SPELL_ENGINE_SYNCED_EFFECTS, "");
dataTracker.set(SPELL_ENGINE_SYNCED_EFFECTS, "");
} else {
entity.getDataTracker().set(SPELL_ENGINE_SYNCED_EFFECTS, SpellEngine_encodedStatusEffects());
dataTracker.set(SPELL_ENGINE_SYNCED_EFFECTS, SpellEngine_encodedStatusEffects());
}
}

Expand Down Expand Up @@ -69,8 +74,7 @@ private String SpellEngine_encodedStatusEffects() {
}

private List<Synchronized.Effect> SpellEngine_decodeStatusEffects() {
var entity = (LivingEntity) ((Object) this);
var string = entity.getDataTracker().get(SPELL_ENGINE_SYNCED_EFFECTS);
var string = dataTracker.get(SPELL_ENGINE_SYNCED_EFFECTS);
var effects = new ArrayList<Synchronized.Effect>();
for (var effect : string.split("-")) {
var components = effect.split(":");
Expand Down