Skip to content

Commit

Permalink
start working on fixing vanilla compat
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Aug 6, 2024
1 parent 1f125f4 commit af8b57d
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 73 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ org.gradle.jvmargs=-Xmx1G

# minecraft, mappings and loader dependencies
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.21-rc1
quilt_mappings=2
minecraft_version=1.21
quilt_mappings=17
loader_version=0.15.11

# mod properties
Expand Down
50 changes: 39 additions & 11 deletions src/main/java/io/ix0rai/rainglow/Rainglow.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.Entity;
import net.minecraft.resource.ResourceType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand All @@ -22,7 +22,10 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class Rainglow implements ModInitializer {
public static final String MOD_ID = "rainglow";
Expand All @@ -37,34 +40,41 @@ public class Rainglow implements ModInitializer {
public static final Identifier SERVER_MODE_DATA_ID = id("server_mode_data");
public static final List<String> RAINGLOW_DATAPACKS = new ArrayList<>();

private static final Map<UUID, RainglowColour> colours = new HashMap<>();

@Override
public void onInitialize() {
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener((RainglowResourceReloader) () -> SERVER_MODE_DATA_ID);

PayloadTypeRegistry.playS2C().register(RainglowNetworking.ConfigSyncPayload.PACKET_ID, RainglowNetworking.ConfigSyncPayload.PACKET_CODEC);
PayloadTypeRegistry.playS2C().register(RainglowNetworking.ModeSyncPayload.PACKET_ID, RainglowNetworking.ModeSyncPayload.PACKET_CODEC);
PayloadTypeRegistry.playS2C().register(RainglowNetworking.ColourPayload.PACKET_ID, RainglowNetworking.ColourPayload.PACKET_CODEC);
PayloadTypeRegistry.playC2S().register(RainglowNetworking.ColourPayload.PACKET_ID, RainglowNetworking.ColourPayload.PACKET_CODEC);

ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
// send modes to client
RainglowNetworking.syncModes(handler.player);

// send config to client
RainglowNetworking.syncConfig(handler.player);

// send all colours to client
RainglowNetworking.sendColoursTo(handler.player);
});
}

public static Identifier id(String id) {
return Identifier.of(MOD_ID, id);
}

