Search in sources :

Example 21 with IntVector3

use of com.bergerkiller.bukkit.common.bases.IntVector3 in project BKCommonLib by bergerhealer.

the class ItemFrameInfo method findLookPosition.

/**
 * Follows an eye ray to see if it lands on this item frame. If it does,
 * returns the exact coordinates on the display shown on this item frame.
 * If there is no map display being displayed, or the eye isn't looking at
 * this item frame, null is returned.<br>
 * <br>
 * Use {@link MapLookPosition#isWithinBounds()} to check whether the player is
 * looking within bounds of this item frame, or not.
 *
 * @param startPosition Start position of the eye ray
 * @param lookDirection Normalized direction vector of the eye ray
 * @return Map Look Position, or null if the eye ray doesn't land on this
 *         item frame, or this item frame isn't displaying a map display.
 */
public MapLookPosition findLookPosition(Vector startPosition, Vector lookDirection) {
    // If it shows no display, don't bother checking
    if (this.lastMapUUID == null) {
        return null;
    }
    // Check whether the item frame is invisible. If so, a different offset is used.
    boolean invisible = this.itemFrameHandle.getDataWatcher().getFlag(EntityHandle.DATA_FLAGS, EntityHandle.DATA_FLAG_INVISIBLE);
    // Offset from block face to canvas
    double FRAME_OFFSET = invisible ? 0.00625 : 0.0625;
    // Compare facing with the eye ray to calculate the eye distance to the item frame
    final double distance;
    boolean withinBounds = true;
    IntVector3 frameBlock = this.coordinates;
    BlockFace facing = this.itemFrameHandle.getFacing();
    switch(facing) {
        case NORTH:
            if (lookDirection.getZ() > 1e-10) {
                distance = (frameBlock.z + 1.0 - FRAME_OFFSET - startPosition.getZ()) / lookDirection.getZ();
            } else {
                withinBounds = false;
                distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
            }
            break;
        case SOUTH:
            if (lookDirection.getZ() < -1e-10) {
                distance = (frameBlock.z + FRAME_OFFSET - startPosition.getZ()) / lookDirection.getZ();
            } else {
                withinBounds = false;
                distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
            }
            break;
        case WEST:
            if (lookDirection.getX() > 1e-10) {
                distance = (frameBlock.x + 1.0 - FRAME_OFFSET - startPosition.getX()) / lookDirection.getX();
            } else {
                withinBounds = false;
                distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
            }
            break;
        case EAST:
            if (lookDirection.getX() < -1e-10) {
                distance = (frameBlock.x + FRAME_OFFSET - startPosition.getX()) / lookDirection.getX();
            } else {
                withinBounds = false;
                distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
            }
            break;
        case DOWN:
            if (lookDirection.getY() > 1e-10) {
                distance = (frameBlock.y + 1.0 - FRAME_OFFSET - startPosition.getY()) / lookDirection.getY();
            } else {
                withinBounds = false;
                distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
            }
            break;
        case UP:
            if (lookDirection.getY() < -1e-10) {
                distance = (frameBlock.y + FRAME_OFFSET - startPosition.getY()) / lookDirection.getY();
            } else {
                withinBounds = false;
                distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
            }
            break;
        default:
            throw new IllegalArgumentException("Invalid facing: " + facing);
    }
    // Add distance * lookDirection to startPosition and subtract item frame coordinates
    // to find the coordinates relative to the middle of the block that are looked at
    final double at_x = distance * lookDirection.getX() + startPosition.getX() - frameBlock.x - 0.5;
    final double at_y = distance * lookDirection.getY() + startPosition.getY() - frameBlock.y - 0.5;
    final double at_z = distance * lookDirection.getZ() + startPosition.getZ() - frameBlock.z - 0.5;
    // If outside range [-0.5 .. 0.5] then this item frame was not looked at
    double edgeDistance = Double.MAX_VALUE;
    if (withinBounds) {
        // Get distance from the edge of each coordinate space
        final Vector edge = new Vector(Math.max(0.0, Math.abs(at_x) - 0.5), Math.max(0.0, Math.abs(at_y) - 0.5), Math.max(0.0, Math.abs(at_z) - 0.5));
        edgeDistance = edge.length();
    }
    // Convert x/y/z into x/y using facing information
    double map_x, map_y;
    switch(facing) {
        case NORTH:
            map_x = 0.5 - at_x;
            map_y = 0.5 - at_y;
            break;
        case SOUTH:
            map_x = 0.5 + at_x;
            map_y = 0.5 - at_y;
            break;
        case WEST:
            map_x = 0.5 + at_z;
            map_y = 0.5 - at_y;
            break;
        case EAST:
            map_x = 0.5 - at_z;
            map_y = 0.5 - at_y;
            break;
        case DOWN:
            map_x = 0.5 + at_x;
            map_y = 0.5 - at_z;
            break;
        case UP:
            map_x = 0.5 + at_x;
            map_y = 0.5 + at_z;
            break;
        default:
            throw new IllegalArgumentException("Invalid facing: " + facing);
    }
    // Adjust the coordinates if a non-zero rotation is set
    switch(this.itemFrameHandle.getRotationOrdinal() & 0x3) {
        case 1:
            {
                double tmp = map_x;
                map_x = map_y;
                map_y = 1.0 - tmp;
                break;
            }
        case 2:
            {
                map_x = 1.0 - map_x;
                map_y = 1.0 - map_y;
                break;
            }
        case 3:
            {
                double tmp = map_x;
                map_x = 1.0 - map_y;
                map_y = tmp;
                break;
            }
        default:
            break;
    }
    // Change to pixel coordinates based on resolution and done!
    return new MapLookPosition(this, MapDisplayTile.RESOLUTION * (map_x + this.lastMapUUID.getTileX()), MapDisplayTile.RESOLUTION * (map_y + this.lastMapUUID.getTileY()), distance, edgeDistance);
}
Also used : MapLookPosition(com.bergerkiller.bukkit.common.map.util.MapLookPosition) BlockFace(org.bukkit.block.BlockFace) IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3) Vector(org.bukkit.util.Vector)

