Skip to content

Commit

Permalink
fix data trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 8, 2024
1 parent 1d479d9 commit 4b0861e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
37 changes: 8 additions & 29 deletions src/main/java/io/ix0rai/rainglow/Rainglow.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public class Rainglow implements ModInitializer {
private static final Map<String, Identifier> GLOWSQUID_TEXTURES = new HashMap<>();
private static final Map<String, Identifier> ALLAY_TEXTURES = new HashMap<>();
private static final Map<String, Identifier> SLIME_TEXTURES = new HashMap<>();
public static final TrackedData<String> glowSquidColour = DataTracker.registerData(GlowSquidEntity.class, TrackedDataHandlerRegistry.STRING);
private static TrackedData<String> allayColour;
private static TrackedData<String> slimeColour;
public static final TrackedData<String> GLOW_SQUID_COLOUR = DataTracker.registerData(GlowSquidEntity.class, TrackedDataHandlerRegistry.STRING);
public static final TrackedData<String> ALLAY_COLOUR = DataTracker.registerData(AllayEntity.class, TrackedDataHandlerRegistry.STRING);
public static final TrackedData<String> SLIME_COLOUR = DataTracker.registerData(SlimeEntity.class, TrackedDataHandlerRegistry.STRING);

public static final String CUSTOM_NBT_KEY = "Colour";

Expand Down Expand Up @@ -144,32 +144,11 @@ public static Text translatableText(String key) {
}

public static TrackedData<String> getTrackedColourData(RainglowEntity entityType) {
// we cannot statically load the tracked data because then it gets registered too early
// it breaks the squids' other tracked data, their dark ticks after being hurt
// this is a workaround to make sure the data is registered at the right time
// we simply ensure it isn't loaded until it's needed, and that fixes the issue

if (entityType == RainglowEntity.GLOW_SQUID) {
// if (glowSquidColour == null) {
// glowSquidColour = DataTracker.registerData(GlowSquidEntity.class, TrackedDataHandlerRegistry.STRING);
// }
//
// return glowSquidColour;
} else if (entityType == RainglowEntity.ALLAY) {
if (allayColour == null) {
allayColour = DataTracker.registerData(AllayEntity.class, TrackedDataHandlerRegistry.STRING);
}

return allayColour;
} else if (entityType == RainglowEntity.SLIME) {
if (slimeColour == null) {
slimeColour = DataTracker.registerData(SlimeEntity.class, TrackedDataHandlerRegistry.STRING);
}

return slimeColour;
}

throw new RuntimeException("called getTrackedColourData on an unsupported entity type!");
return switch (entityType) {
case GLOW_SQUID -> GLOW_SQUID_COLOUR;
case ALLAY -> ALLAY_COLOUR;
case SLIME -> SLIME_COLOUR;
};
}

public static String getColour(RainglowEntity entityType, DataTracker tracker, RandomGenerator random) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.ix0rai.rainglow.data.RainglowEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.data.DataTracker.Builder;
import net.minecraft.entity.passive.AllayEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.random.RandomGenerator;
Expand All @@ -25,8 +26,8 @@ protected AllayEntityMixin(EntityType<? extends AllayEntity> entityType, World w
}

@Inject(method = "initDataTracker", at = @At("TAIL"))
protected void initDataTracker(CallbackInfo ci) {
this.getDataTracker().set(Rainglow.getTrackedColourData(RainglowEntity.ALLAY), RainglowColour.BLUE.getId());
protected void initDataTracker(Builder builder, CallbackInfo ci) {
builder.add(Rainglow.getTrackedColourData(RainglowEntity.ALLAY), RainglowColour.BLUE.getId());
}

@Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.entity.mob.WaterCreatureEntity;
import net.minecraft.entity.passive.GlowSquidEntity;
import net.minecraft.entity.passive.SquidEntity;
import net.minecraft.entity.data.DataTracker.Builder;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.particle.ParticleTypes;
Expand All @@ -18,7 +19,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.entity.data.DataTracker.Builder;

@Mixin(GlowSquidEntity.class)
public abstract class GlowSquidEntityMixin extends SquidEntity implements GlowSquidVariantProvider {
Expand All @@ -29,7 +29,7 @@ protected GlowSquidEntityMixin(EntityType<? extends SquidEntity> entityType, Wor

@Inject(method = "initDataTracker", at = @At("TAIL"))
protected void initDataTracker(Builder builder, CallbackInfo ci) {
builder.add(Rainglow.glowSquidColour, RainglowColour.BLUE.getId());
builder.add(Rainglow.GLOW_SQUID_COLOUR, RainglowColour.BLUE.getId());
}

@Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
Expand Down

0 comments on commit 4b0861e

Please sign in to comment.