From f7562db2e97abfbd9f5ed980bdf2714ce9d77a3e Mon Sep 17 00:00:00 2001 From: NotMyWing Date: Fri, 7 Jun 2024 17:09:18 +1100 Subject: [PATCH] Memoize isModLoaded lookups --- .../implementations/GuiInterfaceTerminal.java | 11 +- .../gui/implementations/GuiMEMonitorable.java | 12 +- .../ContainerMEMonitorable.java | 4 +- .../ContainerSecurityStation.java | 10 +- .../container/slot/SlotCraftingTerm.java | 17 +-- .../core/sync/packets/PacketTerminalUse.java | 3 +- .../java/appeng/helpers/DualityInterface.java | 22 ++-- .../java/appeng/helpers/NonBlockingItems.java | 107 +++++++++--------- .../appeng/integration/IntegrationNode.java | 4 +- .../bogosorter/InventoryBogoSortModule.java | 4 +- .../java/appeng/services/VersionChecker.java | 6 +- src/main/java/appeng/util/Platform.java | 29 +++-- 12 files changed, 98 insertions(+), 131 deletions(-) diff --git a/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java b/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java index 826370af0db..b78928e651e 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java +++ b/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java @@ -54,19 +54,12 @@ import net.minecraft.world.World; import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.util.Constants; -import net.minecraftforge.fml.common.Loader; import org.lwjgl.input.Mouse; import java.awt.*; import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.WeakHashMap; +import java.util.*; import static appeng.client.render.BlockPosHighlighter.hilightBlock; import static appeng.helpers.ItemStackHelper.stackFromNBT; @@ -116,7 +109,7 @@ public GuiInterfaceTerminal(final InventoryPlayer inventoryPlayer, final PartInt this.setScrollBar(scrollbar); this.xSize = 208; this.ySize = 255; - this.jeiEnabled = Loader.isModLoaded("jei"); + this.jeiEnabled = Platform.isModLoaded("jei"); this.jeiButtonPadding = jeiEnabled ? 22 : 0; searchFieldInputs = createTextField(86, 12, ButtonToolTips.SearchFieldInputs.getLocal()); diff --git a/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java b/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java index f413db00d54..8b5b4c015ae 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java +++ b/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java @@ -31,11 +31,7 @@ import appeng.api.util.IConfigurableObject; import appeng.client.ActionKey; import appeng.client.gui.AEBaseMEGui; -import appeng.client.gui.widgets.GuiImgButton; -import appeng.client.gui.widgets.GuiScrollbar; -import appeng.client.gui.widgets.GuiTabButton; -import appeng.client.gui.widgets.ISortSource; -import appeng.client.gui.widgets.MEGuiTextField; +import appeng.client.gui.widgets.*; import appeng.client.me.InternalSlotME; import appeng.client.me.ItemRepo; import appeng.client.me.SlotME; @@ -61,7 +57,6 @@ import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; -import net.minecraftforge.fml.common.Loader; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; @@ -103,7 +98,7 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi private int currentMouseY = 0; private boolean delayedUpdate; - protected int jeiOffset = Loader.isModLoaded("jei") ? 24 : 0; + protected int jeiOffset = Platform.isModLoaded("jei") ? 24 : 0; public GuiMEMonitorable(final InventoryPlayer inventoryPlayer, final ITerminalHost te) { this(inventoryPlayer, te, new ContainerMEMonitorable(inventoryPlayer, te)); @@ -177,10 +172,9 @@ protected void actionPerformed(final GuiButton btn) { NetworkHandler.instance().sendToServer(new PacketSwitchGuis(GuiBridge.GUI_CRAFTING_STATUS)); } - if (btn instanceof GuiImgButton) { + if (btn instanceof GuiImgButton iBtn) { final boolean backwards = Mouse.isButtonDown(1); - final GuiImgButton iBtn = (GuiImgButton) btn; if (iBtn.getSetting() != Settings.ACTIONS) { final Enum cv = iBtn.getCurrentValue(); final Enum next = Platform.rotateEnum(cv, backwards, iBtn.getSetting().getPossibleValues()); diff --git a/src/main/java/appeng/container/implementations/ContainerMEMonitorable.java b/src/main/java/appeng/container/implementations/ContainerMEMonitorable.java index 6d023a8aa7c..2a36921a671 100644 --- a/src/main/java/appeng/container/implementations/ContainerMEMonitorable.java +++ b/src/main/java/appeng/container/implementations/ContainerMEMonitorable.java @@ -46,7 +46,6 @@ import appeng.client.gui.implementations.GuiMEMonitorable; import appeng.container.AEBaseContainer; import appeng.container.guisync.GuiSync; -import appeng.container.slot.AppEngSlot; import appeng.container.slot.SlotPlayerHotBar; import appeng.container.slot.SlotPlayerInv; import appeng.container.slot.SlotRestrictedInput; @@ -66,7 +65,6 @@ import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; -import net.minecraftforge.fml.common.Loader; import net.minecraftforge.items.IItemHandler; import javax.annotation.Nonnull; @@ -89,7 +87,7 @@ public class ContainerMEMonitorable extends AEBaseContainer implements IConfigMa private IConfigManagerHost gui; private IConfigManager serverCM; private IGridNode networkNode; - protected int jeiOffset = Loader.isModLoaded("jei") ? 24 : 0; + protected int jeiOffset = Platform.isModLoaded("jei") ? 24 : 0; public ContainerMEMonitorable(final InventoryPlayer ip, final ITerminalHost monitorable) { diff --git a/src/main/java/appeng/container/implementations/ContainerSecurityStation.java b/src/main/java/appeng/container/implementations/ContainerSecurityStation.java index d7fcdfe3022..4a3596f1388 100644 --- a/src/main/java/appeng/container/implementations/ContainerSecurityStation.java +++ b/src/main/java/appeng/container/implementations/ContainerSecurityStation.java @@ -30,13 +30,13 @@ import appeng.container.slot.SlotRestrictedInput; import appeng.tile.inventory.AppEngInternalInventory; import appeng.tile.misc.TileSecurityStation; +import appeng.util.Platform; import appeng.util.inv.IAEAppEngInventory; import appeng.util.inv.InvOperation; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.IContainerListener; import net.minecraft.item.ItemStack; -import net.minecraftforge.fml.common.Loader; import net.minecraftforge.items.IItemHandler; @@ -53,7 +53,7 @@ public class ContainerSecurityStation extends ContainerMEMonitorable implements @GuiSync(0) public int permissionMode = 0; - protected int jeiOffset = Loader.isModLoaded("jei") ? 24 : 0; + protected int jeiOffset = Platform.isModLoaded("jei") ? 24 : 0; public ContainerSecurityStation(final InventoryPlayer ip, final ITerminalHost monitorable) { super(ip, monitorable, false); @@ -75,8 +75,7 @@ public void toggleSetting(final String value, final EntityPlayer player) { final SecurityPermissions permission = SecurityPermissions.valueOf(value); final ItemStack a = this.configSlot.getStack(); - if (!a.isEmpty() && a.getItem() instanceof IBiometricCard) { - final IBiometricCard bc = (IBiometricCard) a.getItem(); + if (!a.isEmpty() && a.getItem() instanceof IBiometricCard bc) { if (bc.hasPermission(a, permission)) { bc.removePermission(a, permission); } else { @@ -95,8 +94,7 @@ public void detectAndSendChanges() { this.setPermissionMode(0); final ItemStack a = this.configSlot.getStack(); - if (!a.isEmpty() && a.getItem() instanceof IBiometricCard) { - final IBiometricCard bc = (IBiometricCard) a.getItem(); + if (!a.isEmpty() && a.getItem() instanceof IBiometricCard bc) { for (final SecurityPermissions sp : bc.getPermissions(a)) { this.setPermissionMode(this.getPermissionMode() | (1 << sp.ordinal())); diff --git a/src/main/java/appeng/container/slot/SlotCraftingTerm.java b/src/main/java/appeng/container/slot/SlotCraftingTerm.java index e44f196cd01..df400231d9d 100644 --- a/src/main/java/appeng/container/slot/SlotCraftingTerm.java +++ b/src/main/java/appeng/container/slot/SlotCraftingTerm.java @@ -41,9 +41,6 @@ import appeng.util.inv.WrapperInvItemHandler; import appeng.util.item.AEItemStack; import com.blamejared.recipestages.recipes.RecipeStage; -import net.darkhax.gamestages.GameStageHelper; -import net.darkhax.itemstages.ItemStages; -import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.Item; @@ -53,9 +50,6 @@ import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import net.minecraftforge.fml.common.Loader; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.items.IItemHandler; import java.util.ArrayList; @@ -157,8 +151,7 @@ public void doClick(final InventoryAction action, final EntityPlayer who) { // TODO: This is really hacky and NEEDS to be solved with a full container/gui refactoring. protected IRecipe findRecipe(InventoryCrafting ic, World world, EntityPlayer player) { - if (this.container instanceof ContainerCraftingTerm) { - final ContainerCraftingTerm containerTerminal = (ContainerCraftingTerm) this.container; + if (this.container instanceof ContainerCraftingTerm containerTerminal) { final IRecipe recipe = containerTerminal.getCurrentRecipe(); if (recipe != null && recipe.matches(ic, world)) { @@ -171,9 +164,8 @@ protected IRecipe findRecipe(InventoryCrafting ic, World world, EntityPlayer pla // Returns null in case this recipe is not yet unlocked private IRecipe handleRecipe(InventoryCrafting ic, IRecipe recipe, EntityPlayer player) { - if (Loader.isModLoaded("recipestages")) { - if (recipe instanceof RecipeStage) { - final RecipeStage staged = (RecipeStage) recipe; + if (Platform.isModLoaded("recipestages")) { + if (recipe instanceof RecipeStage staged) { if (!staged.isGoodForCrafting(ic)) return null; } @@ -185,8 +177,7 @@ private IRecipe handleRecipe(InventoryCrafting ic, IRecipe recipe, EntityPlayer // TODO: This is really hacky and NEEDS to be solved with a full container/gui refactoring. @Override protected NonNullList getRemainingItems(InventoryCrafting ic, World world) { - if (this.container instanceof ContainerCraftingTerm) { - final ContainerCraftingTerm containerTerminal = (ContainerCraftingTerm) this.container; + if (this.container instanceof ContainerCraftingTerm containerTerminal) { final IRecipe recipe = containerTerminal.getCurrentRecipe(); if (recipe != null && recipe.matches(ic, world)) { diff --git a/src/main/java/appeng/core/sync/packets/PacketTerminalUse.java b/src/main/java/appeng/core/sync/packets/PacketTerminalUse.java index b092f87a19b..5a55d427ce0 100644 --- a/src/main/java/appeng/core/sync/packets/PacketTerminalUse.java +++ b/src/main/java/appeng/core/sync/packets/PacketTerminalUse.java @@ -14,7 +14,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.NonNullList; -import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.Optional; public class PacketTerminalUse extends AppEngPacket { @@ -45,7 +44,7 @@ public void serverPacketData(INetworkInfo manager, AppEngPacket packet, EntityPl } } - if (Loader.isModLoaded("baubles")) { + if (Platform.isModLoaded("baubles")) { tryOpenBauble(player); } } diff --git a/src/main/java/appeng/helpers/DualityInterface.java b/src/main/java/appeng/helpers/DualityInterface.java index 46eaed13bc2..cb40534e123 100644 --- a/src/main/java/appeng/helpers/DualityInterface.java +++ b/src/main/java/appeng/helpers/DualityInterface.java @@ -65,7 +65,10 @@ import appeng.tile.inventory.AppEngInternalInventory; import appeng.tile.inventory.AppEngInternalOversizedInventory; import appeng.tile.networking.TileCableBus; -import appeng.util.*; +import appeng.util.ConfigManager; +import appeng.util.IConfigManagerHost; +import appeng.util.InventoryAdaptor; +import appeng.util.Platform; import appeng.util.inv.*; import appeng.util.item.AEItemStack; import com.google.common.collect.ImmutableSet; @@ -88,7 +91,6 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.fml.common.Loader; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.wrapper.RangedWrapper; @@ -98,7 +100,8 @@ import static appeng.api.config.LockCraftingMode.LOCK_UNTIL_PULSE; import static appeng.api.config.LockCraftingMode.LOCK_UNTIL_RESULT; -import static appeng.helpers.ItemStackHelper.*; +import static appeng.helpers.ItemStackHelper.stackFromNBT; +import static appeng.helpers.ItemStackHelper.stackToNBT; public class DualityInterface implements IGridTickable, IStorageMonitorable, IInventoryDestination, IAEAppEngInventory, IConfigManagerHost, ICraftingProvider, IUpgradeableHost { @@ -525,8 +528,7 @@ private void addToCraftingList(final ItemStack is) { return; } - if (is.getItem() instanceof ICraftingPatternItem) { - final ICraftingPatternItem cpi = (ICraftingPatternItem) is.getItem(); + if (is.getItem() instanceof ICraftingPatternItem cpi) { final ICraftingPatternDetails details = cpi.getPatternForItem(is, this.iHost.getTileEntity().getWorld()); if (details != null) { @@ -1068,8 +1070,7 @@ public boolean pushPattern(final ICraftingPatternDetails patternDetails, final I continue; } - if (te instanceof ICraftingMachine) { - final ICraftingMachine cm = (ICraftingMachine) te; + if (te instanceof ICraftingMachine cm) { if (cm.acceptsPlans()) { visitedFaces.remove(s); if (cm.pushPattern(patternDetails, table, s.getOpposite())) { @@ -1084,7 +1085,7 @@ public boolean pushPattern(final ICraftingPatternDetails patternDetails, final I if (ad != null) { if (this.isBlocking()) { IPhantomTile phantomTE; - if (Loader.isModLoaded("actuallyadditions") && te instanceof IPhantomTile) { + if (Platform.isModLoaded("actuallyadditions") && te instanceof IPhantomTile) { phantomTE = ((IPhantomTile) te); if (phantomTE.hasBoundPosition()) { TileEntity phantom = w.getTileEntity(phantomTE.getBoundPosition()); @@ -1224,8 +1225,7 @@ public boolean isBusy() { final InventoryAdaptor ad = InventoryAdaptor.getAdaptor(te, s.getOpposite()); if (ad != null) { - if (Loader.isModLoaded("actuallyadditions") && Platform.GTLoaded && te instanceof IPhantomTile) { - IPhantomTile phantomTE = ((IPhantomTile) te); + if (Platform.isModLoaded("actuallyadditions") && Platform.GTLoaded && te instanceof IPhantomTile phantomTE) { if (phantomTE.hasBoundPosition()) { TileEntity phantom = w.getTileEntity(phantomTE.getBoundPosition()); if (NonBlockingItems.INSTANCE.getMap().containsKey(w.getBlockState(phantomTE.getBoundPosition()).getBlock().getRegistryName().getNamespace())) { @@ -1453,7 +1453,7 @@ public String getTermName() { public long getSortValue() { final TileEntity te = this.iHost.getTileEntity(); - return (te.getPos().getZ() << 24) ^ (te.getPos().getX() << 8) ^ te.getPos().getY(); + return ((long) te.getPos().getZ() << 24) ^ ((long) te.getPos().getX() << 8) ^ te.getPos().getY(); } public void initialize() { diff --git a/src/main/java/appeng/helpers/NonBlockingItems.java b/src/main/java/appeng/helpers/NonBlockingItems.java index f5fabef767c..668068dc077 100644 --- a/src/main/java/appeng/helpers/NonBlockingItems.java +++ b/src/main/java/appeng/helpers/NonBlockingItems.java @@ -9,7 +9,6 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.oredict.OreDictionary; @@ -24,74 +23,72 @@ public class NonBlockingItems { private NonBlockingItems() { String[] strings = AEConfig.instance().getNonBlockingItems(); String[] modids = new String[0]; - if (strings.length > 0) { - for (String s : strings) { - if (s.startsWith("[") && s.endsWith("]")) { - modids = s.substring(1, s.length() - 1).split("\\|"); - } else { - for (String modid : modids) { - if (!Loader.isModLoaded(modid)) { - continue; - } - NON_BLOCKING_MAP.putIfAbsent(modid, new Object2ObjectOpenHashMap<>()); + for (String s : strings) { + if (s.startsWith("[") && s.endsWith("]")) { + modids = s.substring(1, s.length() - 1).split("\\|"); + } else { + for (String modid : modids) { + if (!Platform.isModLoaded(modid)) { + continue; + } + NON_BLOCKING_MAP.putIfAbsent(modid, new Object2ObjectOpenHashMap<>()); - String[] ModItemMeta = s.split(":"); + String[] ModItemMeta = s.split(":"); - if (ModItemMeta.length < 2 || ModItemMeta.length > 3) { - AELog.error("Invalid non blocking item entry: " + s); - continue; - } + if (ModItemMeta.length < 2 || ModItemMeta.length > 3) { + AELog.error("Invalid non blocking item entry: " + s); + continue; + } - if (ModItemMeta[0].equals("gregtech") && Platform.GTLoaded) { - boolean found = false; - for (MetaItem metaItem : MetaItem.getMetaItems()) { - MetaItem.MetaValueItem metaItem2 = metaItem.getItem(ModItemMeta[1]); - if (metaItem.getItem(ModItemMeta[1]) != null) { - found = true; - ItemStack itemStack = metaItem2.getStackForm(); + if (ModItemMeta[0].equals("gregtech") && Platform.GTLoaded) { + boolean found = false; + for (MetaItem metaItem : MetaItem.getMetaItems()) { + MetaItem.MetaValueItem metaItem2 = metaItem.getItem(ModItemMeta[1]); + if (metaItem.getItem(ModItemMeta[1]) != null) { + found = true; + ItemStack itemStack = metaItem2.getStackForm(); + NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet()); + NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) -> + { + intSet.add(itemStack.getItemDamage()); + return intSet; + }); + } else { + ItemStack itemStack = GameRegistry.makeItemStack(ModItemMeta[0] + ":" + ModItemMeta[1], ModItemMeta.length == 3 ? Integer.parseInt(ModItemMeta[2]) : 0, 1, null); + if (!itemStack.isEmpty()) { NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet()); NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) -> { intSet.add(itemStack.getItemDamage()); return intSet; }); - } else { - ItemStack itemStack = GameRegistry.makeItemStack(ModItemMeta[0] + ":" + ModItemMeta[1], ModItemMeta.length == 3 ? Integer.parseInt(ModItemMeta[2]) : 0, 1, null); - if (!itemStack.isEmpty()) { - NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet()); - NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) -> - { - intSet.add(itemStack.getItemDamage()); - return intSet; - }); - } } } - if (!found) { - AELog.error("Item not found on nonBlocking config: " + s); - } - } else if (ModItemMeta[0].equals("ore")) { - OreDictionary.getOres(ModItemMeta[1]).forEach(itemStack -> + } + if (!found) { + AELog.error("Item not found on nonBlocking config: " + s); + } + } else if (ModItemMeta[0].equals("ore")) { + OreDictionary.getOres(ModItemMeta[1]).forEach(itemStack -> + { + NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet()); + NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) -> { - NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet()); - NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) -> - { - intSet.add(itemStack.getItemDamage()); - return intSet; - }); + intSet.add(itemStack.getItemDamage()); + return intSet; + }); + }); + } else { + ItemStack itemStack = GameRegistry.makeItemStack(ModItemMeta[0] + ":" + ModItemMeta[1], ModItemMeta.length == 3 ? Integer.parseInt(ModItemMeta[2]) : 0, 1, null); + if (!itemStack.isEmpty()) { + NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet()); + NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) -> + { + intSet.add(itemStack.getItemDamage()); + return intSet; }); } else { - ItemStack itemStack = GameRegistry.makeItemStack(ModItemMeta[0] + ":" + ModItemMeta[1], ModItemMeta.length == 3 ? Integer.parseInt(ModItemMeta[2]) : 0, 1, null); - if (!itemStack.isEmpty()) { - NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet()); - NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) -> - { - intSet.add(itemStack.getItemDamage()); - return intSet; - }); - } else { - AELog.error("Item not found on nonBlocking config: " + s); - } + AELog.error("Item not found on nonBlocking config: " + s); } } } diff --git a/src/main/java/appeng/integration/IntegrationNode.java b/src/main/java/appeng/integration/IntegrationNode.java index 03412ab74bb..5315fa84cc8 100644 --- a/src/main/java/appeng/integration/IntegrationNode.java +++ b/src/main/java/appeng/integration/IntegrationNode.java @@ -22,7 +22,7 @@ import appeng.api.exceptions.ModNotInstalledException; import appeng.core.AEConfig; import appeng.core.AELog; -import net.minecraftforge.fml.common.Loader; +import appeng.util.Platform; import net.minecraftforge.fml.common.ModAPIManager; @@ -64,7 +64,7 @@ void call(final IntegrationStage stage) { switch (stage) { case PRE_INIT: final ModAPIManager apiManager = ModAPIManager.INSTANCE; - boolean enabled = this.modID == null || Loader.isModLoaded(this.modID) || apiManager.hasAPI(this.modID); + boolean enabled = this.modID == null || Platform.isModLoaded(this.modID) || apiManager.hasAPI(this.modID); AEConfig.instance() .addCustomCategoryComment("ModIntegration", diff --git a/src/main/java/appeng/integration/modules/bogosorter/InventoryBogoSortModule.java b/src/main/java/appeng/integration/modules/bogosorter/InventoryBogoSortModule.java index b833b4a2bf5..afa0d6e86ac 100644 --- a/src/main/java/appeng/integration/modules/bogosorter/InventoryBogoSortModule.java +++ b/src/main/java/appeng/integration/modules/bogosorter/InventoryBogoSortModule.java @@ -1,13 +1,13 @@ package appeng.integration.modules.bogosorter; import appeng.api.storage.data.IAEItemStack; +import appeng.util.Platform; import com.cleanroommc.bogosorter.common.sort.SortHandler; -import net.minecraftforge.fml.common.Loader; import java.util.Comparator; public class InventoryBogoSortModule { - private static final boolean loaded = Loader.isModLoaded("bogosorter"); + private static final boolean loaded = Platform.isModLoaded("bogosorter"); public static final Comparator COMPARATOR = (o1, o2) -> SortHandler.getClientItemComparator().compare(o1.getDefinition(), o2.getDefinition()); diff --git a/src/main/java/appeng/services/VersionChecker.java b/src/main/java/appeng/services/VersionChecker.java index ab79e139db0..b1ffe49f7b9 100644 --- a/src/main/java/appeng/services/VersionChecker.java +++ b/src/main/java/appeng/services/VersionChecker.java @@ -25,9 +25,9 @@ import appeng.services.version.*; import appeng.services.version.github.FormattedRelease; import appeng.services.version.github.ReleaseFetcher; +import appeng.util.Platform; import com.google.common.base.Preconditions; import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.event.FMLInterModComms; import javax.annotation.Nonnull; @@ -82,7 +82,7 @@ public void run() { final long lastCheck = Long.parseLong(rawLastCheck); final Date now = new Date(); final long nowInMs = now.getTime(); - final long intervalInMs = this.config.interval() * SEC_TO_HOUR * MS_TO_SEC; + final long intervalInMs = (long) this.config.interval() * SEC_TO_HOUR * MS_TO_SEC; final long lastAfterInterval = lastCheck + intervalInMs; this.processInterval(nowInMs, lastAfterInterval); @@ -153,7 +153,7 @@ private void processVersions(@Nonnull final Version modVersion, @Nonnull final F * @param changelog retrieved github changelog */ private void interactWithVersionCheckerMod(@Nonnull final String modFormatted, @Nonnull final String ghFormatted, @Nonnull final String changelog) { - if (Loader.isModLoaded("VersionChecker")) { + if (Platform.isModLoaded("VersionChecker")) { final NBTTagCompound versionInf = new NBTTagCompound(); versionInf.setString("modDisplayName", AppEng.MOD_NAME); versionInf.setString("oldVersion", modFormatted); diff --git a/src/main/java/appeng/util/Platform.java b/src/main/java/appeng/util/Platform.java index 37c13ce7987..4d69f11b277 100644 --- a/src/main/java/appeng/util/Platform.java +++ b/src/main/java/appeng/util/Platform.java @@ -73,6 +73,8 @@ import gregtech.api.metatileentity.MetaTileEntity; import gregtech.api.util.GTUtility; import ic2.api.item.ICustomDamageItem; +import it.unimi.dsi.fastutil.objects.Object2BooleanMap; +import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; @@ -106,7 +108,6 @@ import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.common.Loader; -import net.minecraftforge.fml.common.ModContainer; import net.minecraftforge.fml.common.Optional; import net.minecraftforge.fml.relauncher.ReflectionHelper; import net.minecraftforge.fml.relauncher.Side; @@ -130,6 +131,7 @@ */ @Optional.Interface(iface = "ic2.api.item.ICustomDamageItem", modid = "IC2") public class Platform { + private static final Object2BooleanMap CACHED_MODS = new Object2BooleanOpenHashMap<>(); public static final Block AIR_BLOCK = Blocks.AIR; @@ -526,18 +528,15 @@ public static int getRandomInt() { } public static boolean isModLoaded(final String modid) { - try { - // if this fails for some reason, try the other method. - return Loader.isModLoaded(modid); - } catch (final Throwable ignored) { - } + return CACHED_MODS.computeIfAbsent(modid, (k) -> { + try { + // if this fails for some reason, try the other method. + return Loader.isModLoaded(k); + } catch (final Throwable ignored) {} - for (final ModContainer f : Loader.instance().getActiveModList()) { - if (f.getModId().equals(modid)) { - return true; - } - } - return false; + return Loader.instance().getActiveModList() + .stream().anyMatch(mod -> mod.getModId().equals(k)); + }); } public static ItemStack findMatchingRecipeOutput(final InventoryCrafting ic, final World world) { @@ -551,8 +550,7 @@ public static List getTooltip(final Object o) { } ItemStack itemStack = ItemStack.EMPTY; - if (o instanceof AEItemStack) { - final AEItemStack ais = (AEItemStack) o; + if (o instanceof AEItemStack ais) { return ais.getToolTip(); } else if (o instanceof ItemStack) { itemStack = (ItemStack) o; @@ -655,8 +653,7 @@ public static boolean isWrench(final EntityPlayer player, final ItemStack eq, fi } - if (eq.getItem() instanceof IAEWrench) { - final IAEWrench wrench = (IAEWrench) eq.getItem(); + if (eq.getItem() instanceof IAEWrench wrench) { return wrench.canWrench(eq, player, pos); } }