Skip to content

Commit

Permalink
Start working on overlay polymer interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Dec 12, 2024
1 parent af4e41c commit d4bfc1d
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* This class allows for creation of custom sound effects
* It can be used to play custom sounds for players with resourcepack while keeping fallback for vanilla clients
*/
/*public class PolymerSoundEvent extends SoundEvent implements PolymerSyncedObject<SoundEvent> {
public class PolymerSoundEvent implements PolymerSyncedObject<SoundEvent> {
@Nullable
protected final SoundEvent polymerSound;

Expand All @@ -39,7 +39,6 @@ static PolymerSoundEvent of(UUID uuid, Identifier identifier, float distanceToTr
}

public PolymerSoundEvent(@Nullable UUID uuid, Identifier id, float distanceToTravel, boolean useStaticDistance, @Nullable SoundEvent vanillaEvent) {
super(id, distanceToTravel, useStaticDistance);
this.source = uuid;
this.polymerSound = vanillaEvent;
}
Expand All @@ -48,4 +47,4 @@ public PolymerSoundEvent(@Nullable UUID uuid, Identifier id, float distanceToTra
public SoundEvent getPolymerReplacement(PacketContext context) {
return this.source == null || this.polymerSound == null || PolymerUtils.hasResourcePack(context.getPlayer(), this.source) ? this : (this.polymerSound instanceof PolymerSoundEvent pe ? pe.getPolymerReplacement(context) : this.polymerSound);
}
}*/
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.pb4.polymer.core.api.utils;

import eu.pb4.polymer.core.impl.interfaces.RegistryExtension;
import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;
Expand All @@ -10,7 +11,6 @@
*/

