Untitled

mail@pastecode.io avatar
unknown
java
15 days ago
3.8 kB
4
Indexable
Never
public static final MinecraftChannelIdentifier IDENTIFIER = MinecraftChannelIdentifier.from("rapidchallenge:channel");

    @Subscribe
    public void onPluginMessageFromBackend(PluginMessageEvent event)
    {
        main.getLogger().info("hi");

        if (!(event.getSource() instanceof Player))
        {
            return;
        }
        if (event.getIdentifier() != IDENTIFIER)
        {
            return;
        }

        ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
        String input = in.readUTF();
        String[] reading = input.split(" ");

        main.getLogger().info(input);

        String commandString = reading[0];
        VelocityCommand command = VelocityCommand.valueOf(commandString);

        switch (command)
        {
            case SEND_START_CHALLENGE ->
            {
                if (reading.length > 1)
                {
                    // If name was given with command, look up challenge type for database entry.
                    String challengeName = reading[1];
                    Challenge challenge = main.getChallengeManager().CreateChallenge(challengeName);

                    // Send command back with name
                    SendStartChallengeCommand(challenge.getUuid().toString(), challengeName, challenge.getData().getType().toString());
                }
                else
                {
                    // If no name was given, generate a challenge.
                    SendStartChallengeCommand();
                }
                main.StopChecker();
            }
            case SEND_STOP_CHALLENGE ->
            {
                SendStopChallengeCommand();
                main.RunChecker();
            }
            case SEND_WIN_MESSAGE -> SendWinMessageCommand(input);
        }
    }

    public void SendStartChallengeCommand()
    {
        Challenge challenge = main.getChallengeManager().CreateChallenge();

        String uuid = challenge.getUuid().toString();
        String name = challenge.getData().getName();
        String type = challenge.getData().getType().toString();

        CreateStartChallengeCommand(uuid, name);

        if (!main.getConfig().isSql())
        {
            return;
        }
        main.getSqlHandler().AddChallenge(uuid, name, type);
    }

    public void SendStartChallengeCommand(String uuid, String challengeName, String challengeType)
    {
        String uuidString = uuid.toString();
        CreateStartChallengeCommand(uuidString, challengeName);

        if (!main.getConfig().isSql())
        {
            return;
        }
        main.getSqlHandler().AddChallenge(uuidString, challengeName, challengeType);
    }

    private void CreateStartChallengeCommand(String uuid, String challengeName)
    {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF(VelocityCommand.RECEIVE_START_CHALLENGE + " " + uuid + " " + challengeName);
        SendDataToAllServers(out);
    }

    public void SendStopChallengeCommand()
    {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF(String.valueOf(VelocityCommand.RECEIVE_STOP_CHALLENGE));
        SendDataToAllServers(out);
    }

    public void SendWinMessageCommand(String command)
    {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF(command);
        SendDataToAllServers(out);
    }

    private void SendDataToAllServers(ByteArrayDataOutput out)
    {
        for (RegisteredServer server : main.getServer().getAllServers())
        {
            server.sendPluginMessage(IDENTIFIER, out.toByteArray());
        }
    }
Leave a Comment