Skip to content

Commit

Permalink
more refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 22, 2024
1 parent 830c80e commit 070d71e
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 102 deletions.
44 changes: 7 additions & 37 deletions src/main/java/io/ix0rai/rainglow/Rainglow.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.mob.SlimeEntity;
import net.minecraft.entity.passive.AllayEntity;
import net.minecraft.entity.passive.GlowSquidEntity;
import net.minecraft.item.Item;
import net.minecraft.resource.ResourceType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -95,41 +89,17 @@ private static void addColour(RainglowColour colour) {
}

public static Identifier getTexture(RainglowEntity entityType, String colour) {
if (entityType == RainglowEntity.GLOW_SQUID) return GLOW_SQUID_TEXTURES.get(colour);
else if (entityType == RainglowEntity.ALLAY) return ALLAY_TEXTURES.get(colour);
else return SLIME_TEXTURES.get(colour);
}

public static int getColourIndex(String colour) {
return COLOURS.indexOf(RainglowColour.get(colour));
}

public static RainglowColour.RGB getInkRgb(int index) {
return COLOURS.get(index).getInkRgb();
}

public static RainglowColour.RGB getPassiveParticleRGB(int index, RandomGenerator random) {
RainglowColour colour = COLOURS.get(index);
return random.nextBoolean() ? colour.getPassiveParticleRgb() : colour.getAltPassiveParticleRgb();
}

public static Item getItem(RainglowEntity entity, int index) {
if (index == -1) {
return entity.getDefaultColour().getItem();
}

return COLOURS.get(index).getItem();
return switch (entityType) {
case ALLAY -> ALLAY_TEXTURES.get(colour);
case SLIME -> SLIME_TEXTURES.get(colour);
case GLOW_SQUID -> GLOW_SQUID_TEXTURES.get(colour);
};
}

public static String generateRandomColourId(RandomGenerator random) {
return COLOURS.get(random.nextInt(COLOURS.size())).getId();
}

public static Identifier getDefaultTexture(RainglowEntity entityType) {
if (entityType == RainglowEntity.SLIME) return RainglowColour.LIME.getTexture(entityType);
else return RainglowColour.BLUE.getTexture(entityType);
}

public static boolean colourUnloaded(RainglowEntity entityType, String colour) {
return !COLOURS.contains(RainglowColour.get(colour)) && !colour.equals(entityType.getDefaultColour().getId());
}
Expand All @@ -147,7 +117,7 @@ public static Text translatableText(String key) {
return Text.translatable(translatableTextKey(key));
}

public static String getColour(RainglowEntity entityType, DataTracker tracker, RandomGenerator random) {
public static RainglowColour getColour(RainglowEntity entityType, DataTracker tracker, RandomGenerator random) {
// generate random colour if the squid's colour isn't currently loaded
String colour = tracker.get(entityType.getTrackedData());
if (colourUnloaded(entityType, colour)) {
Expand All @@ -156,6 +126,6 @@ public static String getColour(RainglowEntity entityType, DataTracker tracker, R
colour = tracker.get(entityType.getTrackedData());
}

return colour;
return RainglowColour.get(colour);
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/ix0rai/rainglow/data/RainglowColour.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.item.Items;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraft.util.random.RandomGenerator;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
Expand Down Expand Up @@ -95,6 +96,15 @@ public static RainglowColour get(String id) {
return BY_ID.get(id);
}

public static RainglowColour.RGB getInkRgb(int index) {
return RainglowColour.values()[index].getInkRgb();
}

public static RainglowColour.RGB getPassiveParticleRGB(int index, RandomGenerator random) {
RainglowColour colour = RainglowColour.values()[index];
return random.nextBoolean() ? colour.getPassiveParticleRgb() : colour.getAltPassiveParticleRgb();
}

public record RGB(float r, float g, float b) {
}

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.ix0rai.rainglow.data;

import io.ix0rai.rainglow.Rainglow;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.mob.SlimeEntity;
import net.minecraft.entity.passive.AllayEntity;
import net.minecraft.entity.passive.GlowSquidEntity;
import net.minecraft.item.Item;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraft.util.random.RandomGenerator;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
Expand Down Expand Up @@ -44,6 +49,28 @@ public TrackedData<String> getTrackedData() {
return this.trackedData;
}

public Identifier getDefaultTexture() {
return this.defaultColour.getTexture(this);
}

public Item getItem(int index) {
if (index == -1) {
return this.getDefaultColour().getItem();
}

return RainglowColour.values()[index].getItem();
}

public RainglowColour readNbt(NbtCompound nbt, RandomGenerator random) {
String colour = nbt.getString(Rainglow.CUSTOM_NBT_KEY);

if (Rainglow.colourUnloaded(this, colour)) {
colour = Rainglow.generateRandomColourId(random);
}

return RainglowColour.get(colour);
}

public static RainglowEntity read(PacketByteBuf buf) {
return get(buf.readString());
}
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,41 @@

@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(RainglowEntity.ALLAY.getTrackedData(), RainglowEntity.ALLAY.getDefaultColour().getId());
builder.add(THIS.getTrackedData(), THIS.getDefaultColour().getId());
}

@Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
String colour = Rainglow.getColour(RainglowEntity.ALLAY, this.getDataTracker(), this.getRandom());
nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour);
RainglowColour colour = Rainglow.getColour(THIS, 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) {
String colour = nbt.getString(Rainglow.CUSTOM_NBT_KEY);

if (Rainglow.colourUnloaded(RainglowEntity.ALLAY, colour)) {
colour = Rainglow.generateRandomColourId(this.getRandom());
}

this.setVariant(RainglowColour.get(colour));
this.setVariant(THIS.readNbt(nbt, this.getRandom()));
}

// 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 = RainglowColour.get(Rainglow.getColour(RainglowEntity.ALLAY, this.getDataTracker(), this.getRandom()));
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom());
entity.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId());
return this.getWorld().spawnEntity(entity);
}

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

