Search in sources :

Example 6 with MovecraftRotation

use of net.countercraft.movecraft.MovecraftRotation in project Movecraft by APDevTeam.

the class IWorldHandler method rotateCraft.

// @Override
// public void addPlayerLocation(Player player, double x, double y, double z, float yaw, float pitch){
// ServerPlayer ePlayer = ((CraftPlayer) player).getHandle();
// ePlayer.connection.teleport(x, y, z, yaw, pitch, EnumSet.allOf(ClientboundPlayerPositionPacket.RelativeArgument.class));
// }
@Override
public void rotateCraft(@NotNull Craft craft, @NotNull MovecraftLocation originPoint, @NotNull MovecraftRotation rotation) {
    // *******************************************
    // *      Step one: Convert to Positions     *
    // *******************************************
    HashMap<BlockPos, BlockPos> rotatedPositions = new HashMap<>();
    MovecraftRotation counterRotation = rotation == MovecraftRotation.CLOCKWISE ? MovecraftRotation.ANTICLOCKWISE : MovecraftRotation.CLOCKWISE;
    for (MovecraftLocation newLocation : craft.getHitBox()) {
        rotatedPositions.put(locationToPosition(MathUtils.rotateVec(counterRotation, newLocation.subtract(originPoint)).add(originPoint)), locationToPosition(newLocation));
    }
    // *******************************************
    // *         Step two: Get the tiles         *
    // *******************************************
    ServerLevel nativeWorld = ((CraftWorld) craft.getWorld()).getHandle();
    List<TileHolder> tiles = new ArrayList<>();
    // get the tiles
    for (BlockPos position : rotatedPositions.keySet()) {
        // BlockEntity tile = nativeWorld.removeBlockEntity(position);
        BlockEntity tile = removeBlockEntity(nativeWorld, position);
        if (tile == null)
            continue;
        // tile.a(ROTATION[rotation.ordinal()]);
        // get the nextTick to move with the tile
        tiles.add(new TileHolder(tile, tickProvider.getNextTick(nativeWorld, position), position));
    }
    // *******************************************
    // *   Step three: Translate all the blocks  *
    // *******************************************
    // blockedByWater=false means an ocean-going vessel
    // TODO: Simplify
    // TODO: go by chunks
    // TODO: Don't move unnecessary blocks
    // get the blocks and rotate them
    HashMap<BlockPos, BlockState> blockData = new HashMap<>();
    for (BlockPos position : rotatedPositions.keySet()) {
        blockData.put(position, nativeWorld.getBlockState(position).rotate(ROTATION[rotation.ordinal()]));
    }
    // create the new block
    for (Map.Entry<BlockPos, BlockState> entry : blockData.entrySet()) {
        setBlockFast(nativeWorld, rotatedPositions.get(entry.getKey()), entry.getValue());
    }
    // TODO: go by chunks
    for (TileHolder tileHolder : tiles) {
        moveBlockEntity(nativeWorld, rotatedPositions.get(tileHolder.getTilePosition()), tileHolder.getTile());
        if (tileHolder.getNextTick() == null)
            continue;
        final long currentTime = nativeWorld.E.getGameTime();
        nativeWorld.getBlockTickList().scheduleTick(rotatedPositions.get(tileHolder.getNextTick().pos), (Block) tileHolder.getNextTick().getType(), (int) (tileHolder.getNextTick().triggerTick - currentTime), tileHolder.getNextTick().priority);
    }
    // *******************************************
    // *   Step five: Destroy the leftovers      *
    // *******************************************
    // TODO: add support for pass-through
    Collection<BlockPos> deletePositions = CollectionUtils.filter(rotatedPositions.keySet(), rotatedPositions.values());
    for (BlockPos position : deletePositions) {
        setBlockFast(nativeWorld, position, Blocks.AIR.defaultBlockState());
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MovecraftRotation(net.countercraft.movecraft.MovecraftRotation) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) CraftWorld(org.bukkit.craftbukkit.v1_17_R1.CraftWorld) HashMap(java.util.HashMap) Map(java.util.Map) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 7 with MovecraftRotation

use of net.countercraft.movecraft.MovecraftRotation in project Movecraft by APDevTeam.

the class IWorldHandler method rotateCraft.

// @Override
// public void addPlayerLocation(Player player, double x, double y, double z, float yaw, float pitch){
// ServerPlayer ePlayer = ((CraftPlayer) player).getHandle();
// ePlayer.connection.teleport(x, y, z, yaw, pitch, EnumSet.allOf(ClientboundPlayerPositionPacket.RelativeArgument.class));
// }
@Override
public void rotateCraft(@NotNull Craft craft, @NotNull MovecraftLocation originPoint, @NotNull MovecraftRotation rotation) {
    // *******************************************
    // *      Step one: Convert to Positions     *
    // *******************************************
    HashMap<BlockPos, BlockPos> rotatedPositions = new HashMap<>();
    MovecraftRotation counterRotation = rotation == MovecraftRotation.CLOCKWISE ? MovecraftRotation.ANTICLOCKWISE : MovecraftRotation.CLOCKWISE;
    for (MovecraftLocation newLocation : craft.getHitBox()) {
        rotatedPositions.put(locationToPosition(MathUtils.rotateVec(counterRotation, newLocation.subtract(originPoint)).add(originPoint)), locationToPosition(newLocation));
    }
    // *******************************************
    // *         Step two: Get the tiles         *
    // *******************************************
    ServerLevel nativeWorld = ((CraftWorld) craft.getWorld()).getHandle();
    List<TileHolder> tiles = new ArrayList<>();
    // get the tiles
    for (BlockPos position : rotatedPositions.keySet()) {
        // BlockEntity tile = nativeWorld.removeBlockEntity(position);
        BlockEntity tile = removeBlockEntity(nativeWorld, position);
        if (tile == null)
            continue;
        // tile.a(ROTATION[rotation.ordinal()]);
        // get the nextTick to move with the tile
        tiles.add(new TileHolder(tile, tickProvider.getNextTick(nativeWorld, position), position));
    }
    // *******************************************
    // *   Step three: Translate all the blocks  *
    // *******************************************
    // blockedByWater=false means an ocean-going vessel
    // TODO: Simplify
    // TODO: go by chunks
    // TODO: Don't move unnecessary blocks
    // get the blocks and rotate them
    HashMap<BlockPos, BlockState> blockData = new HashMap<>();
    for (BlockPos position : rotatedPositions.keySet()) {
        blockData.put(position, nativeWorld.getBlockState(position).rotate(ROTATION[rotation.ordinal()]));
    }
    // create the new block
    for (Map.Entry<BlockPos, BlockState> entry : blockData.entrySet()) {
        setBlockFast(nativeWorld, rotatedPositions.get(entry.getKey()), entry.getValue());
    }
    // TODO: go by chunks
    for (TileHolder tileHolder : tiles) {
        moveBlockEntity(nativeWorld, rotatedPositions.get(tileHolder.getTilePosition()), tileHolder.getTile());
        if (tileHolder.getNextTick() == null)
            continue;
        final long currentTime = nativeWorld.M.getGameTime();
        nativeWorld.getBlockTicks().schedule(new ScheduledTick<>((Block) tileHolder.getNextTick().type(), rotatedPositions.get(tileHolder.getNextTick().pos()), tileHolder.getNextTick().triggerTick() - currentTime, tileHolder.getNextTick().priority(), tileHolder.getNextTick().subTickOrder()));
    }
    // *******************************************
    // *   Step five: Destroy the leftovers      *
    // *******************************************
    // TODO: add support for pass-through
    Collection<BlockPos> deletePositions = CollectionUtils.filter(rotatedPositions.keySet(), rotatedPositions.values());
    for (BlockPos position : deletePositions) {
        setBlockFast(nativeWorld, position, Blocks.AIR.defaultBlockState());
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MovecraftRotation(net.countercraft.movecraft.MovecraftRotation) BlockState(net.minecraft.world.level.block.state.BlockState) Block(net.minecraft.world.level.block.Block) BlockPos(net.minecraft.core.BlockPos) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld) HashMap(java.util.HashMap) Map(java.util.Map) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Aggregations

MovecraftRotation (net.countercraft.movecraft.MovecraftRotation)7 MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Craft (net.countercraft.movecraft.craft.Craft)2 BlockPos (net.minecraft.core.BlockPos)2 ServerLevel (net.minecraft.server.level.ServerLevel)2 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)2 BlockState (net.minecraft.world.level.block.state.BlockState)2 Location (org.bukkit.Location)2 BlockState (org.bukkit.block.BlockState)2 Sign (org.bukkit.block.Sign)2 EventHandler (org.bukkit.event.EventHandler)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Logger (java.util.logging.Logger)1 WorldHandler (net.countercraft.movecraft.WorldHandler)1 SinkingCraft (net.countercraft.movecraft.craft.SinkingCraft)1 SubCraft (net.countercraft.movecraft.craft.SubCraft)1