public static String generateRandomColourId(World world, RandomGenerator random) {
public static RainglowColour generateRandomColour(World world, RandomGenerator random) {
var colours = MODE_CONFIG.getMode(world).getColours();
return colours.get(random.nextInt(colours.size())).getId();
return colours.get(random.nextInt(colours.size()));
}

public static boolean colourUnloaded(World world, RainglowEntity entityType, String colour) {
public static boolean colourUnloaded(World world, RainglowEntity entityType, RainglowColour colour) {
var colours = MODE_CONFIG.getMode(world).getColours();
return !colours.contains(RainglowColour.get(colour)) && !colour.equals(entityType.getDefaultColour().getId());
return !colours.contains(colour) && !colour.equals(entityType.getDefaultColour());
}

public static String translatableTextKey(String key) {
Expand All @@ -80,15 +90,33 @@ public static Text translatableText(String key) {
return Text.translatable(translatableTextKey(key));
}

public static RainglowColour getColour(World world, RainglowEntity entityType, DataTracker tracker, RandomGenerator random) {
public static RainglowColour getColour(Entity entity) {
RainglowColour colour = colours.get(entity.getUuid());
RainglowEntity entityType = RainglowEntity.get(entity);

// generate random colour if the squid's colour isn't currently loaded
String colour = tracker.get(entityType.getTrackedData());
if (colourUnloaded(world, entityType, colour)) {
if (colourUnloaded(entity.getWorld(), entityType, colour)) {
// Use last generated colour if not null else generate a new colour
tracker.set(entityType.getTrackedData(), generateRandomColourId(world, random));
colour = tracker.get(entityType.getTrackedData());
colour = generateRandomColour(entity.getWorld(), entity.getRandom());
colours.put(entity.getUuid(), colour);
}

return RainglowColour.get(colour);
return colour;
}

public static void setColour(Entity entity, RainglowColour colour) {
colours.put(entity.getUuid(), colour);

if (entity.getWorld().isClient()) {
// sync to server; will then be synced to all clients
RainglowNetworking.sendColourChangeToServer(entity, colour);
} else {
// sync to all clients
RainglowNetworking.sendColourChangeToClients(entity, colour);
}
}

public static Map<UUID, RainglowColour> getColours() {
return colours;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ public void init() {
}

@Override
protected void method_60325() {}
protected void initOptionButtons() {
// no-op
}

@Override
protected void repositionElements() {
Expand Down
27 changes: 8 additions & 19 deletions src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
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;
import net.minecraft.entity.mob.SlimeEntity;
import net.minecraft.entity.passive.AllayEntity;
import net.minecraft.entity.passive.GlowSquidEntity;
Expand All @@ -16,17 +13,16 @@
import net.minecraft.util.random.RandomGenerator;
import net.minecraft.world.World;
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), 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);
GLOW_SQUID("glow_squid", RainglowColour.BLUE, GlowSquidEntityData::new),
ALLAY("allay", RainglowColour.BLUE, AllayEntityData::new),
SLIME("slime", RainglowColour.LIME, SlimeEntityData::new);

private static final HashMap<String, RainglowEntity> BY_ID = new HashMap<>();
static {
Expand All @@ -35,13 +31,11 @@ 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, Function<RainglowColour, EntityData> entityDataFactory) {
RainglowEntity(String id, RainglowColour defaultColour, Function<RainglowColour, EntityData> entityDataFactory) {
this.id = id;
this.defaultColour = defaultColour;
this.trackedData = trackedData;
this.entityDataFactory = entityDataFactory;
}

Expand All @@ -53,10 +47,6 @@ public RainglowColour getDefaultColour() {
return this.defaultColour;
}

public TrackedData<String> getTrackedData() {
return this.trackedData;
}

public Identifier getDefaultTexture() {
return this.defaultColour.getTexture(this);
}
Expand All @@ -74,13 +64,13 @@ public Item getItem(int index) {
}

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

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

return RainglowColour.get(colour);
return colour;
}

public static RainglowEntity read(PacketByteBuf buf) {
Expand All @@ -96,7 +86,6 @@ public static RainglowEntity get(String id) {
return BY_ID.get(id);
}

@Unique
@SuppressWarnings("all")
public static RainglowEntity get(Entity entity) {
if (entity instanceof GlowSquidEntity) {
Expand All @@ -111,7 +100,7 @@ public static RainglowEntity get(Entity entity) {
}

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

// if the colour is default we don't need to override the method
// this optimises a tiny bit
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package io.ix0rai.rainglow.data;

import io.ix0rai.rainglow.Rainglow;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.Entity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.payload.CustomPayload;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class RainglowNetworking {
public static void syncConfig(ServerPlayerEntity player) {
Expand Down Expand Up @@ -68,4 +72,40 @@ public Id<? extends CustomPayload> getId() {
return PACKET_ID;
}
}

public static void sendColoursTo(ServerPlayerEntity player) {
ServerPlayNetworking.send(player, new ColourPayload(Rainglow.getColours()));
}

public static void sendColourChangeToServer(Entity entity, RainglowColour colour) {
ClientPlayNetworking.send(new ColourPayload(Map.of(entity.getUuid(), colour)));
}

public static void sendColourChangeToClients(Entity entity, RainglowColour colour) {
if (entity.getWorld() instanceof ServerWorld serverWorld) {
serverWorld.getPlayers().forEach(player -> ServerPlayNetworking.send(player, new ColourPayload(Map.of(entity.getUuid(), colour))));
}

throw new RuntimeException("Cannot send colour change to clients from client");
}

// todo: receivers

public record ColourPayload(Map<UUID, RainglowColour> colours) implements CustomPayload {
public static final CustomPayload.Id<ColourPayload> PACKET_ID = new CustomPayload.Id<>(Rainglow.id("colour_change"));
public static final PacketCodec<RegistryByteBuf, ColourPayload> PACKET_CODEC = PacketCodec.create(ColourPayload::write, ColourPayload::read);

public void write(RegistryByteBuf buf) {
buf.writeMap(this.colours, (b, uuid) -> b.writeUuid(uuid), RainglowColour::write);
}

public static ColourPayload read(RegistryByteBuf buf) {
return new ColourPayload(buf.readMap(b -> b.readUuid(), RainglowColour::read));
}

@Override
public Id<? extends CustomPayload> getId() {
return PACKET_ID;
}
}
}
18 changes: 8 additions & 10 deletions src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
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 {
@Shadow public abstract void writeCustomDataToNbt(NbtCompound nbt);

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());
}

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

Expand All @@ -42,18 +40,18 @@ public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
// 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.getWorld(), RainglowEntity.ALLAY, this.getDataTracker(), this.random);
entity.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId());
RainglowColour colour = Rainglow.getColour(this);
((AllayVariantProvider) entity).setVariant(colour);
return this.getWorld().spawnEntity(entity);
}

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

@Override
public void setVariant(RainglowColour colour) {
this.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId());
Rainglow.setColour(this, colour);
}
}
12 changes: 6 additions & 6 deletions src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.ix0rai.rainglow.mixin;

import io.ix0rai.rainglow.Rainglow;
import io.ix0rai.rainglow.data.RainglowColour;
import io.ix0rai.rainglow.data.RainglowEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.data.DataTracker;
Expand All @@ -24,20 +25,19 @@ 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) {
if (Rainglow.CONFIG.allowDyeing.value()) {
String colour = getDye(stack);
RainglowColour colour = RainglowColour.get(getDye(stack));
RainglowEntity entityType = RainglowEntity.get(entity);

if (entityType != null && !Rainglow.colourUnloaded(user.getWorld(), entityType, colour)
if (entityType != null
&& !Rainglow.colourUnloaded(user.getWorld(), entityType, colour)
&& Rainglow.CONFIG.isEntityEnabled(entityType)
&& !Rainglow.getColour(user.getWorld(), entityType, entity.getDataTracker(), entity.getWorld().getRandom()).getId().equals(colour)) {
&& Rainglow.getColour(entity) != 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 = entity.getDataTracker();
tracker.set(entityType.getTrackedData(), colour);

Rainglow.setColour(entity, colour);
cir.setReturnValue(ActionResult.success(user.getWorld().isClient()));
}
}
Expand Down
Loading

0 comments on commit af8b57d

Please sign in to comment.