Skip to content

Commit

Permalink
generify more mixins, fix colouring via item
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 24, 2024
1 parent dd63b3c commit 89c43b8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 73 deletions.
31 changes: 27 additions & 4 deletions src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.ix0rai.rainglow.Rainglow;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityData;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
Expand All @@ -14,15 +15,17 @@
import net.minecraft.util.Identifier;
import net.minecraft.util.random.RandomGenerator;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Arrays;
import java.util.HashMap;
import java.util.function.Function;

public enum RainglowEntity {
GLOW_SQUID("glow_squid", RainglowColour.BLUE, DataTracker.registerData(GlowSquidEntity.class, TrackedDataHandlerRegistry.STRING)),
ALLAY("allay", RainglowColour.BLUE, DataTracker.registerData(AllayEntity.class, TrackedDataHandlerRegistry.STRING)),
SLIME("slime", RainglowColour.LIME, DataTracker.registerData(SlimeEntity.class, TrackedDataHandlerRegistry.STRING));
GLOW_SQUID("glow_squid", RainglowColour.BLUE, DataTracker.registerData(GlowSquidEntity.class, TrackedDataHandlerRegistry.STRING), GlowSquidEntityData::new),
ALLAY("allay", RainglowColour.BLUE, DataTracker.registerData(AllayEntity.class, TrackedDataHandlerRegistry.STRING), AllayEntityData::new),
SLIME("slime", RainglowColour.LIME, DataTracker.registerData(SlimeEntity.class, TrackedDataHandlerRegistry.STRING), SlimeEntityData::new);

private static final HashMap<String, RainglowEntity> BY_ID = new HashMap<>();
static {
Expand All @@ -32,11 +35,13 @@ public enum RainglowEntity {
private final String id;
private final RainglowColour defaultColour;
private final TrackedData<String> trackedData;
private final Function<RainglowColour, EntityData> entityDataFactory;

RainglowEntity(String id, RainglowColour defaultColour, TrackedData<String> trackedData) {
RainglowEntity(String id, RainglowColour defaultColour, TrackedData<String> trackedData, Function<RainglowColour, EntityData> entityDataFactory) {
this.id = id;
this.defaultColour = defaultColour;
this.trackedData = trackedData;
this.entityDataFactory = entityDataFactory;
}

public String getId() {
Expand All @@ -55,6 +60,10 @@ public Identifier getDefaultTexture() {
return this.defaultColour.getTexture(this);
}

public EntityData createEntityData(RainglowColour colour) {
return this.entityDataFactory.apply(colour);
}

public Item getItem(int index) {
if (index == -1) {
return this.getDefaultColour().getItem();
Expand Down Expand Up @@ -86,6 +95,20 @@ public static RainglowEntity get(String id) {
return BY_ID.get(id);
}

@Unique
@SuppressWarnings("all")
public static RainglowEntity get(Entity entity) {
if (entity instanceof GlowSquidEntity) {
return GLOW_SQUID;
} else if (entity instanceof AllayEntity) {
return ALLAY;
} else if (entity instanceof SlimeEntity) {
return SLIME;
}

return null;
}

public void overrideTexture(Entity entity, CallbackInfoReturnable<Identifier> cir) {
RainglowColour colour = Rainglow.getColour(this, entity.getDataTracker(), entity.getWorld().getRandom());

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
@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(RainglowEntity.ALLAY.getTrackedData(), colour.getId());
entity.getDataTracker().set(THIS.getTrackedData(), colour.getId());
return this.getWorld().spawnEntity(entity);
}

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

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

@Unique
Expand Down
35 changes: 6 additions & 29 deletions src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import io.ix0rai.rainglow.data.RainglowEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.mob.SlimeEntity;
import net.minecraft.entity.passive.AllayEntity;
import net.minecraft.entity.passive.GlowSquidEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.DyeItem;
import net.minecraft.item.ItemStack;
Expand All @@ -27,40 +24,20 @@ public class DyeItemMixin {
@Inject(method = "useOnEntity", at = @At("TAIL"), cancellable = true)
private void useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
String colour = getDye(stack);
RainglowEntity entityType = RainglowEntity.get(entity);

if (entity instanceof GlowSquidEntity glowSquid && glowSquid.isAlive() && !Rainglow.getColour(RainglowEntity.GLOW_SQUID, glowSquid.getDataTracker(), glowSquid.getRandom()).equals(colour)) {
glowSquid.getWorld().playSoundFromEntity(user, glowSquid, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.PLAYERS, 1.0f, 1.0f);
if (entityType != null && !Rainglow.colourUnloaded(entityType, colour)
&& !Rainglow.getColour(entityType, entity.getDataTracker(), entity.getWorld().getRandom()).getId().equals(colour)) {
entity.getWorld().playSoundFromEntity(user, entity, SoundEvents.BLOCK_AMETHYST_CLUSTER_BREAK, SoundCategory.PLAYERS, 5.0f, 1.0f);
if (!user.getWorld().isClient()) {
stack.decrement(1);
}

DataTracker tracker = glowSquid.getDataTracker();
tracker.set(RainglowEntity.GLOW_SQUID.getTrackedData(), colour);

cir.setReturnValue(ActionResult.success(user.getWorld().isClient()));
} else if (entity instanceof AllayEntity allayEntity && allayEntity.isAlive() && !Rainglow.getColour(RainglowEntity.ALLAY, allayEntity.getDataTracker(), allayEntity.getRandom()).equals(colour)) {
allayEntity.getWorld().playSoundFromEntity(user, allayEntity, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.PLAYERS, 1.0f, 1.0f);
if (!user.getWorld().isClient()) {
stack.decrement(1);
}

DataTracker tracker = allayEntity.getDataTracker();
tracker.set(RainglowEntity.ALLAY.getTrackedData(), colour);

cir.setReturnValue(ActionResult.success(user.getWorld().isClient()));
} else if (entity instanceof SlimeEntity slimeEntity && slimeEntity.isAlive() && !Rainglow.getColour(RainglowEntity.SLIME, slimeEntity.getDataTracker(), slimeEntity.getRandom()).equals(colour)) {
slimeEntity.getWorld().playSoundFromEntity(user, slimeEntity, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.PLAYERS, 1.0f, 1.0f);
if (!user.getWorld().isClient()) {
stack.decrement(1);
}

DataTracker tracker = slimeEntity.getDataTracker();
tracker.set(RainglowEntity.SLIME.getTrackedData(), colour);
DataTracker tracker = entity.getDataTracker();
tracker.set(entityType.getTrackedData(), colour);

cir.setReturnValue(ActionResult.success(user.getWorld().isClient()));
}

cir.setReturnValue(ActionResult.PASS);
}

@Unique
Expand Down
44 changes: 7 additions & 37 deletions src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package io.ix0rai.rainglow.mixin;

import io.ix0rai.rainglow.Rainglow;
import io.ix0rai.rainglow.data.AllayEntityData;
import io.ix0rai.rainglow.data.AllayVariantProvider;
import io.ix0rai.rainglow.data.RainglowColour;
import io.ix0rai.rainglow.data.GlowSquidEntityData;
import io.ix0rai.rainglow.data.GlowSquidVariantProvider;
import io.ix0rai.rainglow.data.RainglowEntity;
import io.ix0rai.rainglow.data.SlimeEntityData;
import io.ix0rai.rainglow.data.SlimeVariantProvider;
import net.minecraft.entity.EntityData;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.VariantProvider;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.mob.SlimeEntity;
import net.minecraft.entity.passive.AllayEntity;
import net.minecraft.entity.passive.GlowSquidEntity;
import net.minecraft.world.LocalDifficulty;
import net.minecraft.world.ServerWorldAccess;
import net.minecraft.world.World;
Expand All @@ -36,41 +28,19 @@ protected MobEntityMixin(EntityType<? extends MobEntity> entityType, World world
@SuppressWarnings("all")
@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) {
RainglowColour colour = generateColour();
((GlowSquidVariantProvider) glowSquid).setVariant(colour);
cir.setReturnValue(new GlowSquidEntityData(colour));
} else if ((Object) this instanceof AllayEntity allay) {
RainglowColour colour = generateColour();
((AllayVariantProvider) allay).setVariant(colour);
cir.setReturnValue(new AllayEntityData(colour));
} else if ((Object) this instanceof SlimeEntity slime) {
RainglowColour colour = generateColour();
((SlimeVariantProvider) slime).setVariant(colour);
cir.setReturnValue(new SlimeEntityData(colour));
RainglowEntity entity = RainglowEntity.get(this);
if (entity != null) {
RainglowColour colour = generateColour(entity);
((VariantProvider<RainglowColour>) this).setVariant(colour);
cir.setReturnValue(entity.createEntityData(colour));
}
}

@Unique
private RainglowColour generateColour() {
RainglowEntity entity = getCurrentEntity();
private RainglowColour generateColour(RainglowEntity entity) {
int i = random.nextInt(100);
int rarity = Rainglow.CONFIG.getRarity(entity);

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

@Unique
@SuppressWarnings("all")
private RainglowEntity getCurrentEntity() {
if ((Object) this instanceof GlowSquidEntity) {
return RainglowEntity.GLOW_SQUID;
} else if ((Object) this instanceof AllayEntity) {
return RainglowEntity.ALLAY;
} else if ((Object) this instanceof SlimeEntity) {
return RainglowEntity.SLIME;
} else {
throw new RuntimeException("unsupported entity");
}
}
}

0 comments on commit 89c43b8

Please sign in to comment.