mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-21 17:07:39 +00:00
Move our public API into separate modules
This adds two new modules: common-api and forge-api, which contain the common and Forge-specific interfaces for CC's Minecraft-specific API. We add a new PlatformHelper interface, which abstracts over some of the loader-specific functionality, such as reading registries[^1] or calling Forge-specific methods. This interface is then implemented in the main mod, and loaded via ServiceLoaders. Some other notes on this: - We now split shared and client-specific source code into separate modules. This is to make it harder to reference client code on the server, thus crashing the game. Eventually we'll split the main mod up too into separate source sets - this is, of course, a much bigger problem! - There's currently some nastiness here due to wanting to preserve binary compatibility of the API. We'll hopefully be able to remove this when 1.19.3 releases. - In order to build a separate Forge-specific API jar, we compile the common sources twice: once for the common jar and once for the Forge jar. Getting this to play nicely with IDEs is a little tricky and so we provide a cct.inlineProject(...) helper to handle everything. [^1]: We /can/ do this with vanilla's APIs, but it gives a lot of deprecation warnings. It just ends up being nicer to abstract over it.
This commit is contained in:
18
projects/forge-api/build.gradle.kts
Normal file
18
projects/forge-api/build.gradle.kts
Normal file
@@ -0,0 +1,18 @@
|
||||
plugins {
|
||||
id("cc-tweaked.forge")
|
||||
id("cc-tweaked.publishing")
|
||||
}
|
||||
|
||||
java {
|
||||
withJavadocJar()
|
||||
}
|
||||
|
||||
cct.inlineProject(":common-api")
|
||||
|
||||
dependencies {
|
||||
api(project(":core-api"))
|
||||
}
|
||||
|
||||
tasks.javadoc {
|
||||
include("dan200/computercraft/api/**/*.java")
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2022. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
package dan200.computercraft.api;
|
||||
|
||||
import dan200.computercraft.api.lua.GenericSource;
|
||||
import dan200.computercraft.api.network.wired.IWiredElement;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||
import dan200.computercraft.impl.ComputerCraftAPIForgeService;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* The forge-specific entrypoint for ComputerCraft's API.
|
||||
*/
|
||||
public final class ForgeComputerCraftAPI {
|
||||
// TODO(1.19.3): Rename me to ComputerCraftAPIForge
|
||||
|
||||
private ForgeComputerCraftAPI() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a peripheral provider to convert blocks into {@link IPeripheral} implementations.
|
||||
*
|
||||
* @param provider The peripheral provider to register.
|
||||
* @see IPeripheral
|
||||
* @see IPeripheralProvider
|
||||
*/
|
||||
public static void registerPeripheralProvider(@Nonnull IPeripheralProvider provider) {
|
||||
getInstance().registerPeripheralProvider(provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a capability that can be used by generic peripherals.
|
||||
*
|
||||
* @param capability The capability to register.
|
||||
* @see GenericSource
|
||||
*/
|
||||
public static void registerGenericCapability(@Nonnull Capability<?> capability) {
|
||||
getInstance().registerGenericCapability(capability);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the wired network element for a block in world.
|
||||
*
|
||||
* @param world The world the block exists in
|
||||
* @param pos The position the block exists in
|
||||
* @param side The side to extract the network element from
|
||||
* @return The element's node
|
||||
* @see IWiredElement#getNode()
|
||||
*/
|
||||
@Nonnull
|
||||
public static LazyOptional<IWiredElement> getWiredElementAt(@Nonnull BlockGetter world, @Nonnull BlockPos pos, @Nonnull Direction side) {
|
||||
return getInstance().getWiredElementAt(world, pos, side);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private static ComputerCraftAPIForgeService getInstance() {
|
||||
return ComputerCraftAPIForgeService.get();
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2022. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
package dan200.computercraft.api.detail;
|
||||
|
||||
import dan200.computercraft.impl.ComputerCraftAPIForgeService;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
/**
|
||||
* {@link DetailRegistry}s for Forge-specific types.
|
||||
*/
|
||||
public class ForgeDetailRegistries {
|
||||
/**
|
||||
* Provides details for {@link FluidStack}.
|
||||
*/
|
||||
public static final DetailRegistry<FluidStack> FLUID_STACK = ComputerCraftAPIForgeService.get().getFluidStackDetailRegistry();
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2022. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A base class for all events concerning a turtle. This will only ever constructed and fired on the server side,
|
||||
* so sever specific methods on {@link ITurtleAccess} are safe to use.
|
||||
* <p>
|
||||
* You should generally not need to subscribe to this event, preferring one of the more specific classes.
|
||||
*
|
||||
* @deprecated No longer needed, see {@link TurtleRefuelEvent}.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public abstract class TurtleEvent extends Event {
|
||||
private final ITurtleAccess turtle;
|
||||
|
||||
protected TurtleEvent(@Nonnull ITurtleAccess turtle) {
|
||||
Objects.requireNonNull(turtle, "turtle cannot be null");
|
||||
this.turtle = turtle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the turtle which is performing this action.
|
||||
*
|
||||
* @return The access for this turtle.
|
||||
*/
|
||||
@Nonnull
|
||||
public ITurtleAccess getTurtle() {
|
||||
return turtle;
|
||||
}
|
||||
}
|
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2022. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import dan200.computercraft.api.turtle.TurtleRefuelHandler;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Fired when a turtle attempts to refuel from an item.
|
||||
* <p>
|
||||
* One may use {@link #setHandler(Handler)} to register a custom fuel provider for a given item.
|
||||
*
|
||||
* @deprecated Use {@link TurtleRefuelHandler} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public class TurtleRefuelEvent extends TurtleEvent {
|
||||
private final ItemStack stack;
|
||||
private Handler handler;
|
||||
|
||||
public TurtleRefuelEvent(@Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack) {
|
||||
super(turtle);
|
||||
|
||||
Objects.requireNonNull(turtle, "turtle cannot be null");
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the stack we are attempting to refuel from.
|
||||
* <p>
|
||||
* Do not modify the returned stack - all modifications should be done within the {@link Handler}.
|
||||
*
|
||||
* @return The stack to refuel from.
|
||||
*/
|
||||
public ItemStack getStack() {
|
||||
return stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the refuel handler for this stack.
|
||||
*
|
||||
* @return The refuel handler, or {@code null} if none has currently been set.
|
||||
* @see #setHandler(Handler)
|
||||
*/
|
||||
@Nullable
|
||||
public Handler getHandler() {
|
||||
return handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the refuel handler for this stack.
|
||||
* <p>
|
||||
* You should call this if you can actually refuel from this item, and ideally only if there are no existing
|
||||
* handlers.
|
||||
*
|
||||
* @param handler The new refuel handler.
|
||||
* @see #getHandler()
|
||||
*/
|
||||
public void setHandler(@Nullable Handler handler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles refuelling a turtle from a specific item.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Handler {
|
||||
/**
|
||||
* Refuel a turtle using an item.
|
||||
*
|
||||
* @param turtle The turtle to refuel.
|
||||
* @param stack The stack to refuel with.
|
||||
* @param slot The slot the stack resides within. This may be used to modify the inventory afterwards.
|
||||
* @param limit The maximum number of refuel operations to perform. This will often correspond to the number of
|
||||
* items to consume.
|
||||
* @return The amount of fuel gained.
|
||||
*/
|
||||
int refuel(@Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack, int slot, int limit);
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
package dan200.computercraft.impl;
|
||||
|
||||
import dan200.computercraft.api.ForgeComputerCraftAPI;
|
||||
import dan200.computercraft.api.detail.DetailRegistry;
|
||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
/**
|
||||
* A Forge-specific version of {@link ComputerCraftAPIService}.
|
||||
* <p>
|
||||
* Do <strong>NOT</strong> directly reference this class. It exists for internal use by the API.
|
||||
*
|
||||
* @see ForgeComputerCraftAPI
|
||||
*/
|
||||
@ApiStatus.Internal
|
||||
public interface ComputerCraftAPIForgeService extends ComputerCraftAPIService {
|
||||
static ComputerCraftAPIForgeService get() {
|
||||
return (ComputerCraftAPIForgeService) ComputerCraftAPIService.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
void registerPeripheralProvider(IPeripheralProvider provider);
|
||||
|
||||
@Override
|
||||
void registerGenericCapability(Capability<?> capability);
|
||||
|
||||
DetailRegistry<FluidStack> getFluidStackDetailRegistry();
|
||||
}
|
Reference in New Issue
Block a user