Add a tooltip event to fix the tooltips

This commit is contained in:
modmuss50 2019-06-12 22:27:09 +01:00
parent 8d859bd77b
commit 4479116d1c
5 changed files with 131 additions and 115 deletions

View file

@ -77,7 +77,7 @@ dependencies {
mappings "net.fabricmc:yarn:1.14.2 Pre-Release 1+build.1"
modCompile "net.fabricmc:fabric-loader:0.4.7+build.147"
modCompile "net.fabricmc.fabric-api:fabric-api:0.3.0-pre+build.156"
modCompile "net.fabricmc.fabric-api:fabric-api:0.3.0+build.183"
modCompile "io.github.prospector.modmenu:ModMenu:1.5.4-85"

View file

@ -36,12 +36,14 @@ import reborncore.common.util.Torus;
import techreborn.api.TechRebornAPI;
import techreborn.client.GuiHandler;
import techreborn.events.ModRegistry;
import techreborn.events.StackToolTipHandler;
import techreborn.init.*;
import techreborn.packets.ClientboundPackets;
import techreborn.packets.ServerboundPackets;
import techreborn.proxies.CommonProxy;
import techreborn.tiles.fusionReactor.TileFusionControlComputer;
import techreborn.utils.BehaviorDispenseScrapbox;
import techreborn.utils.StackWIPHandler;
public class TechReborn implements ModInitializer {
@ -58,7 +60,6 @@ public class TechReborn implements ModInitializer {
RegistrationManager registrationManager = new RegistrationManager("techreborn");
//CommonProxy.isChiselAround = Loader.isModLoaded("ctm");
TechRebornAPI.subItemRetriever = new SubItemRetriever();
//Done like this to load them here
ModFluids.values();
@ -81,12 +82,15 @@ public class TechReborn implements ModInitializer {
ModSounds.init();
// Client only init, needs to be done before parts system
proxy.init();
StackToolTipHandler.setup();
StackWIPHandler.setup();
// WorldGen
//GameRegistry.registerWorldGenerator(worldGen, 0);
//GameRegistry.registerWorldGenerator(new OilLakeGenerator(), 0);
// Register Gui Handler
// Event busses
// MinecraftForge.EVENT_BUS.register(new StackWIPHandler());
// MinecraftForge.EVENT_BUS.register(new BlockBreakHandler());
// MinecraftForge.EVENT_BUS.register(new TRRecipeHandler());
// MinecraftForge.EVENT_BUS.register(new MultiblockEventHandler());

View file

@ -1,95 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.events;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
public class StackToolTipEvent {
@SuppressWarnings("deprecation")
@Environment(EnvType.CLIENT)
// public void handleItemTooltipEvent(ItemTooltipEvent event) {
// if (event.getEntityPlayer() == null) {
// return;
// }
// Item item = event.getItemStack().getItem();
// if (item instanceof IListInfoProvider) {
// ((IListInfoProvider) item).addInfo(event.getToolTip(), false, false);
// } else if (event.getItemStack().getItem() instanceof IEnergyItemInfo) {
// IEnergyStorage capEnergy = new ItemPowerManager(event.getItemStack());
// TextComponent line1 = new TextComponent(PowerSystem
// .getLocaliszedPowerFormattedNoSuffix(capEnergy.getEnergyStored() / RebornCoreConfig.euPerFU));
// line1.append("/");
// line1.append(PowerSystem
// .getLocaliszedPowerFormattedNoSuffix(capEnergy.getMaxEnergyStored() / RebornCoreConfig.euPerFU));
// line1.append(" ");
// line1.append(PowerSystem.getDisplayPower().abbreviation);
// line1.applyFormat(ChatFormat.GOLD);
//
// event.getToolTip().add(1, line1);
//
// if (InputUtil.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT)
// || InputUtil.isKeyDown(GLFW.GLFW_KEY_RIGHT_SHIFT)) {
// int percentage = percentage(capEnergy.getMaxEnergyStored(), capEnergy.getEnergyStored());
// ChatFormat color = StringUtils.getPercentageColour(percentage);
// event.getToolTip().add(2,
// new TextComponent(color + "" + percentage + "%" + ChatFormat.GRAY + " Charged"));
//// TODO: show both input and output rates
// event.getToolTip().add(3, new TextComponent(ChatFormat.GRAY + "I/O Rate: "
// + ChatFormat.GOLD
// + PowerSystem.getLocaliszedPowerFormatted((int) ((IEnergyItemInfo) item).getMaxInput())));
// }
// } else {
// try {
// Block block = Block.getBlockFromItem(item);
// if (block != null && (block instanceof BlockWithEntity || block instanceof BlockEntityProvider)
// && block.getRegistryName().getNamespace().contains("techreborn")) {
// BlockEntity tile = block.createTileEntity(block.getDefaultState(), MinecraftClient.getInstance().world);
// boolean hasData = false;
// if (event.getItemStack().hasTag() && event.getItemStack().getTag().contains("tile_data")) {
// CompoundTag tileData = event.getItemStack().getTag().getCompound("tile_data");
// tile.fromTag(tileData);
// hasData = true;
// event.getToolTip()
// .add(new TextComponent("Block data contained").applyFormat(ChatFormat.DARK_GREEN));
// }
// if (tile instanceof IListInfoProvider) {
// ((IListInfoProvider) tile).addInfo(event.getToolTip(), false, hasData);
// }
// }
// } catch (NullPointerException e) {
// TechReborn.LOGGER.debug("Failed to load info for " + event.getItemStack().getDisplayName());
// }
// }
// }
public int percentage(int MaxValue, int CurrentValue) {
if (CurrentValue == 0)
return 0;
return (int) ((CurrentValue * 100.0f) / MaxValue);
}
}

View file

@ -0,0 +1,109 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.events;
import net.minecraft.ChatFormat;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.util.registry.Registry;
import reborncore.api.IListInfoProvider;
import reborncore.api.events.ItemTooltipCallback;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.api.power.ItemPowerManager;
import reborncore.common.RebornCoreConfig;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.util.StringUtils;
import techreborn.TechReborn;
import java.util.List;
public class StackToolTipHandler implements ItemTooltipCallback {
public static void setup() {
ItemTooltipCallback.EVENT.register(new StackToolTipHandler());
}
@Override
public void getTooltip(ItemStack stack, TooltipContext tooltipContext, List<Component> components) {
Item item = stack.getItem();
if (item instanceof IListInfoProvider) {
((IListInfoProvider) item).addInfo(components, false, false);
} else if (stack.getItem() instanceof IEnergyItemInfo) {
ItemPowerManager itemPowerManager = new ItemPowerManager(stack);
TextComponent line1 = new TextComponent(PowerSystem.getLocaliszedPowerFormattedNoSuffix(itemPowerManager.getEnergyStored() / RebornCoreConfig.euPerFU));
line1.append("/");
line1.append(PowerSystem.getLocaliszedPowerFormattedNoSuffix(itemPowerManager.getMaxEnergyStored() / RebornCoreConfig.euPerFU));
line1.append(" ");
line1.append(PowerSystem.getDisplayPower().abbreviation);
line1.applyFormat(ChatFormat.GOLD);
components.add(1, line1);
if (Screen.hasShiftDown()) {
int percentage = percentage(itemPowerManager.getMaxEnergyStored(), itemPowerManager.getEnergyStored());
ChatFormat color = StringUtils.getPercentageColour(percentage);
components.add(2, new TextComponent(color + "" + percentage + "%" + ChatFormat.GRAY + " Charged"));
// TODO: show both input and output rates
components.add(3, new TextComponent(ChatFormat.GRAY + "I/O Rate: " + ChatFormat.GOLD + PowerSystem.getLocaliszedPowerFormatted(((IEnergyItemInfo) item).getMaxInput())));
}
} else {
try {
Block block = Block.getBlockFromItem(item);
if (block != null && (block instanceof BlockWithEntity || block instanceof BlockEntityProvider) && Registry.BLOCK.getId(block).getNamespace().contains("techreborn")) {
BlockEntity tile = ((BlockEntityProvider) block).createBlockEntity(MinecraftClient.getInstance().world);
boolean hasData = false;
if (stack.hasTag() && stack.getTag().containsKey("tile_data")) {
CompoundTag tileData = stack.getTag().getCompound("tile_data");
tile.fromTag(tileData);
hasData = true;
components.add(new TextComponent("Block data contained").applyFormat(ChatFormat.DARK_GREEN));
}
if (tile instanceof IListInfoProvider) {
((IListInfoProvider) tile).addInfo(components, false, hasData);
}
}
} catch (NullPointerException e) {
TechReborn.LOGGER.debug("Failed to load info for " + stack.getDisplayName());
}
}
}
public int percentage(int MaxValue, int CurrentValue) {
if (CurrentValue == 0)
return 0;
return (int) ((CurrentValue * 100.0f) / MaxValue);
}
}

View file

@ -24,8 +24,6 @@
package techreborn.utils;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.ChatFormat;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
@ -33,6 +31,7 @@ import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.network.chat.TextComponent;
import reborncore.api.events.ItemTooltipCallback;
import reborncore.common.util.StringUtils;
import techreborn.init.TRContent;
@ -42,10 +41,10 @@ import java.util.ArrayList;
* Created by Mark on 23/03/2016.
*/
public class StackWIPHandler {
ArrayList<Block> wipBlocks = new ArrayList<>();
private static ArrayList<Block> wipBlocks = new ArrayList<>();
public static ArrayList<ItemStack> devHeads = new ArrayList<>();
public StackWIPHandler() {
public static void setup() {
wipBlocks.add(TRContent.Machine.MAGIC_ENERGY_ABSORBER.block);
wipBlocks.add(TRContent.Machine.CHUNK_LOADER.block);
wipBlocks.add(TRContent.Machine.MAGIC_ENERGY_CONVERTER.block);
@ -54,25 +53,24 @@ public class StackWIPHandler {
addHead("Gigabit101");
addHead("ProfProspector");
addHead("Rushmead");
ItemTooltipCallback.EVENT.register((stack, tooltipContext, components) -> {
Block block = Block.getBlockFromItem(stack.getItem());
if (block != null && wipBlocks.contains(block)) {
components.add(new TextComponent(ChatFormat.RED + StringUtils.t("techreborn.tooltip.wip")));
}
if (devHeads.contains(stack)) {
components.add(new TextComponent(ChatFormat.GOLD + "TechReborn Developer"));
}
});
}
private void addHead(String name) {
private static void addHead(String name) {
ItemStack head = new ItemStack(Items.PLAYER_HEAD, 3);
head.setTag(new CompoundTag());
head.getTag().put("SkullOwner", new StringTag(name));
devHeads.add(head);
}
// @Environment(EnvType.CLIENT)
// @SubscribeEvent
// public void toolTip(ItemTooltipEvent event) {
// Block block = Block.getBlockFromItem(event.getItemStack().getItem());
// if (block != null && wipBlocks.contains(block)) {
// event.getToolTip().add(new TextComponent(ChatFormat.RED + StringUtils.t("techreborn.tooltip.wip")));
// }
//
// if (devHeads.contains(event.getItemStack())) {
// event.getToolTip().add(new TextComponent(ChatFormat.GOLD + "TechReborn Developer"));
// }
// }
}