public interface PolymerSyncedObject<T> extends PolymerObject {

/**
* Generic method to get polymer replacement sent to player
*
Expand All @@ -35,6 +35,21 @@ default boolean canSyncRawToClient(PacketContext context) {
}

static <T> boolean canSyncRawToClient(Registry<T> registry, T obj, PacketContext context) {
return obj instanceof PolymerSyncedObject<?> pol ? pol.canSyncRawToClient(context) : !PolymerUtils.isServerOnly(registry, obj);
var pol = getSyncedObjectDefinition(registry, obj);
return pol != null ? pol.canSyncRawToClient(context) : !PolymerUtils.isServerOnly(registry, obj);
}

@Nullable
static <T> PolymerSyncedObject<T> getSyncedObjectDefinition(@Nullable Registry<T> registry, T obj) {
if (obj instanceof PolymerSyncedObject<?> instance) {
//noinspection unchecked
return (PolymerSyncedObject<T>) instance;
}
return registry instanceof RegistryExtension<?> extension ? ((RegistryExtension<T>) extension).polymer$getOverlay(obj) : null;
}

static <T> boolean canSynchronizeToPolymerClient(Registry<T> registry, T entry, PacketContext.NotNullWithPlayer ctx) {
var obj = getSyncedObjectDefinition(registry, entry);
return obj == null || obj.canSynchronizeToPolymerClient(ctx);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.pb4.polymer.core.impl.interfaces;

import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import net.minecraft.registry.Registry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.registry.tag.TagKey;
Expand All @@ -12,7 +13,9 @@ static <T> List<T> getPolymerEntries(Registry<T> registry) {
return ((RegistryExtension<T>) registry).polymer$getEntries();
}


Map<TagKey<T>, RegistryEntryList.Named<T>> polymer$getTagsInternal();
List<T> polymer$getEntries();

void polymer$setOverlay(T value, PolymerSyncedObject<T> syncedObject);
PolymerSyncedObject<T> polymer$getOverlay(T value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,18 @@ private static <T, A> void sendSync(ServerPlayNetworkHandler handler, CustomPayl
private static <T, A> void sendSync(ServerPlayNetworkHandler handler, CustomPayload.Id<PolymerGenericListPayload<A>> packetId, Iterable<T> iterable, boolean bypassPolymerCheck, BufferWritableCreator<T, A> writableFunction) {
var version = PolymerServerNetworking.getSupportedVersion(handler, packetId.id());

Registry<T> registry = null;

if (iterable instanceof RegistryExtension && !bypassPolymerCheck) {
iterable = ((RegistryExtension<T>) iterable).polymer$getEntries();
registry = (Registry<T>) iterable;
}

if (version != -1) {
var entries = new ArrayList<A>();
var ctx = PacketContext.create(handler);
for (var entry : iterable) {
if (!bypassPolymerCheck || (entry instanceof PolymerSyncedObject<?> obj && obj.canSynchronizeToPolymerClient(ctx))) {
if (!bypassPolymerCheck || PolymerSyncedObject.canSynchronizeToPolymerClient(registry, entry, ctx)) {
var val = writableFunction.serialize(entry, handler, version);
if (val != null) {
entries.add(val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public abstract class PacketCodecsEntriesMixin {
@ModifyVariable(method = "encode(Lio/netty/buffer/ByteBuf;Ljava/lang/Object;)V", at = @At("HEAD"), argsOnly = true)
private Object polymer$changeData(Object val, ByteBuf buf) {
var player = PacketContext.get();

if (val instanceof PolymerSyncedObject<?> polymerSyncedObject) {


var polymerSyncedObject = PolymerSyncedObject.getSyncedObjectDefinition(null, val);
if (polymerSyncedObject != null) {
var obj = polymerSyncedObject.getPolymerReplacement(player);

if (obj != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
package eu.pb4.polymer.core.mixin.other;


import eu.pb4.polymer.core.api.other.PolymerSoundEvent;
import eu.pb4.polymer.core.api.utils.PolymerObject;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import eu.pb4.polymer.core.api.utils.PolymerUtils;
import eu.pb4.polymer.rsm.api.RegistrySyncUtils;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.sound.SoundEvent;
import org.spongepowered.asm.mixin.Final;
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.ModifyVariable;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({"rawtypes", "unchecked"})
@Mixin(targets = "net/minecraft/network/codec/PacketCodecs$20", priority = 500)
public abstract class PacketCodecsRegistryEntryListMixin {
@Shadow @Final private RegistryKey field_54511;

@ModifyVariable(method = "encode(Lnet/minecraft/network/RegistryByteBuf;Lnet/minecraft/registry/entry/RegistryEntryList;)V", at = @At("HEAD"), argsOnly = true)
private RegistryEntryList polymer$changeData(RegistryEntryList registryEntryList, RegistryByteBuf registryByteBuf) {
if (registryEntryList.getTagKey().isEmpty()) {
var player = PacketContext.get();

var arr = new ArrayList<RegistryEntry>();
var reg = registryByteBuf.getRegistryManager().getOrThrow(this.field_54511);
for (var i = 0; i < registryEntryList.size(); i++) {
var val = registryEntryList.get(i);
/*if (val.value() instanceof PolymerSoundEvent syncedObject) {
var replacement = syncedObject.getPolymerReplacement(player);

if (replacement instanceof PolymerSoundEvent) {
arr.add(RegistryEntry.of(replacement));
var obj = PolymerSyncedObject.getSyncedObjectDefinition(reg, val);
if (obj instanceof PolymerSoundEvent syncedObject) {
var replacement = syncedObject.getPolymerReplacement(player);
if (replacement != null) {
arr.add(reg.getEntry(replacement));
}
arr.add(Registries.SOUND_EVENT.getEntry(replacement));
} */

if (val.value() instanceof SoundEvent soundEvent && RegistrySyncUtils.isServerEntry(Registries.SOUND_EVENT, soundEvent)) {
} else if (val.value() instanceof SoundEvent soundEvent && RegistrySyncUtils.isServerEntry(Registries.SOUND_EVENT, soundEvent)) {
arr.add(RegistryEntry.of(val.value()));
} else if ((val.value() instanceof PolymerSyncedObject<?> s && s.canSyncRawToClient(player)) || !(val.value() instanceof PolymerObject)) {
} else if ((obj != null && obj.canSyncRawToClient(player)) || !(val.value() instanceof PolymerObject)) {
arr.add(val);
}
}

return RegistryEntryList.of((List) arr);
}

