Add computercraft support for the Insolator #26
@ -56,7 +56,7 @@ mod_name=Utamacraft Mod
|
|||||||
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
|
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
|
||||||
mod_license=MIT License
|
mod_license=MIT License
|
||||||
# The mod version. See https://semver.org/
|
# The mod version. See https://semver.org/
|
||||||
mod_version=0.2.2-1.19
|
mod_version=0.2.3-1.19
|
||||||
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
|
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
|
||||||
# This should match the base package used for the mod sources.
|
# This should match the base package used for the mod sources.
|
||||||
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
||||||
|
@ -3,7 +3,9 @@ package net.banutama.utamacraft;
|
|||||||
import com.mojang.logging.LogUtils;
|
import com.mojang.logging.LogUtils;
|
||||||
import dan200.computercraft.api.ForgeComputerCraftAPI;
|
import dan200.computercraft.api.ForgeComputerCraftAPI;
|
||||||
import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser;
|
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.PeripheralProvider;
|
||||||
|
import net.banutama.utamacraft.integrations.computercraft.peripheral.InsolatorPeripheral;
|
||||||
import net.banutama.utamacraft.integrations.computercraft.turtles.TurtlePlayerUpgrade;
|
import net.banutama.utamacraft.integrations.computercraft.turtles.TurtlePlayerUpgrade;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
import net.minecraftforge.eventbus.api.IEventBus;
|
import net.minecraftforge.eventbus.api.IEventBus;
|
||||||
@ -26,6 +28,9 @@ public class CCRegistration {
|
|||||||
|
|
||||||
public static void register(IEventBus bus) {
|
public static void register(IEventBus bus) {
|
||||||
TURTLE_SERIALIZERS.register(bus);
|
TURTLE_SERIALIZERS.register(bus);
|
||||||
|
|
||||||
|
peripheralProvider.registerBlockPeripheral(InsolatorPeripheral::new, InsolatorBlockEntity.class::isInstance);
|
||||||
|
|
||||||
ForgeComputerCraftAPI.registerPeripheralProvider(peripheralProvider);
|
ForgeComputerCraftAPI.registerPeripheralProvider(peripheralProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,16 +2,35 @@ package net.banutama.utamacraft.integrations.computercraft;
|
|||||||
|
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||||
|
import net.banutama.utamacraft.integrations.computercraft.peripheral.BlockEntityPeripheral;
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.core.Direction;
|
import net.minecraft.core.Direction;
|
||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
import net.minecraftforge.common.util.LazyOptional;
|
import net.minecraftforge.common.util.LazyOptional;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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 {
|
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
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public LazyOptional<IPeripheral> getPeripheral(@NotNull Level world, @NotNull BlockPos pos, @NotNull Direction side) {
|
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();
|
return LazyOptional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,15 @@
|
|||||||
package net.banutama.utamacraft.integrations.computercraft.peripheral;
|
package net.banutama.utamacraft.integrations.computercraft.peripheral;
|
||||||
|
|
||||||
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
|
|
||||||
public class BlockEntityPeripheralOwner extends BasePeripheralOwner {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
|
|||||||
import dan200.computercraft.api.turtle.TurtleSide;
|
import dan200.computercraft.api.turtle.TurtleSide;
|
||||||
import net.banutama.utamacraft.integrations.computercraft.turtles.TurtlePlayerCache;
|
import net.banutama.utamacraft.integrations.computercraft.turtles.TurtlePlayerCache;
|
||||||
import net.minecraft.world.InteractionResult;
|
import net.minecraft.world.InteractionResult;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
public class PlayerPeripheral extends BasePeripheral {
|
public class PlayerPeripheral extends BasePeripheral {
|
||||||
@ -22,7 +23,7 @@ public class PlayerPeripheral extends BasePeripheral {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@LuaFunction(mainThread = true)
|
@LuaFunction(mainThread = true)
|
||||||
public final MethodResult use() {
|
public final @NotNull MethodResult use() {
|
||||||
if (!(owner instanceof TurtlePeripheralOwner turtleOwner)) {
|
if (!(owner instanceof TurtlePeripheralOwner turtleOwner)) {
|
||||||
LOGGER.info("Owner of this PlayerPeripheral is not a TurtlePeripheralOwner");
|
LOGGER.info("Owner of this PlayerPeripheral is not a TurtlePeripheralOwner");
|
||||||
return MethodResult.of();
|
return MethodResult.of();
|
||||||
|
@ -12,7 +12,6 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public abstract class PeripheralTurtleUpgrade<T extends BasePeripheral> extends AbstractTurtleUpgrade {
|
public abstract class PeripheralTurtleUpgrade<T extends BasePeripheral> extends AbstractTurtleUpgrade {
|
||||||
|
|
||||||
protected PeripheralTurtleUpgrade(ResourceLocation id, ItemStack item) {
|
protected PeripheralTurtleUpgrade(ResourceLocation id, ItemStack item) {
|
||||||
super(id, TurtleUpgradeType.PERIPHERAL, String.format("turtle.utamacraft.%s", id.getPath()), item);
|
super(id, TurtleUpgradeType.PERIPHERAL, String.format("turtle.utamacraft.%s", id.getPath()), item);
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"homepage": "https://git.blakerain.com/bans-minecraft/utamacraft",
|
"homepage": "https://git.blakerain.com/bans-minecraft/utamacraft",
|
||||||
"1.19.2": {
|
"1.19.2": {
|
||||||
|
"0.2.3-1.19": "Add CC peripheral for Insolator",
|
||||||
"0.2.2-1.19": "Fix bugs with Insolator",
|
"0.2.2-1.19": "Fix bugs with Insolator",
|
||||||
"0.2.1-1.19": "Fixed mixin bug",
|
"0.2.1-1.19": "Fixed mixin bug",
|
||||||
"0.2.0-1.19": "Added insolator and fire pendant",
|
"0.2.0-1.19": "Added insolator and fire pendant",
|
||||||
@ -9,7 +10,7 @@
|
|||||||
"0.0.1-1.19": "Initial release"
|
"0.0.1-1.19": "Initial release"
|
||||||
},
|
},
|
||||||
"promos": {
|
"promos": {
|
||||||
"1.19.2-latest": "0.2.2-1.19",
|
"1.19.2-latest": "0.2.3-1.19",
|
||||||
"1.19.2-recommended": "0.2.2-1.19"
|
"1.19.2-recommended": "0.2.3-1.19"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user