Mod integration API, readme update
This commit is contained in:
parent
f4a036b1e7
commit
429fe90cb7
4 changed files with 250 additions and 5 deletions
14
README.md
14
README.md
|
@ -1,26 +1,32 @@
|
|||
[![](https://jitpack.io/v/paulevsGitch/BCLib.svg)](https://jitpack.io/#paulevsGitch/BCLib)
|
||||
# BCLib
|
||||
BCLib is a library mod for BetterX team mods, developed for Fabric, MC 1.16.4+
|
||||
BCLib is a library mod for BetterX team mods, developed for Fabric, MC 1.16.5
|
||||
|
||||
## Features:
|
||||
### API:
|
||||
* Simple Mod Integration API;
|
||||
* Structure Features API;
|
||||
* World Data API;
|
||||
* Bonemeal API;
|
||||
* Features API;
|
||||
* Biome API;
|
||||
* Tag API;
|
||||
* Tag API.
|
||||
|
||||
### Libs:
|
||||
* Spline library (simple);
|
||||
* Noise library;
|
||||
* Math library;
|
||||
* SDF library;
|
||||
* SDF library.
|
||||
|
||||
### Helpers And Utils:
|
||||
* Custom surface builders;
|
||||
* Translation helper;
|
||||
* Weighted list;
|
||||
* Block helper;
|
||||
* Block helper.
|
||||
|
||||
### Rendering:
|
||||
* Procedural block models (from paterns or from code);
|
||||
* Block render layer interface.
|
||||
|
||||
## Importing:
|
||||
* Clone repo
|
||||
|
|
|
@ -8,7 +8,7 @@ yarn_mappings=6
|
|||
loader_version=0.11.3
|
||||
|
||||
# Mod Properties
|
||||
mod_version = 0.1.33
|
||||
mod_version = 0.1.34
|
||||
maven_group = ru.bclib
|
||||
archives_base_name = bclib
|
||||
|
||||
|
|
30
src/main/java/ru/bclib/api/ModIntegrationAPI.java
Normal file
30
src/main/java/ru/bclib/api/ModIntegrationAPI.java
Normal file
|
@ -0,0 +1,30 @@
|
|||
package ru.bclib.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import ru.bclib.integration.ModIntegration;
|
||||
|
||||
public class ModIntegrationAPI {
|
||||
private static final List<ModIntegration> INTEGRATIONS = Lists.newArrayList();
|
||||
|
||||
/**
|
||||
* Registers mod integration
|
||||
* @param integration
|
||||
* @return
|
||||
*/
|
||||
public static ModIntegration register(ModIntegration integration) {
|
||||
INTEGRATIONS.add(integration);
|
||||
integration.init();
|
||||
return integration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered mod integrations.
|
||||
* @return {@link List} of {@link ModIntegration}.
|
||||
*/
|
||||
public static List<ModIntegration> getIntegrations() {
|
||||
return INTEGRATIONS;
|
||||
}
|
||||
}
|
209
src/main/java/ru/bclib/integration/ModIntegration.java
Normal file
209
src/main/java/ru/bclib/integration/ModIntegration.java
Normal file
|
@ -0,0 +1,209 @@
|
|||
package ru.bclib.integration;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import net.fabricmc.fabric.api.tag.TagRegistry;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.data.BuiltinRegistries;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.BlockTags;
|
||||
import net.minecraft.tags.ItemTags;
|
||||
import net.minecraft.tags.Tag;
|
||||
import net.minecraft.tags.Tag.Named;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.levelgen.GenerationStep;
|
||||
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
|
||||
import net.minecraft.world.level.levelgen.feature.Feature;
|
||||
import ru.bclib.BCLib;
|
||||
import ru.bclib.world.features.BCLFeature;
|
||||
|
||||
public abstract class ModIntegration {
|
||||
private final String modID;
|
||||
|
||||
public void init() {}
|
||||
|
||||
public ModIntegration(String modID) {
|
||||
this.modID = modID;
|
||||
}
|
||||
|
||||
public ResourceLocation getID(String name) {
|
||||
return new ResourceLocation(modID, name);
|
||||
}
|
||||
|
||||
public Block getBlock(String name) {
|
||||
return Registry.BLOCK.get(getID(name));
|
||||
}
|
||||
|
||||
public Item getItem(String name) {
|
||||
return Registry.ITEM.get(getID(name));
|
||||
}
|
||||
|
||||
public BlockState getDefaultState(String name) {
|
||||
return getBlock(name).defaultBlockState();
|
||||
}
|
||||
|
||||
public ResourceKey<Biome> getKey(String name) {
|
||||
return ResourceKey.create(Registry.BIOME_REGISTRY, getID(name));
|
||||
}
|
||||
|
||||
public boolean modIsInstalled() {
|
||||
return FabricLoader.getInstance().isModLoaded(modID);
|
||||
}
|
||||
|
||||
public BCLFeature getFeature(String featureID, String configuredFeatureID, GenerationStep.Decoration featureStep) {
|
||||
Feature<?> feature = Registry.FEATURE.get(getID(featureID));
|
||||
ConfiguredFeature<?, ?> featureConfigured = BuiltinRegistries.CONFIGURED_FEATURE.get(getID(configuredFeatureID));
|
||||
return new BCLFeature(feature, featureConfigured, featureStep);
|
||||
}
|
||||
|
||||
public BCLFeature getFeature(String name, GenerationStep.Decoration featureStep) {
|
||||
return getFeature(name, name, featureStep);
|
||||
}
|
||||
|
||||
public ConfiguredFeature<?, ?> getConfiguredFeature(String name) {
|
||||
return BuiltinRegistries.CONFIGURED_FEATURE.get(getID(name));
|
||||
}
|
||||
|
||||
public Biome getBiome(String name) {
|
||||
return BuiltinRegistries.BIOME.get(getID(name));
|
||||
}
|
||||
|
||||
public Class<?> getClass(String path) {
|
||||
Class<?> cl = null;
|
||||
try {
|
||||
cl = Class.forName(path);
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
BCLib.LOGGER.error(e.getMessage());
|
||||
if (BCLib.isDevEnvironment()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return cl;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Object> T getStaticFieldValue(Class<?> cl, String name) {
|
||||
if (cl != null) {
|
||||
try {
|
||||
Field field = cl.getDeclaredField(name);
|
||||
if (field != null) {
|
||||
return (T) field.get(null);
|
||||
}
|
||||
}
|
||||
catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object getFieldValue(Class<?> cl, String name, Object classInstance) {
|
||||
if (cl != null) {
|
||||
try {
|
||||
Field field = cl.getDeclaredField(name);
|
||||
if (field != null) {
|
||||
return field.get(classInstance);
|
||||
}
|
||||
}
|
||||
catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Method getMethod(Class<?> cl, String functionName, Class<?>... args) {
|
||||
if (cl != null) {
|
||||
try {
|
||||
return cl.getMethod(functionName, args);
|
||||
}
|
||||
catch (NoSuchMethodException | SecurityException e) {
|
||||
BCLib.LOGGER.error(e.getMessage());
|
||||
if (BCLib.isDevEnvironment()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object executeMethod(Object instance, Method method, Object... args) {
|
||||
if (method != null) {
|
||||
try {
|
||||
return method.invoke(instance, args);
|
||||
}
|
||||
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
BCLib.LOGGER.error(e.getMessage());
|
||||
if (BCLib.isDevEnvironment()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object getAndExecuteStatic(Class<?> cl, String functionName, Object... args) {
|
||||
if (cl != null) {
|
||||
Class<?>[] classes = new Class<?>[args.length];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
classes[i] = args[i].getClass();
|
||||
}
|
||||
Method method = getMethod(cl, functionName, classes);
|
||||
return executeMethod(null, method, args);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Object> T getAndExecuteRuntime(Class<?> cl, Object instance, String functionName, Object... args) {
|
||||
if (instance != null) {
|
||||
Class<?>[] classes = new Class<?>[args.length];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
classes[i] = args[i].getClass();
|
||||
}
|
||||
Method method = getMethod(cl, functionName, classes);
|
||||
return (T) executeMethod(instance, method, args);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object newInstance(Class<?> cl, Object... args) {
|
||||
if (cl != null) {
|
||||
for (Constructor<?> constructor: cl.getConstructors()) {
|
||||
if (constructor.getParameterCount() == args.length) {
|
||||
try {
|
||||
return constructor.newInstance(args);
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
BCLib.LOGGER.error(e.getMessage());
|
||||
if (BCLib.isDevEnvironment()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Tag.Named<Item> getItemTag(String name) {
|
||||
ResourceLocation id = getID(name);
|
||||
Tag<Item> tag = ItemTags.getAllTags().getTag(id);
|
||||
return tag == null ? (Named<Item>) TagRegistry.item(id) : (Named<Item>) tag;
|
||||
}
|
||||
|
||||
public Tag.Named<Block> getBlockTag(String name) {
|
||||
ResourceLocation id = getID(name);
|
||||
Tag<Block> tag = BlockTags.getAllTags().getTag(id);
|
||||
return tag == null ? (Named<Block>) TagRegistry.block(id) : (Named<Block>) tag;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue