Skip to content

Commit

Permalink
further fix
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Sep 19, 2024
1 parent e449a9c commit 4bb0f7d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 43 deletions.
59 changes: 25 additions & 34 deletions src/main/java/com/teammoeg/caupona/util/RegistryAccessor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.teammoeg.caupona.util;

import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.function.Function;

Expand All @@ -9,66 +10,56 @@
import net.neoforged.neoforge.network.connection.ConnectionType;

public class RegistryAccessor {
public static class CloseableRegistryAccessor implements AutoCloseable{
ThreadRegistryAccess access;
public CloseableRegistryAccessor(ThreadRegistryAccess access) {
public static class RegistryAccessorStack implements AutoCloseable{
private ThreadRegistryAccess parent;
private RegistryAccess accessor;
private ConnectionType connectionType;
public RegistryAccessorStack(ThreadRegistryAccess parent, RegistryAccess accessor, ConnectionType connectionType) {
super();
this.access = access;
this.parent = parent;
this.accessor = accessor;
this.connectionType = connectionType;
}
public Function<ByteBuf, RegistryFriendlyByteBuf> getDecorator() {
return RegistryFriendlyByteBuf.decorator(accessor, connectionType);
}
@Override
public void close() {
access.close();
parent.pop();
}
}
public static class ThreadRegistryAccess {
private transient RegistryAccess accessor;
private transient ConnectionType connectionType;
private transient CloseableRegistryAccessor closer=new CloseableRegistryAccessor(this);
LinkedList<RegistryAccessorStack> stack=new LinkedList<>();
public ThreadRegistryAccess() {
super();
}
public void provideRegistryAccess(RegistryFriendlyByteBuf pb) {
accessor=pb.registryAccess();
connectionType=pb.getConnectionType();
}
public void close() {
accessor=null;
connectionType=null;
public RegistryAccessorStack provideRegistryAccess(RegistryFriendlyByteBuf pb) {
RegistryAccessorStack ras=new RegistryAccessorStack(this,pb.registryAccess(),pb.getConnectionType());
stack.add(ras);
return ras;
}
public Function<ByteBuf, RegistryFriendlyByteBuf> getDecorator() {
if(!haveAccess())
throw new NoSuchElementException("no registry access found");
return RegistryFriendlyByteBuf.decorator(accessor, connectionType);
return stack.getLast().getDecorator();
}
public boolean haveAccess() {
return accessor!=null;
return !stack.isEmpty();
}
public RegistryAccess getRegistryAccess() {
return accessor;
}
public CloseableRegistryAccessor automated(RegistryFriendlyByteBuf pb) {
provideRegistryAccess(pb);
return closer;
public void pop() {
stack.pollLast();
}

}
public static ThreadLocal<ThreadRegistryAccess> access=ThreadLocal.withInitial(ThreadRegistryAccess::new);
public static boolean haveAccess() {
return access.get().haveAccess();
}
public static RegistryAccess getRegistryAccess() {
return access.get().getRegistryAccess();
}
public static Function<ByteBuf, RegistryFriendlyByteBuf> getDecorator() {
return access.get().getDecorator();
}

public static void provideRegistryAccess(RegistryFriendlyByteBuf pb) {
access.get().provideRegistryAccess(pb);
}
public static void close() {
access.get().close();
}
public static CloseableRegistryAccessor automated(RegistryFriendlyByteBuf pb) {
return access.get().automated(pb);
public static RegistryAccessorStack provideRegistryAccess(RegistryFriendlyByteBuf pb) {
return access.get().provideRegistryAccess(pb);
}
}
13 changes: 4 additions & 9 deletions src/main/java/com/teammoeg/caupona/util/SerializeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import com.mojang.serialization.MapCodec;
import com.teammoeg.caupona.CPConfig;
import com.teammoeg.caupona.CPMain;
import com.teammoeg.caupona.util.RegistryAccessor.CloseableRegistryAccessor;
import com.teammoeg.caupona.util.RegistryAccessor.RegistryAccessorStack;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
Expand Down Expand Up @@ -178,8 +178,7 @@ public static <K, V> Map<K, V> readMap(FriendlyByteBuf buffer, Map<K, V> map, Fu
return map;
}
public static <T> void writeCodec(RegistryFriendlyByteBuf pb, Codec<T> codec, T obj) {
try {
RegistryAccessor.provideRegistryAccess(pb);
try (RegistryAccessorStack stack=RegistryAccessor.provideRegistryAccess(pb)){
if(!CPConfig.COMMON.compressCodecs.get()) {
DataResult<Tag> out=codec.encodeStart(NbtOps.INSTANCE, obj);
Optional<Tag> ret=out.resultOrPartial(CPMain.logger::error);
Expand All @@ -192,13 +191,11 @@ public static <T> void writeCodec(RegistryFriendlyByteBuf pb, Codec<T> codec, T
throw new DecoderException("Can not write Object "+obj+" with Codec "+codec);
}
ObjectWriter.writeObject(pb,ret.get());
}finally {
RegistryAccessor.close();
}
}
public static <T> T readCodec(RegistryFriendlyByteBuf pb, Codec<T> codec) {
try {
RegistryAccessor.provideRegistryAccess(pb);
try(RegistryAccessorStack stack=RegistryAccessor.provideRegistryAccess(pb)){
;
if(!CPConfig.COMMON.compressCodecs.get()) {
DataResult<Pair<T, Tag>> ob=codec.decode(NbtOps.INSTANCE,pb.readNbt());
Optional<Pair<T, Tag>> ret=ob.resultOrPartial(CPMain.logger::error);
Expand All @@ -211,8 +208,6 @@ public static <T> T readCodec(RegistryFriendlyByteBuf pb, Codec<T> codec) {
throw new DecoderException("Can not read Object "+res+" with Codec "+codec);
}
return ret.get().getFirst();
}finally {
RegistryAccessor.close();
}
}

Expand Down

0 comments on commit 4bb0f7d

Please sign in to comment.