Skip to content

Commit

Permalink
Add NeoForge support
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Nov 11, 2023
1 parent 3b3c264 commit f2bf71d
Show file tree
Hide file tree
Showing 48 changed files with 3,339 additions and 6 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ subprojects {
"license": license,
"description": project.description,
"neoforge_version": neoforge_version,
"neoforge_loader_version_range": neoforge_loader_version_range
"neoforge_version_range": neoforge_version_range,
"neoforge_loader_version_range": neoforge_loader_version_range,
"pack_format_number": pack_format_number
]

filesMatching(['pack.mcmeta', 'fabric.mod.json', 'META-INF/mods.toml', '*.mixins.json']) {
Expand Down
4 changes: 2 additions & 2 deletions fabric/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ dependencies {
modRuntimeOnly "mezz.jei:jei-$jei_minecraft_version-fabric:$jei_version"
}

modCompileOnly "me.shedaniel.cloth:cloth-config-fabric:11.0.99"
modCompileOnly "com.terraformersmc:modmenu:$modmenu_version"
modImplementation "com.terraformersmc:modmenu:$modmenu_version"

modCompileOnly "me.shedaniel.cloth:cloth-config-fabric:11.0.99"
modCompileOnly("de.siphalor:amecsapi-1.20:1.5.2+mc1.20-pre1")
}
2 changes: 1 addition & 1 deletion forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ sourceSets.main.resources.srcDir 'src/generated/resources'

