Skip to content

Commit

Permalink
add tooltip for coils and moderators
Browse files Browse the repository at this point in the history
  • Loading branch information
RogueLogix committed Dec 30, 2022
1 parent 9a24300 commit a63a6aa
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Block> 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<String>();
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<String>) 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")));
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Block> 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<String>();
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<String>) 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")));
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/assets/biggerreactors/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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).",
Expand Down

0 comments on commit a63a6aa

Please sign in to comment.