Example 22 with IntVector3

use of com.bergerkiller.bukkit.common.bases.IntVector3 in project BKCommonLib by bergerhealer.

the class NBTTest method testNBTCompoundGetPut.

@Test
public void testNBTCompoundGetPut() {
    CommonTagCompound compound = new CommonTagCompound();
    testGetPutRemove(compound, "integerType", 12);
    testGetPutRemove(compound, "longType", 15L);
    testGetPutRemove(compound, "doubleType", 12D);
    testGetPutRemove(compound, "floatType", 12F);
    testGetPutRemove(compound, "byteType", (byte) 12);
    testGetPutRemove(compound, "stringType", "hello, world!");
    testGetPutRemove(compound, "blockPos", new BlockLocation("World2", 4, 6, 8));
    testGetPutRemove(compound, "intVector3Pos", new IntVector3(20, 44, 66));
    testGetPutRemove(compound, "random", UUID.randomUUID());
    testGetPutRemove(compound, "face", BlockFace.EAST);
    testGetPutRemove(compound, "perm", PermissionDefault.OP);
    testGetPutRemove(compound, "boolT", Boolean.TRUE);
    testGetPutRemove(compound, "boolF", Boolean.FALSE);
}
Also used : CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3) Test(org.junit.Test)

Example 23 with IntVector3

use of com.bergerkiller.bukkit.common.bases.IntVector3 in project BKCommonLib by bergerhealer.

the class OctreeTest method testSmallCuboid.

