Search in sources :

Example 6 with QueryableShipData

use of org.valkyrienskies.mod.common.ships.QueryableShipData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class MixinChunk method pre_setBlockState.

/**
 * If this chunk is part of a ship, then tell that ship about the IBlockState update.
 *
 * Note that we're assuming that a Chunk cannot deny the setBlockState request. Therefore its safe to assume that
 * the parameter "state" will be the final IBlockState of BlockPos "pos".
 */
@Inject(method = "setBlockState", at = @At("HEAD"))
private void pre_setBlockState(BlockPos pos, IBlockState state, CallbackInfoReturnable<IBlockState> cir) {
    if (!world.isRemote) {
        IBlockState oldState = getBlockState(pos);
        QueryableShipData queryableShipData = QueryableShipData.get(world);
        Optional<ShipData> shipDataOptional = queryableShipData.getShipFromChunk(pos.getX() >> 4, pos.getZ() >> 4);
        shipDataOptional.ifPresent(shipData -> ShipDataMethods.onSetBlockState(shipData, pos, oldState, state));
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) ShipData(org.valkyrienskies.mod.common.ships.ShipData) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 7 with QueryableShipData

use of org.valkyrienskies.mod.common.ships.QueryableShipData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class MixinNetHandlerPlayServer method preProcessPlayer.

/**
 * This mixin fixes "Player Moved Wrongly" errors.
 *
 * @param packetPlayer The packet the player sent us
 * @param info We can use this to cancel the invocation
 */
@Inject(method = "processPlayer", at = @At("HEAD"))
private void preProcessPlayer(final CPacketPlayer packetPlayer, final CallbackInfo info) {
    // Don't run any of this code on the network thread!
    if (this.player.getServerWorld().isCallingFromMinecraftThread()) {
        // This fixes players dying of fall damage when changing dimensions
        if (this.player.isInvulnerableDimensionChange()) {
            return;
        }
        final PlayerMovementData addedPlayerMovementData = IHasPlayerMovementData.class.cast(packetPlayer).getPlayerMovementData();
        final World world = player.world;
        final UUID lastTouchedShipId = addedPlayerMovementData.getLastTouchedShipId();
        final int ticksSinceTouchedLastShip = addedPlayerMovementData.getTicksSinceTouchedLastShip();
        if (ticksSinceTouchedLastShip > 40) {
            // If the player hasn't touched the ship in over 40 ticks, then ignore its coordinates relative to that ship.
            final IDraggable playerAsDraggable = IDraggable.class.cast(this.player);
            playerAsDraggable.setEntityShipMovementData(playerAsDraggable.getEntityShipMovementData().withLastTouchedShip(null).withAddedLinearVelocity(new Vector3d()).withAddedYawVelocity(0).withTicksPartOfGround(addedPlayerMovementData.getTicksPartOfGround()).withTicksSinceTouchedShip(ticksSinceTouchedLastShip));
            return;
        }
        final int ticksPartOfGround = addedPlayerMovementData.getTicksPartOfGround();
        final Vector3d playerPosInShip = new Vector3d(addedPlayerMovementData.getPlayerPosInShip());
        final Vector3d playerLookInShip = new Vector3d(addedPlayerMovementData.getPlayerLookInShip());
        ShipData lastTouchedShip = null;
        if (lastTouchedShipId != null) {
            final QueryableShipData queryableShipData = QueryableShipData.get(world);
            final Optional<ShipData> shipDataOptional = queryableShipData.getShip(lastTouchedShipId);
            if (shipDataOptional.isPresent()) {
                lastTouchedShip = shipDataOptional.get();
                final PhysicsObject shipObject = ValkyrienUtils.getServerShipManager(world).getPhysObjectFromUUID(lastTouchedShip.getUuid());
                if (shipObject != null) {
                    if (shipObject.getTicksSinceShipTeleport() > PhysicsObject.TICKS_SINCE_TELEPORT_TO_START_DRAGGING) {
                        final ShipTransform shipTransform = lastTouchedShip.getShipTransform();
                        shipTransform.transformPosition(playerPosInShip, TransformType.SUBSPACE_TO_GLOBAL);
                        shipTransform.transformDirection(playerLookInShip, TransformType.SUBSPACE_TO_GLOBAL);
                    } else {
                        // Don't move the player relative to the ship until the TicksSinceShipTeleport timer expires.
                        playerPosInShip.set(player.posX, player.posY, player.posZ);
                    }
                }
            } else {
                // info.cancel();
                return;
            }
        }
        // Get the player pitch/yaw from the look vector
        final Tuple<Double, Double> pitchYawTuple = VSMath.getPitchYawFromVector(playerLookInShip);
        final double playerPitchInGlobal = pitchYawTuple.getFirst();
        final double playerYawInGlobal = pitchYawTuple.getSecond();
        // Idk if this is needed, but I'm too bothered to change it
        packetPlayer.moving = true;
        // Then update the packet values to match the ones above.
        packetPlayer.x = playerPosInShip.x();
        packetPlayer.y = playerPosInShip.y();
        packetPlayer.z = playerPosInShip.z();
        packetPlayer.yaw = (float) playerYawInGlobal;
        packetPlayer.pitch = (float) playerPitchInGlobal;
        // Set the player motion values to tell the NetHandlerPlayServer that the player is allowed to move this fast.
        this.player.motionX = packetPlayer.x - this.firstGoodX;
        this.player.motionY = packetPlayer.y - this.firstGoodY;
        this.player.motionZ = packetPlayer.z - this.firstGoodZ;
        // Update the player draggable
        final IDraggable playerAsDraggable = IDraggable.class.cast(this.player);
        playerAsDraggable.setEntityShipMovementData(playerAsDraggable.getEntityShipMovementData().withLastTouchedShip(lastTouchedShip).withAddedLinearVelocity(new Vector3d()).withAddedYawVelocity(0).withTicksPartOfGround(ticksPartOfGround).withTicksSinceTouchedShip(ticksSinceTouchedLastShip));
    }
}
Also used : QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) ShipData(org.valkyrienskies.mod.common.ships.ShipData) IHasPlayerMovementData(org.valkyrienskies.mod.common.network.IHasPlayerMovementData) PlayerMovementData(org.valkyrienskies.mod.common.network.PlayerMovementData) World(net.minecraft.world.World) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) Vector3d(org.joml.Vector3d) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) IDraggable(org.valkyrienskies.mod.common.ships.entity_interaction.IDraggable) IHasPlayerMovementData(org.valkyrienskies.mod.common.network.IHasPlayerMovementData)

