Search in sources :

Example 1 with WorldClientRemote

use of io.xol.chunkstories.world.WorldClientRemote in project chunkstories by Hugobros3.

the class PacketInitializeRemoteWorld method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
    String initializationString = in.readUTF();
    ByteArrayInputStream bais = new ByteArrayInputStream(initializationString.getBytes("UTF-8"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(bais, "UTF-8"));
    info = new WorldInfoImplementation(reader);
    if (processor instanceof ClientPacketsContext) {
        processor.logger().info("Received World initialization packet");
        ClientPacketsContext cpp = (ClientPacketsContext) processor;
        OnlineContentTranslator contentTranslator = cpp.getContentTranslator();
        if (contentTranslator == null) {
            processor.logger().error("Can't initialize a world without a ContentTranslator initialized first!");
            return;
        }
        // TODO should we expose this to the interface ?
        Client client = (Client) cpp.getContext();
        Fence fence = client.getGameWindow().queueSynchronousTask(new Runnable() {

            @Override
            public void run() {
                WorldClientRemote world;
                try {
                    world = new WorldClientRemote(client, info, contentTranslator, cpp.getConnection());
                    client.changeWorld(world);
                    cpp.getConnection().handleSystemRequest("world/ok");
                } catch (WorldLoadingException e) {
                    client.exitToMainMenu(e.getMessage());
                }
            }
        });
        fence.traverse();
    }
}
Also used : WorldLoadingException(io.xol.chunkstories.world.WorldLoadingException) OnlineContentTranslator(io.xol.chunkstories.api.content.OnlineContentTranslator) InputStreamReader(java.io.InputStreamReader) WorldClientRemote(io.xol.chunkstories.world.WorldClientRemote) ClientPacketsContext(io.xol.chunkstories.client.net.ClientPacketsContext) WorldInfoImplementation(io.xol.chunkstories.world.WorldInfoImplementation) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) Fence(io.xol.chunkstories.api.util.concurrency.Fence) Client(io.xol.chunkstories.client.Client)

Example 2 with WorldClientRemote

use of io.xol.chunkstories.world.WorldClientRemote in project chunkstories by Hugobros3.

the class TCPServerConnection method handleDatagram.

@Override
public void handleDatagram(LogicalPacketDatagram datagram) throws IOException, PacketProcessingException, IllegalPacketException {
    // (PacketDefinitionImpl) getPacketsContext().getContentTranslator().getPacketForId(datagram.packetTypeId);
    PacketDefinitionImplementation definition = (PacketDefinitionImplementation) datagram.packetDefinition;
    if (definition.getGenre() == PacketGenre.GENERAL_PURPOSE) {
        Packet packet = definition.createNew(true, null);
        packet.process(getRemoteServer(), datagram.getData(), getPacketsContext());
        datagram.dispose();
    } else if (definition.getGenre() == PacketGenre.SYSTEM) {
        Packet packet = definition.createNew(true, null);
        packet.process(getRemoteServer(), datagram.getData(), getPacketsContext());
        if (packet instanceof PacketText) {
            handleSystemRequest(((PacketText) packet).text);
        }
        datagram.dispose();
    } else if (definition.getGenre() == PacketGenre.WORLD) {
        WorldClientRemote world = getPacketsContext().getWorld();
        world.queueDatagram(datagram);
    } else if (definition.getGenre() == PacketGenre.WORLD_STREAMING) {
        WorldClientRemote world = getPacketsContext().getWorld();
        PacketWorldStreaming packet = (PacketWorldStreaming) definition.createNew(true, world);
        packet.process(getRemoteServer(), datagram.getData(), getPacketsContext());
        world.ioHandler().handlePacketWorldStreaming(packet);
        datagram.dispose();
    } else {
        throw new RuntimeException("whut");
    }
}
Also used : Packet(io.xol.chunkstories.api.net.Packet) PacketText(io.xol.chunkstories.api.net.packets.PacketText) PacketWorldStreaming(io.xol.chunkstories.api.net.PacketWorldStreaming) PacketDefinitionImplementation(io.xol.chunkstories.net.PacketDefinitionImplementation) WorldClientRemote(io.xol.chunkstories.world.WorldClientRemote)

Example 3 with WorldClientRemote

use of io.xol.chunkstories.world.WorldClientRemote in project chunkstories by Hugobros3.

the class LocalClientLoadingAgent method updateUsedWorldBits.

