Awareness block #53
@ -2,8 +2,10 @@ package net.banutama.utamacraft;
|
|||||||
|
|
||||||
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.AwarenessBlockEntity;
|
||||||
import net.banutama.utamacraft.block.entity.InsolatorBlockEntity;
|
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.AwarenessBlockPeripheral;
|
||||||
import net.banutama.utamacraft.integrations.computercraft.peripheral.InsolatorPeripheral;
|
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;
|
||||||
@ -29,6 +31,8 @@ public class CCRegistration {
|
|||||||
TURTLE_SERIALIZERS.register(bus);
|
TURTLE_SERIALIZERS.register(bus);
|
||||||
|
|
||||||
peripheralProvider.registerBlockPeripheral(InsolatorPeripheral::new, InsolatorBlockEntity.class::isInstance);
|
peripheralProvider.registerBlockPeripheral(InsolatorPeripheral::new, InsolatorBlockEntity.class::isInstance);
|
||||||
|
peripheralProvider.registerBlockPeripheral(AwarenessBlockPeripheral::new,
|
||||||
|
AwarenessBlockEntity.class::isInstance);
|
||||||
|
|
||||||
ForgeComputerCraftAPI.registerPeripheralProvider(peripheralProvider);
|
ForgeComputerCraftAPI.registerPeripheralProvider(peripheralProvider);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,83 @@
|
|||||||
|
package net.banutama.utamacraft.integrations.computercraft.peripheral;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import dan200.computercraft.api.lua.IArguments;
|
||||||
|
import dan200.computercraft.api.lua.LuaException;
|
||||||
|
import dan200.computercraft.api.lua.LuaFunction;
|
||||||
|
import dan200.computercraft.api.lua.MethodResult;
|
||||||
|
import net.banutama.utamacraft.block.entity.AwarenessBlockEntity;
|
||||||
|
import net.banutama.utamacraft.util.LuaConverter;
|
||||||
|
import net.banutama.utamacraft.util.WorldScan;
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.world.level.Level;
|
||||||
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
|
import net.minecraftforge.registries.ForgeRegistries;
|
||||||
|
|
||||||
|
public class AwarenessBlockPeripheral extends BasePeripheral {
|
||||||
|
public static final String PERIPHERAL_TYPE = "awareness_block";
|
||||||
|
|
||||||
|
protected AwarenessBlockPeripheral(BasePeripheralOwner owner) {
|
||||||
|
super(PERIPHERAL_TYPE, owner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AwarenessBlockPeripheral(BlockEntity blockEntity) {
|
||||||
|
this(new BlockEntityPeripheralOwner(blockEntity));
|
||||||
|
}
|
||||||
|
|
||||||
|
@LuaFunction(mainThread = true)
|
||||||
|
public final @NotNull MethodResult scan(@NotNull IArguments arguments) throws LuaException {
|
||||||
|
int radius = arguments.getInt(0);
|
||||||
|
if (radius < 1) {
|
||||||
|
return MethodResult.of(null, "Radius must be greater than zero");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(owner instanceof BlockEntityPeripheralOwner blockOwner)) {
|
||||||
|
return MethodResult.of(null, "Owner of this AwarenessBlockPeripheral is not a BlockEntityPeripheralOwner");
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockEntity blockEntity = blockOwner.getBlockEntity();
|
||||||
|
if (!(blockEntity instanceof AwarenessBlockEntity block)) {
|
||||||
|
return MethodResult.of(null,
|
||||||
|
"Owner of this AwarenessBlockEntity has a BlockEntityProviderOwner with a BlockEntity that is not an AwarenessBlockEntity");
|
||||||
|
}
|
||||||
|
|
||||||
|
Level level = blockEntity.getLevel();
|
||||||
|
BlockPos origin = blockEntity.getBlockPos();
|
||||||
|
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
|
||||||
|
{
|
||||||
|
Map<String, Object> originMap = new HashMap<>();
|
||||||
|
originMap.put("x", origin.getX());
|
||||||
|
originMap.put("y", origin.getY());
|
||||||
|
originMap.put("z", origin.getZ());
|
||||||
|
result.put("origin", originMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Map<String, ?>> blocks = new ArrayList<>();
|
||||||
|
WorldScan.scanBlocks(level, origin, radius, true, (state, pos) -> {
|
||||||
|
HashMap<String, Object> blockInfo = new HashMap<>(6);
|
||||||
|
|
||||||
|
blockInfo.put("x", pos.getX());
|
||||||
|
blockInfo.put("y", pos.getY());
|
||||||
|
blockInfo.put("z", pos.getZ());
|
||||||
|
|
||||||
|
ResourceLocation name = ForgeRegistries.BLOCKS.getKey(state.getBlock());
|
||||||
|
blockInfo.put("name", name == null ? "unknown" : name.toString());
|
||||||
|
blockInfo.put("tags", LuaConverter.tagsToList(() -> state.getBlock().builtInRegistryHolder().tags()));
|
||||||
|
|
||||||
|
blocks.add(blockInfo);
|
||||||
|
});
|
||||||
|
|
||||||
|
result.put("blocks", blocks);
|
||||||
|
|
||||||
|
return MethodResult.of(result);
|
||||||
|
}
|
||||||
|
}
|
19
src/main/java/net/banutama/utamacraft/util/LuaConverter.java
Normal file
19
src/main/java/net/banutama/utamacraft/util/LuaConverter.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package net.banutama.utamacraft.util;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import net.minecraft.tags.TagKey;
|
||||||
|
|
||||||
|
public class LuaConverter {
|
||||||
|
public static <T> List<String> tagsToList(@NotNull Supplier<Stream<TagKey<T>>> tags) {
|
||||||
|
return tags.get().map(LuaConverter::tagToString).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> String tagToString(@NotNull TagKey<T> tag) {
|
||||||
|
return tag.registry().location() + "/" + tag.location();
|
||||||
|
}
|
||||||
|
}
|
30
src/main/java/net/banutama/utamacraft/util/WorldScan.java
Normal file
30
src/main/java/net/banutama/utamacraft/util/WorldScan.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package net.banutama.utamacraft.util;
|
||||||
|
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.world.level.Level;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
|
||||||
|
public class WorldScan {
|
||||||
|
public static void scanBlocks(Level level, BlockPos origin, int r, boolean relative,
|
||||||
|
BiConsumer<BlockState, BlockPos> consumer) {
|
||||||
|
for (int x = origin.getX() - r; x <= origin.getX() + r; ++x) {
|
||||||
|
for (int y = origin.getY() - r; y <= origin.getY() + r; ++y) {
|
||||||
|
for (int z = origin.getZ() - r; z <= origin.getZ() + r; ++z) {
|
||||||
|
BlockPos pos = new BlockPos(x, y, z);
|
||||||
|
BlockState state = level.getBlockState(pos);
|
||||||
|
|
||||||
|
if (!state.isAir()) {
|
||||||
|
if (relative) {
|
||||||
|
consumer.accept(state,
|
||||||
|
new BlockPos(origin.getX() - x, origin.getY() - y, origin.getZ() - z));
|
||||||
|
} else {
|
||||||
|
consumer.accept(state, pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user