Example 8 with QueryableShipData

use of org.valkyrienskies.mod.common.ships.QueryableShipData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class MixinChunkSponge method onPreSpongeBridgeSetBlockState.

@Inject(method = "bridge$setBlockState", at = @At("HEAD"), remap = false)
private void onPreSpongeBridgeSetBlockState(BlockPos pos, IBlockState newState, IBlockState currentState, BlockChangeFlag flag, CallbackInfoReturnable<IBlockState> cir) {
    if (!world.isRemote) {
        QueryableShipData queryableShipData = QueryableShipData.get(world);
        Optional<ShipData> shipDataOptional = queryableShipData.getShipFromChunk(pos.getX() >> 4, pos.getZ() >> 4);
        shipDataOptional.ifPresent(shipData -> ShipDataMethods.onSetBlockState(shipData, pos, currentState, newState));
    }
}
Also used : ShipData(org.valkyrienskies.mod.common.ships.ShipData) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 9 with QueryableShipData

use of org.valkyrienskies.mod.common.ships.QueryableShipData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class WorldServerShipManager method getBackgroundShipChunks.

/**
 * Used to prevent the world from unloading the chunks of ships loading in background.
 */
public Iterable<Long> getBackgroundShipChunks() throws CalledFromWrongThreadException {
    enforceGameThread();
    List<Long> backgroundChunks = new ArrayList<>();
    QueryableShipData queryableShipData = QueryableShipData.get(world);
    for (UUID shipID : loadingInBackground) {
        Optional<ShipData> shipDataOptional = queryableShipData.getShip(shipID);
        if (!shipDataOptional.isPresent()) {
            throw new IllegalStateException("Ship data not present for:\n" + shipID);
        }
        backgroundChunks.addAll(shipDataOptional.get().getChunkClaim().getClaimedChunks());
    }
    return backgroundChunks;
}
Also used : QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) ShipData(org.valkyrienskies.mod.common.ships.ShipData) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData)

Example 10 with QueryableShipData

use of org.valkyrienskies.mod.common.ships.QueryableShipData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class WorldServerShipManager method loadAndUnloadShips.