@Test
public void testSmallCuboid() {
    // Tests a single-block area cuboid, which is challenging because
    // the last data-entry holding node will be filtered only
    Octree<String> tree = new Octree<String>();
    putDemoValues(tree);
    IntVector3 block = new IntVector3(10, 100, 1000);
    OctreeIterator<String> iter = tree.cuboid(block, block).iterator();
    assertTrue(iter.hasNext());
    assertEquals("D", iter.next());
    assertEquals(10, iter.getX());
    assertEquals(100, iter.getY());
    assertEquals(1000, iter.getZ());
    assertFalse(iter.hasNext());
}
Also used : Octree(com.bergerkiller.bukkit.common.collections.octree.Octree) IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3) Test(org.junit.Test)

Example 24 with IntVector3

use of com.bergerkiller.bukkit.common.bases.IntVector3 in project BKCommonLib by bergerhealer.

the class CommonTagCompound method putGetRemove.

// implements get/put/remove with a simplified single implementation function
private final <T> T putGetRemove(PutGetRemoveOp op, String key, Class<T> type, T value) {
    Object rawNBTResult = null;
    if (type == UUID.class) {
        // == UUID ==
        UUID uuid = (UUID) value;
        Long uuidMost = putGetRemove(op, key + "UUIDMost", Long.class, (uuid == null) ? null : uuid.getMostSignificantBits());
        Long uuidLeast = putGetRemove(op, key + "UUIDLeast", Long.class, (uuid == null) ? null : uuid.getLeastSignificantBits());
        if (uuidMost != null && uuidLeast != null) {
            return (T) new UUID(uuidMost.longValue(), uuidLeast.longValue());
        }
    } else if (type == BlockLocation.class) {
        // == BlockLocation ==
        BlockLocation pos = (BlockLocation) value;
        String world = putGetRemove(op, key + "World", String.class, (pos == null) ? null : pos.world);
        Integer x = putGetRemove(op, key + "X", Integer.class, (pos == null) ? null : pos.x);
        Integer y = putGetRemove(op, key + "Y", Integer.class, (pos == null) ? null : pos.y);
        Integer z = putGetRemove(op, key + "Z", Integer.class, (pos == null) ? null : pos.z);
        if (world != null && !world.isEmpty() && x != null && y != null && z != null) {
            return (T) new BlockLocation(world, x.intValue(), y.intValue(), z.intValue());
        }
    } else if (type == IntVector3.class) {
        // == IntVector3 ==
        IntVector3 pos = (IntVector3) value;
        Integer x = putGetRemove(op, key + "X", Integer.class, (pos == null) ? null : pos.x);
        Integer y = putGetRemove(op, key + "Y", Integer.class, (pos == null) ? null : pos.y);
        Integer z = putGetRemove(op, key + "Z", Integer.class, (pos == null) ? null : pos.z);
        if (x != null && y != null && z != null) {
            return (T) new IntVector3(x.intValue(), y.intValue(), z.intValue());
        }
    } else if (type == MinecraftKeyHandle.class) {
        // == MinecraftKeyHandle ==
        String v = putGetRemove(op, key, String.class, (value == null) ? null : ((MinecraftKeyHandle) value).toString());
        if (v != null) {
            return (T) MinecraftKeyHandle.createNew(v);
        }
    } else if (type == boolean.class || type == Boolean.class) {
        // == Booleans (serialized as Byte) ==
        Byte v = putGetRemove(op, key, Byte.class, (value == null) ? null : ((Boolean) value) ? (byte) 1 : (byte) 0);
        if (v != null) {
            return (T) ((v.byteValue() != (byte) 0) ? Boolean.TRUE : Boolean.FALSE);
        }
    } else if (op == PutGetRemoveOp.GET) {
        // Get other types of values
        rawNBTResult = NBTTagCompoundHandle.T.get.raw.invoke(getRawHandle(), key);
    } else if (op == PutGetRemoveOp.REMOVE || (op == PutGetRemoveOp.PUT && value == null)) {
        // Remove other types of values
        rawNBTResult = getRawData().remove(key);
    } else if (op == PutGetRemoveOp.PUT) {
        // Put other types of values
        Object putValueNBT = NBTBaseHandle.createRawHandleForData(value);
        rawNBTResult = NBTTagCompoundHandle.T.put.raw.invoke(getRawHandle(), key, putValueNBT);
    }
    // Failure fallback + convert raw NBT tags to their converted values
    if (rawNBTResult == null) {
        // failure or no previous value
        return null;
    } else if (type == null) {
        return (T) wrapGetDataForHandle(rawNBTResult);
    } else if (NBTBaseHandle.class.isAssignableFrom(type)) {
        return Conversion.convert(NBTBaseHandle.createHandleForData(rawNBTResult), type, null);
    } else if (CommonTag.class.isAssignableFrom(type)) {
        return Conversion.convert(NBTBaseHandle.createHandleForData(rawNBTResult).toCommonTag(), type, null);
    } else if (NBTBaseHandle.T.isAssignableFrom(type)) {
        return Conversion.convert(rawNBTResult, type, null);
    } else if (NBTTagCompoundHandle.T.isAssignableFrom(rawNBTResult)) {
        return Conversion.convert(wrapRawData(NBTTagCompoundHandle.T.data.get(rawNBTResult)), type, null);
    } else if (NBTTagListHandle.T.isAssignableFrom(rawNBTResult)) {
        return Conversion.convert(wrapRawData(NBTTagListHandle.T.data.get(rawNBTResult)), type, null);
    } else {
        return Conversion.convert(NBTBaseHandle.getDataForHandle(rawNBTResult), type, null);
    }
}
Also used : MinecraftKeyHandle(com.bergerkiller.generated.net.minecraft.resources.MinecraftKeyHandle) BlockLocation(com.bergerkiller.bukkit.common.BlockLocation) IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3)

