I was wondering if there was some way of grabbing the value of @p during a console/command block command. When I grab the command being sent as of now it grabs literally “@p” instead of the value that @p stands for.
You could try extracting out the selector and parsing it? SpongeAPI/src/main/java/org/spongepowered/api/text/selector at 6ee796d6f3e53f155976f485a030ed8c7848985f · SpongePowered/SpongeAPI · GitHub
But what do you need the value for? Some more context would help us help you a lot better.
I have a selector PR that went stale; I’ll try to bump it back to the “active” state. With it, you can just use one of the GenericArguments.*
methods based on your needs.
I am trying to intercept the command from the command block or console command; cancel it but at the same time use modifiers to alter data about the player @p represented. Lets say for example off the top of my head cancel it and then cut the players health in half. I will try what you suggested and I’ll reply back with results.
cancel it? cut the health in half?
Are you trying to change the meaning of existing commands, or trying to create your own commands? I’m still not 100% sure on what you are trying to do, but it sounds like you would be better off creating your own commands then intercepting things?
Possible reasons for wanting to intercept things would be to create your own selectors, or selector arguments, or change the behaviour of what players are selected, like not targeting players that are vanished or something.
But most other things I can think of could be solved by registering your own commands and using them in the command blocks / console. If you want to change the behaviour of vanilla commands, you could probably unregister them somehow, or create commands with the same alias. Then provide whatever functionality you want, or delegate to the original command callable.
That was just an example. The main there here is that:
- A command is being sent from a command block or console and it is using @p to register what player the command is being used on
- I need to intercept this @p and get the player that the command is being used on
- Do various stuff using the players name like I said one of the infinite possibilities is cutting the players health in half. I have no intention of doing such a thing but its a possibility.
Creating my own commands etc. is not really an option in the current situation I’m faced with as it all revolves around getting this particular value from this particular situation.
Why not post some code examples. I’m thinking if you’re using an event to intercept the command being sent, you already have an instance of player and @p is a bit irrelevant
When I grab the command being sent as of now it grabs literally “@p”
I’d say no. When they are grabbing the command in the event they do NOT have an instance of player and that’s the problem.
Creating my own commands etc. is not really an option in the current situation I’m faced with as it all revolves around getting this particular value from this particular situation.
I’m still really confused. Are you able to parse the selector yourself, grab the player and replace the selector with the player like I originally suggested?
I see. No cause is given, when fired. That’s lame. You guys could probably create a feature request for that on github.
The whole selector thing didn’t work out as planned but I found an alternate solution. The issue with the selector was when grabbing the value of @p using the selector it would give an array containing all current players logged in.
But even then cause is different from the selector resolving. my suggestion of parsing the selector and replacing it with a given player was more a work around by forcing it to resolve early using your own logic.