From a63a6aa46d79adc59df2d1fb8c4dd6d85e5bb830 Mon Sep 17 00:00:00 2001 From: RogueLogix Date: Thu, 29 Dec 2022 23:34:52 -0800 Subject: [PATCH] add tooltip for coils and moderators #3 --- Phosphophyllite | 2 +- .../biggerreactors/BiggerReactors.java | 1 + .../registries/ReactorModeratorRegistry.java | 92 ++++++++++++++++++- .../registries/TurbineCoilRegistry.java | 86 ++++++++++++++++- .../assets/biggerreactors/lang/en_us.json | 3 + 5 files changed, 181 insertions(+), 3 deletions(-) diff --git a/Phosphophyllite b/Phosphophyllite index 688fb024..75743ee0 160000 --- a/Phosphophyllite +++ b/Phosphophyllite @@ -1 +1 @@ -Subproject commit 688fb0247135a39d50c27d549b92bc1b01e7afcd +Subproject commit 75743ee0c42f3aab66b94d22a7af62519dcda44b diff --git a/src/main/java/net/roguelogix/biggerreactors/BiggerReactors.java b/src/main/java/net/roguelogix/biggerreactors/BiggerReactors.java index 8fefb08f..8c1eb759 100644 --- a/src/main/java/net/roguelogix/biggerreactors/BiggerReactors.java +++ b/src/main/java/net/roguelogix/biggerreactors/BiggerReactors.java @@ -38,6 +38,7 @@ public class BiggerReactors { public static final String modid = "biggerreactors"; public static final Logger LOGGER = LogManager.getLogger(); + public static final boolean LOG_DEBUG = LOGGER.isDebugEnabled(); public BiggerReactors() { new Registry(); diff --git a/src/main/java/net/roguelogix/biggerreactors/registries/ReactorModeratorRegistry.java b/src/main/java/net/roguelogix/biggerreactors/registries/ReactorModeratorRegistry.java index d6f1806c..a64d440a 100644 --- a/src/main/java/net/roguelogix/biggerreactors/registries/ReactorModeratorRegistry.java +++ b/src/main/java/net/roguelogix/biggerreactors/registries/ReactorModeratorRegistry.java @@ -1,15 +1,27 @@ package net.roguelogix.biggerreactors.registries; -import net.minecraft.core.Registry; +import com.mojang.datafixers.util.Either; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.BucketItem; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.material.Fluid; +import net.minecraftforge.client.event.RenderTooltipEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.OnDatapackSyncEvent; +import net.minecraftforge.fml.loading.FMLEnvironment; import net.minecraftforge.registries.ForgeRegistries; import net.roguelogix.biggerreactors.BiggerReactors; import net.roguelogix.phosphophyllite.data.DataLoader; +import net.roguelogix.phosphophyllite.networking.SimplePhosChannel; +import net.roguelogix.phosphophyllite.registry.OnModLoad; import net.roguelogix.phosphophyllite.robn.ROBNObject; +import net.roguelogix.phosphophyllite.serialization.PhosphophylliteCompound; import org.apache.commons.lang3.NotImplementedException; import java.util.Collections; @@ -171,4 +183,82 @@ public static void loadRegistry() { } BiggerReactors.LOGGER.info("Loaded " + registry.size() + " moderator entries"); } + + public static class Client { + + private static final SimplePhosChannel CHANNEL = new SimplePhosChannel(new ResourceLocation(BiggerReactors.modid, "moderator_sync_channel"), "0", Client::readSync); + private static final ObjectOpenHashSet moderatorBlocks = new ObjectOpenHashSet<>(); + + @OnModLoad(required = true) + private static void onModLoad() { + MinecraftForge.EVENT_BUS.addListener(Client::datapackEvent); + if (FMLEnvironment.dist.isClient()) { + MinecraftForge.EVENT_BUS.addListener(Client::toolTipEvent); + } + } + + public static void datapackEvent(OnDatapackSyncEvent e) { + final var player = e.getPlayer(); + if (player == null) { + return; + } + + if (BiggerReactors.LOG_DEBUG) { + BiggerReactors.LOGGER.debug("Sending moderator list to player: " + player); + } + CHANNEL.sendToPlayer(player, writeSync()); + } + + private static PhosphophylliteCompound writeSync() { + final var list = new ObjectArrayList(); + for (final var value : registry.keySet()) { + final var location = ForgeRegistries.BLOCKS.getKey(value); + if (location == null) { + continue; + } + list.add(location.toString()); + } + final var compound = new PhosphophylliteCompound(); + compound.put("list", list); + return compound; + } + + private static void readSync(PhosphophylliteCompound compound) { + moderatorBlocks.clear(); + //noinspection unchecked + final var list = (List) compound.getList("list"); + if (BiggerReactors.LOG_DEBUG) { + BiggerReactors.LOGGER.debug("Received moderator list from server with length of " + list.size()); + } + for (final var value : list) { + final var block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(value)); + if (block == null) { + return; + } + if (BiggerReactors.LOG_DEBUG) { + BiggerReactors.LOGGER.debug("Block " + value + " added as moderator on client"); + } + moderatorBlocks.add(block); + } + } + + public static void toolTipEvent(RenderTooltipEvent.GatherComponents event) { + // TODO: sync this, currently reaching across sides + // there is an event for doing that + final var item = event.getItemStack().getItem(); + if (item instanceof BlockItem blockItem) { + if (!moderatorBlocks.contains(blockItem.getBlock())) { + return; + } + } else if (item instanceof BucketItem bucketItem) { + final var fluidBlock = bucketItem.getFluid().defaultFluidState().createLegacyBlock().getBlock(); + if (fluidBlock.defaultBlockState().isAir() || !moderatorBlocks.contains(fluidBlock)) { + return; + } + } else { + return; + } + event.getTooltipElements().add(Either.left(Component.translatable("tooltip.biggerreactors.is_a_moderator"))); + } + } } diff --git a/src/main/java/net/roguelogix/biggerreactors/registries/TurbineCoilRegistry.java b/src/main/java/net/roguelogix/biggerreactors/registries/TurbineCoilRegistry.java index 13be3f48..3544135b 100644 --- a/src/main/java/net/roguelogix/biggerreactors/registries/TurbineCoilRegistry.java +++ b/src/main/java/net/roguelogix/biggerreactors/registries/TurbineCoilRegistry.java @@ -1,13 +1,24 @@ package net.roguelogix.biggerreactors.registries; -import net.minecraft.core.Registry; +import com.mojang.datafixers.util.Either; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; +import net.minecraft.world.item.BlockItem; import net.minecraft.world.level.block.Block; +import net.minecraftforge.client.event.RenderTooltipEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.OnDatapackSyncEvent; +import net.minecraftforge.fml.loading.FMLEnvironment; import net.minecraftforge.registries.ForgeRegistries; import net.roguelogix.biggerreactors.BiggerReactors; import net.roguelogix.phosphophyllite.data.DataLoader; +import net.roguelogix.phosphophyllite.networking.SimplePhosChannel; +import net.roguelogix.phosphophyllite.registry.OnModLoad; +import net.roguelogix.phosphophyllite.serialization.PhosphophylliteCompound; import java.util.Collections; import java.util.HashMap; @@ -89,4 +100,77 @@ public static void loadRegistry() { } BiggerReactors.LOGGER.info("Loaded " + registry.size() + " coil entries"); } + + public static class Client { + + private static final SimplePhosChannel CHANNEL = new SimplePhosChannel(new ResourceLocation(BiggerReactors.modid, "coil_sync_channel"), "0", Client::readSync); + private static final ObjectOpenHashSet coilBlocks = new ObjectOpenHashSet<>(); + + @OnModLoad(required = true) + private static void onModLoad() { + MinecraftForge.EVENT_BUS.addListener(Client::datapackEvent); + if (FMLEnvironment.dist.isClient()) { + MinecraftForge.EVENT_BUS.addListener(Client::toolTipEvent); + } + } + + public static void datapackEvent(OnDatapackSyncEvent e) { + final var player = e.getPlayer(); + if (player == null) { + return; + } + + if (BiggerReactors.LOG_DEBUG) { + BiggerReactors.LOGGER.debug("Sending coil list to player: " + player); + } + CHANNEL.sendToPlayer(player, writeSync()); + } + + private static PhosphophylliteCompound writeSync() { + final var list = new ObjectArrayList(); + for (final var value : registry.keySet()) { + final var location = ForgeRegistries.BLOCKS.getKey(value); + if (location == null) { + continue; + } + list.add(location.toString()); + } + final var compound = new PhosphophylliteCompound(); + compound.put("list", list); + return compound; + } + + private static void readSync(PhosphophylliteCompound compound) { + coilBlocks.clear(); + //noinspection unchecked + final var list = (List) compound.getList("list"); + if (BiggerReactors.LOG_DEBUG) { + BiggerReactors.LOGGER.debug("Received coil list from server with length of " + list.size()); + } + for (final var value : list) { + final var block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(value)); + if (block == null) { + return; + } + if (BiggerReactors.LOG_DEBUG) { + BiggerReactors.LOGGER.debug("Block " + value + " added as coil on client"); + } + coilBlocks.add(block); + } + } + + public static void toolTipEvent(RenderTooltipEvent.GatherComponents event) { + // TODO: sync this, currently reaching across sides + // there is an event for doing that + final var item = event.getItemStack().getItem(); + if (item instanceof BlockItem blockItem) { + if (!coilBlocks.contains(blockItem.getBlock())) { + return; + } + } else { + return; + } + event.getTooltipElements().add(Either.left(Component.translatable("tooltip.biggerreactors.is_a_coil"))); + } + } } diff --git a/src/main/resources/assets/biggerreactors/lang/en_us.json b/src/main/resources/assets/biggerreactors/lang/en_us.json index 735ff180..64bef15a 100644 --- a/src/main/resources/assets/biggerreactors/lang/en_us.json +++ b/src/main/resources/assets/biggerreactors/lang/en_us.json @@ -34,6 +34,9 @@ "item.biggerreactors.ludicrite_dust": "Ludicrite Dust", "item.biggerreactors.ludicrite_ingot": "Ludicrite Ingot", + "tooltip.biggerreactors.is_a_moderator" : "§eThis can be used as a moderator in a Bigger Reactor", + "tooltip.biggerreactors.is_a_coil" : "§eThis block can be used as a coil in a Bigger Turbine", + "multiblock.error.biggerreactors.no_terminal": "Reactors require at least one terminal.", "multiblock.error.biggerreactors.no_rods": "Reactors must have at least one fuel element.", "multiblock.error.biggerreactors.control_rod_not_on_top": "Control rods can only be placed on the top of the reactor (%d, %d, %d).",