dependencies {
minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"
annotationProcessor 'org.spongepowered:mixin:0.8.5-SNAPSHOT:processor'
annotationProcessor "org.spongepowered:mixin:${mixin_version}:processor"
compileOnly project(":shared")

compileOnly "org.jetbrains:annotations:22.0.0"
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mod
mod_id=balm
mod_name=Balm
description=Abstraction Layer (but not really)? for Blay's multiplatform mods
description=Abstraction Layer (but not really) for Blay's multiplatform mods
version = 8.0.1
group=net.blay09.mods
license=All Rights Reserved
Expand Down Expand Up @@ -40,7 +40,7 @@ jei_minecraft_version=1.19.2
jei_version=11.4.0.288
rei_version=10.0.581
mixin_version=0.8.5
modmenu_version=7.2.2
modmenu_version=8.0.0

# Gradle
org.gradle.jvmargs=-Xmx3G
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package net.blay09.mods.balm.api.block.entity;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.mojang.datafixers.util.Pair;
import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.energy.EnergyStorage;
import net.blay09.mods.balm.api.fluid.FluidTank;
import net.blay09.mods.balm.api.provider.BalmProvider;
import net.blay09.mods.balm.api.provider.BalmProviderHolder;
import net.blay09.mods.balm.neoforge.container.BalmInvWrapper;
import net.blay09.mods.balm.neoforge.energy.NeoForgeEnergyStorage;
import net.blay09.mods.balm.neoforge.fluid.NeoForgeFluidTank;
import net.blay09.mods.balm.neoforge.provider.NeoForgeBalmProviders;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.Container;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.common.capabilities.Capabilities;
import net.neoforged.neoforge.common.capabilities.Capability;
import net.neoforged.neoforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BalmBlockEntityBase extends BlockEntity {

private final Map<Capability<?>, LazyOptional<?>> capabilities = new HashMap<>();
private final Table<Capability<?>, Direction, LazyOptional<?>> sidedCapabilities = HashBasedTable.create();
private boolean capabilitiesInitialized;

public BalmBlockEntityBase(BlockEntityType<?> blockEntityType, BlockPos blockPos, BlockState blockState) {
super(blockEntityType, blockPos, blockState);
}

private void addCapabilities(BalmProvider<?> provider, Map<Capability<?>, LazyOptional<?>> capabilities) {
NeoForgeBalmProviders forgeProviders = (NeoForgeBalmProviders) Balm.getProviders();
Capability<?> capability = forgeProviders.getCapability(provider.getProviderClass());
capabilities.put(capability, LazyOptional.of(provider::getInstance));

if (provider.getProviderClass() == Container.class) {
capabilities.put(Capabilities.ITEM_HANDLER, LazyOptional.of(() -> new BalmInvWrapper((Container) provider.getInstance())));
} else if (provider.getProviderClass() == FluidTank.class) {
capabilities.put(Capabilities.FLUID_HANDLER, LazyOptional.of(() -> new NeoForgeFluidTank((FluidTank) provider.getInstance())));
} else if (provider.getProviderClass() == EnergyStorage.class) {
capabilities.put(Capabilities.ENERGY, LazyOptional.of(() -> new NeoForgeEnergyStorage((EnergyStorage) provider.getInstance())));
}
}

@SuppressWarnings("unchecked")
public <T> T getProvider(Class<T> clazz) {
NeoForgeBalmProviders forgeProviders = (NeoForgeBalmProviders) Balm.getProviders();
Capability<?> capability = forgeProviders.getCapability(clazz);
return (T) getCapability(capability).resolve().orElse(null);
}

@NotNull
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
if (!capabilitiesInitialized) {
List<Object> providers = new ArrayList<>();
buildProviders(providers);

for (Object holder : providers) {
BalmProviderHolder providerHolder = (BalmProviderHolder) holder;
for (BalmProvider<?> provider : providerHolder.getProviders()) {
addCapabilities(provider, capabilities);
}

for (Pair<Direction, BalmProvider<?>> pair : providerHolder.getSidedProviders()) {
Direction direction = pair.getFirst();
BalmProvider<?> provider = pair.getSecond();
Map<Capability<?>, LazyOptional<?>> sidedCapabilities = this.sidedCapabilities.column(direction);
addCapabilities(provider, sidedCapabilities);
}
}
capabilitiesInitialized = true;
}

LazyOptional<?> result = null;
if (side != null) {
result = sidedCapabilities.get(cap, side);
}
if (result == null) {
result = capabilities.get(cap);
}
return result != null ? result.cast() : super.getCapability(cap, side);
}

protected abstract void buildProviders(List<Object> providers);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.blay09.mods.balm.api.event;

import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.ICancellableEvent;

public class BalmEvent extends Event implements ICancellableEvent {
}
38 changes: 38 additions & 0 deletions neoforge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.blay09.mods.balm.mixin;

import net.blay09.mods.balm.api.entity.BalmEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.Entity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Entity.class)
public class EntityMixin implements BalmEntity {

private CompoundTag fabricBalmData = new CompoundTag();

@Inject(method = "load(Lnet/minecraft/nbt/CompoundTag;)V", at = @At("HEAD"))
private void load(CompoundTag compound, CallbackInfo callbackInfo) {
if (compound.contains("BalmData")) {
fabricBalmData = compound.getCompound("BalmData");
}
}

@Inject(method = "saveWithoutId(Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/nbt/CompoundTag;", at = @At("HEAD"))
private void saveWithoutId(CompoundTag compound, CallbackInfoReturnable<CompoundTag> callbackInfo) {
compound.put("BalmData", fabricBalmData);
}

@Override
public CompoundTag getFabricBalmData() {
return fabricBalmData;
}

@Override
public void setFabricBalmData(CompoundTag tag) {
this.fabricBalmData = tag;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.blay09.mods.balm.mixin;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.event.client.ClientStartedEvent;
import net.minecraft.client.Minecraft;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Minecraft.class)
public class MinecraftMixin {
@Inject(method = "run()V", at = @At("HEAD"))
void run(CallbackInfo callbackInfo) {
final ClientStartedEvent event = new ClientStartedEvent(Minecraft.getInstance());
Balm.getEvents().fireEvent(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.blay09.mods.balm.mixin;

import net.blay09.mods.balm.api.client.BalmClient;
import net.blay09.mods.balm.neoforge.client.rendering.NeoForgeBalmModels;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.resources.ResourceLocation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.function.BiFunction;

@Mixin(ModelBakery.class)
public class ModelBakeryMixin {
@Inject(method = "bakeModels(Ljava/util/function/BiFunction;)V", at = @At("RETURN"))
private void apply(BiFunction<ResourceLocation, Material, TextureAtlasSprite> spriteBiFunction, CallbackInfo callbackInfo) {
((NeoForgeBalmModels) BalmClient.getModels()).onBakeModels((ModelBakery) (Object) this, spriteBiFunction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.blay09.mods.balm.neoforge;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Tables;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.registries.IForgeRegistry;

import java.util.Collection;

public class DeferredRegisters {
private static final Table<ResourceKey<?>, String, DeferredRegister<?>> deferredRegisters = Tables.synchronizedTable(HashBasedTable.create());

public static <T> DeferredRegister<T> get(IForgeRegistry<T> registry, String modId) {
return get(registry.getRegistryKey(), modId);
}

@SuppressWarnings("unchecked")
public static <T> DeferredRegister<T> get(ResourceKey<Registry<T>> registry, String modId) {
DeferredRegister<?> register = deferredRegisters.get(registry, modId);
if (register == null) {
register = DeferredRegister.create(registry, modId);
deferredRegisters.put(registry, modId, register);
}

return (DeferredRegister<T>) register;
}

public static Collection<DeferredRegister<?>> getByModId(String modId) {
return deferredRegisters.column(modId).values();
}

public static void register(String modId, IEventBus modEventBus) {
synchronized (deferredRegisters) {
for (DeferredRegister<?> deferredRegister : DeferredRegisters.getByModId(modId)) {
deferredRegister.register(modEventBus);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.blay09.mods.balm.neoforge;

import net.neoforged.fml.common.Mod;

@Mod("balm")
public class NeoForgeBalm {

public NeoForgeBalm() {
// ((AbstractBalmConfig) Balm.getConfig()).initialize();
// ExampleConfig.initialize();

// ForgeBalmWorldGen.initializeBalmBiomeModifiers();
// FMLJavaModLoadingContext.get().getModEventBus().addListener(ForgeBalmClient::onInitializeClient);

// ForgeBalmProviders providers = (ForgeBalmProviders) Balm.getProviders();
// providers.register(IItemHandler.class, new CapabilityToken<>() {
// });
// providers.register(IFluidHandler.class, new CapabilityToken<>() {
// });
// providers.register(IFluidHandlerItem.class, new CapabilityToken<>() {
// });
// providers.register(IEnergyStorage.class, new CapabilityToken<>() {
// });
// providers.register(Container.class, new CapabilityToken<>() {
// });
// providers.register(FluidTank.class, new CapabilityToken<>() {
// });
// providers.register(EnergyStorage.class, new CapabilityToken<>() {
// });
}

}
Loading

0 comments on commit f2bf71d

Please sign in to comment.