Skip to content

Commit

Permalink
Memoize isModLoaded lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
NotMyWing committed Jun 7, 2024
1 parent 68de8fa commit f7562db
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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()));
Expand Down
17 changes: 4 additions & 13 deletions src/main/java/appeng/container/slot/SlotCraftingTerm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand All @@ -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<ItemStack> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -45,7 +44,7 @@ public void serverPacketData(INetworkInfo manager, AppEngPacket packet, EntityPl

}
}
if (Loader.isModLoaded("baubles")) {
if (Platform.isModLoaded("baubles")) {
tryOpenBauble(player);
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/appeng/helpers/DualityInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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())) {
Expand All @@ -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());
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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() {
Expand Down
Loading

2 comments on commit f7562db

@Rongmario
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a redundant change btw

@NotMyWing
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Profiling results show a significant decrease in time spent checking if mods are loaded, however.

Please sign in to comment.