public void updateUsedWorldBits() {
    Entity controlledEntity = player.getControlledEntity();
    if (controlledEntity == null)
        return;
    try {
        lock.lock();
        // Subscribe to nearby wanted chunks
        int cameraChunkX = Math2.floor((controlledEntity.getLocation().x()) / 32);
        int cameraChunkY = Math2.floor((controlledEntity.getLocation().y()) / 32);
        int cameraChunkZ = Math2.floor((controlledEntity.getLocation().z()) / 32);
        int chunksViewDistance = (int) (world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance") / 32);
        for (int chunkX = (cameraChunkX - chunksViewDistance - 1); chunkX <= cameraChunkX + chunksViewDistance + 1; chunkX++) {
            for (int chunkZ = (cameraChunkZ - chunksViewDistance - 1); chunkZ <= cameraChunkZ + chunksViewDistance + 1; chunkZ++) for (int chunkY = cameraChunkY - 3; chunkY <= cameraChunkY + 3; chunkY++) {
                WorldInfo worldInfo = world.getWorldInfo();
                WorldInfo.WorldSize size = worldInfo.getSize();
                int filteredChunkX = chunkX & (size.maskForChunksCoordinates);
                int filteredChunkY = Math2.clampi(chunkY, 0, 31);
                int filteredChunkZ = chunkZ & (size.maskForChunksCoordinates);
                int summed = ((filteredChunkX << size.bitlengthOfVerticalChunksCoordinates) | filteredChunkY) << size.bitlengthOfHorizontalChunksCoordinates | filteredChunkZ;
                if (fastChunksMask.contains(summed))
                    continue;
                ChunkHolder holder = world.aquireChunkHolder(player, chunkX, chunkY, chunkZ);
                assert holder != null;
                if (holder == null)
                    continue;
                usedChunks.add(holder);
                fastChunksMask.add(summed);
                if (world instanceof WorldClientRemote) {
                    WorldClientRemote remote = (WorldClientRemote) world;
                    remote.getConnection().pushPacket(PacketWorldUser.registerChunkPacket(world, filteredChunkX, filteredChunkY, filteredChunkZ));
                }
            }
        }
        // Unsubscribe for far ones
        Iterator<ChunkHolder> i = usedChunks.iterator();
        while (i.hasNext()) {
            ChunkHolder holder = i.next();
            if ((LoopingMathHelper.moduloDistance(holder.getChunkCoordinateX(), cameraChunkX, world.getSizeInChunks()) > chunksViewDistance + 1) || (LoopingMathHelper.moduloDistance(holder.getChunkCoordinateZ(), cameraChunkZ, world.getSizeInChunks()) > chunksViewDistance + 1) || (Math.abs(holder.getChunkCoordinateY() - cameraChunkY) > 4)) {
                WorldInfo worldInfo = world.getWorldInfo();
                WorldInfo.WorldSize size = worldInfo.getSize();
                int filteredChunkX = holder.getChunkCoordinateX() & (size.maskForChunksCoordinates);
                int filteredChunkY = Math2.clampi(holder.getChunkCoordinateY(), 0, 31);
                int filteredChunkZ = holder.getChunkCoordinateZ() & (size.maskForChunksCoordinates);
                int summed = ((filteredChunkX << size.bitlengthOfVerticalChunksCoordinates) | filteredChunkY) << size.bitlengthOfHorizontalChunksCoordinates | filteredChunkZ;
                fastChunksMask.remove(summed);
                i.remove();
                holder.unregisterUser(player);
                if (world instanceof WorldClientRemote) {
                    WorldClientRemote remote = (WorldClientRemote) world;
                    remote.getConnection().pushPacket(PacketWorldUser.unregisterChunkPacket(world, filteredChunkX, filteredChunkY, filteredChunkZ));
                }
            }
        }
        // We load the region summaries we fancy
        int summaryDistance = 32;
        for (int chunkX = (cameraChunkX - summaryDistance); chunkX < cameraChunkX + summaryDistance; chunkX++) for (int chunkZ = (cameraChunkZ - summaryDistance); chunkZ < cameraChunkZ + summaryDistance; chunkZ++) {
            if (chunkX % 8 == 0 && chunkZ % 8 == 0) {
                int regionX = chunkX / 8;
                int regionZ = chunkZ / 8;
                // TODO bad to aquire each time!!!
                Heightmap regionSummary = world.getRegionsSummariesHolder().aquireHeightmap(player, regionX, regionZ);
                if (regionSummary != null) {
                    if (usedRegionSummaries.add(regionSummary)) {
                        if (world instanceof WorldClientRemote) {
                            WorldClientRemote remote = (WorldClientRemote) world;
                            remote.getConnection().pushPacket(PacketWorldUser.registerSummary(world, regionX, regionZ));
                        }
                    }
                }
            }
        }
        int cameraRegionX = cameraChunkX / 8;
        int cameraRegionZ = cameraChunkZ / 8;
        int distInRegions = summaryDistance / 8;
        int sizeInRegions = world.getSizeInChunks() / 8;
        // And we unload the ones we no longer need
        Iterator<Heightmap> iterator = usedRegionSummaries.iterator();
        while (iterator.hasNext()) {
            Heightmap entry = iterator.next();
            int regionX = entry.getRegionX();
            int regionZ = entry.getRegionZ();
            int dx = LoopingMathHelper.moduloDistance(cameraRegionX, regionX, sizeInRegions);
            int dz = LoopingMathHelper.moduloDistance(cameraRegionZ, regionZ, sizeInRegions);
            if (dx > distInRegions || dz > distInRegions) {
                entry.unregisterUser(player);
                iterator.remove();
                if (world instanceof WorldClientRemote) {
                    WorldClientRemote remote = (WorldClientRemote) world;
                    remote.getConnection().pushPacket(PacketWorldUser.unregisterSummary(world, regionX, regionZ));
                }
            }
        }
    } finally {
        lock.unlock();
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Heightmap(io.xol.chunkstories.api.world.heightmap.Heightmap) ChunkHolder(io.xol.chunkstories.api.world.chunk.ChunkHolder) WorldClientRemote(io.xol.chunkstories.world.WorldClientRemote) WorldInfo(io.xol.chunkstories.api.world.WorldInfo)

Example 4 with WorldClientRemote

use of io.xol.chunkstories.world.WorldClientRemote in project chunkstories by Hugobros3.

the class ChatManager method processTextInput.

private void processTextInput(String input) {
    String username = ingame.getGameWindow().getClient().username();
    if (input.startsWith("/")) {
        String chatMsg = input;
        chatMsg = chatMsg.substring(1, chatMsg.length());
        String cmdName = chatMsg.toLowerCase();
        String[] args = {};
        if (chatMsg.contains(" ")) {
            cmdName = chatMsg.substring(0, chatMsg.indexOf(" "));
            args = chatMsg.substring(chatMsg.indexOf(" ") + 1, chatMsg.length()).split(" ");
        }
        if (ingame.getGameWindow().getClient().getPluginManager().dispatchCommand(Client.getInstance().getPlayer(), cmdName, args)) {
            if (sent.size() == 0 || !sent.get(0).equals(input)) {
                sent.add(0, input);
                sentMessages++;
            }
            return;
        } else if (cmdName.equals("plugins")) {
            String list = "";
            Iterator<ChunkStoriesPlugin> i = ingame.getGameWindow().getClient().getPluginManager().activePlugins();
            while (i.hasNext()) {
                ChunkStoriesPlugin plugin = i.next();
                list += plugin.getName() + (i.hasNext() ? ", " : "");
            }
            if (Client.getInstance().getWorld() instanceof WorldClientLocal)
                insert("#00FFD0" + i + " active client [master] plugins : " + list);
            else
                insert("#74FFD0" + i + " active client [remote] plugins : " + list);
            if (sent.size() == 0 || !sent.get(0).equals(input)) {
                sent.add(0, input);
                sentMessages++;
            }
        } else if (cmdName.equals("mods")) {
            String list = "";
            int i = 0;
            for (Mod mod : Client.getInstance().getContent().modsManager().getCurrentlyLoadedMods()) {
                i++;
                list += mod.getModInfo().getName() + (i == Client.getInstance().getContent().modsManager().getCurrentlyLoadedMods().size() ? "" : ", ");
            }
            if (Client.getInstance().getWorld() instanceof WorldClientLocal)
                insert("#FF0000" + i + " active client [master] mods : " + list);
            else
                insert("#FF7070" + i + " active client [remote] mods : " + list);
            if (sent.size() == 0 || !sent.get(0).equals(input)) {
                sent.add(0, input);
                sentMessages++;
            }
        }
    }
    if (input.equals("/locclear")) {
        chat.clear();
    } else if (input.equals("I am Mr Debug")) {
        // it was you this whole time
        ClientLimitations.isDebugAllowed = true;
    }
    if (ingame.getGameWindow().getClient().getWorld() instanceof WorldClientRemote)
        ((WorldClientRemote) ingame.getGameWindow().getClient().getWorld()).getConnection().sendTextMessage("chat/" + input);
    else
        insert(ColorsTools.getUniqueColorPrefix(username) + username + "#FFFFFF > " + input);
    System.out.println(username + " > " + input);
    if (sent.size() == 0 || !sent.get(0).equals(input)) {
        sent.add(0, input);
        sentMessages++;
    }
}
Also used : Mod(io.xol.chunkstories.api.content.mods.Mod) ChunkStoriesPlugin(io.xol.chunkstories.api.plugin.ChunkStoriesPlugin) Iterator(java.util.Iterator) WorldClientRemote(io.xol.chunkstories.world.WorldClientRemote) WorldClientLocal(io.xol.chunkstories.world.WorldClientLocal)

Example 5 with WorldClientRemote

use of io.xol.chunkstories.world.WorldClientRemote in project chunkstories by Hugobros3.

the class Lwjgl3ClientInputsManager method onInputPressed.

public boolean onInputPressed(Input input) {
    if (input.equals("fullscreen")) {
        gameWindow.toggleFullscreen();
        return true;
    }
    // System.out.println("Input pressed "+input.getName());
    // Try the client-side event press
    ClientInputPressedEvent event = new ClientInputPressedEvent(gameWindow.getClient(), input);
    ClientPluginManager cpm = gameWindow.getClient().getPluginManager();
    if (cpm != null) {
        cpm.fireEvent(event);
        if (event.isCancelled())
            return false;
    }
    // Try the GUI handling
    Layer layer = gameWindow.getLayer();
    if (layer.handleInput(input))
        return true;
    // System.out.println("wasn't handled");
    final LocalPlayer player = Client.getInstance().getPlayer();
    if (player == null)
        return false;
    final EntityControllable entityControlled = player.getControlledEntity();
    // There has to be a controlled entity for sending inputs to make sense.
    if (entityControlled == null)
        return false;
    // Send input to server
    World world = entityControlled.getWorld();
    if (world instanceof WorldClientRemote) {
        // MouseScroll inputs are strictly client-side
        if (!(input instanceof MouseScroll)) {
            ServerConnection connection = ((WorldClientRemote) entityControlled.getWorld()).getConnection();
            PacketInput packet = new PacketInput(world);
            packet.input = input;
            packet.isPressed = true;
            connection.pushPacket(packet);
        }
        return entityControlled.onControllerInput(input, Client.getInstance().getPlayer());
    } else {
        PlayerInputPressedEvent event2 = new PlayerInputPressedEvent(Client.getInstance().getPlayer(), input);
        cpm.fireEvent(event2);
        if (event2.isCancelled())
            return false;
    // entity.handleInteraction(input, entity.getControllerComponent().getController());
    }
    // Handle interaction locally
    return entityControlled.onControllerInput(input, Client.getInstance().getPlayer());
}
Also used : ClientPluginManager(io.xol.chunkstories.api.plugin.ClientPluginManager) PacketInput(io.xol.chunkstories.net.packets.PacketInput) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) WorldClientRemote(io.xol.chunkstories.world.WorldClientRemote) ServerConnection(io.xol.chunkstories.client.net.ServerConnection) ClientInputPressedEvent(io.xol.chunkstories.api.events.client.ClientInputPressedEvent) World(io.xol.chunkstories.api.world.World) Layer(io.xol.chunkstories.api.gui.Layer) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) MouseScroll(io.xol.chunkstories.api.input.Mouse.MouseScroll) PlayerInputPressedEvent(io.xol.chunkstories.api.events.player.PlayerInputPressedEvent)

Aggregations

WorldClientRemote (io.xol.chunkstories.world.WorldClientRemote)7 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)3 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)2 ClientPluginManager (io.xol.chunkstories.api.plugin.ClientPluginManager)2 World (io.xol.chunkstories.api.world.World)2 ServerConnection (io.xol.chunkstories.client.net.ServerConnection)2 PacketInput (io.xol.chunkstories.net.packets.PacketInput)2 Location (io.xol.chunkstories.api.Location)1 OnlineContentTranslator (io.xol.chunkstories.api.content.OnlineContentTranslator)1 Mod (io.xol.chunkstories.api.content.mods.Mod)1 Entity (io.xol.chunkstories.api.entity.Entity)1 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)1 EntityCreative (io.xol.chunkstories.api.entity.interfaces.EntityCreative)1 EntityWithSelectedItem (io.xol.chunkstories.api.entity.interfaces.EntityWithSelectedItem)1 ClientInputPressedEvent (io.xol.chunkstories.api.events.client.ClientInputPressedEvent)1 ClientInputReleasedEvent (io.xol.chunkstories.api.events.client.ClientInputReleasedEvent)1 PlayerInputPressedEvent (io.xol.chunkstories.api.events.player.PlayerInputPressedEvent)1 PlayerInputReleasedEvent (io.xol.chunkstories.api.events.player.PlayerInputReleasedEvent)1 CameraSetupEvent (io.xol.chunkstories.api.events.rendering.CameraSetupEvent)1 Layer (io.xol.chunkstories.api.gui.Layer)1