Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 21, 2024
1 parent e6e3513 commit 5b83400
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/main/java/io/ix0rai/rainglow/Rainglow.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ public static RainglowColour.RGB getPassiveParticleRGB(int index, RandomGenerato
return random.nextBoolean() ? colour.getPassiveParticleRgb() : colour.getAltPassiveParticleRgb();
}

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

return COLOURS.get(index).getItem();
}

Expand Down Expand Up @@ -164,7 +168,7 @@ public static TrackedData<String> getTrackedColourData(RainglowEntity entityType
public static String getColour(RainglowEntity entityType, DataTracker tracker, RandomGenerator random) {
// generate random colour if the squid's colour isn't currently loaded
String colour = tracker.get(getTrackedColourData(entityType));
if (colourUnloaded(colour)) {
if (colourUnloaded(colour) && !colour.equals(entityType.getDefaultColour().getId())) {
// Use last generated colour if not null else generate a new colour
tracker.set(getTrackedColourData(entityType), generateRandomColourId(random));
colour = tracker.get(getTrackedColourData(entityType));
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@
import java.util.HashMap;

public enum RainglowEntity {
GLOW_SQUID("glow_squid"),
ALLAY("allay"),
SLIME("slime");
GLOW_SQUID("glow_squid", RainglowColour.BLUE),
ALLAY("allay", RainglowColour.BLUE),
SLIME("slime", RainglowColour.LIME);

private static final HashMap<String, RainglowEntity> BY_ID = new HashMap<>();
static {
Arrays.stream(values()).forEach(mode -> BY_ID.put(mode.getId(), mode));
}

private final String id;
private final RainglowColour defaultColour;

RainglowEntity(String id) {
RainglowEntity(String id, RainglowColour defaultColour) {
this.id = id;
this.defaultColour = defaultColour;
}

public String getId() {
return this.id;
}

public RainglowColour getDefaultColour() {
return this.defaultColour;
}

public static RainglowEntity read(PacketByteBuf buf) {
return get(buf.readString());
}
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import net.minecraft.entity.mob.SlimeEntity;
import net.minecraft.entity.passive.AllayEntity;
import net.minecraft.entity.passive.GlowSquidEntity;
import net.minecraft.util.random.RandomGenerator;
import net.minecraft.world.LocalDifficulty;
import net.minecraft.world.ServerWorldAccess;
import net.minecraft.world.World;
Expand All @@ -38,23 +37,27 @@ protected MobEntityMixin(EntityType<? extends MobEntity> entityType, World world
@Inject(method = "initialize", at = @At("RETURN"), cancellable = true)
public void initialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, @Nullable EntityData entityData, CallbackInfoReturnable<EntityData> cir) {
if ((Object) this instanceof GlowSquidEntity glowSquid) {
String colour = Rainglow.generateRandomColourId(this.getRandom());
((GlowSquidVariantProvider) glowSquid).setVariant(getColourOrDefault(this.random, RainglowColour.BLUE, colour));
cir.setReturnValue(new GlowSquidEntityData(getColourOrDefault(this.random, RainglowColour.BLUE, colour)));
RainglowColour colour = generateColour();
((GlowSquidVariantProvider) glowSquid).setVariant(colour);
cir.setReturnValue(new GlowSquidEntityData(colour));
} else if ((Object) this instanceof AllayEntity allay) {
String colour = Rainglow.generateRandomColourId(this.getRandom());
((AllayVariantProvider) allay).setVariant(getColourOrDefault(this.random, RainglowColour.BLUE, colour));
cir.setReturnValue(new AllayEntityData(getColourOrDefault(this.random, RainglowColour.BLUE, colour)));
RainglowColour colour = generateColour();
((AllayVariantProvider) allay).setVariant(colour);
cir.setReturnValue(new AllayEntityData(colour));
} else if ((Object) this instanceof SlimeEntity slime) {
String colour = Rainglow.generateRandomColourId(this.getRandom());
((SlimeVariantProvider) slime).setVariant(getColourOrDefault(this.random, RainglowColour.LIME, colour));
cir.setReturnValue(new SlimeEntityData(getColourOrDefault(this.random, RainglowColour.LIME, colour)));
RainglowColour colour = generateColour();
((SlimeVariantProvider) slime).setVariant(colour);
cir.setReturnValue(new SlimeEntityData(colour));
}
}

@Unique
private RainglowColour getColourOrDefault(RandomGenerator random, RainglowColour defaultColour, String randomColour) {
return random.nextInt(100) >= Rainglow.CONFIG.getRarity(this.getCurrentEntity()) ? defaultColour : RainglowColour.get(randomColour);
private RainglowColour generateColour() {
RainglowEntity entity = getCurrentEntity();
int i = random.nextInt(100);
int rarity = Rainglow.CONFIG.getRarity(entity);

return i >= rarity ? entity.getDefaultColour() : RainglowColour.get(Rainglow.generateRandomColourId(this.random));
}

@Unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

@Mixin(AllayEntityRenderer.class)
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void createParticle(DefaultParticleType defaultParticleType, ClientWorld
cir.setReturnValue(new ItemBreakParticle(clientWorld, d, e, f, new ItemStack(Items.SLIME_BALL)));
// 99.9d and 100.1d are used to account for floating point errors
} else if (h >= 99.9d && h <= 100.1d) {
ItemStack stack = Rainglow.getItem((int) g).getDefaultStack();
ItemStack stack = Rainglow.getItem(RainglowEntity.SLIME, (int) g).getDefaultStack();
cir.setReturnValue(new ItemBreakParticle(clientWorld, d, e, f, stack));
} else {
cir.setReturnValue(null);
Expand Down

0 comments on commit 5b83400

Please sign in to comment.