Skip to content

Commit 085e73f

Browse files
omergunr100screret
andauthored
Refactor LDLib calls (GregTechCEu#2609)
Co-authored-by: screret <[email protected]>
1 parent 6eece36 commit 085e73f

File tree

114 files changed

+453
-385
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+453
-385
lines changed

docs/content/Development/General-Topics/Global-Data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ When doing so, you need to ensure that remote and serverside instances don't get
1818
To make working with this requirement easier, You can use `SideLocal<T>` to store your global data.
1919
It is similar to Java's `ThreadLocal`, but operates on the game's sides instead.
2020

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

2424
??? example "Example Usage"

src/main/java/com/gregtechceu/gtceu/GTCEu.java

Lines changed: 121 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@
66
import com.gregtechceu.gtceu.common.CommonProxy;
77
import com.gregtechceu.gtceu.utils.FormattingUtil;
88

9-
import com.lowdragmc.lowdraglib.LDLib;
10-
import com.lowdragmc.lowdraglib.Platform;
11-
9+
import net.minecraft.client.Minecraft;
1210
import net.minecraft.resources.ResourceLocation;
11+
import net.minecraft.server.MinecraftServer;
1312
import net.minecraftforge.fml.DistExecutor;
13+
import net.minecraftforge.fml.ModList;
1414
import net.minecraftforge.fml.common.Mod;
15+
import net.minecraftforge.fml.loading.FMLEnvironment;
16+
import net.minecraftforge.fml.loading.FMLLoader;
17+
import net.minecraftforge.fml.loading.FMLPaths;
18+
import net.minecraftforge.server.ServerLifecycleHooks;
1519

20+
import dev.emi.emi.config.EmiConfig;
21+
import me.shedaniel.rei.api.client.REIRuntime;
1622
import org.slf4j.Logger;
1723
import org.slf4j.LoggerFactory;
1824

25+
import java.nio.file.Path;
26+
1927
@Mod(GTCEu.MOD_ID)
2028
public class GTCEu {
2129

@@ -30,7 +38,7 @@ public GTCEu() {
3038
}
3139

3240
public static void init() {
33-
LOGGER.info("{} is initializing on platform: {}", NAME, Platform.platformName());
41+
LOGGER.info("{} is initializing...", NAME);
3442
}
3543

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

56-
public static boolean isKubeJSLoaded() {
57-
return LDLib.isModLoaded(GTValues.MODID_KUBEJS);
64+
/**
65+
* @return if we're running in a production environment
66+
*/
67+
public static boolean isProd() {
68+
return FMLLoader.isProduction();
5869
}
5970

60-
public static boolean isIrisOculusLoaded() {
61-
return LDLib.isModLoaded(GTValues.MODID_IRIS) || LDLib.isModLoaded(GTValues.MODID_OCULUS);
71+
/**
72+
* @return if we're not running in a production environment
73+
*/
74+
public static boolean isDev() {
75+
return !isProd();
6276
}
6377

64-
public static boolean isSodiumRubidiumEmbeddiumLoaded() {
65-
return LDLib.isModLoaded(GTValues.MODID_SODIUM) || LDLib.isModLoaded(GTValues.MODID_RUBIDIUM) ||
66-
LDLib.isModLoaded(GTValues.MODID_EMBEDDIUM);
78+
/**
79+
* @return if we're running data generation
80+
*/
81+
public static boolean isDataGen() {
82+
return FMLLoader.getLaunchHandler().isData();
6783
}
6884

69-
public static boolean isAE2Loaded() {
70-
return LDLib.isModLoaded(GTValues.MODID_APPENG);
85+
/**
86+
* A friendly reminder that the server instance is populated on the server side only, so null/side check it!
87+
*
88+
* @return the current minecraft server instance
89+
*/
90+
public static MinecraftServer getMinecraftServer() {
91+
return ServerLifecycleHooks.getCurrentServer();
7192
}
7293

73-
public static boolean isCuriosLoaded() {
74-
return LDLib.isModLoaded(GTValues.MODID_CURIOS);
94+
/**
95+
* @param modId the mod id to check for
96+
* @return if the mod whose id is {@code modId} is loaded or not
97+
*/
98+
public static boolean isModLoaded(String modId) {
99+
return ModList.get().isLoaded(modId);
75100
}
76101

77-
public static boolean isShimmerLoaded() {
78-
return LDLib.isModLoaded(GTValues.MODID_SHIMMER);
102+
/**
103+
* For async stuff use this, otherwise use {@link GTCEu isClientSide}
104+
*
105+
* @return if the current thread is the client thread
106+
*/
107+
public static boolean isClientThread() {
108+
return isClientSide() && Minecraft.getInstance().isSameThread();
79109
}
80110

81-
public static boolean isJAVDLoaded() {
82-
return LDLib.isModLoaded(GTValues.MODID_JAVD);
111+
/**
112+
* @return if the FML environment is a client
113+
*/
114+
public static boolean isClientSide() {
115+
return FMLEnvironment.dist.isClient();
83116
}
84117

85-
public static boolean isFTBTeamsLoaded() {
86-
return LDLib.isModLoaded(GTValues.MODID_FTB_TEAMS);
118+
/**
119+
* This check isn't the same for client and server!
120+
*
121+
* @return if it's safe to access the current instance {@link net.minecraft.world.level.Level Level} on client or if
122+
* it's safe to access any level on server.
123+
*/
124+
public static boolean canGetServerLevel() {
125+
if (isClientSide()) {
126+
return Minecraft.getInstance().level != null;
127+
}
128+
var server = getMinecraftServer();
129+
return server != null &&
130+
!(server.isStopped() || server.isShutdown() || !server.isRunning() || server.isCurrentlySaving());
87131
}
88132

89-
public static boolean isArgonautsLoaded() {
90-
return LDLib.isModLoaded(GTValues.MODID_ARGONAUTS);
133+
/**
134+
* @return the path to the minecraft instance directory
135+
*/
136+
public static Path getGameDir() {
137+
return FMLPaths.GAMEDIR.get();
91138
}
92139

93-
@Deprecated(forRemoval = true, since = "1.0.21")
94-
public static boolean isHighTier() {
95-
return GTCEuAPI.isHighTier();
140+
public static class Mods {
141+
142+
public static boolean isJEILoaded() {
143+
return !(isModLoaded(GTValues.MODID_EMI) || isModLoaded(GTValues.MODID_REI)) &&
144+
isModLoaded(GTValues.MODID_JEI);
145+
}
146+
147+
public static boolean isREILoaded() {
148+
return isModLoaded(GTValues.MODID_REI) && !(isClientSide() || REIRuntime.getInstance().isOverlayVisible());
149+
}
150+
151+
public static boolean isEMILoaded() {
152+
return isModLoaded(GTValues.MODID_EMI) && !(isClientSide() || EmiConfig.enabled);
153+
}
154+
155+
public static boolean isKubeJSLoaded() {
156+
return isModLoaded(GTValues.MODID_KUBEJS);
157+
}
158+
159+
public static boolean isIrisOculusLoaded() {
160+
return isModLoaded(GTValues.MODID_IRIS) || isModLoaded(GTValues.MODID_OCULUS);
161+
}
162+
163+
public static boolean isSodiumRubidiumEmbeddiumLoaded() {
164+
return isModLoaded(GTValues.MODID_SODIUM) || isModLoaded(GTValues.MODID_RUBIDIUM) ||
165+
isModLoaded(GTValues.MODID_EMBEDDIUM);
166+
}
167+
168+
public static boolean isAE2Loaded() {
169+
return isModLoaded(GTValues.MODID_APPENG);
170+
}
171+
172+
public static boolean isCuriosLoaded() {
173+
return isModLoaded(GTValues.MODID_CURIOS);
174+
}
175+
176+
public static boolean isShimmerLoaded() {
177+
return isModLoaded(GTValues.MODID_SHIMMER);
178+
}
179+
180+
public static boolean isJAVDLoaded() {
181+
return isModLoaded(GTValues.MODID_JAVD);
182+
}
183+
184+
public static boolean isFTBTeamsLoaded() {
185+
return isModLoaded(GTValues.MODID_FTB_TEAMS);
186+
}
187+
188+
public static boolean isArgonautsLoaded() {
189+
return isModLoaded(GTValues.MODID_ARGONAUTS);
190+
}
96191
}
97192
}

src/main/java/com/gregtechceu/gtceu/api/GTCEuAPI.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import com.gregtechceu.gtceu.common.block.CoilBlock;
1313
import com.gregtechceu.gtceu.config.ConfigHolder;
1414

15-
import com.lowdragmc.lowdraglib.Platform;
16-
1715
import net.minecraft.resources.ResourceLocation;
1816
import net.minecraft.world.level.block.Block;
1917
import net.minecraftforge.eventbus.api.GenericEvent;
@@ -49,7 +47,7 @@ public class GTCEuAPI {
4947
public static void initializeHighTier() {
5048
if (highTierInitialized) throw new IllegalStateException("High-Tier is already initialized.");
5149
highTier = ConfigHolder.INSTANCE.machines.highTierContent ||
52-
AddonFinder.getAddons().stream().anyMatch(IGTAddon::requiresHighTier) || Platform.isDevEnv();
50+
AddonFinder.getAddons().stream().anyMatch(IGTAddon::requiresHighTier) || GTCEu.isDev();
5351
highTierInitialized = true;
5452

5553
if (isHighTier()) GTCEu.LOGGER.info("High-Tier is Enabled.");

src/main/java/com/gregtechceu/gtceu/api/GTValues.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public static int[] tiersBetween(int minInclusive, int maxInclusive) {
104104

105105
public static final String MODID_TOP = "theoneprobe",
106106
MODID_JEI = "jei",
107+
MODID_REI = "roughlyenoughitems",
108+
MODID_EMI = "emi",
107109
MODID_APPENG = "ae2",
108110
MODID_KUBEJS = "kubejs",
109111
MODID_IRIS = "iris",

src/main/java/com/gregtechceu/gtceu/api/block/MaterialBlock.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.gregtechceu.gtceu.api.block;
22

3+
import com.gregtechceu.gtceu.GTCEu;
34
import com.gregtechceu.gtceu.api.blockentity.PipeBlockEntity;
45
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
56
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
@@ -10,8 +11,6 @@
1011
import com.gregtechceu.gtceu.config.ConfigHolder;
1112
import com.gregtechceu.gtceu.data.recipe.VanillaRecipeHelper;
1213

13-
import com.lowdragmc.lowdraglib.Platform;
14-
1514
import net.minecraft.MethodsReturnNonnullByDefault;
1615
import net.minecraft.client.color.block.BlockColor;
1716
import net.minecraft.core.BlockPos;
@@ -71,7 +70,7 @@ public MaterialBlock(Properties properties, TagPrefix tagPrefix, Material materi
7170
super(properties);
7271
this.material = material;
7372
this.tagPrefix = tagPrefix;
74-
if (registerModel && Platform.isClient()) {
73+
if (registerModel && GTCEu.isClientSide()) {
7574
MaterialBlockRenderer.create(this, tagPrefix.materialIconType(), material.getMaterialIconSet());
7675
}
7776
}

src/main/java/com/gregtechceu/gtceu/api/block/OreBlock.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.gregtechceu.gtceu.api.block;
22

3+
import com.gregtechceu.gtceu.GTCEu;
34
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
45
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
56
import com.gregtechceu.gtceu.client.renderer.block.OreBlockRenderer;
67
import com.gregtechceu.gtceu.config.ConfigHolder;
78
import com.gregtechceu.gtceu.integration.map.cache.server.ServerCache;
89

9-
import com.lowdragmc.lowdraglib.Platform;
10-
1110
import net.minecraft.core.BlockPos;
1211
import net.minecraft.server.level.ServerPlayer;
1312
import net.minecraft.world.InteractionHand;
@@ -21,7 +20,7 @@ public class OreBlock extends MaterialBlock {
2120

2221
public OreBlock(Properties properties, TagPrefix tagPrefix, Material material, boolean registerModel) {
2322
super(properties, tagPrefix, material, false);
24-
if (registerModel && Platform.isClient()) {
23+
if (registerModel && GTCEu.isClientSide()) {
2524
OreBlockRenderer.create(this);
2625
}
2726
}

src/main/java/com/gregtechceu/gtceu/api/blockentity/MetaMachineBlockEntity.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
import org.jetbrains.annotations.NotNull;
5050
import org.jetbrains.annotations.Nullable;
5151

52-
import java.util.*;
52+
import java.util.ArrayList;
53+
import java.util.Collections;
54+
import java.util.List;
55+
import java.util.Set;
5356

5457
/**
5558
* @author KilaBash
@@ -269,7 +272,7 @@ public static <T> LazyOptional<T> getCapability(MetaMachine machine, @NotNull Ca
269272
return GTCapability.CAPABILITY_DATA_ACCESS.orEmpty(cap, LazyOptional.of(() -> list.get(0)));
270273
}
271274
}
272-
if (GTCEu.isAE2Loaded()) {
275+
if (GTCEu.Mods.isAE2Loaded()) {
273276
if (cap == Capabilities.IN_WORLD_GRID_NODE_HOST) {
274277
if (machine instanceof IInWorldGridNodeHost nodeHost) {
275278
return Capabilities.IN_WORLD_GRID_NODE_HOST.orEmpty(cap, LazyOptional.of(() -> nodeHost));

src/main/java/com/gregtechceu/gtceu/api/capability/ICoverable.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.gregtechceu.gtceu.api.capability;
22

3+
import com.gregtechceu.gtceu.GTCEu;
34
import com.gregtechceu.gtceu.api.block.IAppearance;
45
import com.gregtechceu.gtceu.api.blockentity.ITickSubscription;
56
import com.gregtechceu.gtceu.api.cover.CoverBehavior;
67
import com.gregtechceu.gtceu.api.cover.CoverDefinition;
78
import com.gregtechceu.gtceu.api.transfer.fluid.IFluidHandlerModifiable;
89
import com.gregtechceu.gtceu.utils.GTUtil;
910

10-
import com.lowdragmc.lowdraglib.LDLib;
11-
1211
import net.minecraft.core.BlockPos;
1312
import net.minecraft.core.Direction;
1413
import net.minecraft.server.level.ServerPlayer;
@@ -169,7 +168,7 @@ default boolean hasCover(Direction facing) {
169168
}
170169

171170
default boolean isRemote() {
172-
return getLevel() == null ? LDLib.isRemote() : getLevel().isClientSide;
171+
return getLevel() == null ? GTCEu.isClientThread() : getLevel().isClientSide;
173172
}
174173

175174
default VoxelShape[] addCoverCollisionBoundingBox() {

src/main/java/com/gregtechceu/gtceu/api/cover/filter/FilterHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.gregtechceu.gtceu.api.cover.filter;
22

3+
import com.gregtechceu.gtceu.GTCEu;
34
import com.gregtechceu.gtceu.api.cover.CoverBehavior;
45
import com.gregtechceu.gtceu.api.gui.GuiTextures;
56
import com.gregtechceu.gtceu.api.gui.widget.SlotWidget;
67
import com.gregtechceu.gtceu.api.machine.MachineCoverContainer;
78
import com.gregtechceu.gtceu.api.machine.MetaMachine;
89
import com.gregtechceu.gtceu.api.transfer.item.CustomItemStackHandler;
910

10-
import com.lowdragmc.lowdraglib.LDLib;
1111
import com.lowdragmc.lowdraglib.gui.texture.GuiTextureGroup;
1212
import com.lowdragmc.lowdraglib.gui.widget.Widget;
1313
import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;
@@ -128,7 +128,7 @@ private CustomItemStackHandler getFilterSlot() {
128128
private void updateFilter() {
129129
var filterContainer = getFilterSlot();
130130

131-
if (LDLib.isRemote()) {
131+
if (GTCEu.isClientThread()) {
132132
if (!filterContainer.getStackInSlot(0).isEmpty() && !this.filterItem.isEmpty()) {
133133
return;
134134
}

src/main/java/com/gregtechceu/gtceu/api/data/chemical/material/info/MaterialIconSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public String toString() {
9898
}
9999

100100
public static void init() {
101-
if (GTCEu.isKubeJSLoaded()) {
101+
if (GTCEu.Mods.isKubeJSLoaded()) {
102102
GTRegistryInfo.registerFor(GTRegistryInfo.MATERIAL_ICON_SET.registryKey);
103103
}
104104
}

0 commit comments

Comments
 (0)