From 1215d93645e520a912321ed2fc785b5d5c8abd21 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sat, 31 Dec 2022 17:49:57 +0000 Subject: [PATCH] Populate item groups on server start --- .../computercraft/shared/CommonHooks.java | 7 +++++ .../computercraft/gametest/Details_Test.kt | 28 +++++++++++++++++++ .../computercraft/gametest/core/TestHooks.kt | 1 + .../computercraft/shared/ComputerCraft.java | 1 + .../shared/ForgeCommonHooks.java | 6 ++++ 5 files changed, 43 insertions(+) create mode 100644 projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Details_Test.kt diff --git a/projects/common/src/main/java/dan200/computercraft/shared/CommonHooks.java b/projects/common/src/main/java/dan200/computercraft/shared/CommonHooks.java index a1d12c9da..a2928ad9c 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/CommonHooks.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/CommonHooks.java @@ -21,6 +21,7 @@ import net.minecraft.server.dedicated.DedicatedServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.packs.resources.PreparableReloadListener; import net.minecraft.world.entity.Entity; +import net.minecraft.world.item.CreativeModeTabs; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.world.level.storage.loot.BuiltInLootTables; @@ -63,6 +64,12 @@ public final class CommonHooks { ComputerMBean.start(server); } + public static void onServerStarted(MinecraftServer server) { + // ItemDetails requires creative tabs to be populated, however by default this is done lazily on the client and + // not at all on the server! We instead do this once on server startup. + CreativeModeTabs.tryRebuildTabContents(server.getWorldData().enabledFeatures(), false); + } + public static void onServerStopped() { resetState(); } diff --git a/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Details_Test.kt b/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Details_Test.kt new file mode 100644 index 000000000..59fc12eaf --- /dev/null +++ b/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Details_Test.kt @@ -0,0 +1,28 @@ +package dan200.computercraft.gametest + +import dan200.computercraft.api.detail.VanillaDetailRegistries +import dan200.computercraft.gametest.api.Structures +import dan200.computercraft.gametest.api.sequence +import net.minecraft.gametest.framework.GameTest +import net.minecraft.gametest.framework.GameTestHelper +import net.minecraft.world.item.ItemStack +import net.minecraft.world.item.Items +import org.junit.jupiter.api.Assertions.assertEquals + +class Details_Test { + @GameTest(template = Structures.DEFAULT) + fun Has_item_groups(helper: GameTestHelper) = helper.sequence { + thenExecute { + val details = VanillaDetailRegistries.ITEM_STACK.getDetails(ItemStack(Items.DIRT)) + assertEquals( + listOf( + mapOf( + "displayName" to "Natural Blocks", + "id" to "minecraft:natural", + ), + ), + details["itemGroups"], + ) + } + } +} diff --git a/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/core/TestHooks.kt b/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/core/TestHooks.kt index 25f351bb9..f9a7c2ea5 100644 --- a/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/core/TestHooks.kt +++ b/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/core/TestHooks.kt @@ -73,6 +73,7 @@ object TestHooks { private val testClasses = listOf( Computer_Test::class.java, CraftOs_Test::class.java, + Details_Test::class.java, Disk_Drive_Test::class.java, Inventory_Test::class.java, Loot_Test::class.java, diff --git a/projects/fabric/src/main/java/dan200/computercraft/shared/ComputerCraft.java b/projects/fabric/src/main/java/dan200/computercraft/shared/ComputerCraft.java index 9bbc38690..ae2bbb472 100644 --- a/projects/fabric/src/main/java/dan200/computercraft/shared/ComputerCraft.java +++ b/projects/fabric/src/main/java/dan200/computercraft/shared/ComputerCraft.java @@ -82,6 +82,7 @@ public class ComputerCraft { // Register hooks ServerLifecycleEvents.SERVER_STARTING.register(CommonHooks::onServerStarting); + ServerLifecycleEvents.SERVER_STARTED.register(CommonHooks::onServerStarted); ServerLifecycleEvents.SERVER_STOPPED.register(s -> CommonHooks.onServerStopped()); ServerLifecycleEvents.SYNC_DATA_PACK_CONTENTS.register((player, joined) -> PlatformHelper.get().sendToPlayer(new UpgradesLoadedMessage(), player)); diff --git a/projects/forge/src/main/java/dan200/computercraft/shared/ForgeCommonHooks.java b/projects/forge/src/main/java/dan200/computercraft/shared/ForgeCommonHooks.java index 70c48915a..bd8903873 100644 --- a/projects/forge/src/main/java/dan200/computercraft/shared/ForgeCommonHooks.java +++ b/projects/forge/src/main/java/dan200/computercraft/shared/ForgeCommonHooks.java @@ -29,6 +29,7 @@ import net.minecraftforge.event.*; import net.minecraftforge.event.entity.EntityJoinLevelEvent; import net.minecraftforge.event.entity.living.LivingDropsEvent; import net.minecraftforge.event.level.ChunkWatchEvent; +import net.minecraftforge.event.server.ServerStartedEvent; import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.event.server.ServerStoppedEvent; import net.minecraftforge.eventbus.api.EventPriority; @@ -58,6 +59,11 @@ public class ForgeCommonHooks { CommonHooks.onServerStarting(event.getServer()); } + @SubscribeEvent + public static void onServerStarted(ServerStartedEvent event) { + CommonHooks.onServerStarted(event.getServer()); + } + @SubscribeEvent public static void onServerStopped(ServerStoppedEvent event) { CommonHooks.onServerStopped();