Skip to content

Commit

Permalink
fix first crash in #28
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 28, 2024
1 parent 64a98c9 commit b66654e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
24 changes: 7 additions & 17 deletions src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,51 @@
import net.minecraft.entity.data.DataTracker.Builder;
import net.minecraft.entity.passive.AllayEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.random.RandomGenerator;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(AllayEntity.class)
public abstract class AllayEntityMixin extends Entity implements AllayVariantProvider {
@Unique
private static final RainglowEntity THIS = RainglowEntity.ALLAY;

protected AllayEntityMixin(EntityType<? extends AllayEntity> entityType, World world) {
super(entityType, world);
throw new UnsupportedOperationException();
}

@Inject(method = "initDataTracker", at = @At("TAIL"))
protected void initDataTracker(Builder builder, CallbackInfo ci) {
builder.add(THIS.getTrackedData(), THIS.getDefaultColour().getId());
builder.add(RainglowEntity.ALLAY.getTrackedData(), RainglowEntity.ALLAY.getDefaultColour().getId());
}

@Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom());
RainglowColour colour = Rainglow.getColour(RainglowEntity.ALLAY, this.getDataTracker(), this.random);
nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour.getId());
}

@Inject(method = "readCustomDataFromNbt", at = @At("TAIL"))
public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
this.setVariant(THIS.readNbt(nbt, this.getRandom()));
this.setVariant(RainglowEntity.ALLAY.readNbt(nbt, this.random));
}

// triggered when an allay duplicates, to apply the same colour as parent
@Redirect(method = "duplicate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z"))
public boolean spawnWithColour(World instance, Entity entity) {
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom());
entity.getDataTracker().set(THIS.getTrackedData(), colour.getId());
RainglowColour colour = Rainglow.getColour(RainglowEntity.ALLAY, this.getDataTracker(), this.random);
entity.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId());
return this.getWorld().spawnEntity(entity);
}

@Override
public RainglowColour getVariant() {
return Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom());
return Rainglow.getColour(RainglowEntity.ALLAY, this.getDataTracker(), this.random);
}

@Override
public void setVariant(RainglowColour colour) {
this.getDataTracker().set(THIS.getTrackedData(), colour.getId());
}

@Unique
public RandomGenerator getRandom() {
return this.random;
this.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId());
}
}
18 changes: 7 additions & 11 deletions src/main/java/io/ix0rai/rainglow/mixin/GlowSquidEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,32 @@
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(GlowSquidEntity.class)
public abstract class GlowSquidEntityMixin extends SquidEntity implements GlowSquidVariantProvider {
@Unique
private static final RainglowEntity THIS = RainglowEntity.GLOW_SQUID;

protected GlowSquidEntityMixin(EntityType<? extends SquidEntity> entityType, World world) {
super(entityType, world);
throw new UnsupportedOperationException();
}

@Inject(method = "initDataTracker", at = @At("TAIL"))
protected void initDataTracker(Builder builder, CallbackInfo ci) {
builder.add(THIS.getTrackedData(), THIS.getDefaultColour().getId());
builder.add(RainglowEntity.GLOW_SQUID.getTrackedData(), RainglowEntity.GLOW_SQUID.getDefaultColour().getId());
}

@Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom());
RainglowColour colour = Rainglow.getColour(RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.getRandom());
nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour.getId());
}

@Inject(method = "readCustomDataFromNbt", at = @At("TAIL"))
public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
this.setVariant(THIS.readNbt(nbt, this.getRandom()));
this.setVariant(RainglowEntity.GLOW_SQUID.readNbt(nbt, this.getRandom()));
}

/**
Expand All @@ -53,7 +49,7 @@ public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
*/
@Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;addParticle(Lnet/minecraft/particle/ParticleEffect;DDDDDD)V"), cancellable = true)
public void tickMovement(CallbackInfo ci) {
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom());
RainglowColour colour = Rainglow.getColour(RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.getRandom());

