#Code not executing properly

1 messages · Page 1 of 1 (latest)

atomic cradle
#

So I'm basically making a java program execute functions when it receives data but its not running when it finds a specifc player. can someone just read through the code pls and tell me what is gong wrong.

MAIN: https://srcb.in/SKchK60CEj
UTILS: https://srcb.in/neEWBCOX8s

peak bearBOT
#

<@&987246883653156906> please have a look, thanks.

sharp sun
#

can you please be more specific

#

what is not working

#

where is that in the code

atomic cradle
#

so

#

in main

#

when it recives say player:etheboi &4blah blah blah it should reply with the player etheboi seeing in chat &4Blah Blah blah (coloured)

#

but that isnt working

#

but say all::players blah blah blah is working

sharp sun
#

you tried going through it using a debugger?

atomic cradle
#

?

#

ive used chatgbt

#

but 9it sucks

sharp sun
#

bruh

brittle ether
#

No, the tool can only help you so much.

#

And try sharing the relevant bit of code.

atomic cradle
#

idk

#

i think its

sharp sun
#

also the concurrency structure here is weird

atomic cradle
#

?

#

chatgbt helped a ton

sharp sun
#

with a debugger you can step through the code line by line and check values of variables available
pretty helpful to verify that it does what you want

atomic cradle
#

it does execute

#

it executes

sharp sun
#

yeah

atomic cradle
#

no errors

sharp sun
#

but the logic is wrong

atomic cradle
#

but it just doesnt correctly

sharp sun
#

thats why you would use a debugger

atomic cradle
#

can u send me one?

sharp sun
#

your ide should have one

#

you using intellij?

atomic cradle
#

ye

sharp sun
#

next to the run button

#

there should be a debugger run button

#

which you can press instead

#

but before that

#

make a break point

atomic cradle
#

?

sharp sun
#

at the line you want it to stop

atomic cradle
#

idk

#

its a mc plugin

#

so it doesnt run

sharp sun
sharp sun
#

you can still debug

#

but need to run the server through intellij

atomic cradle
#

how 😭

sharp sun
#

create a new run configuration

#

give me a second

#

something like this should be on the top right

#

it might not have all symbols I got

#

because I have some plugins

#

this pops up when clicking on your current run configuration

atomic cradle
#

dat dont work

sharp sun
#

click on "edit configurations"

atomic cradle
#

when i run and debug

sharp sun
atomic cradle
#

it just runs the pom.xml

sharp sun
#

to debug the servers jar

atomic cradle
#

um

#

imma just

sharp sun
#

it should open such a menu to edit your configurations

atomic cradle
#

look through the code

#

its gonna be way quicker

sharp sun
#

uh no

#

especially not if you use old switch statement with break

#

instead of switch expression

#

anyways

#

your target isnt covered by the switch

#

none of these match "player:etheboi"

atomic cradle
#

its player:<playername>

#

etheboi is a placeholder

sharp sun
#

not in the switch

#

thats not what the code is checking though

#

you probably want a proper check instead of a switch here

atomic cradle
#

how do i do that

sharp sun
#

like String#startsWith or similar

#

simple if statement

atomic cradle
#

so

brittle ether
#

But in your case: learn to write the application yourself, rather than using chatGPT. That'll save you time.

sharp sun
atomic cradle
#

1 mo

#

thinking

brittle ether
sharp sun
#

not really

#

because intellij often just contains the plugin

#

not the server

#

for testing

#

but you can setup a jar application run configuration so that you can run the server through intellij

atomic cradle
#
if parts[1] == "all::players" {
  Utils.send(Bukkie.getConsoleSender(), message);
} else {
  // the rest of the code
}
atomic cradle
#

so parts[1] is a split string

sharp sun
#

you can generate if/else directly from your current switch statement

#

in intellij

#

alt + enter ont he switch statement

#

and then Replace switch with if

atomic cradle
#

ok

#

1 mo

#

so

#

?

#

if (state)

sharp sun
#

no

#

thats my code

#

just showing the replace switch with if action

atomic cradle
#

so

#

i just do if (executor)

#

@sharp sun

sharp sun
#

well in your current code the target is stored in this variable:

String target = sayArgs[0].toLowerCase();
#

and it is either exactly "all::players" (use equals method) or it is "player:<name>" where <name> is a placeholder (use startsWith to check if its player:) and if its not any of them you respond with an error (in the else)

#

what?

atomic cradle
#

i am confised

#

confused

sharp sun
#

