mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-23 21:49:46 +00:00
Minecraft 1.21.5
Co-authored-by: Ven <[email protected]> Co-authored-by: Jonathing <[email protected]>
This commit is contained in:
548 files changed
+5432
-7867
No files matched your search
@@ -8,7 +8,9 @@ package net.minecraftforge.common.util;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.minecraft.core.UUIDUtil;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtUtils;
|
||||
import net.minecraft.nbt.Tag;
|
||||
|
||||
public interface INBTBuilder {
|
||||
@@ -63,9 +65,14 @@ public interface INBTBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @deprecated Use {@link #putByteArray(String, byte...)} */
|
||||
@Deprecated(forRemoval = true, since = "1.21.5")
|
||||
public Builder putByteArray(String key, List<Byte> value) {
|
||||
tag.putByteArray(key, value);
|
||||
return this;
|
||||
var array = new byte[value.size()];
|
||||
for (var i = 0; i < value.size(); i++)
|
||||
array[i] = value.get(i);
|
||||
|
||||
return this.putByteArray(key, array);
|
||||
}
|
||||
|
||||
public Builder putIntArray(String key, int... value) {
|
||||
@@ -73,9 +80,14 @@ public interface INBTBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @deprecated Use {@link #putIntArray(String, int...)} */
|
||||
@Deprecated(forRemoval = true, since = "1.21.5")
|
||||
public Builder putIntArray(String key, List<Integer> value) {
|
||||
tag.putIntArray(key, value);
|
||||
return this;
|
||||
var array = new int[value.size()];
|
||||
for (var i = 0; i < value.size(); i++)
|
||||
array[i] = value.get(i);
|
||||
|
||||
return this.putIntArray(key, array);
|
||||
}
|
||||
|
||||
public Builder putLongArray(String key, long... value) {
|
||||
@@ -83,9 +95,14 @@ public interface INBTBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @deprecated Use {@link #putLongArray(String, long...)} */
|
||||
@Deprecated(forRemoval = true, since = "1.21.5")
|
||||
public Builder putLongArray(String key, List<Long> value) {
|
||||
tag.putLongArray(key, value);
|
||||
return this;
|
||||
var array = new long[value.size()];
|
||||
for (var i = 0; i < value.size(); i++)
|
||||
array[i] = value.get(i);
|
||||
|
||||
return this.putLongArray(key, array);
|
||||
}
|
||||
|
||||
public Builder put(String key, boolean value) {
|
||||
@@ -99,7 +116,7 @@ public interface INBTBuilder {
|
||||
}
|
||||
|
||||
public Builder put(String key, UUID value) {
|
||||
tag.putUUID(key, value);
|
||||
tag.putIntArray(key, UUIDUtil.uuidToIntArray(value));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,20 +24,19 @@ import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import net.minecraft.nbt.TagParser;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.util.GsonHelper;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class JsonUtils
|
||||
{
|
||||
public class JsonUtils {
|
||||
// http://stackoverflow.com/questions/7706772/deserializing-immutablelist-using-gson/21677349#21677349
|
||||
public enum ImmutableListTypeAdapter implements JsonDeserializer<ImmutableList<?>>, JsonSerializer<ImmutableList<?>>
|
||||
{
|
||||
public enum ImmutableListTypeAdapter implements JsonDeserializer<ImmutableList<?>>, JsonSerializer<ImmutableList<?>> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public ImmutableList<?> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
public ImmutableList<?> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
|
||||
final Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments();
|
||||
final Type parametrizedType = listOf(typeArguments[0]).getType();
|
||||
final List<?> list = context.deserialize(json, parametrizedType);
|
||||
@@ -45,8 +44,7 @@ public class JsonUtils
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ImmutableList<?> src, Type type, JsonSerializationContext context)
|
||||
{
|
||||
public JsonElement serialize(ImmutableList<?> src, Type type, JsonSerializationContext context) {
|
||||
final Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments();
|
||||
final Type parametrizedType = listOf(typeArguments[0]).getType();
|
||||
return context.serialize(src, parametrizedType);
|
||||
@@ -54,18 +52,15 @@ public class JsonUtils
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "serial", "unchecked" })
|
||||
private static <E> TypeToken<List<E>> listOf(final Type arg)
|
||||
{
|
||||
private static <E> TypeToken<List<E>> listOf(final Type arg) {
|
||||
return new TypeToken<List<E>>() {}.where(new TypeParameter<E>() {}, (TypeToken<E>) TypeToken.of(arg));
|
||||
}
|
||||
|
||||
public enum ImmutableMapTypeAdapter implements JsonDeserializer<ImmutableMap<String, ?>>, JsonSerializer<ImmutableMap<String, ?>>
|
||||
{
|
||||
public enum ImmutableMapTypeAdapter implements JsonDeserializer<ImmutableMap<String, ?>>, JsonSerializer<ImmutableMap<String, ?>> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public ImmutableMap<String, ?> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
public ImmutableMap<String, ?> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
|
||||
final Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments();
|
||||
final Type parameterizedType = mapOf(typeArguments[1]).getType();
|
||||
final Map<String, ?> map = context.deserialize(json, parameterizedType);
|
||||
@@ -73,8 +68,7 @@ public class JsonUtils
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ImmutableMap<String, ?> src, Type type, JsonSerializationContext context)
|
||||
{
|
||||
public JsonElement serialize(ImmutableMap<String, ?> src, Type type, JsonSerializationContext context) {
|
||||
final Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments();
|
||||
final Type parameterizedType = mapOf(typeArguments[1]).getType();
|
||||
return context.serialize(src, parameterizedType);
|
||||
@@ -82,20 +76,14 @@ public class JsonUtils
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CompoundTag readNBT(JsonObject json, String key)
|
||||
{
|
||||
if (net.minecraft.util.GsonHelper.isValidNode(json, key))
|
||||
{
|
||||
try
|
||||
{
|
||||
return TagParser.parseTag(net.minecraft.util.GsonHelper.getAsString(json, key));
|
||||
} catch (CommandSyntaxException e)
|
||||
{
|
||||
throw new JsonSyntaxException("Malformed NBT tag", e);
|
||||
}
|
||||
} else
|
||||
{
|
||||
public static CompoundTag readNBT(JsonObject json, String key) {
|
||||
if (!net.minecraft.util.GsonHelper.isValidNode(json, key))
|
||||
return null;
|
||||
|
||||
try {
|
||||
return TagParser.parseCompoundFully(GsonHelper.getAsString(json, key));
|
||||
} catch (CommandSyntaxException e) {
|
||||
throw new JsonSyntaxException("Malformed NBT tag", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Proxy object for a value that is calculated on first access
|
||||
* @param <T> The type of the value
|
||||
*/
|
||||
public sealed interface LazyFunction<T, R> extends Function<T, R> permits LazyFunction.Fast, LazyFunction.Concurrent {
|
||||
/**
|
||||
* Constructs a lazy-initialized object
|
||||
* @param function The function for the value, to be called the first time the value is needed.
|
||||
*/
|
||||
static <T, R> LazyFunction<T, R> of(@NotNull Function<T, R> function) {
|
||||
return new LazyFunction.Fast<>(function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a thread-safe lazy-initialized object
|
||||
* @param function The function for the value, to be called the first time the value is needed.
|
||||
*/
|
||||
static <T, R> LazyFunction<T, R> concurrentOf(@NotNull Function<T, R> function) {
|
||||
return new LazyFunction.Concurrent<>(function);
|
||||
}
|
||||
|
||||
R get() throws IllegalStateException;
|
||||
|
||||
/** Non-thread-safe implementation. */
|
||||
final class Fast<T, R> implements LazyFunction<T, R> {
|
||||
private Function<T, R> function;
|
||||
private R instance;
|
||||
|
||||
private Fast(Function<T, R> function) {
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable R apply(T t) {
|
||||
if (function != null) {
|
||||
instance = function.apply(t);
|
||||
function = null;
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public R get() throws IllegalStateException {
|
||||
if (function != null)
|
||||
throw new IllegalStateException("Cannot call get() before apply()");
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
/** Thread-safe implementation. */
|
||||
final class Concurrent<T, R> implements LazyFunction<T, R> {
|
||||
private volatile Object lock = new Object();
|
||||
private volatile Function<T, R> function;
|
||||
private volatile R instance;
|
||||
|
||||
private Concurrent(Function<T, R> function)
|
||||
{
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable R apply(T t) {
|
||||
// Copy the lock to a local variable to prevent NPEs if the lock field is set to null between the
|
||||
// null-check and the synchronization
|
||||
Object localLock = this.lock;
|
||||
if (function != null)
|
||||
{
|
||||
// localLock is not null here because supplier was non-null after we copied the lock and both of them
|
||||
// are volatile
|
||||
synchronized (localLock)
|
||||
{
|
||||
if (function != null)
|
||||
{
|
||||
instance = function.apply(t);
|
||||
function = null;
|
||||
this.lock = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public R get() throws IllegalStateException {
|
||||
if (function != null)
|
||||
throw new IllegalStateException("Cannot call get() before apply()");
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,47 +5,47 @@
|
||||
|
||||
package net.minecraftforge.common.util;
|
||||
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.world.level.saveddata.SavedData;
|
||||
import net.minecraft.world.level.storage.DimensionDataStorage;
|
||||
import net.minecraft.world.level.saveddata.SavedDataType;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class LevelCapabilityData extends SavedData {
|
||||
public static final String ID = "capabilities";
|
||||
public static final SavedDataType<LevelCapabilityData> TYPE = new SavedDataType<LevelCapabilityData>(
|
||||
"capabilities",
|
||||
LevelCapabilityData::new,
|
||||
ctx ->
|
||||
RecordCodecBuilder.create(b ->
|
||||
b.group(
|
||||
CompoundTag.CODEC.fieldOf("data").forGetter(i -> {
|
||||
if (i.serializable == null)
|
||||
return new CompoundTag();
|
||||
return i.serializable.serializeNBT(ctx.levelOrThrow().registryAccess());
|
||||
})
|
||||
).apply(b, nbt -> new LevelCapabilityData(ctx, nbt))
|
||||
),
|
||||
null
|
||||
);
|
||||
|
||||
@Nullable
|
||||
private final INBTSerializable<CompoundTag> serializable;
|
||||
private CompoundTag capNBT = null;
|
||||
|
||||
public LevelCapabilityData(@Nullable INBTSerializable<CompoundTag> serializable) {
|
||||
this.serializable = serializable;
|
||||
private LevelCapabilityData(SavedData.Context ctx) {
|
||||
this.serializable = ctx.levelOrThrow().getCapabilityDispatcher();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag save(CompoundTag nbt, HolderLookup.Provider provider) {
|
||||
if (serializable != null)
|
||||
nbt = serializable.serializeNBT(provider);
|
||||
return nbt;
|
||||
private LevelCapabilityData(SavedData.Context ctx, CompoundTag data) {
|
||||
this(ctx);
|
||||
if (this.serializable != null)
|
||||
this.serializable.deserializeNBT(ctx.levelOrThrow().registryAccess(), data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static LevelCapabilityData compute(DimensionDataStorage data, @Nullable INBTSerializable<CompoundTag> caps) {
|
||||
var factory = new Factory<>(
|
||||
() -> new LevelCapabilityData(caps),
|
||||
(tag, lookup) -> {
|
||||
var ret = new LevelCapabilityData(caps);
|
||||
if (caps != null)
|
||||
caps.deserializeNBT(lookup, tag);
|
||||
return ret;
|
||||
},
|
||||
null
|
||||
);
|
||||
|
||||
return data.computeIfAbsent(factory, ID);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user