return registryEntryList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,32 @@ public abstract class PacketCodecsRegistryMixin {
@ModifyVariable(method = "encode(Lnet/minecraft/network/RegistryByteBuf;Ljava/lang/Object;)V", at = @At("HEAD"), argsOnly = true)
private Object polymer$changeData(Object val, RegistryByteBuf buf) {
var player = PacketContext.get();
//noinspection unchecked
var reg = buf.getRegistryManager().getOrThrow(this.field_53746);

if (val instanceof PolymerSyncedObject<?> polymerSyncedObject) {
var obj = polymerSyncedObject.getPolymerReplacement(player);

if (obj != null) {
return obj;
}
} else if (val instanceof RegistryEntry<?> registryEntry) {
if (val instanceof RegistryEntry<?> registryEntry) {
var value = registryEntry.value();
if (value instanceof PolymerSyncedObject<?> polymerSyncedObject) {
var obj = polymerSyncedObject.getPolymerReplacement(player);
var obj = PolymerSyncedObject.getSyncedObjectDefinition(reg, value);

if (obj != null) {
if (obj != null) {
var replacement = obj.getPolymerReplacement(player);

if (replacement != null) {
//noinspection unchecked
return buf.getRegistryManager().getOrThrow(this.field_53746).getEntry(obj);
return reg.getEntry(replacement);
}
}
}
} else {
var obj = PolymerSyncedObject.getSyncedObjectDefinition(reg, val);
if (obj != null) {
var replacement = obj.getPolymerReplacement(player);

if (replacement != null) {
return replacement;
}
}
}

return val;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.pb4.polymer.core.mixin.other;

import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.serialization.DynamicOps;
import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
Expand All @@ -10,6 +12,7 @@
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryFixedCodec;
import org.spongepowered.asm.mixin.Final;
Expand All @@ -29,8 +32,9 @@ public class RegistryFixedCodecMixin {
method = "encode(Lnet/minecraft/registry/entry/RegistryEntry;Lcom/mojang/serialization/DynamicOps;Ljava/lang/Object;)Lcom/mojang/serialization/DataResult;",
at = @At("HEAD")
)
private RegistryEntry<?> polymerCore$swapEntry(RegistryEntry<?> entry) {
private RegistryEntry<?> polymerCore$swapEntry(RegistryEntry<?> entry, @Local(argsOnly = true) DynamicOps<?> ops) {
if (PolymerCommonUtils.isServerNetworkingThread()) {
// Todo
var player = PacketContext.get();
try {
if (entry.value() instanceof PolymerSyncedObject<?> polymerSyncedObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.mojang.serialization.Lifecycle;
import eu.pb4.polymer.core.api.utils.PolymerObject;
import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import eu.pb4.polymer.core.impl.ImplPolymerRegistryEvent;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import eu.pb4.polymer.core.impl.interfaces.RegistryExtension;
import eu.pb4.polymer.rsm.api.RegistrySyncUtils;
import joptsimple.internal.AbbreviationMap;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
Expand All @@ -23,8 +25,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -34,6 +36,8 @@ public abstract class SimpleRegistryMixin<T> implements RegistryExtension<T>, Re
@Nullable
@Unique
private List<T> polymer$objects = null;
@Unique
private final IdentityHashMap<T, PolymerSyncedObject<T>> overlays = new IdentityHashMap<>();

@Inject(method = "add", at = @At("TAIL"))
private <V extends T> void polymer$storeStatus(RegistryKey<T> key, T value, RegistryEntryInfo info, CallbackInfoReturnable<RegistryEntry.Reference<T>> cir) {
Expand Down Expand Up @@ -63,4 +67,14 @@ public abstract class SimpleRegistryMixin<T> implements RegistryExtension<T>, Re
public Map<TagKey<T>, RegistryEntryList.Named<T>> polymer$getTagsInternal() {
return this.tags;
}

@Override
public void polymer$setOverlay(T value, PolymerSyncedObject<T> syncedObject) {
this.overlays.put(value, syncedObject);
}

@Override
public PolymerSyncedObject<T> polymer$getOverlay(T value) {
return this.overlays.get(value);
}
}

0 comments on commit d4bfc1d

Please sign in to comment.