Skip to content

Commit

Permalink
more work on changing to a java services system
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Jun 13, 2024
1 parent 708babf commit a5fa093
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 55 deletions.
15 changes: 3 additions & 12 deletions common/src/main/java/muramasa/antimatter/AntimatterAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,27 +435,18 @@ else if (registrar.isEnabled()) {
registerInternal(IAntimatterRegistrar.class, registrar.getId(), registrar.getDomain(), registrar);
}
}
registerEventBus();
}

@ExpectPlatform
private static void registerEventBus(){
throw new AssertionError();
}

@ExpectPlatform
public static boolean isRegistryEntry(Object object, String domain){
throw new AssertionError();
return AntimatterAPIPlatformHelper.INSTANCE.isRegistryEntry(object, domain);
}

@ExpectPlatform
public static void registerTransferApi(BlockEntityType<? extends BlockEntityMachine<?>> type){
throw new AssertionError();
AntimatterAPIPlatformHelper.INSTANCE.registerTransferApi(type);
}

@ExpectPlatform
public static void registerTransferApiPipe(BlockEntityType<? extends BlockEntityPipe<?>> type){
throw new AssertionError();
AntimatterAPIPlatformHelper.INSTANCE.registerTransferApiPipe(type);
}

public static Optional<IAntimatterRegistrar> getRegistrar(String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package muramasa.antimatter;

import dev.architectury.injectables.annotations.ExpectPlatform;
import muramasa.antimatter.blockentity.BlockEntityMachine;
import muramasa.antimatter.blockentity.pipe.BlockEntityPipe;
import muramasa.antimatter.util.ImplLoader;
import net.minecraft.world.level.block.entity.BlockEntityType;

public interface AntimatterAPIPlatformHelper {
AntimatterAPIPlatformHelper INSTANCE = ImplLoader.load(AntimatterAPIPlatformHelper.class);

boolean isRegistryEntry(Object object, String domain);


void registerTransferApi(BlockEntityType<? extends BlockEntityMachine<?>> type);


void registerTransferApiPipe(BlockEntityType<? extends BlockEntityPipe<?>> type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.*;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -164,4 +165,9 @@ public static void setRenderLayer(Block block, RenderType renderType){
public static void setRenderLayer(Fluid fluid, RenderType renderType){
throw new AssertionError();
}

@ExpectPlatform
public static void registerProperty(Item item, ResourceLocation location, ClampedItemPropertyFunction function){
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,21 @@ public static Vector3f normalFromQuad(BakedQuad quad, int index) {


public static void registerBatteryPropertyOverrides(ItemBattery battery) {
registerProperty(battery, new ResourceLocation(Ref.ID, "battery"), (stack, world, living, some_int) -> {
ModelUtils.registerProperty(battery, new ResourceLocation(Ref.ID, "battery"), (stack, world, living, some_int) -> {
Optional<IEnergyHandlerItem> handler = TesseractCapUtils.INSTANCE.getEnergyHandlerItem(stack);
return handler.map(h -> ((float) h.getEnergy() / (float) h.getCapacity())).orElse(1.0F);
});
}

public static void registerProbePropertyOverrides(MaterialArmor armor) {
registerProperty(armor, new ResourceLocation(Ref.ID, "probe"), (stack, world, living, some_int) -> {
ModelUtils.registerProperty(armor, new ResourceLocation(Ref.ID, "probe"), (stack, world, living, some_int) -> {
CompoundTag nbt = stack.getTag();
return nbt != null && nbt.contains("theoneprobe") && nbt.getBoolean("theoneprobe") ? 1.0F : 0.0F;
});
}

@ExpectPlatform
public static void registerProperty(Item item, ResourceLocation location, ClampedItemPropertyFunction function){
throw new AssertionError();
ModelUtils.registerProperty(item, location, function);
}

public static void drawFluid(PoseStack mstack, Minecraft mc, int posX, int posY, int width, int height, int scaledAmount, FluidHolder stack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
import java.util.ServiceLoader;

public interface AntimatterPreLaunchUtil {
AntimatterPreLaunchUtil INSTANCE = ServiceLoader.load(AntimatterPreLaunchUtil.class).findFirst().orElseThrow(() -> new IllegalStateException("No implementation of AntimatterPreLaunchUtil found"));
AntimatterPreLaunchUtil INSTANCE = ImplLoader.load(AntimatterPreLaunchUtil.class);
boolean isModLoaded(String modid);
}
11 changes: 11 additions & 0 deletions common/src/main/java/muramasa/antimatter/util/ImplLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package muramasa.antimatter.util;

import java.util.ServiceLoader;

public class ImplLoader {
public static <T> T load(Class<T> clazz) {
return ServiceLoader.load(clazz)
.findFirst()
.orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.*;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -103,4 +106,8 @@ public static void setRenderLayer(Block block, RenderType renderType){
public static void setRenderLayer(Fluid fluid, RenderType renderType){
BlockRenderLayerMap.INSTANCE.putFluid(fluid, renderType);
}

public static void registerProperty(Item item, ResourceLocation location, ClampedItemPropertyFunction function){
ItemProperties.register(item, location, function);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import earth.terrarium.botarium.common.fluid.base.FluidContainer;
import earth.terrarium.botarium.fabric.fluid.storage.FabricBlockFluidContainer;
import muramasa.antimatter.AntimatterAPI;
import muramasa.antimatter.AntimatterAPIPlatformHelper;
import muramasa.antimatter.blockentity.BlockEntityMachine;
import muramasa.antimatter.blockentity.pipe.*;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage;
Expand All @@ -21,9 +22,9 @@
import tesseract.api.item.ExtendedItemContainer;
import tesseract.fabric.TesseractImpl;

public class AntimatterAPIImpl {
public class AntimatterAPIImpl implements AntimatterAPIPlatformHelper {
@SuppressWarnings("UnstableApiUsage")
public static void registerTransferApi(BlockEntityType<? extends BlockEntityMachine<?>> type){
public void registerTransferApi(BlockEntityType<? extends BlockEntityMachine<?>> type){
FluidStorage.SIDED.registerForBlockEntity((be, direction) -> be.fluidHandler.side(direction).map(f -> new FabricBlockFluidContainer(f, t -> {}, be)).orElse(null), type);
ItemStorage.SIDED.registerForBlockEntity((be, direction) -> be.itemHandler.side(direction).map(ExtendedContainerWrapper::new).orElse(null), type);
TesseractLookups.ENERGY_HANDLER_SIDED.registerForBlockEntity((be, direction) -> be.energyHandler.map(i -> i).orElse(null), type);
Expand All @@ -33,7 +34,7 @@ public static void registerTransferApi(BlockEntityType<? extends BlockEntityMach
}
}

public static void registerTransferApiPipe(BlockEntityType<? extends BlockEntityPipe<?>> type){
public void registerTransferApiPipe(BlockEntityType<? extends BlockEntityPipe<?>> type){
FluidStorage.SIDED.registerForBlockEntity((be, direction) -> {
if (!(be instanceof BlockEntityFluidPipe<?> fluidPipe)) return null;
return (Storage<FluidVariant>) fluidPipe.getPipeCapHolder().side(direction).map(f -> new FabricBlockFluidContainer((FluidContainer) f, b -> {}, be)).orElse(null);
Expand Down Expand Up @@ -68,10 +69,8 @@ public static void registerItemTransferAPI(Item item){
}
}

public static void registerEventBus(){
}

public static boolean isRegistryEntry(Object object, String domain){
public boolean isRegistryEntry(Object object, String domain){
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
muramasa.antimatter.fabric.AntimatterAPIImpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.*;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -95,4 +98,8 @@ public static void setRenderLayer(Block block, RenderType renderType){
public static void setRenderLayer(Fluid fluid, RenderType renderType){
ItemBlockRenderTypes.setRenderLayer(fluid, renderType);
}

public static void registerProperty(Item item, ResourceLocation location, ClampedItemPropertyFunction function){
ItemProperties.register(item, location, function);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package muramasa.antimatter.forge;

import muramasa.antimatter.AntimatterAPIPlatformHelper;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.registries.IForgeRegistryEntry;

public class AntimatterAPIImpl {
public static void registerTransferApi(BlockEntityType<?> type){}
public class AntimatterAPIImpl implements AntimatterAPIPlatformHelper {
public void registerTransferApi(BlockEntityType<? extends muramasa.antimatter.blockentity.BlockEntityMachine<?>> type){}

public static void registerTransferApiPipe(BlockEntityType<?> type){}
public void registerTransferApiPipe(BlockEntityType<? extends muramasa.antimatter.blockentity.pipe.BlockEntityPipe<?>> type){}

public static void registerEventBus(){
//FMLJavaModLoadingContext.get().getModEventBus().register(AntimatterRegistration.class);
}

public static boolean isRegistryEntry(Object object, String domain){
public boolean isRegistryEntry(Object object, String domain){
return object instanceof IForgeRegistryEntry<?> r && r.getRegistryName() != null
&& r.getRegistryName().getNamespace().equals(domain);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
muramasa.antimatter.forge.AntimatterAPIImpl

0 comments on commit a5fa093

Please sign in to comment.