-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFactoryAPI.java
More file actions
285 lines (262 loc) · 11.9 KB
/
FactoryAPI.java
File metadata and controls
285 lines (262 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package wily.factoryapi;
import net.minecraft.core.Direction;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.Identifier;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.spongepowered.asm.mixin.MixinEnvironment;
import wily.factoryapi.base.config.FactoryCommonOptions;
import wily.factoryapi.base.config.FactoryConfig;
//? if fabric {
import net.fabricmc.api.EnvType;
import wily.factoryapi.base.fabric.FabricStorages;
import net.fabricmc.loader.api.FabricLoader;
//?} else if forge {
/*import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.LoadingModList;
*///?} else if neoforge {
/*import net.neoforged.fml.common.Mod;
import net.neoforged.fml.ModList;
import net.neoforged.fml.loading.LoadingModList;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
//? if >=1.21.9 {
/^import net.neoforged.fml.loading.FMLLoader;
^///?}
*///?}
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import wily.factoryapi.base.*;
import wily.factoryapi.base.network.*;
import wily.factoryapi.init.FactoryRegistries;
import wily.factoryapi.util.ModInfo;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
//? if forge || neoforge
/*@Mod(FactoryAPI.MOD_ID)*/
public class FactoryAPI {
public static final String MOD_ID = "factory_api";
public static final SecureExecutor SECURE_EXECUTOR = new SecureExecutor() {
@Override
public boolean isSecure() {
return true;
}
};
public static final Logger LOGGER = LogManager.getLogger(MOD_ID);
public static MinecraftServer currentServer;
public FactoryAPI(){
init();
//? if forge && <1.21.6 {
/*MinecraftForge.EVENT_BUS.<AttachCapabilitiesEvent<BlockEntity>, BlockEntity>addGenericListener(BlockEntity.class, event->{
if (event.getObject() instanceof IFactoryStorage be){
event.addCapability(FactoryAPI.createModLocation("fallback_capabilities"), new ICapabilityProvider() {
@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction arg) {
FactoryStorage<?> storage = FactoryAPIPlatform.BLOCK_CAPABILITY_MAP.get(capability);
ArbitrarySupplier<? extends IPlatformHandler> handler = be.getStorage(storage,arg);
if (storage != null && handler.isPresent())
return LazyOptional.of(handler::get).cast();
return LazyOptional.empty();
}
});
}
});
MinecraftForge.EVENT_BUS.<AttachCapabilitiesEvent<ItemStack>, ItemStack>addGenericListener(ItemStack.class,event->{
if (event.getObject().getItem() instanceof IFactoryItem i){
event.addCapability(FactoryAPI.createModLocation("item_fallback_capabilities"), new ICapabilityProvider() {
@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction arg) {
FactoryStorage<?> storage = FactoryAPIPlatform.ITEM_CAPABILITY_MAP.get(capability);
ArbitrarySupplier<? extends IPlatformHandler> handler = i.getStorage(storage, event.getObject());
if (storage != null && handler.isPresent())
return LazyOptional.of(handler::get).cast();
return LazyOptional.empty();
}
});
}
});
*///?} else if forge {
/*AttachCapabilitiesEvent.BlockEntities.BUS.addListener(event->{
if (event.getObject() instanceof IFactoryStorage be){
event.addCapability(FactoryAPI.createModLocation("fallback_capabilities"), new ICapabilityProvider() {
@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction arg) {
FactoryStorage<?> storage = FactoryAPIPlatform.BLOCK_CAPABILITY_MAP.get(capability);
ArbitrarySupplier<? extends IPlatformHandler> handler = be.getStorage(storage,arg);
if (storage != null && handler.isPresent())
return LazyOptional.of(handler::get).cast();
return LazyOptional.empty();
}
});
}
});
AttachCapabilitiesEvent.ItemStacks.BUS.addListener(event->{
if (event.getObject().getItem() instanceof IFactoryItem i){
event.addCapability(FactoryAPI.createModLocation("item_fallback_capabilities"), new ICapabilityProvider() {
@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction arg) {
FactoryStorage<?> storage = FactoryAPIPlatform.ITEM_CAPABILITY_MAP.get(capability);
ArbitrarySupplier<? extends IPlatformHandler> handler = i.getStorage(storage, event.getObject());
if (storage != null && handler.isPresent())
return LazyOptional.of(handler::get).cast();
return LazyOptional.empty();
}
});
}
});
*///?}
//? if forge || neoforge
/*if (isClient()) FactoryAPIClient.init();*/
}
public static void init() {
LOGGER.info("Initializing FactoryAPI and running mixin audit!");
MixinEnvironment.getCurrentEnvironment().audit();
FactoryConfig.registerCommonStorage(createModLocation("common"), FactoryCommonOptions.COMMON_STORAGE);
FactoryEvent.registerPayload(r->{
r.register( false, FactoryAPICommand.UIDefinitionPayload.ID);
r.register( false, HelloPayload.ID_S2C);
r.register( true, HelloPayload.ID_C2S);
r.register( false, CommonConfigSyncPayload.ID_S2C);
r.register( true, CommonConfigSyncPayload.ID_C2S);
r.register(false, OpenExtraMenuPayload.ID);
//? if >=1.21.2 {
r.register(false, CommonRecipeManager.ClientPayload.ID);
//?}
});
FactoryEvent.preServerTick(s-> SECURE_EXECUTOR.executeAll());
FactoryEvent.registerCommands(((commandSourceStackCommandDispatcher, commandBuildContext, commandSelection) -> FactoryAPICommand.register(commandSourceStackCommandDispatcher,commandBuildContext)));
FactoryRegistries.init();
FactoryIngredient.init();
FactoryEvent.setup(()->{
FactoryAPIPlatform.registerByClassArgumentType(FactoryAPICommand.JsonArgument.class, FactoryRegistries.JSON_ARGUMENT_TYPE.get());
FactoryCommonOptions.COMMON_STORAGE.load();
});
FactoryEvent.PlayerEvent.JOIN_EVENT.register(HelloPayload::sendInitialPayloads);
//? if >=1.21.2 {
Consumer<MinecraftServer> updateRecipes = server -> CommonRecipeManager.updateRecipes(server.getRecipeManager());
FactoryEvent.PlayerEvent.RELOAD_RESOURCES_EVENT.register(playerList-> {
updateRecipes.accept(playerList.getServer());
CommonNetwork.sendToPlayers(playerList.getPlayers(), CommonRecipeManager.ClientPayload.getInstance());
});
FactoryEvent.serverStarted(updateRecipes);
//?}
FactoryEvent.PlayerEvent.REMOVED_EVENT.register(sp->CommonNetwork.ENABLED_PLAYERS.removeAll(sp.getUUID()));
FactoryEvent.serverStopped(s-> {
SECURE_EXECUTOR.clear();
CommonNetwork.ENABLED_PLAYERS.clear();
//? if >=1.21.2 {
CommonRecipeManager.clearRecipes();
//?}
currentServer = null;
});
//? if fabric {
FabricStorages.registerDefaultStorages();
//?}
}
public static Identifier createLocation(String namespace, String path){
return Identifier.tryBuild(namespace,path);
}
public static Identifier createLocation(String location){
return Identifier.tryParse(location);
}
public static Identifier createModLocation(String path){
return createLocation(MOD_ID,path);
}
public static Identifier createVanillaLocation(String path){
//? if <1.20.5 {
/*return new Identifier(path);
*///?} else
return Identifier.withDefaultNamespace(path);
}
public static Loader getLoader() {
//? if fabric {
return Loader.FABRIC;
//?} elif forge {
/*return Loader.FORGE;
*///?} elif neoforge {
/*return Loader.NEOFORGE;
*///?} else
/*return null;*/
}
public static boolean isLoadingMod(String modId) {
//? if fabric {
return isModLoaded(modId);
//?} else if forge || (neoforge && <1.21.9) {
/*return LoadingModList.get().getModFileById(modId) != null;
*///?} else if neoforge {
/*return FMLLoader.getCurrent().getLoadingModList().getModFileById(modId) != null;
*///?} else
/*throw new AssertionError();*/
}
public static boolean isModLoaded(String modId) {
//? if fabric {
return FabricLoader.getInstance().isModLoaded(modId);
//?} else if forge || neoforge {
/*return ModList.get().isLoaded(modId);
*///?} else
/*throw new AssertionError();*/
}
public enum Loader {
FABRIC,FORGE,NEOFORGE;
public boolean isForgeLike(){
return this == FORGE || this == NEOFORGE;
}
public boolean isFabric(){
return this == FABRIC;
}
}
public static Path getConfigDirectory() {
//? if fabric {
return FabricLoader.getInstance().getConfigDir();
//?} elif forge || neoforge {
/*return FMLPaths.CONFIGDIR.get();
*///?} else
/*throw new AssertionError();*/
}
public static boolean isClient() {
//? if fabric {
return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT;
//?} else if forge || (neoforge && <1.21.9) {
/*return FMLEnvironment.dist.isClient();
*///?} else if neoforge {
/*return FMLEnvironment.getDist().isClient();
*///?} else
/*throw new AssertionError();*/
}
public static <T> Field getAccessibleField(Class<T> fieldClass, String field){
try {
Field f = fieldClass.getDeclaredField(field);
f.setAccessible(true);
return f;
} catch (NoSuchFieldException var1) {
throw new IllegalStateException("Couldn't get field %s for %s".formatted(field, fieldClass), var1);
}
}
public static <T> Map<String, Field> getAccessibleFieldsMap(Class<T> fieldsClass, String... fields){
Map<String,Field> map = new HashMap<>();
for (String s : fields) {
map.put(s, getAccessibleField(fieldsClass, s));
}
return map;
}
}