From fd3ba6a55e5f607912383998cad9e21ddf83a5f0 Mon Sep 17 00:00:00 2001 From: payonel Date: Sat, 6 Oct 2018 18:32:10 -0700 Subject: [PATCH] add scanContentsAt debug card method --- .../cil/oc/server/component/DebugCard.scala | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/scala/li/cil/oc/server/component/DebugCard.scala b/src/main/scala/li/cil/oc/server/component/DebugCard.scala index a1be1d594..e5e58b45b 100644 --- a/src/main/scala/li/cil/oc/server/component/DebugCard.scala +++ b/src/main/scala/li/cil/oc/server/component/DebugCard.scala @@ -23,10 +23,13 @@ import li.cil.oc.server.network.DebugNetwork.DebugNode import li.cil.oc.server.component.DebugCard.{AccessContext, CommandSender} import li.cil.oc.util.BlockPosition import li.cil.oc.util.ExtendedArguments._ +import li.cil.oc.util.ExtendedBlock._ import li.cil.oc.util.ExtendedNBT._ import li.cil.oc.util.ExtendedWorld._ import li.cil.oc.util.InventoryUtils import net.minecraft.block.Block +import net.minecraft.entity.item.EntityMinecart +import net.minecraft.entity.{Entity, EntityLivingBase} import net.minecraft.entity.player.EntityPlayerMP import net.minecraft.item.Item import net.minecraft.item.ItemStack @@ -37,10 +40,11 @@ import net.minecraft.tileentity.TileEntity import net.minecraft.util.IChatComponent import net.minecraft.world.{World, WorldServer, WorldSettings} import net.minecraft.world.WorldSettings.GameType -import net.minecraftforge.common.DimensionManager +import net.minecraftforge.common.{DimensionManager, MinecraftForge} import net.minecraftforge.common.util.FakePlayer import net.minecraftforge.common.util.FakePlayerFactory import net.minecraftforge.common.util.ForgeDirection +import net.minecraftforge.event.world.BlockEvent import net.minecraftforge.fluids.FluidRegistry import net.minecraftforge.fluids.FluidStack import net.minecraftforge.fluids.IFluidHandler @@ -129,6 +133,50 @@ class DebugCard(host: EnvironmentHost) extends prefab.ManagedEnvironment with De result(MinecraftServer.getServer.getAllUsernames) } + + @Callback(doc = "function(x: number, y: number, z: number[, worldId: number]):boolean, string, table -- returns contents at the location in world by id (default host world)") + def scanContentsAt(context: Context, args: Arguments): Array[AnyRef] = { + checkAccess() + val x = args.checkInteger(0) + val y = args.checkInteger(1) + val z = args.checkInteger(2) + val worldServer = if (args.count() > 3) DimensionManager.getWorld(args.checkInteger(3)) else host.world + val world = worldServer.world + + val position: BlockPosition = new BlockPosition(x, y, z, Option(world)) + val fakePlayer = FakePlayerFactory.get(world.asInstanceOf[WorldServer], Settings.get.fakePlayerProfile) + fakePlayer.posX = position.x + 0.5 + fakePlayer.posY = position.y + 0.5 + fakePlayer.posZ = position.z + 0.5 + + Option(world.findNearestEntityWithinAABB(classOf[Entity], position.bounds, fakePlayer)) match { + case Some(living: EntityLivingBase) => result(true, "EntityLivingBase", living) + case Some(minecart: EntityMinecart) => result(true, "EntityMinecart", minecart) + case _ => + val block = world.getBlock(position) + val metadata = world.getBlockMetadata(position) + if (block.isAir(position)) { + result(false, "air", block) + } + else if (FluidRegistry.lookupFluidForBlock(block) != null) { + val event = new BlockEvent.BreakEvent(position.x, position.y, position.z, world, block, metadata, fakePlayer) + MinecraftForge.EVENT_BUS.post(event) + result(event.isCanceled, "liquid", block) + } + else if (block.isReplaceable(position)) { + val event = new BlockEvent.BreakEvent(position.x, position.y, position.z, world, block, metadata, fakePlayer) + MinecraftForge.EVENT_BUS.post(event) + result(event.isCanceled, "replaceable", block) + } + else if (block.getCollisionBoundingBoxFromPool(position) == null) { + result(true, "passable", block) + } + else { + result(true, "solid", block) + } + } + } + @Callback(doc = """function(name:string):boolean -- Get whether a mod or API is loaded.""") def isModLoaded(context: Context, args: Arguments): Array[AnyRef] = { checkAccess()