@Override
Expand Down
34 changes: 16 additions & 18 deletions src/main/java/io/ix0rai/rainglow/mixin/GlowSquidEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,36 @@
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(RainglowEntity.GLOW_SQUID.getTrackedData(), RainglowColour.BLUE.getId());
builder.add(THIS.getTrackedData(), THIS.getDefaultColour().getId());
}

@Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
String colour = Rainglow.getColour(RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.getRandom());
nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour);
RainglowColour colour = Rainglow.getColour(THIS, 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) {
String colour = nbt.getString(Rainglow.CUSTOM_NBT_KEY);

// if read colour does not exist in the colour map, generate the squid a new one
if (Rainglow.colourUnloaded(RainglowEntity.GLOW_SQUID, colour)) {
colour = Rainglow.generateRandomColourId(this.getRandom());
}

this.setVariant(RainglowColour.get(colour));
this.setVariant(THIS.readNbt(nbt, this.getRandom()));
}

/**
Expand All @@ -56,22 +53,23 @@ 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) {
String colour = Rainglow.getColour(RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.getRandom());
if (!colour.equals(RainglowColour.BLUE.getId())) {
RainglowColour colour = Rainglow.getColour(THIS, 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
this.getWorld().addParticle(ParticleTypes.GLOW, this.getParticleX(0.6), this.getRandomBodyY(), this.getParticleZ(0.6), Rainglow.getColourIndex(colour) + 100, 0, 0);
this.getWorld().addParticle(ParticleTypes.GLOW, this.getParticleX(0.6), this.getRandomBodyY(), this.getParticleZ(0.6), colour.ordinal() + 100, 0, 0);
ci.cancel();
}
}

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

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

@Mixin(SquidEntity.class)
Expand All @@ -91,8 +89,8 @@ 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
String colour = Rainglow.getColour(RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.getRandom());
int index = Rainglow.getColourIndex(colour);
RainglowColour colour = Rainglow.getColour(THIS, 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);
} else {
Expand Down
33 changes: 14 additions & 19 deletions src/main/java/io/ix0rai/rainglow/mixin/SlimeEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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 @@ -22,11 +23,11 @@

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

@Shadow
public abstract int getSize();
protected abstract ParticleEffect getParticles();

protected SlimeEntityMixin(EntityType<? extends SlimeEntity> entityType, World world) {
super(entityType, world);
Expand All @@ -35,33 +36,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(RainglowEntity.SLIME.getTrackedData(), RainglowColour.LIME.getId());
builder.add(THIS.getTrackedData(), THIS.getDefaultColour().getId());
}

@Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
String colour = Rainglow.getColour(RainglowEntity.SLIME, this.getDataTracker(), this.random);
nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour);
RainglowColour colour = Rainglow.getColour(THIS, 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) {
String colour = nbt.getString(Rainglow.CUSTOM_NBT_KEY);

if (Rainglow.colourUnloaded(RainglowEntity.SLIME, colour)) {
colour = Rainglow.generateRandomColourId(this.random);
}

this.setVariant(RainglowColour.get(colour));
this.setVariant(THIS.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 = RainglowColour.get(Rainglow.getColour(RainglowEntity.SLIME, this.getDataTracker(), this.random));
entity.getDataTracker().set(RainglowEntity.SLIME.getTrackedData(), colour.getId());
RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.random);
entity.getDataTracker().set(THIS.getTrackedData(), colour.getId());
return this.getWorld().spawnEntity(entity);
}

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

for (int j = 0; j < size / 2; j ++) {
float f = this.random.nextFloat() * 6.2831855F;
Expand All @@ -92,11 +87,11 @@ public void tick(CallbackInfo ci) {

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

@Override
public void setVariant(RainglowColour colour) {
this.getDataTracker().set(RainglowEntity.SLIME.getTrackedData(), colour.getId());
this.getDataTracker().set(THIS.getTrackedData(), colour.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
public class AllayEntityRendererMixin {
@Inject(method = "getTexture*", at = @At("HEAD"), cancellable = true)
public void getTexture(AllayEntity allayEntity, CallbackInfoReturnable<Identifier> cir) {
String colour = Rainglow.getColour(RainglowEntity.ALLAY, allayEntity.getDataTracker(), allayEntity.getRandom());
RainglowColour colour = Rainglow.getColour(RainglowEntity.ALLAY, allayEntity.getDataTracker(), allayEntity.getRandom());

// if the colour is blue we don't need to override the method
// this optimises a tiny bit
if (Rainglow.CONFIG.isEntityEnabled(RainglowEntity.ALLAY) && !colour.equals(RainglowColour.BLUE.getId())) {
Identifier texture = Rainglow.getTexture(RainglowEntity.ALLAY, colour);
cir.setReturnValue(texture != null ? texture : Rainglow.getDefaultTexture(RainglowEntity.ALLAY));
if (Rainglow.CONFIG.isEntityEnabled(RainglowEntity.ALLAY) && colour != RainglowEntity.ALLAY.getDefaultColour()) {
Identifier texture = Rainglow.getTexture(RainglowEntity.ALLAY, colour.getId());
cir.setReturnValue(texture != null ? texture : RainglowEntity.ALLAY.getDefaultTexture());
}
}
}
Loading

0 comments on commit 070d71e

Please sign in to comment.