Skip to content

Commit

Permalink
Merge pull request #292 from BasiqueEvangelist/fix-registry-attribute
Browse files Browse the repository at this point in the history
make SyncedProperty and ScreenHandlerMixin pass in registry context
  • Loading branch information
gliscowo authored Aug 27, 2024
2 parents 68b0aa6 + 06b2299 commit 345c9ad
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
18 changes: 15 additions & 3 deletions src/main/java/io/wispforest/owo/client/screens/SyncedProperty.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package io.wispforest.owo.client.screens;

import io.wispforest.endec.Endec;
import io.wispforest.endec.SerializationContext;
import io.wispforest.owo.serialization.RegistriesAttribute;
import io.wispforest.owo.util.Observable;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.ScreenHandler;
import org.jetbrains.annotations.ApiStatus;

public class SyncedProperty<T> extends Observable<T> {
private final int index;
private final Endec<T> endec;
private final ScreenHandler owner;
private boolean needsSync;

@ApiStatus.Internal
public SyncedProperty(int index, Endec<T> endec, T initial) {
public SyncedProperty(int index, Endec<T> endec, T initial, ScreenHandler owner) {
super(initial);

this.index = index;
this.endec = endec;
this.owner = owner;
}

public int index() {
Expand All @@ -30,12 +35,12 @@ public boolean needsSync() {
@ApiStatus.Internal
public void write(PacketByteBuf buf) {
needsSync = false;
buf.write(this.endec, value);
buf.write(serializationContext(), this.endec, value);
}

@ApiStatus.Internal
public void read(PacketByteBuf buf) {
this.set(buf.read(this.endec));
this.set(buf.read(serializationContext(), this.endec));
}

@Override
Expand All @@ -48,4 +53,11 @@ protected void notifyObservers(T value) {
public void markDirty() {
notifyObservers(value);
}

private SerializationContext serializationContext() {
var player = this.owner.player();
if (player == null) return SerializationContext.empty();

return SerializationContext.attributes(RegistriesAttribute.of(player.getRegistryManager()));
}
}
10 changes: 7 additions & 3 deletions src/main/java/io/wispforest/owo/mixin/ScreenHandlerMixin.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.wispforest.owo.mixin;

import io.wispforest.endec.SerializationContext;
import io.wispforest.endec.impl.ReflectiveEndecBuilder;
import io.wispforest.owo.client.screens.OwoScreenHandler;
import io.wispforest.owo.client.screens.ScreenInternals;
import io.wispforest.owo.client.screens.ScreenhandlerMessageData;
import io.wispforest.owo.client.screens.SyncedProperty;
import io.wispforest.owo.network.NetworkException;
import io.wispforest.endec.Endec;
import io.wispforest.owo.serialization.RegistriesAttribute;
import io.wispforest.owo.serialization.endec.MinecraftEndecs;
import io.wispforest.owo.util.pond.OwoScreenHandlerExtension;
import net.fabricmc.api.EnvType;
Expand Down Expand Up @@ -106,8 +108,9 @@ public <R extends Record> void sendMessage(@NotNull R message) {
throw new NetworkException("Tried to send message of unknown type " + message.getClass());
}

var ctx = SerializationContext.attributes(RegistriesAttribute.of(this.owo$player.getRegistryManager()));
var buf = PacketByteBufs.create();
buf.write(messageData.endec(), message);
buf.write(ctx, messageData.endec(), message);

var packet = new ScreenInternals.LocalPacket(messageData.id(), buf);

Expand Down Expand Up @@ -136,13 +139,14 @@ public <R extends Record> void sendMessage(@NotNull R message) {
@SuppressWarnings({"rawtypes", "unchecked"})
public void owo$handlePacket(ScreenInternals.LocalPacket packet, boolean clientbound) {
ScreenhandlerMessageData messageData = (clientbound ? this.owo$clientboundMessages : this.owo$serverboundMessages).get(packet.packetId());
var ctx = SerializationContext.attributes(RegistriesAttribute.of(this.owo$player.getRegistryManager()));

messageData.handler().accept(packet.payload().read(messageData.endec()));
messageData.handler().accept(packet.payload().read(ctx, messageData.endec()));
}

@Override
public <T> SyncedProperty<T> createProperty(Class<T> clazz, Endec<T> endec, T initial) {
var prop = new SyncedProperty<>(this.owo$properties.size(), endec, initial);
var prop = new SyncedProperty<>(this.owo$properties.size(), endec, initial, (ScreenHandler)(Object) this);
this.owo$properties.add(prop);
return prop;
}
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/io/wispforest/owo/network/OwoNetChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,27 +445,6 @@ private <R extends Record> void createEndec(Class<R> messageClass, int handlerIn
}
}

@SuppressWarnings("unchecked")
private <R extends Record> PacketByteBuf encode(R message, EnvType target) {
var buffer = PacketByteBufs.create();

final var messageClass = message.getClass();

if (!this.endecsByClass.containsKey(messageClass)) {
throw new NetworkException("Message class '" + messageClass + "' is not registered");
}

final IndexedEndec<R> endec = (IndexedEndec<R>) this.endecsByClass.get(messageClass);
if (endec.handlerIndex(target) == -1) {
throw new NetworkException("Message class '" + messageClass + "' has no handler registered for target environment " + target);
}

buffer.writeVarInt(endec.handlerIndex(target));
buffer.write(endec.endec, message);

return buffer;
}

public class ClientHandle {

/**
Expand Down
10 changes: 10 additions & 0 deletions src/testmod/java/io/wispforest/uwu/EpicScreenHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import io.wispforest.owo.client.screens.ScreenUtils;
import io.wispforest.owo.client.screens.SlotGenerator;
import io.wispforest.owo.client.screens.SyncedProperty;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Text;

import java.util.concurrent.ThreadLocalRandom;

Expand All @@ -20,6 +23,7 @@ public class EpicScreenHandler extends ScreenHandler {
private final ScreenHandlerContext context;

public final SyncedProperty<String> epicNumber;
public final SyncedProperty<ItemStack> cringeStack;

public EpicScreenHandler(int syncId, PlayerInventory inventory) {
this(syncId, inventory, ScreenHandlerContext.EMPTY);
Expand All @@ -35,6 +39,8 @@ public EpicScreenHandler(int syncId, PlayerInventory inventory, ScreenHandlerCon
this.epicNumber = this.createProperty(String.class, "");
this.epicNumber.set(generateEpicName());

this.cringeStack = this.createProperty(ItemStack.class, ItemStack.EMPTY);

this.addClientboundMessage(MaldMessage.class, this::handleMald);
this.addServerboundMessage(EpicMessage.class, this::handleEpic);
}
Expand All @@ -45,6 +51,10 @@ private void handleMald(MaldMessage r) {

private void handleEpic(EpicMessage r) {
this.epicNumber.set(generateEpicName() + " " + r.number);

var stacc = Items.FEATHER.getDefaultStack();
stacc.set(DataComponentTypes.CUSTOM_NAME, Text.literal(this.epicNumber.get()));
this.cringeStack.set(stacc);
}

@Override
Expand Down

0 comments on commit 345c9ad

Please sign in to comment.