Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements and code organization changes #45

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/main/java/com/snowshock35/jeiintegration/JEIIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,24 @@

@Mod(JEIIntegration.MOD_ID)
public class JEIIntegration {
public static final String MOD_ID = "jeiintegration";
public static final String MOD_ID = "jeiintegration";

public static Logger logger = LogManager.getLogger();;
public static final Logger logger = LogManager.getLogger();

public JEIIntegration() {
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, Config.clientSpec);
FMLJavaModLoadingContext.get().getModEventBus().register(Config.class);
public JEIIntegration() {
TooltipModuleManager tooltipModuleManager = TooltipModuleManager.create();
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, tooltipModuleManager.getSpec());
FMLJavaModLoadingContext.get().getModEventBus().register(tooltipModuleManager.getConfig());

DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
MinecraftForge.EVENT_BUS.register(new TooltipEventHandler());
});
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
MinecraftForge.EVENT_BUS.register(new TooltipEventHandler(tooltipModuleManager));
});

//Make sure the mod being absent on the other network side does not cause the client to display the server as incompatible
ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest(() -> NetworkConstants.IGNORESERVERONLY, (a, b) -> true));
}
// Make sure the mod being absent on the other network side does not cause the client to display the server as
// incompatible
ModLoadingContext
.get()
.registerExtensionPoint(IExtensionPoint.DisplayTest.class,
() -> new IExtensionPoint.DisplayTest(() -> NetworkConstants.IGNORESERVERONLY, (a, b) -> true));
}
}
276 changes: 57 additions & 219 deletions src/main/java/com/snowshock35/jeiintegration/TooltipEventHandler.java
Original file line number Diff line number Diff line change
@@ -1,219 +1,57 @@
/*
* MIT License
*
* Copyright (c) 2020 SnowShock35
*
* 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 com.snowshock35.jeiintegration;

import com.mojang.blaze3d.platform.InputConstants;
import com.snowshock35.jeiintegration.config.Config;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.registries.ForgeRegistries;
import org.apache.logging.log4j.Level;
import org.lwjgl.glfw.GLFW;

import java.text.DecimalFormat;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import static net.minecraftforge.common.ForgeHooks.getBurnTime;

public class TooltipEventHandler {

private Config.Client config = Config.CLIENT;

private static boolean isDebugMode() {
return Minecraft.getInstance().options.advancedItemTooltips;
}

private static boolean isShiftKeyDown() {
return InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_KEY_LEFT_SHIFT) ||
InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_KEY_RIGHT_SHIFT);
}

private void registerTooltip(ItemTooltipEvent e, Component tooltip, String configOption) {
boolean isEnabled = false;

if (Objects.equals(configOption, "enabled")) {
isEnabled = true;
} else if (
Objects.equals(configOption, "onShift")
&& isShiftKeyDown()
) {
isEnabled = true;
} else if (
Objects.equals(configOption, "onDebug")
&& isDebugMode()
) {
isEnabled = true;
} else if (
Objects.equals(configOption, "onShiftAndDebug")
&& isShiftKeyDown()
&& isDebugMode()
) {
isEnabled = true;
}
if (isEnabled) {
e.getToolTip().add(tooltip);
}
}

private void registerTooltips(ItemTooltipEvent e, Collection<Component> tooltips, String configValue) {
for (Component tooltip : tooltips) {
registerTooltip(e, tooltip, configValue);
}
}

@SubscribeEvent
public void onItemTooltip(ItemTooltipEvent e) {

// Set number formatting to display large numbers more clearly
DecimalFormat decimalFormat = new DecimalFormat("#.##");
decimalFormat.setGroupingUsed(true);
decimalFormat.setGroupingSize(3);

// Retrieve the ItemStack and Item
ItemStack itemStack = e.getItemStack();
Item item = itemStack.getItem();

// If item stack empty do nothing
if (e.getItemStack().isEmpty()) {
return;
}

// Tooltip - Burn Time
int burnTime = 0;
try {
burnTime = getBurnTime(itemStack, RecipeType.SMELTING);
} catch (Exception ex) {
JEIIntegration.logger.log(Level.WARN, "):\n\nSomething went wrong!");
}

if (burnTime > 0) {
Component burnTooltip = Component.translatable("tooltip.jeiintegration.burnTime")
.append(Component.literal(" " + decimalFormat.format(burnTime) + " "))
.append(Component.translatable("tooltip.jeiintegration.burnTime.suffix"))
.withStyle(ChatFormatting.DARK_GRAY);

registerTooltip(e, burnTooltip, config.burnTimeTooltipMode.get());
}

// Tooltip - Durability
int maxDamage = itemStack.getMaxDamage();
int currentDamage = maxDamage - itemStack.getDamageValue();
if (maxDamage > 0) {
Component durabilityTooltip = Component.translatable("tooltip.jeiintegration.durability")
.append(Component.literal(" " + currentDamage + "/" + maxDamage))
.withStyle(ChatFormatting.DARK_GRAY);

registerTooltip(e, durabilityTooltip, config.durabilityTooltipMode.get());
}

// Tooltip - Enchantability
int enchantability = item.getEnchantmentValue(itemStack);
if (enchantability > 0) {
Component enchantabilityTooltip = Component.translatable("tooltip.jeiintegration.enchantability")
.append(Component.literal(" " + enchantability))
.withStyle(ChatFormatting.DARK_GRAY);

registerTooltip(e, enchantabilityTooltip, config.enchantabilityTooltipMode.get());
}

// Tooltip - Hunger / Saturation
FoodProperties foodProperties = item.getFoodProperties(itemStack, Minecraft.getInstance().player);
if (item.isEdible() && foodProperties != null) {
int healVal = foodProperties.getNutrition();
float satVal = healVal * (foodProperties.getSaturationModifier() * 2);

Component foodTooltip = Component.translatable("tooltip.jeiintegration.hunger")
.append(Component.literal(" " + healVal + " "))
.append(Component.translatable("tooltip.jeiintegration.saturation"))
.append(Component.literal(" " + decimalFormat.format(satVal)))
.withStyle(ChatFormatting.DARK_GRAY);

registerTooltip(e, foodTooltip, config.foodTooltipMode.get());
}

// Tooltip - NBT Data
CompoundTag nbtData = item.getShareTag(itemStack);
if (nbtData != null) {
Component nbtTooltip = Component.translatable("tooltip.jeiintegration.nbtTagData")
.append(Component.literal(" " + nbtData))
.withStyle(ChatFormatting.DARK_GRAY);

registerTooltip(e, nbtTooltip, config.nbtTooltipMode.get());
}

// Tooltip - Registry Name
Component registryTooltip = Component.translatable("tooltip.jeiintegration.registryName")
.append(Component.literal(" " + ForgeRegistries.ITEMS.getKey(item)))
.withStyle(ChatFormatting.DARK_GRAY);

registerTooltip(e, registryTooltip, config.registryNameTooltipMode.get());


// Tooltip - Max Stack Size
int stackSize = e.getItemStack().getMaxStackSize();
if (stackSize > 0) {
Component stackSizeTooltip = Component.translatable("tooltip.jeiintegration.maxStackSize")
.append(Component.literal(" " + itemStack.getMaxStackSize()))
.withStyle(ChatFormatting.DARK_GRAY);

registerTooltip(e, stackSizeTooltip, config.maxStackSizeTooltipMode.get());
}

// Tooltip - Tags
if (itemStack.getTags().toList().size() > 0) {
Component tagsTooltip = Component.translatable("tooltip.jeiintegration.tags")
.withStyle(ChatFormatting.DARK_GRAY);

Set<Component> tags = new HashSet<>();

for (ResourceLocation tag : itemStack.getTags().map(TagKey::location).toList()) {
tags.add(Component.literal(" " + tag).withStyle(ChatFormatting.DARK_GRAY));
}

registerTooltip(e, tagsTooltip, config.tagsTooltipMode.get());
registerTooltips(e, tags, config.tagsTooltipMode.get());
}

// Tooltip - Translation Key
Component translationKeyTooltip = Component.translatable("tooltip.jeiintegration.translationKey")
.append(Component.literal(" " + itemStack.getDescriptionId()))
.withStyle(ChatFormatting.DARK_GRAY);

registerTooltip(e, translationKeyTooltip, config.translationKeyTooltipMode.get());
}
}
package com.snowshock35.jeiintegration;

import com.mojang.blaze3d.platform.InputConstants;
import com.snowshock35.jeiintegration.config.OptionState;
import com.snowshock35.jeiintegration.modules.TooltipModuleHolder;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.lwjgl.glfw.GLFW;

public class TooltipEventHandler {
private final TooltipModuleManager tooltipModuleManager;

public TooltipEventHandler(TooltipModuleManager tooltipModuleManager) {
this.tooltipModuleManager = tooltipModuleManager;
}


private static boolean isDebugMode() {
return Minecraft.getInstance().options.advancedItemTooltips;
}

private static boolean isShiftKeyDown() {
return InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_KEY_LEFT_SHIFT) ||
InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_KEY_RIGHT_SHIFT);
}

private static boolean isEnabled(ForgeConfigSpec.ConfigValue<OptionState> configValue) {
OptionState configOption = configValue.get();
boolean isEnabled = false;

if (configOption == OptionState.ENABLED) {
isEnabled = true;
} else if (configOption == OptionState.ON_SHIFT && isShiftKeyDown()) {
isEnabled = true;
} else if (configOption == OptionState.ON_DEBUG && isDebugMode()) {
isEnabled = true;
} else if (configOption == OptionState.ON_SHIFT_AND_DEBUG && isShiftKeyDown() && isDebugMode()) {
isEnabled = true;
}
return isEnabled;
}

@SubscribeEvent
public void onItemTooltip(ItemTooltipEvent e) {
if (e.getItemStack().isEmpty()) {
return;
}

for (TooltipModuleHolder module : tooltipModuleManager.getEnabledModules()) {
if (isEnabled(module.config())) {
module.definition().apply(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.snowshock35.jeiintegration;

import com.snowshock35.jeiintegration.config.Config;
import com.snowshock35.jeiintegration.modules.*;
import net.minecraftforge.common.ForgeConfigSpec;
import org.apache.commons.lang3.tuple.Pair;

import java.util.List;

public class TooltipModuleManager {
private final ForgeConfigSpec spec;
private final Config config;

public static TooltipModuleManager create() {
List<TooltipModule> modules = List.of(new BurnTimeModule(), new DurabilityModule(),
new EnchantabilityModule(), new HungerModule(), new NbtDataModule(), new RegistryNameModule(),
new MaxStackSizeModule(), new TagsModule(), new TranslationModule());

Pair<Config, ForgeConfigSpec> specPair =
new ForgeConfigSpec.Builder().configure(builder -> new Config(builder, modules));
return new TooltipModuleManager(specPair.getRight(), specPair.getLeft());
}

private TooltipModuleManager(ForgeConfigSpec spec, Config config) {
this.spec = spec;
this.config = config;
this.config.updateCache();
}

public ForgeConfigSpec getSpec() {
return spec;
}

public Config getConfig() {
return config;
}

public List<TooltipModuleHolder> getEnabledModules() {
return config.getEnabledModules();
}
}
Loading