private void loadAndUnloadShips() {
    QueryableShipData queryableShipData = QueryableShipData.get(world);
    // Load the ships that are required immediately.
    for (final UUID toLoadID : loadQueue) {
        Optional<ShipData> toLoadOptional = queryableShipData.getShip(toLoadID);
        if (!toLoadOptional.isPresent()) {
            throw new IllegalStateException("No ship found for ID:\n" + toLoadID);
        }
        ShipData toLoad = toLoadOptional.get();
        if (loadedShips.containsKey(toLoadID)) {
            throw new IllegalStateException("Tried loading a ShipData that was already loaded?\n" + toLoad);
        }
        // Remove this ship from the background loading set, if it is in it.
        loadingInBackground.remove(toLoadID);
        // Finally, load the ship.
        if (VSConfig.showAnnoyingDebugOutput) {
            System.out.println("Attempting to load ship " + toLoad);
        }
        PhysicsObject physicsObject = new PhysicsObject(world, toLoad);
        PhysicsObject old = loadedShips.put(toLoad.getUuid(), physicsObject);
        if (old != null) {
            throw new IllegalStateException("How did we already have a ship loaded for " + toLoad);
        }
    }
    loadQueue.clear();
    // Load ships that aren't required immediately in the background.
    for (final UUID toLoadID : backgroundLoadQueue) {
        // Skip if this ship is already being loaded in the background,.
        if (loadingInBackground.contains(toLoadID)) {
            // Already loading this ship in the background
            continue;
        }
        // Make sure there isn't an already loaded ship with this UUID.
        if (loadedShips.containsKey(toLoadID)) {
            // continue; // temp, need to fix WorldShipLoadingController.determineLoadAndUnload()
            throw new IllegalStateException("Tried loading a ShipData that was already loaded? Ship ID is\n" + toLoadID);
        }
        // Then try getting the ShipData for this UUID.
        Optional<ShipData> toLoadOptional = queryableShipData.getShip(toLoadID);
        if (!toLoadOptional.isPresent()) {
            throw new IllegalStateException("No ship found for ID:\n" + toLoadID);
        }
        ShipData toLoad = toLoadOptional.get();
        loadingInBackground.add(toLoadID);
        if (VSConfig.showAnnoyingDebugOutput) {
            System.out.println("Attempting to load " + toLoad + " in the background.");
        }
        ChunkProviderServer chunkProviderServer = world.getChunkProvider();
        for (ChunkPos chunkPos : toLoad.getChunkClaim()) {
            @Nonnull Runnable returnTask = () -> {
                if (VSConfig.showAnnoyingDebugOutput) {
                    System.out.println("Loaded ship chunk " + chunkPos);
                }
            };
            chunkProviderServer.loadChunk(chunkPos.x, chunkPos.z, returnTask);
        }
    }
    backgroundLoadQueue.clear();
    // Unload far away ships immediately.
    for (final UUID toUnloadID : unloadQueue) {
        // Make sure we have a ship with this ID that can be unloaded
        if (!loadedShips.containsKey(toUnloadID)) {
            throw new IllegalStateException("Tried unloading a ShipData that isn't loaded? Ship ID is\n" + toUnloadID);
        }
        PhysicsObject physicsObject = getPhysObjectFromUUID(toUnloadID);
        if (VSConfig.showAnnoyingDebugOutput) {
            System.out.println("Attempting to unload " + physicsObject);
        }
        physicsObject.unload();
        boolean success = loadedShips.remove(toUnloadID, physicsObject);
        if (!success) {
            throw new IllegalStateException("How did we fail to unload " + physicsObject.getShipData());
        }
    }
    unloadQueue.clear();
}
Also used : Nonnull(javax.annotation.Nonnull) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) ShipData(org.valkyrienskies.mod.common.ships.ShipData) ChunkProviderServer(net.minecraft.world.gen.ChunkProviderServer) ChunkPos(net.minecraft.util.math.ChunkPos) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData)

Aggregations

QueryableShipData (org.valkyrienskies.mod.common.ships.QueryableShipData)10 ShipData (org.valkyrienskies.mod.common.ships.ShipData)8 World (net.minecraft.world.World)4 IPhysObjectWorld (org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld)3 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)3 UUID (java.util.UUID)2 IThreadListener (net.minecraft.util.IThreadListener)2 Chunk (net.minecraft.world.chunk.Chunk)2 Inject (org.spongepowered.asm.mixin.injection.Inject)2 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)2 Nonnull (javax.annotation.Nonnull)1 IBlockState (net.minecraft.block.state.IBlockState)1 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)1 ChunkPos (net.minecraft.util.math.ChunkPos)1 ChunkProviderServer (net.minecraft.world.gen.ChunkProviderServer)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1 Vector3d (org.joml.Vector3d)1 IHasPlayerMovementData (org.valkyrienskies.mod.common.network.IHasPlayerMovementData)1 PlayerMovementData (org.valkyrienskies.mod.common.network.PlayerMovementData)1 IDraggable (org.valkyrienskies.mod.common.ships.entity_interaction.IDraggable)1