Skip to content

Commit

Permalink
Merge branch 'neoforged:1.20.x' into with-pr-publish
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt authored Dec 26, 2023
2 parents 485c6e1 + 97e83fa commit 4da47c6
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 91 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ neoform_version=20231207.154220

mergetool_version=2.0.0
accesstransformers_version=10.0.1
coremods_version=6.0.2
coremods_version=6.0.4
eventbus_version=7.2.0
modlauncher_version=10.0.9
securejarhandler_version=2.1.24
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
--- a/net/minecraft/network/syncher/EntityDataSerializers.java
+++ b/net/minecraft/network/syncher/EntityDataSerializers.java
@@ -157,16 +_,16 @@
@@ -156,17 +_,29 @@
FriendlyByteBuf::writeQuaternion, FriendlyByteBuf::readQuaternion
);

+ private static final org.slf4j.Logger LOGGER = com.mojang.logging.LogUtils.getLogger();
+ private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
+ /**
+ * @deprecated NeoForge: Use {@link net.neoforged.neoforge.registries.NeoForgeRegistries#ENTITY_DATA_SERIALIZERS} instead
+ */
+ @Deprecated
public static void registerSerializer(EntityDataSerializer<?> p_135051_) {
- SERIALIZERS.add(p_135051_);
+ if (SERIALIZERS.add(p_135051_) >= 256) throw new RuntimeException("Vanilla DataSerializer ID limit exceeded");
+ if (!STACK_WALKER.getCallerClass().equals(EntityDataSerializers.class)) {
+ // TODO 1.20.5: throw an exception in dev instead
+ LOGGER.error("Modded EntityDataSerializers must be registered to NeoForgeRegistries.ENTITY_DATA_SERIALIZERS instead to prevent ID mismatches between client and server!", new Throwable());
+ }
+
+ if (SERIALIZERS.add(p_135051_) >= net.neoforged.neoforge.common.CommonHooks.VANILLA_SERIALIZER_LIMIT)
+ throw new RuntimeException("Vanilla EntityDataSerializer ID limit exceeded");
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
+
+ public static Optional<RecipeHolder<?>> fromJson(ResourceLocation p_44046_, JsonObject p_44047_, com.mojang.serialization.DynamicOps<com.google.gson.JsonElement> jsonElementOps) {
+ Optional<? extends Recipe<?>> recipe = net.neoforged.neoforge.common.conditions.ICondition.getWithWithConditionsCodec(net.neoforged.neoforge.common.util.NeoForgeExtraCodecs.CONDITIONAL_RECIPE_CODEC, jsonElementOps, p_44047_);
+ return recipe.filter(r -> r != net.neoforged.neoforge.common.crafting.CraftingHelper.EMPTY_RECIPE).map(r -> new RecipeHolder<>(p_44046_, r));
+ return recipe.map(r -> new RecipeHolder<>(p_44046_, r));
}

public void replaceRecipes(Iterable<RecipeHolder<?>> p_44025_) {
9 changes: 7 additions & 2 deletions src/main/java/net/neoforged/neoforge/common/CommonHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -895,19 +895,24 @@ public static boolean hasNoElements(Ingredient ingredient) {
@Deprecated(forRemoval = true, since = "1.20.1") // Tags use a codec now This was never used in 1.20
public static <T> void deserializeTagAdditions(List<TagEntry> list, JsonObject json, List<TagEntry> allList) {}

public static final int VANILLA_SERIALIZER_LIMIT = 256;

@Nullable
public static EntityDataSerializer<?> getSerializer(int id, CrudeIncrementalIntIdentityHashBiMap<EntityDataSerializer<?>> vanilla) {
EntityDataSerializer<?> serializer = vanilla.byId(id);
if (serializer == null) {
return NeoForgeRegistries.ENTITY_DATA_SERIALIZERS.byId(id);
return NeoForgeRegistries.ENTITY_DATA_SERIALIZERS.byId(id - VANILLA_SERIALIZER_LIMIT);
}
return serializer;
}

public static int getSerializerId(EntityDataSerializer<?> serializer, CrudeIncrementalIntIdentityHashBiMap<EntityDataSerializer<?>> vanilla) {
int id = vanilla.getId(serializer);
if (id < 0) {
return NeoForgeRegistries.ENTITY_DATA_SERIALIZERS.getId(serializer);
id = NeoForgeRegistries.ENTITY_DATA_SERIALIZERS.getId(serializer);
if (id >= 0) {
return id + VANILLA_SERIALIZER_LIMIT;
}
}
return id;
}
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.GameRules;
Expand Down Expand Up @@ -94,7 +93,6 @@
import net.neoforged.neoforge.common.conditions.TagEmptyCondition;
import net.neoforged.neoforge.common.conditions.TrueCondition;
import net.neoforged.neoforge.common.crafting.CompoundIngredient;
import net.neoforged.neoforge.common.crafting.ConditionalRecipe;
import net.neoforged.neoforge.common.crafting.DifferenceIngredient;
import net.neoforged.neoforge.common.crafting.IngredientType;
import net.neoforged.neoforge.common.crafting.IntersectionIngredient;
Expand Down Expand Up @@ -287,9 +285,6 @@ public class NeoForgeMod {
public static final DeferredHolder<IngredientType<?>, IngredientType<DifferenceIngredient>> DIFFERENCE_INGREDIENT_TYPE = INGREDIENT_TYPES.register("difference", () -> new IngredientType<>(DifferenceIngredient.CODEC, DifferenceIngredient.CODEC_NONEMPTY));
public static final DeferredHolder<IngredientType<?>, IngredientType<IntersectionIngredient>> INTERSECTION_INGREDIENT_TYPE = INGREDIENT_TYPES.register("intersection", () -> new IngredientType<>(IntersectionIngredient.CODEC, IntersectionIngredient.CODEC_NONEMPTY));

private static final DeferredRegister<RecipeSerializer<?>> RECIPE_SERIALIZERS = DeferredRegister.create(Registries.RECIPE_SERIALIZER, "neoforge");
public static final DeferredHolder<RecipeSerializer<?>, ConditionalRecipe<?>> CONDITIONAL_RECIPE = RECIPE_SERIALIZERS.register("conditional", ConditionalRecipe::new);

private static final DeferredRegister<Codec<? extends ICondition>> CONDITION_CODECS = DeferredRegister.create(NeoForgeRegistries.Keys.CONDITION_CODECS, "neoforge");
public static final DeferredHolder<Codec<? extends ICondition>, Codec<AndCondition>> AND_CONDITION = CONDITION_CODECS.register("and", () -> AndCondition.CODEC);
public static final DeferredHolder<Codec<? extends ICondition>, Codec<FalseCondition>> FALSE_CONDITION = CONDITION_CODECS.register("false", () -> FalseCondition.CODEC);
Expand Down Expand Up @@ -487,7 +482,6 @@ public NeoForgeMod(IEventBus modEventBus, Dist dist) {
VANILLA_INGREDIENT_TYPES.register(modEventBus);
INGREDIENT_TYPES.register(modEventBus);
CONDITION_CODECS.register(modEventBus);
RECIPE_SERIALIZERS.register(modEventBus);
NeoForge.EVENT_BUS.addListener(this::serverStopping);
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, NeoForgeConfig.clientSpec);
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, NeoForgeConfig.serverSpec);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,16 @@

package net.neoforged.neoforge.common.crafting;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import java.util.function.Function;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.TagParser;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
Expand All @@ -38,39 +30,6 @@ public class CraftingHelper {
private static final Logger LOGGER = LogManager.getLogger();
@SuppressWarnings("unused")
private static final Marker CRAFTHELPER = MarkerManager.getMarker("CRAFTHELPER");
private static Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
public static final Recipe<?> EMPTY_RECIPE = new Recipe<>() {
@Override
public boolean matches(Container p_44002_, Level p_44003_) {
return false;
}

@Override
public ItemStack assemble(Container p_44001_, RegistryAccess p_267165_) {
return ItemStack.EMPTY;
}

@Override
public boolean canCraftInDimensions(int p_43999_, int p_44000_) {
return false;
}

@Override
public ItemStack getResultItem(RegistryAccess p_267052_) {
return ItemStack.EMPTY;
}

@Override
public RecipeSerializer<?> getSerializer() {
throw new UnsupportedOperationException("Empty recipe has no serializer");
}

@Override
public RecipeType<?> getType() {
throw new UnsupportedOperationException("Empty recipe has no type");
}
};

public static final Codec<CompoundTag> TAG_CODEC = ExtraCodecs.withAlternative(TagParser.AS_CODEC, net.minecraft.nbt.CompoundTag.CODEC);

@ApiStatus.Internal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.debug.entity;

import io.netty.buffer.Unpooled;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializer;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.CommonHooks;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import net.neoforged.testframework.DynamicTest;
import net.neoforged.testframework.TestFramework;
import net.neoforged.testframework.annotation.ForEachTest;
import net.neoforged.testframework.annotation.OnInit;
import net.neoforged.testframework.annotation.TestHolder;
import net.neoforged.testframework.gametest.EmptyTemplate;
import net.neoforged.testframework.registration.RegistrationHelper;

@ForEachTest(groups = EntityDataSerializerTest.GROUP)
public class EntityDataSerializerTest {
public static final String GROUP = "level.entity.data_serializer";

private static final RegistrationHelper REG_HELPER = RegistrationHelper.create("neotests_custom_entity_data_serializer");
private static final DeferredHolder<EntityDataSerializer<?>, EntityDataSerializer<Byte>> TEST_SERIALIZER = REG_HELPER
.registrar(NeoForgeRegistries.Keys.ENTITY_DATA_SERIALIZERS)
.register("test_serializer", () -> EntityDataSerializer.simple((buf, b) -> buf.writeByte(b), FriendlyByteBuf::readByte));

@OnInit
static void register(final TestFramework framework) {
REG_HELPER.register(framework.modEventBus());
}

@GameTest
@EmptyTemplate(floor = true)
@TestHolder(description = "Tests if custom EntityDataSerializers are properly handled")
static void customEntityDataSerializer(final DynamicTest test, final RegistrationHelper reg) {
var testEntity = reg.entityTypes().registerType("serializer_test_entity", () -> EntityType.Builder.of(TestEntity::new, MobCategory.CREATURE)
.sized(1, 1)).withRenderer(() -> TestEntityRenderer::new);

test.onGameTest(helper -> {
var entity = helper.spawn(testEntity.get(), 1, 2, 1);
var items = entity.getEntityData().packDirty();
if (items == null) {
helper.fail("Expected dirty entity data, got none");
return;
}
var pkt = new ClientboundSetEntityDataPacket(entity.getId(), items);
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
pkt.write(buf);
helper.assertTrue(buf.readVarInt() == entity.getId(), "Entity ID didn't match"); // Drop entity ID
buf.readByte(); // Drop item ID
int expectedId = NeoForgeRegistries.ENTITY_DATA_SERIALIZERS.getId(TEST_SERIALIZER.get()) + CommonHooks.VANILLA_SERIALIZER_LIMIT;
helper.assertTrue(buf.readVarInt() == expectedId, "Serializer ID didn't match");
buf.readByte(); // Drop data
buf.readByte(); // Drop EOF marker
helper.assertTrue(buf.readableBytes() == 0, "Buffer not empty");

helper.succeed();
});
}

private static class TestEntity extends Entity {
private static final EntityDataAccessor<Byte> DATA_TEST_VALUE = SynchedEntityData.defineId(TestEntity.class, TEST_SERIALIZER.value());

public TestEntity(EntityType<? extends Entity> entityType, Level level) {
super(entityType, level);
entityData.set(DATA_TEST_VALUE, (byte) 1);
}

@Override
protected void defineSynchedData() {
entityData.define(DATA_TEST_VALUE, (byte) 0);
}

@Override
protected void readAdditionalSaveData(CompoundTag tag) {}

@Override
protected void addAdditionalSaveData(CompoundTag tag) {}
}

private static class TestEntityRenderer extends EntityRenderer<TestEntity> {
public TestEntityRenderer(EntityRendererProvider.Context context) {
super(context);
}

@Override
public ResourceLocation getTextureLocation(TestEntity entity) {
return null;
}
}
}

0 comments on commit 4da47c6

Please sign in to comment.