Skip to content

Commit

Permalink
Refactor LDLib calls (#2609)
Browse files Browse the repository at this point in the history
Co-authored-by: screret <[email protected]>
  • Loading branch information
omergunr100 and screret authored Jan 4, 2025
1 parent 6eece36 commit 085e73f
Show file tree
Hide file tree
Showing 114 changed files with 453 additions and 385 deletions.
2 changes: 1 addition & 1 deletion docs/content/Development/General-Topics/Global-Data.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ When doing so, you need to ensure that remote and serverside instances don't get
To make working with this requirement easier, You can use `SideLocal<T>` to store your global data.
It is similar to Java's `ThreadLocal`, but operates on the game's sides instead.

If you are currently on the remote side (`LDLib.isRemote()` / on the client's `main` thread), it will return the
If you are currently on the remote side (`GTCEuAPI.isClientThread()` / on the client's `main` thread), it will return the
remote side's instance of your data. Otherwise, you will get the server side's instance.

??? example "Example Usage"
Expand Down
147 changes: 121 additions & 26 deletions src/main/java/com/gregtechceu/gtceu/GTCEu.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@
import com.gregtechceu.gtceu.common.CommonProxy;
import com.gregtechceu.gtceu.utils.FormattingUtil;

import com.lowdragmc.lowdraglib.LDLib;
import com.lowdragmc.lowdraglib.Platform;

import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.server.ServerLifecycleHooks;

import dev.emi.emi.config.EmiConfig;
import me.shedaniel.rei.api.client.REIRuntime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;

@Mod(GTCEu.MOD_ID)
public class GTCEu {

Expand All @@ -30,7 +38,7 @@ public GTCEu() {
}

public static void init() {
LOGGER.info("{} is initializing on platform: {}", NAME, Platform.platformName());
LOGGER.info("{} is initializing...", NAME);
}

public static ResourceLocation id(String path) {
Expand All @@ -53,45 +61,132 @@ public static ResourceLocation appendId(String id) {
return new ResourceLocation(strings[0], strings[1]);
}

public static boolean isKubeJSLoaded() {
return LDLib.isModLoaded(GTValues.MODID_KUBEJS);
/**
* @return if we're running in a production environment
*/
public static boolean isProd() {
return FMLLoader.isProduction();
}

public static boolean isIrisOculusLoaded() {
return LDLib.isModLoaded(GTValues.MODID_IRIS) || LDLib.isModLoaded(GTValues.MODID_OCULUS);
/**
* @return if we're not running in a production environment
*/
public static boolean isDev() {
return !isProd();
}

public static boolean isSodiumRubidiumEmbeddiumLoaded() {
return LDLib.isModLoaded(GTValues.MODID_SODIUM) || LDLib.isModLoaded(GTValues.MODID_RUBIDIUM) ||
LDLib.isModLoaded(GTValues.MODID_EMBEDDIUM);
/**
* @return if we're running data generation
*/
public static boolean isDataGen() {
return FMLLoader.getLaunchHandler().isData();
}

public static boolean isAE2Loaded() {
return LDLib.isModLoaded(GTValues.MODID_APPENG);
/**
* A friendly reminder that the server instance is populated on the server side only, so null/side check it!
*
* @return the current minecraft server instance
*/
public static MinecraftServer getMinecraftServer() {
return ServerLifecycleHooks.getCurrentServer();
}

public static boolean isCuriosLoaded() {
return LDLib.isModLoaded(GTValues.MODID_CURIOS);
/**
* @param modId the mod id to check for
* @return if the mod whose id is {@code modId} is loaded or not
*/
public static boolean isModLoaded(String modId) {
return ModList.get().isLoaded(modId);
}

public static boolean isShimmerLoaded() {
return LDLib.isModLoaded(GTValues.MODID_SHIMMER);
/**
* For async stuff use this, otherwise use {@link GTCEu isClientSide}
*
* @return if the current thread is the client thread
*/
public static boolean isClientThread() {
return isClientSide() && Minecraft.getInstance().isSameThread();
}

public static boolean isJAVDLoaded() {
return LDLib.isModLoaded(GTValues.MODID_JAVD);
/**
* @return if the FML environment is a client
*/
public static boolean isClientSide() {
return FMLEnvironment.dist.isClient();
}

public static boolean isFTBTeamsLoaded() {
return LDLib.isModLoaded(GTValues.MODID_FTB_TEAMS);
/**
* This check isn't the same for client and server!
*
* @return if it's safe to access the current instance {@link net.minecraft.world.level.Level Level} on client or if
* it's safe to access any level on server.
*/
public static boolean canGetServerLevel() {
if (isClientSide()) {
return Minecraft.getInstance().level != null;
}
var server = getMinecraftServer();
return server != null &&
!(server.isStopped() || server.isShutdown() || !server.isRunning() || server.isCurrentlySaving());
}

public static boolean isArgonautsLoaded() {
return LDLib.isModLoaded(GTValues.MODID_ARGONAUTS);
/**
* @return the path to the minecraft instance directory
*/
public static Path getGameDir() {
return FMLPaths.GAMEDIR.get();
}

@Deprecated(forRemoval = true, since = "1.0.21")
public static boolean isHighTier() {
return GTCEuAPI.isHighTier();
public static class Mods {

public static boolean isJEILoaded() {
return !(isModLoaded(GTValues.MODID_EMI) || isModLoaded(GTValues.MODID_REI)) &&
isModLoaded(GTValues.MODID_JEI);
}

public static boolean isREILoaded() {
return isModLoaded(GTValues.MODID_REI) && !(isClientSide() || REIRuntime.getInstance().isOverlayVisible());
}

public static boolean isEMILoaded() {
return isModLoaded(GTValues.MODID_EMI) && !(isClientSide() || EmiConfig.enabled);
}

public static boolean isKubeJSLoaded() {
return isModLoaded(GTValues.MODID_KUBEJS);
}

public static boolean isIrisOculusLoaded() {
return isModLoaded(GTValues.MODID_IRIS) || isModLoaded(GTValues.MODID_OCULUS);
}

public static boolean isSodiumRubidiumEmbeddiumLoaded() {
return isModLoaded(GTValues.MODID_SODIUM) || isModLoaded(GTValues.MODID_RUBIDIUM) ||
isModLoaded(GTValues.MODID_EMBEDDIUM);
}

public static boolean isAE2Loaded() {
return isModLoaded(GTValues.MODID_APPENG);
}

public static boolean isCuriosLoaded() {
return isModLoaded(GTValues.MODID_CURIOS);
}

public static boolean isShimmerLoaded() {
return isModLoaded(GTValues.MODID_SHIMMER);
}

public static boolean isJAVDLoaded() {
return isModLoaded(GTValues.MODID_JAVD);
}

public static boolean isFTBTeamsLoaded() {
return isModLoaded(GTValues.MODID_FTB_TEAMS);
}

public static boolean isArgonautsLoaded() {
return isModLoaded(GTValues.MODID_ARGONAUTS);
}
}
}
4 changes: 1 addition & 3 deletions src/main/java/com/gregtechceu/gtceu/api/GTCEuAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import com.gregtechceu.gtceu.common.block.CoilBlock;
import com.gregtechceu.gtceu.config.ConfigHolder;

import com.lowdragmc.lowdraglib.Platform;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.eventbus.api.GenericEvent;
Expand Down Expand Up @@ -49,7 +47,7 @@ public class GTCEuAPI {
public static void initializeHighTier() {
if (highTierInitialized) throw new IllegalStateException("High-Tier is already initialized.");
highTier = ConfigHolder.INSTANCE.machines.highTierContent ||
AddonFinder.getAddons().stream().anyMatch(IGTAddon::requiresHighTier) || Platform.isDevEnv();
AddonFinder.getAddons().stream().anyMatch(IGTAddon::requiresHighTier) || GTCEu.isDev();
highTierInitialized = true;

if (isHighTier()) GTCEu.LOGGER.info("High-Tier is Enabled.");
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/api/GTValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public static int[] tiersBetween(int minInclusive, int maxInclusive) {

public static final String MODID_TOP = "theoneprobe",
MODID_JEI = "jei",
MODID_REI = "roughlyenoughitems",
MODID_EMI = "emi",
MODID_APPENG = "ae2",
MODID_KUBEJS = "kubejs",
MODID_IRIS = "iris",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gregtechceu.gtceu.api.block;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.blockentity.PipeBlockEntity;
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
Expand All @@ -10,8 +11,6 @@
import com.gregtechceu.gtceu.config.ConfigHolder;
import com.gregtechceu.gtceu.data.recipe.VanillaRecipeHelper;

import com.lowdragmc.lowdraglib.Platform;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.client.color.block.BlockColor;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -71,7 +70,7 @@ public MaterialBlock(Properties properties, TagPrefix tagPrefix, Material materi
super(properties);
this.material = material;
this.tagPrefix = tagPrefix;
if (registerModel && Platform.isClient()) {
if (registerModel && GTCEu.isClientSide()) {
MaterialBlockRenderer.create(this, tagPrefix.materialIconType(), material.getMaterialIconSet());
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/gregtechceu/gtceu/api/block/OreBlock.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.gregtechceu.gtceu.api.block;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
import com.gregtechceu.gtceu.client.renderer.block.OreBlockRenderer;
import com.gregtechceu.gtceu.config.ConfigHolder;
import com.gregtechceu.gtceu.integration.map.cache.server.ServerCache;

import com.lowdragmc.lowdraglib.Platform;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
Expand All @@ -21,7 +20,7 @@ public class OreBlock extends MaterialBlock {

public OreBlock(Properties properties, TagPrefix tagPrefix, Material material, boolean registerModel) {
super(properties, tagPrefix, material, false);
if (registerModel && Platform.isClient()) {
if (registerModel && GTCEu.isClientSide()) {
OreBlockRenderer.create(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* @author KilaBash
Expand Down Expand Up @@ -269,7 +272,7 @@ public static <T> LazyOptional<T> getCapability(MetaMachine machine, @NotNull Ca
return GTCapability.CAPABILITY_DATA_ACCESS.orEmpty(cap, LazyOptional.of(() -> list.get(0)));
}
}
if (GTCEu.isAE2Loaded()) {
if (GTCEu.Mods.isAE2Loaded()) {
if (cap == Capabilities.IN_WORLD_GRID_NODE_HOST) {
if (machine instanceof IInWorldGridNodeHost nodeHost) {
return Capabilities.IN_WORLD_GRID_NODE_HOST.orEmpty(cap, LazyOptional.of(() -> nodeHost));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.gregtechceu.gtceu.api.capability;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.block.IAppearance;
import com.gregtechceu.gtceu.api.blockentity.ITickSubscription;
import com.gregtechceu.gtceu.api.cover.CoverBehavior;
import com.gregtechceu.gtceu.api.cover.CoverDefinition;
import com.gregtechceu.gtceu.api.transfer.fluid.IFluidHandlerModifiable;
import com.gregtechceu.gtceu.utils.GTUtil;

import com.lowdragmc.lowdraglib.LDLib;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -169,7 +168,7 @@ default boolean hasCover(Direction facing) {
}

default boolean isRemote() {
return getLevel() == null ? LDLib.isRemote() : getLevel().isClientSide;
return getLevel() == null ? GTCEu.isClientThread() : getLevel().isClientSide;
}

default VoxelShape[] addCoverCollisionBoundingBox() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.gregtechceu.gtceu.api.cover.filter;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.cover.CoverBehavior;
import com.gregtechceu.gtceu.api.gui.GuiTextures;
import com.gregtechceu.gtceu.api.gui.widget.SlotWidget;
import com.gregtechceu.gtceu.api.machine.MachineCoverContainer;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
import com.gregtechceu.gtceu.api.transfer.item.CustomItemStackHandler;

import com.lowdragmc.lowdraglib.LDLib;
import com.lowdragmc.lowdraglib.gui.texture.GuiTextureGroup;
import com.lowdragmc.lowdraglib.gui.widget.Widget;
import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;
Expand Down Expand Up @@ -128,7 +128,7 @@ private CustomItemStackHandler getFilterSlot() {
private void updateFilter() {
var filterContainer = getFilterSlot();

if (LDLib.isRemote()) {
if (GTCEu.isClientThread()) {
if (!filterContainer.getStackInSlot(0).isEmpty() && !this.filterItem.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String toString() {
}

public static void init() {
if (GTCEu.isKubeJSLoaded()) {
if (GTCEu.Mods.isKubeJSLoaded()) {
GTRegistryInfo.registerFor(GTRegistryInfo.MATERIAL_ICON_SET.registryKey);
}
}
Expand Down
Loading

0 comments on commit 085e73f

Please sign in to comment.