Fix REGISTER packet encode/decode issues.

Changed ForgeConfig values to return the default value if the config is not loaded, and exposed the value objects as a internal config.
This commit is contained in:
LexManos 2019-09-05 15:38:14 -07:00
parent e1863383ff
commit 3ef1d6919c
2 changed files with 29 additions and 8 deletions

View file

@ -51,6 +51,7 @@ import com.electronwill.nightconfig.core.EnumGetMethod;
import com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction; import com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction;
import com.electronwill.nightconfig.core.ConfigSpec.CorrectionListener; import com.electronwill.nightconfig.core.ConfigSpec.CorrectionListener;
import com.electronwill.nightconfig.core.InMemoryFormat; import com.electronwill.nightconfig.core.InMemoryFormat;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import com.electronwill.nightconfig.core.file.FileConfig; import com.electronwill.nightconfig.core.file.FileConfig;
import com.electronwill.nightconfig.core.utils.UnmodifiableConfigWrapper; import com.electronwill.nightconfig.core.utils.UnmodifiableConfigWrapper;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
@ -63,17 +64,18 @@ import com.google.common.collect.ObjectArrays;
* Like {@link com.electronwill.nightconfig.core.ConfigSpec} except in builder format, and extended to accept comments, language keys, * Like {@link com.electronwill.nightconfig.core.ConfigSpec} except in builder format, and extended to accept comments, language keys,
* and other things Forge configs would find useful. * and other things Forge configs would find useful.
*/ */
public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfig> //TODO: Remove extends and pipe everything through getSpec/getValues?
public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
{ {
private Map<List<String>, String> levelComments = new HashMap<>(); private Map<List<String>, String> levelComments = new HashMap<>();
private UnmodifiableConfig values;
private Config childConfig; private Config childConfig;
private boolean isCorrecting = false; private boolean isCorrecting = false;
private ForgeConfigSpec(Config storage, Map<List<String>, String> levelComments) { private ForgeConfigSpec(UnmodifiableConfig storage, UnmodifiableConfig values, Map<List<String>, String> levelComments) {
super(storage); super(storage);
this.values = values;
this.levelComments = levelComments; this.levelComments = levelComments;
} }
@ -94,6 +96,18 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
return isCorrecting; return isCorrecting;
} }
public boolean isLoaded() {
return childConfig != null;
}
public UnmodifiableConfig getSpec() {
return this.config;
}
public UnmodifiableConfig getValues() {
return this.values;
}
public void save() public void save()
{ {
Preconditions.checkNotNull(childConfig, "Cannot save config value without assigned Config object present"); Preconditions.checkNotNull(childConfig, "Cannot save config value without assigned Config object present");
@ -123,7 +137,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
return ret; return ret;
} }
private int correct(Config spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, boolean dryRun) private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, boolean dryRun)
{ {
int count = 0; int count = 0;
@ -511,7 +525,10 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
public ForgeConfigSpec build() public ForgeConfigSpec build()
{ {
context.ensureEmpty(); context.ensureEmpty();
ForgeConfigSpec ret = new ForgeConfigSpec(storage, levelComments); Config valueCfg = Config.of(InMemoryFormat.withSupport(ConfigValue.class::isAssignableFrom));
values.forEach(v -> valueCfg.set(v.getPath(), v));
ForgeConfigSpec ret = new ForgeConfigSpec(storage, valueCfg, levelComments);
values.forEach(v -> v.spec = ret); values.forEach(v -> v.spec = ret);
return ret; return ret;
} }
@ -695,7 +712,8 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
public T get() public T get()
{ {
Preconditions.checkNotNull(spec, "Cannot get config value before spec is built"); Preconditions.checkNotNull(spec, "Cannot get config value before spec is built");
Preconditions.checkNotNull(spec.childConfig, "Cannot get config value without assigned Config object present"); if (spec.childConfig == null)
return defaultSupplier.get();
return getRaw(spec.childConfig, path, defaultSupplier); return getRaw(spec.childConfig, path, defaultSupplier);
} }

View file

@ -6,6 +6,7 @@ import net.minecraft.network.IPacket;
import net.minecraft.network.NetworkManager; import net.minecraft.network.NetworkManager;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -21,7 +22,9 @@ public class FMLMCRegisterPacketHandler {
private Set<ResourceLocation> locations = new HashSet<>(); private Set<ResourceLocation> locations = new HashSet<>();
public void updateFrom(PacketBuffer buffer) { public void updateFrom(PacketBuffer buffer) {
locations = bytesToResLocation(buffer.readByteArray()); byte[] data = new byte[buffer.readableBytes() < 0 ? 0 : buffer.readableBytes()];
buffer.readBytes(data);
locations = bytesToResLocation(data);
} }
byte[] toByteArray() { byte[] toByteArray() {
@ -82,7 +85,7 @@ public class FMLMCRegisterPacketHandler {
public void sendRegistry(NetworkManager manager, final NetworkDirection dir) { public void sendRegistry(NetworkManager manager, final NetworkDirection dir) {
PacketBuffer pb = new PacketBuffer(Unpooled.buffer()); PacketBuffer pb = new PacketBuffer(Unpooled.buffer());
pb.writeByteArray(getFrom(manager).toByteArray()); pb.writeBytes(getFrom(manager).toByteArray());
final ICustomPacket<IPacket<?>> iPacketICustomPacket = dir.buildPacket(Pair.of(pb, 0), FMLNetworkConstants.MC_REGISTER_RESOURCE); final ICustomPacket<IPacket<?>> iPacketICustomPacket = dir.buildPacket(Pair.of(pb, 0), FMLNetworkConstants.MC_REGISTER_RESOURCE);
manager.sendPacket(iPacketICustomPacket.getThis()); manager.sendPacket(iPacketICustomPacket.getThis());
} }