Awareness block #53

Merged
BlakeRain merged 13 commits from BlakeRain/utamacraft:27-cc-awareness into main 2024-01-18 12:03:14 +00:00
Showing only changes of commit b86b7ebf12 - Show all commits

View File

@ -17,7 +17,11 @@ 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.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.registries.ForgeRegistries;
public class AwarenessBlockPeripheral extends BasePeripheral {
@ -63,21 +67,59 @@ public class AwarenessBlockPeripheral extends BasePeripheral {
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);
blocks.add(describeBlock(level, state, pos));
});
result.put("blocks", blocks);
return MethodResult.of(result);
}
private static HashMap<String, Object> describeBlock(Level level, BlockState state, BlockPos pos) {
HashMap<String, Object> blockInfo = new HashMap<>(5);
blockInfo.put("x", pos.getX());
blockInfo.put("y", pos.getY());
blockInfo.put("z", pos.getZ());
Block block = state.getBlock();
ResourceLocation name = ForgeRegistries.BLOCKS.getKey(block);
blockInfo.put("name", name == null ? "unknown" : name.toString());
blockInfo.put("tags", LuaConverter.tagsToList(() -> block.builtInRegistryHolder().tags()));
describeBlockEntity(blockInfo, level, pos);
return blockInfo;
}
private static void describeBlockEntity(HashMap<String, Object> blockInfo, Level level, BlockPos pos) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity == null) {
return;
}
describeInventory(blockInfo, blockEntity);
}
private static void describeInventory(HashMap<String, Object> blockInfo, BlockEntity entity) {
IItemHandler inventory = entity.getCapability(ForgeCapabilities.ITEM_HANDLER).orElse(null);
if (inventory == null) {
return;
}
List<Map<String, ?>> items = new ArrayList<>();
for (int i = 0; i < inventory.getSlots(); ++i) {
HashMap<String, Object> itemInfo = new HashMap<>(2);
ResourceLocation itemName = ForgeRegistries.ITEMS.getKey(inventory.getStackInSlot(i).getItem());
itemInfo.put("name", itemName == null ? "unknown" : itemName.toString());
itemInfo.put("count", inventory.getStackInSlot(i).getCount());
items.add(itemInfo);
}
HashMap<String, Object> inventoryInfo = new HashMap<>(2);
inventoryInfo.put("size", inventory.getSlots());
inventoryInfo.put("items", items);
blockInfo.put("inventory", inventoryInfo);
}
}