From b26347a026f4b9fc8712bc3f75df957f5199a4ce Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 6 Feb 2024 17:32:25 +0900 Subject: [PATCH 1/4] [ci skip] Add ClientTooltipComponentRegistry for custom tooltips (#475) * Add ClientTooltipComponentRegistry for custom tooltips (#463) Signed-off-by: Sergey Shatunov Signed-off-by: shedaniel * Fix testmod for client tooltip components Signed-off-by: shedaniel --------- Signed-off-by: Sergey Shatunov Signed-off-by: shedaniel Co-authored-by: Sergey Shatunov --- .../gui/ClientTooltipComponentRegistry.java | 50 ++++++++++++++ .../ClientTooltipComponentRegistryImpl.java | 42 +++++++++++ .../ClientTooltipComponentRegistryImpl.java | 69 +++++++++++++++++++ .../java/dev/architectury/test/TestMod.java | 3 + .../test/registry/TestRegistries.java | 4 ++ .../registry/objects/ItemWithTooltip.java | 69 +++++++++++++++++++ 6 files changed, 237 insertions(+) create mode 100644 common/src/main/java/dev/architectury/registry/client/gui/ClientTooltipComponentRegistry.java create mode 100644 fabric/src/main/java/dev/architectury/registry/client/gui/fabric/ClientTooltipComponentRegistryImpl.java create mode 100644 forge/src/main/java/dev/architectury/registry/client/gui/forge/ClientTooltipComponentRegistryImpl.java create mode 100644 testmod-common/src/main/java/dev/architectury/test/registry/objects/ItemWithTooltip.java diff --git a/common/src/main/java/dev/architectury/registry/client/gui/ClientTooltipComponentRegistry.java b/common/src/main/java/dev/architectury/registry/client/gui/ClientTooltipComponentRegistry.java new file mode 100644 index 000000000..b7e9a798b --- /dev/null +++ b/common/src/main/java/dev/architectury/registry/client/gui/ClientTooltipComponentRegistry.java @@ -0,0 +1,50 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021, 2022 architectury + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package dev.architectury.registry.client.gui; + +import dev.architectury.injectables.annotations.ExpectPlatform; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; +import net.minecraft.world.inventory.tooltip.TooltipComponent; + +import java.util.function.Function; + +/** + * Registry for {@link ClientTooltipComponent} factories + */ +@Environment(EnvType.CLIENT) +public final class ClientTooltipComponentRegistry { + private ClientTooltipComponentRegistry() { + } + + /** + * Allows users to register custom {@link ClientTooltipComponent} + * factories for their {@link TooltipComponent} types. + * + * @param clazz class of {@link T} + * @param factory factory to create instances of {@link ClientTooltipComponent} from {@link T} + * @param the type of {@link TooltipComponent} factory + */ + @ExpectPlatform + public static void register(Class clazz, Function factory) { + throw new AssertionError(); + } +} diff --git a/fabric/src/main/java/dev/architectury/registry/client/gui/fabric/ClientTooltipComponentRegistryImpl.java b/fabric/src/main/java/dev/architectury/registry/client/gui/fabric/ClientTooltipComponentRegistryImpl.java new file mode 100644 index 000000000..f74c8447d --- /dev/null +++ b/fabric/src/main/java/dev/architectury/registry/client/gui/fabric/ClientTooltipComponentRegistryImpl.java @@ -0,0 +1,42 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021, 2022 architectury + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package dev.architectury.registry.client.gui.fabric; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; +import net.minecraft.world.inventory.tooltip.TooltipComponent; +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Function; + +@Environment(EnvType.CLIENT) +@ApiStatus.Internal +public class ClientTooltipComponentRegistryImpl { + public static void register(Class clazz, Function factory) { + TooltipComponentCallback.EVENT.register((tooltipComponent) -> { + if (clazz.isInstance(tooltipComponent)) { + return factory.apply(clazz.cast(tooltipComponent)); + } + return null; + }); + } +} diff --git a/forge/src/main/java/dev/architectury/registry/client/gui/forge/ClientTooltipComponentRegistryImpl.java b/forge/src/main/java/dev/architectury/registry/client/gui/forge/ClientTooltipComponentRegistryImpl.java new file mode 100644 index 000000000..932ed66f7 --- /dev/null +++ b/forge/src/main/java/dev/architectury/registry/client/gui/forge/ClientTooltipComponentRegistryImpl.java @@ -0,0 +1,69 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021, 2022 architectury + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package dev.architectury.registry.client.gui.forge; + +import dev.architectury.platform.forge.EventBuses; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; +import net.minecraft.world.inventory.tooltip.TooltipComponent; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.client.event.RegisterClientTooltipComponentFactoriesEvent; +import net.minecraftforge.eventbus.api.EventPriority; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Function; + +@OnlyIn(Dist.CLIENT) +@ApiStatus.Internal +public class ClientTooltipComponentRegistryImpl { + @Nullable + private static List> entries = new ArrayList<>(); + + static { + EventBuses.onRegistered("architectury", bus -> { + bus.addListener(EventPriority.HIGH, event -> { + if (entries != null) { + for (Entry entry : entries) { + Entry casted = (Entry) entry; + event.register(casted.clazz(), casted.factory()); + } + + entries = null; + } + }); + }); + } + + public static void register(Class clazz, Function factory) { + if (entries == null) { + throw new IllegalStateException("Cannot register ClientTooltipComponent factory when factories are already aggregated!"); + } + entries.add(new Entry<>(clazz, factory)); + } + + public record Entry( + Class clazz, Function factory + ) { + } +} diff --git a/testmod-common/src/main/java/dev/architectury/test/TestMod.java b/testmod-common/src/main/java/dev/architectury/test/TestMod.java index 009bcf22b..5829979b1 100644 --- a/testmod-common/src/main/java/dev/architectury/test/TestMod.java +++ b/testmod-common/src/main/java/dev/architectury/test/TestMod.java @@ -20,6 +20,7 @@ package dev.architectury.test; import dev.architectury.event.events.client.ClientLifecycleEvent; +import dev.architectury.registry.client.gui.ClientTooltipComponentRegistry; import dev.architectury.registry.client.level.entity.EntityRendererRegistry; import dev.architectury.test.debug.ConsoleMessageSink; import dev.architectury.test.debug.MessageSink; @@ -33,6 +34,7 @@ import dev.architectury.test.particle.TestParticles; import dev.architectury.test.registry.TestRegistries; import dev.architectury.test.registry.client.TestKeybinds; +import dev.architectury.test.registry.objects.ItemWithTooltip; import dev.architectury.test.tags.TestTags; import dev.architectury.test.trade.TestTrades; import dev.architectury.test.worldgen.TestWorldGeneration; @@ -72,6 +74,7 @@ public static void initializeClient() { TestModNet.initializeClient(); EntityRendererRegistry.register(TestRegistries.TEST_ENTITY, CowRenderer::new); EntityRendererRegistry.register(TestRegistries.TEST_ENTITY_2, CowRenderer::new); + ClientTooltipComponentRegistry.register(ItemWithTooltip.MyTooltipComponent.class, ItemWithTooltip.MyClientTooltipComponent::new); } } } diff --git a/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java b/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java index b8e1ff73e..1cbcfe832 100644 --- a/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java +++ b/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java @@ -34,6 +34,7 @@ import dev.architectury.test.entity.TestEntity; import dev.architectury.test.recipes.TestRecipeSerializer; import dev.architectury.test.registry.objects.EquippableTickingItem; +import dev.architectury.test.registry.objects.ItemWithTooltip; import dev.architectury.test.tab.TestCreativeTabs; import net.minecraft.core.BlockPos; import net.minecraft.core.Registry; @@ -127,6 +128,9 @@ public TestInt(int value) { } }); + public static final RegistrySupplier TEST_TOOLTIP = ITEMS.register("test_tooltip", + () -> new ItemWithTooltip(new Item.Properties().tab(TestCreativeTabs.TEST_TAB))); + public static final RegistrySupplier TEST_BLOCK = BLOCKS.register("test_block", () -> new Block(BlockBehaviour.Properties.copy(Blocks.STONE))); public static final RegistrySupplier COLLISION_BLOCK = BLOCKS.register("collision_block", () -> diff --git a/testmod-common/src/main/java/dev/architectury/test/registry/objects/ItemWithTooltip.java b/testmod-common/src/main/java/dev/architectury/test/registry/objects/ItemWithTooltip.java new file mode 100644 index 000000000..b826f8dc2 --- /dev/null +++ b/testmod-common/src/main/java/dev/architectury/test/registry/objects/ItemWithTooltip.java @@ -0,0 +1,69 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021, 2022 architectury + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package dev.architectury.test.registry.objects; + +import com.mojang.blaze3d.vertex.PoseStack; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; +import net.minecraft.client.renderer.entity.ItemRenderer; +import net.minecraft.world.inventory.tooltip.TooltipComponent; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; + +public class ItemWithTooltip extends Item { + public ItemWithTooltip(Properties properties) { + super(properties); + } + + @Override + public @NotNull Optional getTooltipImage(ItemStack itemStack) { + return Optional.of(new MyTooltipComponent(itemStack.getCount())); + } + + public record MyTooltipComponent(int count) implements TooltipComponent { + + } + + @Environment(EnvType.CLIENT) + public record MyClientTooltipComponent(MyTooltipComponent component) implements ClientTooltipComponent { + @Override + public int getHeight() { + return 100; + } + + @Override + public int getWidth(Font font) { + return 100; + } + + @Override + public void renderImage(Font font, int x, int y, PoseStack poseStack, ItemRenderer itemRenderer, int blitOffset) { + poseStack.pushPose(); + poseStack.translate(0, 0, blitOffset); + font.draw(poseStack, "Count: " + component.count, x + getWidth(font) / 2, y + (getHeight() - font.lineHeight) / 2, 0xFF00FF00); + poseStack.popPose(); + } + } +} From a333232d8557fc4da0c647596f89652134ed851a Mon Sep 17 00:00:00 2001 From: Josiah Glosson Date: Tue, 6 Feb 2024 02:32:35 -0600 Subject: [PATCH 2/4] [ci skip] Add defaultId to RegistrarBuilder (#474) * Add defaultId to RegistrarBuilder * Make the default key an registrar option --------- Co-authored-by: shedaniel --- .../options/DefaultIdRegistrarOption.java | 25 +++++++++++++++++++ .../registries/fabric/RegistriesImpl.java | 24 +++++++++++++----- .../registries/forge/RegistriesImpl.java | 3 +++ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 common/src/main/java/dev/architectury/registry/registries/options/DefaultIdRegistrarOption.java diff --git a/common/src/main/java/dev/architectury/registry/registries/options/DefaultIdRegistrarOption.java b/common/src/main/java/dev/architectury/registry/registries/options/DefaultIdRegistrarOption.java new file mode 100644 index 000000000..9add28ecc --- /dev/null +++ b/common/src/main/java/dev/architectury/registry/registries/options/DefaultIdRegistrarOption.java @@ -0,0 +1,25 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021, 2022 architectury + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package dev.architectury.registry.registries.options; + +import net.minecraft.resources.ResourceLocation; + +public record DefaultIdRegistrarOption(ResourceLocation defaultId) implements RegistrarOption { +} diff --git a/fabric/src/main/java/dev/architectury/registry/registries/fabric/RegistriesImpl.java b/fabric/src/main/java/dev/architectury/registry/registries/fabric/RegistriesImpl.java index 46c9cabac..c9c3b9715 100644 --- a/fabric/src/main/java/dev/architectury/registry/registries/fabric/RegistriesImpl.java +++ b/fabric/src/main/java/dev/architectury/registry/registries/fabric/RegistriesImpl.java @@ -28,6 +28,7 @@ import dev.architectury.registry.registries.RegistrarBuilder; import dev.architectury.registry.registries.Registries; import dev.architectury.registry.registries.RegistrySupplier; +import dev.architectury.registry.registries.options.DefaultIdRegistrarOption; import dev.architectury.registry.registries.options.RegistrarOption; import dev.architectury.registry.registries.options.StandardRegistrarOption; import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; @@ -91,7 +92,7 @@ public void forRegistry(ResourceKey> key, Consumer> @Override public RegistrarBuilder builder(Class type, ResourceLocation registryId) { - return new RegistrarBuilderWrapper<>(modId, FabricRegistryBuilder.createSimple(type, registryId)); + return new RegistrarBuilderWrapper<>(modId, type, registryId); } } @@ -120,24 +121,35 @@ public int hashCode() { public static class RegistrarBuilderWrapper implements RegistrarBuilder { private final String modId; - private FabricRegistryBuilder> builder; + private final Class type; + private final ResourceLocation registryId; + private final List>>> apply = new ArrayList<>(); + @Nullable + private ResourceLocation defaultId; - public RegistrarBuilderWrapper(String modId, FabricRegistryBuilder> builder) { + public RegistrarBuilderWrapper(String modId, Class type, ResourceLocation registryId) { this.modId = modId; - this.builder = builder; + this.type = type; + this.registryId = registryId; } @Override public Registrar build() { + final var builder = defaultId == null + ? FabricRegistryBuilder.createSimple(type, registryId) + : FabricRegistryBuilder.createDefaulted(type, registryId, defaultId); + apply.forEach(consumer -> consumer.accept(builder)); return Registries.get(modId).get(builder.buildAndRegister()); } @Override public RegistrarBuilder option(RegistrarOption option) { if (option == StandardRegistrarOption.SAVE_TO_DISC) { - this.builder.attribute(RegistryAttribute.PERSISTED); + this.apply.add(builder -> builder.attribute(RegistryAttribute.PERSISTED)); } else if (option == StandardRegistrarOption.SYNC_TO_CLIENTS) { - this.builder.attribute(RegistryAttribute.SYNCED); + this.apply.add(builder -> builder.attribute(RegistryAttribute.SYNCED)); + } else if (option instanceof DefaultIdRegistrarOption opt) { + this.defaultId = opt.defaultId(); } return this; } diff --git a/forge/src/main/java/dev/architectury/registry/registries/forge/RegistriesImpl.java b/forge/src/main/java/dev/architectury/registry/registries/forge/RegistriesImpl.java index 174fc538c..59d084df3 100644 --- a/forge/src/main/java/dev/architectury/registry/registries/forge/RegistriesImpl.java +++ b/forge/src/main/java/dev/architectury/registry/registries/forge/RegistriesImpl.java @@ -28,6 +28,7 @@ import dev.architectury.registry.registries.RegistrarBuilder; import dev.architectury.registry.registries.Registries; import dev.architectury.registry.registries.RegistrySupplier; +import dev.architectury.registry.registries.options.DefaultIdRegistrarOption; import dev.architectury.registry.registries.options.RegistrarOption; import dev.architectury.registry.registries.options.StandardRegistrarOption; import net.minecraft.core.Registry; @@ -306,6 +307,8 @@ public RegistrarBuilder option(RegistrarOption option) { this.saveToDisk = true; } else if (option == StandardRegistrarOption.SYNC_TO_CLIENTS) { this.syncToClients = true; + } else if (option instanceof DefaultIdRegistrarOption opt) { + this.builder.setDefaultKey(opt.defaultId()); } return this; } From f1fdba9897c70dc5cf04588cee59f49c35477953 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 6 Feb 2024 17:32:50 +0900 Subject: [PATCH 3/4] [ci skip] SpawnPlacements.register Hook (Closes architectury/architectury-api#375) (#473) --- .../level/entity/SpawnPlacementsRegistry.java | 44 ++++++++++++++ .../fabric/SpawnPlacementsRegistryImpl.java | 33 ++++++++++ .../forge/SpawnPlacementsRegistryImpl.java | 60 +++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 common/src/main/java/dev/architectury/registry/level/entity/SpawnPlacementsRegistry.java create mode 100644 fabric/src/main/java/dev/architectury/registry/level/entity/fabric/SpawnPlacementsRegistryImpl.java create mode 100644 forge/src/main/java/dev/architectury/registry/level/entity/forge/SpawnPlacementsRegistryImpl.java diff --git a/common/src/main/java/dev/architectury/registry/level/entity/SpawnPlacementsRegistry.java b/common/src/main/java/dev/architectury/registry/level/entity/SpawnPlacementsRegistry.java new file mode 100644 index 000000000..386e9ffb2 --- /dev/null +++ b/common/src/main/java/dev/architectury/registry/level/entity/SpawnPlacementsRegistry.java @@ -0,0 +1,44 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021, 2022 architectury + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package dev.architectury.registry.level.entity; + +import dev.architectury.injectables.annotations.ExpectPlatform; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.SpawnPlacements; +import net.minecraft.world.level.levelgen.Heightmap; + +import java.util.function.Supplier; + +public final class SpawnPlacementsRegistry { + /** + * Registers the spawn placement of an entity. + * + * @param type the type of entity + * @param spawnPlacement the type of spawn placement + * @param heightmapType the type of heightmap + * @param spawnPredicate the spawn predicate + * @see net.minecraft.world.entity.SpawnPlacements + */ + @ExpectPlatform + public static void register(Supplier> type, SpawnPlacements.Type spawnPlacement, Heightmap.Types heightmapType, SpawnPlacements.SpawnPredicate spawnPredicate) { + throw new AssertionError(); + } +} diff --git a/fabric/src/main/java/dev/architectury/registry/level/entity/fabric/SpawnPlacementsRegistryImpl.java b/fabric/src/main/java/dev/architectury/registry/level/entity/fabric/SpawnPlacementsRegistryImpl.java new file mode 100644 index 000000000..962efd22c --- /dev/null +++ b/fabric/src/main/java/dev/architectury/registry/level/entity/fabric/SpawnPlacementsRegistryImpl.java @@ -0,0 +1,33 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021, 2022 architectury + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package dev.architectury.registry.level.entity.fabric; + +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.SpawnPlacements; +import net.minecraft.world.level.levelgen.Heightmap; + +import java.util.function.Supplier; + +public class SpawnPlacementsRegistryImpl { + public static void register(Supplier> type, SpawnPlacements.Type spawnPlacement, Heightmap.Types heightmapType, SpawnPlacements.SpawnPredicate spawnPredicate) { + SpawnPlacements.register(type.get(), spawnPlacement, heightmapType, spawnPredicate); + } +} diff --git a/forge/src/main/java/dev/architectury/registry/level/entity/forge/SpawnPlacementsRegistryImpl.java b/forge/src/main/java/dev/architectury/registry/level/entity/forge/SpawnPlacementsRegistryImpl.java new file mode 100644 index 000000000..63afd1183 --- /dev/null +++ b/forge/src/main/java/dev/architectury/registry/level/entity/forge/SpawnPlacementsRegistryImpl.java @@ -0,0 +1,60 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021, 2022 architectury + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package dev.architectury.registry.level.entity.forge; + +import dev.architectury.platform.forge.EventBuses; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.SpawnPlacements; +import net.minecraft.world.level.levelgen.Heightmap; +import net.minecraftforge.event.entity.SpawnPlacementRegisterEvent; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Supplier; + +public class SpawnPlacementsRegistryImpl { + private static List> entries = new ArrayList<>(); + + private record Entry(Supplier> type, SpawnPlacements.Type spawnPlacement, + Heightmap.Types heightmapType, + SpawnPlacements.SpawnPredicate spawnPredicate) { + } + + static { + EventBuses.onRegistered("architectury", bus -> { + bus.addListener(event -> { + for (Entry entry : entries) { + Entry casted = (Entry) entry; + event.register(casted.type().get(), casted.spawnPlacement(), casted.heightmapType(), casted.spawnPredicate(), SpawnPlacementRegisterEvent.Operation.OR); + } + entries = null; + }); + }); + } + + public static void register(Supplier> type, SpawnPlacements.Type spawnPlacement, Heightmap.Types heightmapType, SpawnPlacements.SpawnPredicate spawnPredicate) { + if (entries != null) { + entries.add(new Entry<>(type, spawnPlacement, heightmapType, spawnPredicate)); + } else { + throw new IllegalStateException("SpawnPlacementsRegistry.register must not be called after the registry has been collected!"); + } + } +} From 4ed66609b0c8bdf60535427ba7cf94363782955b Mon Sep 17 00:00:00 2001 From: digitalseraphim Date: Tue, 6 Feb 2024 03:33:01 -0500 Subject: [PATCH 4/4] =?UTF-8?q?Make=20ArchitecturyFlowingFluid=20use=20the?= =?UTF-8?q?=20same=20Forge=20FluidType=20when=20the=20s=E2=80=A6=20(#467)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Make ArchitecturyFlowingFluid use the same Forge FluidType when the same ArchitecturyFluidAttributes object is passed in * Use the same fluid type instead of the same properties for simplity * Fix packages import --------- Co-authored-by: shedaniel --- .../imitator/ArchitecturyFlowingFluid.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/forge/src/main/java/dev/architectury/core/fluid/forge/imitator/ArchitecturyFlowingFluid.java b/forge/src/main/java/dev/architectury/core/fluid/forge/imitator/ArchitecturyFlowingFluid.java index 902018b59..50a30567e 100644 --- a/forge/src/main/java/dev/architectury/core/fluid/forge/imitator/ArchitecturyFlowingFluid.java +++ b/forge/src/main/java/dev/architectury/core/fluid/forge/imitator/ArchitecturyFlowingFluid.java @@ -42,24 +42,24 @@ import net.minecraftforge.fluids.ForgeFlowingFluid; import org.jetbrains.annotations.NotNull; +import java.util.IdentityHashMap; +import java.util.Map; import java.util.Optional; -import java.util.function.Supplier; public abstract class ArchitecturyFlowingFluid extends ForgeFlowingFluid { + private static final Map FLUID_TYPE_MAP = new IdentityHashMap<>(); private final ArchitecturyFluidAttributes attributes; - private final Supplier forgeType; ArchitecturyFlowingFluid(ArchitecturyFluidAttributes attributes) { super(toForgeProperties(attributes)); this.attributes = attributes; - this.forgeType = Suppliers.memoize(() -> { - return new ArchitecturyFluidAttributesForge(FluidType.Properties.create(), this, attributes); - }); } private static Properties toForgeProperties(ArchitecturyFluidAttributes attributes) { Properties forge = new Properties(Suppliers.memoize(() -> { - return new ArchitecturyFluidAttributesForge(FluidType.Properties.create(), attributes.getSourceFluid(), attributes); + return FLUID_TYPE_MAP.computeIfAbsent(attributes, attr -> { + return new ArchitecturyFluidAttributesForge(FluidType.Properties.create(), attr.getSourceFluid(), attr); + }); }), attributes::getSourceFluid, attributes::getFlowingFluid); forge.slopeFindDistance(attributes.getSlopeFindDistance()); forge.levelDecreasePerBlock(attributes.getDropOff()); @@ -70,11 +70,6 @@ private static Properties toForgeProperties(ArchitecturyFluidAttributes attribut return forge; } - @Override - public FluidType getFluidType() { - return forgeType.get(); - } - @Override public Fluid getFlowing() { return attributes.getFlowingFluid();