Scenario: So I have an onCommand() method, the command is /safehouse and the argument will be a player name. 1. I want the code to get the argument and check if the player is online, if the player is not, do something, if the player is online, then run something. 2. How do I make the plugin send a message to players and have a ClickEvent on the text? Will this require an event class? When a player presses "accept", it sends a message to another player for example: "Accepted!" and when a player presses "Decline", it sends a message to another player for example: "Declined!". How would I do this? 3. How do I send a title message (/title)? do I use Bukkit.dispatchCommand? Because I would prefer using the plugin instead of getting console to issue the command.
1. Code (Java): if (player != null) { } else { } 2. This is not an event, it is actually done through json text. Look at BaseComponent in the spigot api docs for more info. 3. You do not have to dispatch a command, there are title methods within the api. However since you are using 1.8 I'm pretty sure you have to use packets (someone correct me if I am wrong please).
You can also use TextComponent, example: What i'm doing here is separating all the messages to only show a hover message on 'accept' or 'deny' Code (Java): TextComponent mainMessage = new TextComponent("If you accept, please "); TextComponent acceptMessage = new TextComponent("[Click Here]"); acceptMessage.setClickEvent(new ClickEvent(Action.RUN_COMMAND, "yourcommand))); acceptMessage.setHoverEvent(new HoverEvent(Action.SHOW_TEXT, new ComponentBuilder("Click to accept!").create())); player.sendMessage(mainMessage,acceptMessage);
1. Ik you put player != null which checks whether a player is online. But I'm doing this from an onCommand() method, so I'm doing /safehouse (player) which makes (player) an argument (args), how do I check if args is a Player and pinpoint the name of the player? (I just realised I missed one word after not, I meant "if the player is not online") 2. A Component? I'll try that out and play around with it. 3. I have heard of packets, but never really knew how to put them to use as I have no idea what they are used for (Titles? Chat?), so I don't know how to insert a packet too.
Bukkit.getPlayer(nameEntered) will get the Player targeted, or null if they are not online. For packets, there is a tutorial somewhere on spigot that explains how to send packets to send a title. I can't remember where it is right now - in some sort of wiki?
Using Bukkit#getPlayer and null-checking is not a good idea since it can return null if a player doesn’t exist as well if the player is Offilne. You should instead use Bukkit#getOfflinePlayer, which can never return null, and then check if the player is online (OfflinePlayer#isOnline())
Not a good idea. If you've had thousands of players joined your server, you'll see a huge lag spike when searching for an offline player by name.
You said it was a JSON Text, but is it still possible to do it through the plugin itself? I would need to run multiple commands upon accept/decline, so I would prefer it to be done through the plugin, so that I can just add more lines of code for it to do multiple things.
Here's the +1 question: I need a randomizer between two things, and for two players, they have a chance to either break in to the house or defend the house, and if they are chosen to break in, a code runs for them only, if they are chosen to defend, another code for them is triggered.
Code (Java): Random random = new Random(); if (random.nextBoolean()) { // 50% chance to break in or whatever } else { // 50% chance not to } if you want to have a chance different from 50%, use random.nextInt or random.nextDouble
This is the code I used: TextComponent acceptmessage = new TextComponent("[Accept]") and it returned: The constructor TextComponent(String) is not visible My only import is java.awt.TextComponent
I literally understand the 50%, but why is it a boolean tho, the Javadocs I looked up on this also doesn't seem to explain in English.
random.nextBoolean() returns either true or false, equally distributed. This means that you either get true or false with the same chance
Using random.nextBoolean(), it's randomizer but 50-50, if you want 30% or so, you want to use Code (Java): // Here your getting if a random number between 0 and 100 is less than 30. if(random.nextInt(100) <= 30){ // The player goes to a place }else{ // the player goes to another place }
I'm talking I have Player A and Player B This was Schottky's code: Code (Java): Random random = new Random(); if (random.nextBoolean()) { // 50% chance to break in or whatever } else { // 50% chance not to } 50% chance to break in and 50% chance to defend. This only tells me where they go, but how do I put the two players into this context? If I put this: Code (Java): p1 = Player A p2 = Player B if(random.nextBoolean()) { // 50% chance to break in p1.sendMessage("Your breaking in"); } else { //50% chance to defend p2.sendMessage("your defending"); } This wouldn't work as there are set players, how do I make it so whoever gets the role will get the message instead of set players? and I also require multiple commands to be run, but the JSON would only be able to run one command, and I don't wish for that to run any setblock or smth, is there a way to check whether the challenged player accepts and do something in the code?
So... so you only have two players? Or a set that you want to extract two random players out of? Code (Text): Player p1, p2; Player rober, defender; if (random.nextBoolean()) { rober = p1; defender = p2; } else { defender = p1; rober = p2; } this is a simple example for the first case
Only 2 players I think the example will help, thanks! Anything that can still help with my JSON issue: I also require multiple commands to be run, but the JSON would only be able to run one command, and I don't wish for that to run any setblock or smth, is there a way to check whether the challenged player accepts and do something in the code?