Add computercraft support for the Insolator #26

Merged
BlakeRain merged 2 commits from BlakeRain/utamacraft:main into main 2023-12-03 22:33:34 +00:00
8 changed files with 194 additions and 3 deletions
Showing only changes of commit e242ff1614 - Show all commits

View File

@ -3,7 +3,9 @@ package net.banutama.utamacraft;
import com.mojang.logging.LogUtils;
import dan200.computercraft.api.ForgeComputerCraftAPI;
import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser;
import net.banutama.utamacraft.block.entity.InsolatorBlockEntity;
import net.banutama.utamacraft.integrations.computercraft.PeripheralProvider;
import net.banutama.utamacraft.integrations.computercraft.peripheral.InsolatorPeripheral;
import net.banutama.utamacraft.integrations.computercraft.turtles.TurtlePlayerUpgrade;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.eventbus.api.IEventBus;
@ -26,6 +28,9 @@ public class CCRegistration {
public static void register(IEventBus bus) {
TURTLE_SERIALIZERS.register(bus);
peripheralProvider.registerBlockPeripheral(InsolatorPeripheral::new, InsolatorBlockEntity.class::isInstance);
ForgeComputerCraftAPI.registerPeripheralProvider(peripheralProvider);
}

View File

@ -2,16 +2,35 @@ package net.banutama.utamacraft.integrations.computercraft;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralProvider;
import net.banutama.utamacraft.integrations.computercraft.peripheral.BlockEntityPeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
public class PeripheralProvider implements IPeripheralProvider {
private final List<BlockEntityPeripheral> blockEntityPeripherals = new ArrayList<>();
public void registerBlockPeripheral(Function<BlockEntity, ? extends IPeripheral> build, Predicate<BlockEntity> predicate) {
blockEntityPeripherals.add(new BlockEntityPeripheral(build, predicate));
}
@NotNull
@Override
public LazyOptional<IPeripheral> getPeripheral(@NotNull Level world, @NotNull BlockPos pos, @NotNull Direction side) {
for (BlockEntityPeripheral peripheral : blockEntityPeripherals) {
if (peripheral.hasPeripheral(world, pos, side)) {
return LazyOptional.of(() -> peripheral.buildPeripheral(world, pos, side));
}
}
return LazyOptional.empty();
}
}

View File

@ -0,0 +1,39 @@
package net.banutama.utamacraft.integrations.computercraft.peripheral;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;
import java.util.function.Function;
import java.util.function.Predicate;
public class BlockEntityPeripheral {
private final Function<BlockEntity, ? extends IPeripheral> build;
private final Predicate<BlockEntity> predicate;
public BlockEntityPeripheral(Function<BlockEntity, ? extends IPeripheral> build, Predicate<BlockEntity> predicate) {
this.build = build;
this.predicate = predicate;
}
public boolean hasPeripheral(@NotNull Level level, @NotNull BlockPos pos, @NotNull Direction direction) {
BlockEntity entity = level.getBlockEntity(pos);
if (entity == null) {
return false;
}
return predicate.test(entity);
}
public @NotNull IPeripheral buildPeripheral(@NotNull Level level, @NotNull BlockPos pos, @NotNull Direction direction) {
BlockEntity entity = level.getBlockEntity(pos);
if (entity == null) {
throw new IllegalArgumentException("No BlockEntity at this location");
}
return build.apply(entity);
}
}

View File

@ -1,5 +1,15 @@
package net.banutama.utamacraft.integrations.computercraft.peripheral;
import net.minecraft.world.level.block.entity.BlockEntity;
public class BlockEntityPeripheralOwner extends BasePeripheralOwner {
// TODO: After we add PeripheralBlockEntity we want to add it to a field and constructor here.
private final BlockEntity blockEntity;
public BlockEntityPeripheralOwner(BlockEntity blockEntity) {
this.blockEntity = blockEntity;
}
public BlockEntity getBlockEntity() {
return blockEntity;
}
}

View File

@ -0,0 +1,56 @@
package net.banutama.utamacraft.integrations.computercraft.peripheral;
import com.mojang.logging.LogUtils;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import net.banutama.utamacraft.block.entity.InsolatorBlockEntity;
import net.banutama.utamacraft.integrations.computercraft.utils.WrapResult;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.items.ItemStackHandler;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import java.util.HashMap;
import java.util.Map;
public class InsolatorPeripheral extends BasePeripheral {
public static final String PERIPHERAL_TYPE = "insolator";
private static final Logger LOGGER = LogUtils.getLogger();
protected InsolatorPeripheral(BasePeripheralOwner owner) {
super(PERIPHERAL_TYPE, owner);
}
public InsolatorPeripheral(BlockEntity blockEntity) {
this(new BlockEntityPeripheralOwner(blockEntity));
}
@LuaFunction(mainThread = true)
public final @NotNull MethodResult getState() {
if (!(owner instanceof BlockEntityPeripheralOwner blockOwner)) {
return MethodResult.of(null, "Owner of this InsolatorPeripheral is not a BlockEntityPeripheralOwner");
}
BlockEntity blockEntity = blockOwner.getBlockEntity();
if (!(blockEntity instanceof InsolatorBlockEntity insolator)) {
return MethodResult.of(null, "Owner of this InsolatorPeripheral has a BlockEntityProviderOwner with a BlockEntity that is not an InsolatorBlockEntity");
}
Map<String, Object> result = new HashMap<>();
result.put("active", insolator.getActive());
result.put("progress", insolator.getProgress());
result.put("maxProgress", InsolatorBlockEntity.MAX_PROGRESS);
result.put("energy", WrapResult.wrap(insolator.getEnergy()));
insolator.getInventoryOptional().ifPresent(inventory -> result.put("inventory", WrapResult.wrap(inventory)));
FluidStack fluidStack = insolator.getFluidTank().getFluid();
if (!fluidStack.isEmpty()) {
result.put("fluid", WrapResult.wrap(fluidStack));
}
return MethodResult.of(result);
}
}

View File

@ -7,6 +7,7 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.TurtleSide;
import net.banutama.utamacraft.integrations.computercraft.turtles.TurtlePlayerCache;
import net.minecraft.world.InteractionResult;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
public class PlayerPeripheral extends BasePeripheral {
@ -22,7 +23,7 @@ public class PlayerPeripheral extends BasePeripheral {
}
@LuaFunction(mainThread = true)
public final MethodResult use() {
public final @NotNull MethodResult use() {
if (!(owner instanceof TurtlePeripheralOwner turtleOwner)) {
LOGGER.info("Owner of this PlayerPeripheral is not a TurtlePeripheralOwner");
return MethodResult.of();

View File

@ -12,7 +12,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class PeripheralTurtleUpgrade<T extends BasePeripheral> extends AbstractTurtleUpgrade {
protected PeripheralTurtleUpgrade(ResourceLocation id, ItemStack item) {
super(id, TurtleUpgradeType.PERIPHERAL, String.format("turtle.utamacraft.%s", id.getPath()), item);
}

View File

@ -0,0 +1,62 @@
package net.banutama.utamacraft.integrations.computercraft.utils;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.energy.EnergyStorage;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;
import java.util.*;
public class WrapResult {
public static Map<String, Object> wrap(FluidStack fluid) {
Map<String, Object> wrapped = new HashMap<>(2);
wrapped.put("name", getName(fluid.getFluid()).toString());
wrapped.put("amount", fluid.getAmount());
return wrapped;
}
public static Map<String, Object> wrap(EnergyStorage energy) {
Map<String, Object> wrapped = new HashMap<>(2);
wrapped.put("stored", energy.getEnergyStored());
wrapped.put("maxStored", energy.getMaxEnergyStored());
return wrapped;
}
public static Map<String, Object> wrap(ItemStack stack) {
Map<String, Object> wrapped = new HashMap<>(3);
wrapped.put("item", getName(stack.getItem()).toString());
wrapped.put("count", stack.getCount());
wrapped.put("maxStackSize", stack.getMaxStackSize());
return wrapped;
}
public static List<Object> wrap(IItemHandler itemHandler) {
List<Object> wrapped = new ArrayList<>(itemHandler.getSlots());
for (int slot = 0; slot < itemHandler.getSlots(); ++slot) {
Map<String, Object> inner = wrap(itemHandler.getStackInSlot(slot));
inner.put("slotLimit", itemHandler.getSlotLimit(slot));
wrapped.add(inner);
}
return wrapped;
}
public static ResourceLocation getName(Item item) {
return getName(ForgeRegistries.ITEMS, item);
}
public static ResourceLocation getName(Fluid fluid) {
return getName(ForgeRegistries.FLUIDS, fluid);
}
public static <T> ResourceLocation getName(IForgeRegistry<T> registry, T element) {
return registry.getKey(element);
}
}