if (colour != RainglowColour.BLUE) {
// we add 100 to g to let the mixin know that we want to override the method
Expand All @@ -64,12 +60,12 @@ public void tickMovement(CallbackInfo ci) {

@Override
public RainglowColour getVariant() {
return Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom());
return Rainglow.getColour(RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.getRandom());
}

@Override
public void setVariant(RainglowColour colour) {
this.getDataTracker().set(THIS.getTrackedData(), colour.getId());
this.getDataTracker().set(RainglowEntity.GLOW_SQUID.getTrackedData(), colour.getId());
}

@Mixin(SquidEntity.class)
Expand All @@ -89,7 +85,7 @@ protected SquidEntityMixin(EntityType<? extends WaterCreatureEntity> entityType,
private int spawnParticles(ServerWorld instance, ParticleEffect particle, double x, double y, double z, int count, double deltaX, double deltaY, double deltaZ, double speed) {
if (((Object) this) instanceof GlowSquidEntity) {
// send in custom colour data
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom());
RainglowColour colour = Rainglow.getColour(RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.getRandom());
int index = colour.ordinal();
// round x to 1 decimal place and append index data to the next two
return ((ServerWorld) this.getWorld()).spawnParticles(particle, (Math.round(x * 10)) / 10D + index / 1000D, y + 0.5, z, 0, deltaX, deltaY, deltaZ, speed);
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/io/ix0rai/rainglow/mixin/SlimeEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -23,9 +22,6 @@

@Mixin(SlimeEntity.class)
public abstract class SlimeEntityMixin extends Entity implements SlimeVariantProvider {
@Unique
private static final RainglowEntity THIS = RainglowEntity.SLIME;

@Shadow
protected abstract ParticleEffect getParticles();

Expand All @@ -36,27 +32,27 @@ protected SlimeEntityMixin(EntityType<? extends SlimeEntity> entityType, World w

@Inject(method = "initDataTracker", at = @At("TAIL"))
protected void initDataTracker(DataTracker.Builder builder, CallbackInfo ci) {
builder.add(THIS.getTrackedData(), THIS.getDefaultColour().getId());
builder.add(RainglowEntity.SLIME.getTrackedData(), RainglowEntity.SLIME.getDefaultColour().getId());
}

@Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.random);
RainglowColour colour = Rainglow.getColour(RainglowEntity.SLIME, this.getDataTracker(), this.random);
nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour.getId());
}

@Inject(method = "readCustomDataFromNbt", at = @At("TAIL"))
public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
this.setVariant(THIS.readNbt(nbt, this.random));
this.setVariant(RainglowEntity.SLIME.readNbt(nbt, this.random));
}

/**
* @reason make smaller slimes spawn with the same colour as the parent in a split
*/
@Redirect(method = "remove", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z"))
public boolean spawnWithParentColour(World instance, Entity entity) {
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.random);
entity.getDataTracker().set(THIS.getTrackedData(), colour.getId());
RainglowColour colour = Rainglow.getColour(RainglowEntity.SLIME, this.getDataTracker(), this.random);
entity.getDataTracker().set(RainglowEntity.SLIME.getTrackedData(), colour.getId());
return this.getWorld().spawnEntity(entity);
}

Expand All @@ -72,7 +68,7 @@ public boolean spawnWithParentColour(World instance, Entity entity) {
)
public void tick(CallbackInfo ci) {
float size = this.getDimensions(this.getPose()).width();
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.random);
RainglowColour colour = Rainglow.getColour(RainglowEntity.SLIME, this.getDataTracker(), this.random);
int index = colour.ordinal();

for (int j = 0; j < size / 2; j ++) {
Expand All @@ -87,11 +83,11 @@ public void tick(CallbackInfo ci) {

@Override
public RainglowColour getVariant() {
return Rainglow.getColour(THIS, this.getDataTracker(), this.random);
return Rainglow.getColour(RainglowEntity.SLIME, this.getDataTracker(), this.random);
}

@Override
public void setVariant(RainglowColour colour) {
this.getDataTracker().set(THIS.getTrackedData(), colour.getId());
this.getDataTracker().set(RainglowEntity.SLIME.getTrackedData(), colour.getId());
}
}

0 comments on commit b66654e

Please sign in to comment.