-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b3c264
commit f2bf71d
Showing
48 changed files
with
3,339 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
neoforge/src/main/java/net/blay09/mods/balm/api/block/entity/BalmBlockEntityBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
neoforge/src/main/java/net/blay09/mods/balm/api/event/BalmEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
neoforge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
neoforge/src/main/java/net/blay09/mods/balm/mixin/MinecraftMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
neoforge/src/main/java/net/blay09/mods/balm/mixin/ModelBakeryMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
neoforge/src/main/java/net/blay09/mods/balm/neoforge/DeferredRegisters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>() { | ||
// }); | ||
} | ||
|
||
} |
Oops, something went wrong.