what exactly

atomic cradle
#

what i need to do to fix it

#

ur giving me a ton of things

#

and idk what

sharp sun
#

create a proper check for the target

atomic cradle
#

😭 how

sharp sun
#

do you know what and how to write an if/else?

atomic cradle
#

ye

#
if (blah) {
  // do something
} else {
  // do something else
}
sharp sun
#

so then start with the first step

#
  1. check if target equals "all:players"
atomic cradle
#

so

#

1 mo

#

do i need to change switch (target)

sharp sun
#

yeah

atomic cradle
#

to if (target)

sharp sun
#

no

#

the expression in the () needs to be a boolean

#

it must be either true -> executes code in the if or false -> doesnt execute code in the if

#
if (<expression>) {
    // executes if expression is true
}
atomic cradle
#

so

sharp sun
#

and your expression is target equals "all:players"

#

if that evaluates to true it is going to run the code in the if

sharp sun
atomic cradle
#
if (target == "all::players") {
  // blah
} else {
  // blahg=
}
sharp sun
#

kinda, now the thing is you cant use == for String

#

you need to use a method for that

#

target.equals("all:players")

atomic cradle
#

oh ok

#

so ```java
if (target.equals("all::players")) {
// blah
} else {
// blah
}

sharp sun
#

yes

#

you can remove the else for now

atomic cradle
#

kk

sharp sun
#

we going to create the whole if/else structure step by step

atomic cradle
#

ty

#

i need that

#

attention problems

#

so imma do some stuff with my code rn

#
switch (target) {
                                case "all::players":
                                    Utils.send(Bukkit.getConsoleSender(), message);
                                    break;
                                case "player":
                                    String[] playerArgs = message.split(" ", 2);
                                    if (playerArgs.length != 2 || !playerArgs[0].startsWith("player:")) {
                                        writer.println("Invalid player format in say command: " + message);
                                        break;
                                    }
#

this is a snappet rn

#

so do i need to change it to

#
if (target.equals("all::players")) {
  Utils.send(Bukkit.getConsoleSender(), message)
  // utils is another file dw abt it
} else if (target.equals("player:")) {
  // code
}
sharp sun
#

yeah kinda

#

but now it contains the same problem

#

that we are going to fix now

#

if target is "player:ethaboi" it wont be the exact same as "player:", you just want to check if the string starts with that prefix

atomic cradle
#

oh so

sharp sun
#

so instead of the equals method you going to use the startsWith method

atomic cradle
#
... else if (target.startswith("player:")) {
  // do something
}
sharp sun
#

yeah but the method is startsWith, notice the upper case W

atomic cradle
#

ye ok

#

imma update my code

#

1 mo

#

ive done this now

sharp sun
#

remove all the break

atomic cradle
#

oh kk

#

breaks gone

sharp sun
#

but now we need to update the inner if statement

atomic cradle
#

ok...

sharp sun
#

first you need an if else now

#

because it doesnt exit if the if is true

#

so it is going to execute both

atomic cradle
#

ok

sharp sun
#
String[] playerArgs = message.split(" ", 2);
if (playerArgs.length != 2 || !playerArgs[0].startsWith("player:")) {
    writer.println("Invalid player format in say command: " + message);
} else {
    String playerName = playerArgs[0].substring("player:".length());
    String playerMessage = playerArgs[1];
    
    Player targetPlayer = Bukkit.getPlayerExact(playerName);
    if (targetPlayer != null) {
        Utils.send(targetPlayer, playerMessage);
    } else {
        writer.println("Player " + playerName + " is not online.");
    }
}

it would look something liek that

atomic cradle
#

k 1 mo

#

done dat

sharp sun
#

also I think this is wrong:

String[] playerArgs = message.split(" ", 2);
#

but depends what you want

#

if we look at the input: say player:etheboi &4blah blah blah

atomic cradle
#

its limited to 2

#

so that anything after the 2nd space

#

is not counted

sharp sun
#

well what do you expect happens if you call the split method on this string "&4blah blah blah"

#

would you expect an array that contains this: "&4blah", "blah"

atomic cradle
#

no

#

its just

#

"say", "player:etheboi", "blah blah blah"

sharp sun
#

thats not what happens here

atomic cradle
#

oh

sharp sun
#

so you need to update your code

atomic cradle
#

ok so how do i make this happen

sharp sun
#

so lets check what happens step by step currently:
input: "say player:etheboi &4blah blah blah"
line 81: String[] parts = text.split(" ", 2);

#

parts now contains this:

#

"say" and "player:etheboi &4blah blah blah"

atomic cradle
#

mhm

sharp sun
#

we can verify with quick testing

#

the first switch (switch(command)), takes care of the "say" input

#

then you further split the arguments

#

line 93: String[] sayArgs = arguments.split(" ", 2);

#

that results into this

#

which you store into these variables:

String target = sayArgs[0].toLowerCase();
String message = sayArgs[1];
#

the if else we created check the target

#

now if it startsWith player: you want to extract the etheboi value, so the name of the player

#

you do that by using the message variable though

#

which is wrong

#

you want to extract it from the target variable

atomic cradle
#

so

#

do we do

#

like

sharp sun
#

so you would split the target by the : character

atomic cradle
#

parts[1]

#

whick is player:etheboi

#

then split it again

#

like

sharp sun
#

no parts[1] is this: player:etheboi &4blah blah blah

atomic cradle
#

String[] parts = parts[1].split(" ", 1)

#

oh

#

so if we remove the 2

#

it will just constantly split it

sharp sun
#

you already got the target and message in the current code

sharp sun
#
String target = sayArgs[0].toLowerCase();
String message = sayArgs[1];
atomic cradle
#

mhm

#

where do i put this

sharp sun
#

?

#

can you share your current code

#

I am still looking at the old source bin

atomic cradle
#
switch (command) {
                        case "say":
                            // format: say <player:<player>/all::players><text>
                            String[] sayArgs = arguments.split(" ", 2);
                            if (sayArgs.length != 2) {
                                writer.println("Invalid say command format: " + arguments);
                                break;
                            }

                            String target = sayArgs[0].toLowerCase();
                            String message = sayArgs[1];

                            if (target.equals("all::players")) {
                                Utils.send(Bukkit.getConsoleSender(), message);
                                break;
                            } else if (target.startsWith("player:")) {
                                String[] playerArgs = message.split(" ", 2);
                                if (playerArgs.length != 2 || !playerArgs[0].startsWith("player:")) {
                                    writer.println("Invalid player format in say command: " + message);
                                } else {
                                    String playerName = playerArgs[0].substring("player:".length());
                                    String playerMessage = playerArgs[1];

                                    Player targetPlayer = Bukkit.getPlayerExact(playerName);
                                    if (targetPlayer != null) {
                                        Utils.send(targetPlayer, playerMessage);
                                    } else {
                                        writer.println("Player " + playerName + " is not online.");
                                    }
                                }
                            }
sharp sun
#

yeah

#

so there you have target and message

#

which you want to use in the else if

#

remove all of the code in the else if for now

atomic cradle
#

which else

#

there are meny

sharp sun
#
} else if (target.startsWith("players:")) {

}
#

and lets continue step by step

atomic cradle
#

so in that one

#

ok

#

ill do the code here for now

sharp sun
#

so the next step would be to extract the player name from the target variable

#

for that you can either use split

#

or substring

atomic cradle
#

mhm

#

so

sharp sun
#
String player = target.substring("player:".length());
#

this works

#

but you should try to understand what this does

atomic cradle
#
} else if (target.startsWith("players:")) {
  String player = target.substring("player:".length());
}
sharp sun
#

substring, as the name suggests, takes a part of the original string

#

the argument of the method is the start index

#

so you cut of the beginning

atomic cradle
#

sp

#

so

#

player

sharp sun
#

if we check with this example it seems to work

atomic cradle
#

OH!

#

ok ty

#

imma put that in my code now

sharp sun
#

the javadocs got some examples for substring

#

but you need to be careful

#

and add some validation before you use that variable

#

for example what if the target variable is just player: without specifying a name

#

hm you would get an empty string

#

which is not a valid name

atomic cradle
#

so ive updated the main file

#

but we need to change

sharp sun
#

?

#

we are not done yet

atomic cradle
#

oh

sharp sun
#

as I said step by step

atomic cradle
#

oh srry

#

continue

sharp sun
#

so this should be the current else if:

} else if (target.startsWith("players:")) {
  String player = target.substring("player:".length());
  
}
#

now as I said you need to verify that the player string is valid

#

and for that I would simply check if the player variable is blank, because that indicates an invalid player name

#
if (player.isBlank()) {
    // handle bad format
}
#

you can use the isBlank method

#

or isEmpty

#

both would work kinda the same here

atomic cradle
#

ooooh

#

ok

#

smart

sharp sun
#

and then you basically use the code you had here, writing a response that the player format is invalid and then exiting the current execution

atomic cradle
#

mhm

#

so so far we have

#
} else if (target.startsWith("players:")) {
  String player = target.substring("player:".length());
  if (player.isBlank()) {
    if (playerArgs.length != 2 || !playerArgs[0].startsWith("player:")) {
      writer.println("Invalid player format!")
      break;
    }
  }
}
sharp sun
#

into the isblank if

atomic cradle
#

oh kk

sharp sun
#

but only the writer.println and break not the whole if

atomic cradle
#

updated

sharp sun
#

as I said not the whole if

#

just move the content of the old if into the new if

atomic cradle
#

oh

#

so replace player.isBlank with that

sharp sun
#

no

#

it was basically the other way around

#
if (playerArgs.length != 2 || !playerArgs[0].startsWith("player:")) {
    writer.println("Invalid player format!")
    break;
}

turns into

if (player.isBlank()) {
    writer.println("Invalid player format!")
    break;
}
atomic cradle
#

oh

#

so a shortener

sharp sun
#

well the old validation was wrong in the first place

atomic cradle
#

oh ok lol

#
} else if (target.startsWith("players:")) {
  String player = target.substring("player:".length());
  if (player.isBlank()) {
    writer.println("Invalid player format!")
    break;
  }
}
sharp sun
#

yeah now lets continue with the actual logic that happens if the input is correct

#

for that you need to turn the name into the actual Player object

atomic cradle
#

mhm

sharp sun
#

you can use Bukkit.getPlayerExact(name) for that

atomic cradle
#

so

sharp sun
#

though in your case use Bukkit.getPlayer instead

atomic cradle
#
} else if (target.startsWith("players:")) {
  String player = target.substring("player:".length());
  if (player.isBlank()) {
    writer.println("Invalid player format!")
    break;
  } else {
    Bukkit.getPlayerExact(player); // or do we have to put it into a variable
  }
}
sharp sun
#

put it into a variable

atomic cradle
#

String player = getPlayerExact(strPlayer); (player is changed to strPlayer)

#

i dont think its that

#

as it is a string

sharp sun
#

nah its more like this: Player sender = Bukkit.getPlayerExact(player);

atomic cradle
#

oh kk

#
} else if (target.startsWith("players:")) {
  String strPlayer = target.substring("player:".length());
  if (player.isBlank()) {
    writer.println("Invalid player format!")
    break;
  } else {
    Player player = Bukkit.getPlayerExact(strPlayer);
    Utils.send(player, "&bIt works!")
  }
}
sharp sun
#

kinda

#

but now use the message argument instead of &bIt works!

atomic cradle
#

ye

#

so is that the finneshed code?

sharp sun
#

for that else if?

#

probably

atomic cradle
#

dis is the code now

#

look right?

sharp sun
#

yeah looks fine

#

though I would generally move some stuff into methods for better readability

#

but whatever

atomic cradle
#

imma test it

#

1 min

#

it broke

sharp sun
#

can you share the current code

atomic cradle
sharp sun
#

well that error seems unrelated to the new code

#

it says its failing here:
text = reader.readLine();

atomic cradle
#

oh lol

#

how do i fix it

sharp sun
#

well the error doesnt really make sense peepo_think

#

because the log the line after that runs fine

#
getLogger().info("Received from client: " + text);
#

im not really familiar with sockets

atomic cradle
#

ok

#

mhm

#

well it wasnt happening be4

#

so something weve done has done this

#

wait..

sharp sun
#

error isnt saying that

atomic cradle
#

well

#

it works with all::players

#

just not player:player

sharp sun
#

well the error is clearly saying that reading is causing this

#

so idk 🤷‍♂️

atomic cradle
#

imma check the python side rq

#

ik this isnt java but do u see any differences?

#

its called as player:etheboi

#

hold up

#

1 mo

#

i may have it

#

so its having problems reading the player: part

#

cuz it works with anything else

sharp sun
#

well the code checks for players: not player:

#

that is probably your issue

atomic cradle
#

oh

#

hold up

#

OHH

#

wait

#

imma retry this

#

omg

#

ty

#

it works

#

just errors are thrown

#

can i like not show the errors?

sharp sun
#

instead handle them properly

atomic cradle
#

sp

#

so

#

do i just do a try and catch

#

omg tysm @sharp sun ur goated

#

it works

#

i managed to hide the error messages

#

so it doesnt spam the console