Example 25 with IntVector3

use of com.bergerkiller.bukkit.common.bases.IntVector3 in project BKCommonLib by bergerhealer.

the class PlayerRespawnPoint method forPlayer.

/**
 * Reads the respawn point set for a particular player. Returns {@link #NONE} if none is set.
 *
 * @param player The player to read the respawn point from
 * @return respawn point set for the player
 */
public static PlayerRespawnPoint forPlayer(Player player) {
    EntityPlayerHandle handle = EntityPlayerHandle.fromBukkit(player);
    World world = handle.getSpawnWorld();
    if (world == null) {
        return NONE;
    }
    IntVector3 coord = handle.getSpawnCoord();
    if (coord == null) {
        return NONE;
    }
    float angle = handle.getSpawnAngle();
    return new PlayerRespawnPoint(world, coord.x, coord.y, coord.z, angle);
}
Also used : World(org.bukkit.World) IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3) EntityPlayerHandle(com.bergerkiller.generated.net.minecraft.server.level.EntityPlayerHandle)

Aggregations

IntVector3 (com.bergerkiller.bukkit.common.bases.IntVector3)27 HashSet (java.util.HashSet)9 IntVector2 (com.bergerkiller.bukkit.common.bases.IntVector2)6 Chunk (org.bukkit.Chunk)5 BlockFace (org.bukkit.block.BlockFace)4 Test (org.junit.Test)4 MapUUID (com.bergerkiller.bukkit.common.map.util.MapUUID)3 WorldHandle (com.bergerkiller.generated.net.minecraft.world.level.WorldHandle)3 File (java.io.File)3 UUID (java.util.UUID)3 World (org.bukkit.World)3 BlockLocation (com.bergerkiller.bukkit.common.BlockLocation)2 Octree (com.bergerkiller.bukkit.common.collections.octree.Octree)2 CommonTagCompound (com.bergerkiller.bukkit.common.nbt.CommonTagCompound)2 BlockData (com.bergerkiller.bukkit.common.wrappers.BlockData)2 EntityPlayerHandle (com.bergerkiller.generated.net.minecraft.server.level.EntityPlayerHandle)2 BitSet (java.util.BitSet)2 Random (java.util.Random)2 Set (java.util.Set)2 ItemFrame (org.bukkit.entity.ItemFrame)2