Command Errors

Hi, I was wondering if I could get some assistance regarding my code. If you could help, that would be wonderful!
No description
68 Replies
JavaBot
JavaBotβ€’2mo ago
βŒ› This post has been reserved for your question.
Hey @shazim! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
shazim
shazimOPβ€’2mo ago
package me.shazim.shazimsPlugin.commands;

import me.shazim.shazimsPlugin.ShazimsPlugin;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;

public class CreativeCommand implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


if (sender instanceof Player p) {

p.setGameMode(GameMode.CREATIVE);
String creativeMessage = this.plugin.getConfig().getString("creative-message");
if (creativeMessage != null) {
creativeMessage = creativeMessage.replace("%player%", p.getName());
event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', creativeMessage));

}
return true;

}
return true;
}
}
package me.shazim.shazimsPlugin.commands;

import me.shazim.shazimsPlugin.ShazimsPlugin;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;

public class CreativeCommand implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


if (sender instanceof Player p) {

p.setGameMode(GameMode.CREATIVE);
String creativeMessage = this.plugin.getConfig().getString("creative-message");
if (creativeMessage != null) {
creativeMessage = creativeMessage.replace("%player%", p.getName());
event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', creativeMessage));

}
return true;

}
return true;
}
}
dan1st
dan1stβ€’2mo ago
What exactly is the issue you are facing?
JavaBot
JavaBotβ€’2mo ago
πŸ’€ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
j
jβ€’2mo ago
Mind if i budge into this with ya... but this looks like copy and pasted code from one class to another Basically the command is "Creative" which i can assume he wants to make a command to change the player to creative mode instead of a "join-message" event thats happening. usually event.getPlayer() would come from an argument from a class that implements Listener interface
dan1st
dan1stβ€’2mo ago
yeah I got that (what it's trying to do) but I am not sure what the actual problem is
j
jβ€’2mo ago
the easist way for him to solve this would be to simply create Map that takes in the UUID (player Id) and the value which would be the gamemode! If map contains UUID then do this and he would then need to in the Listener class he makes...he would need a PlayerJoinEvent method and within playerjoinevent then implement the creativeMessage functionality
dan1st
dan1stβ€’2mo ago
oh I didn't look at the screenshot and there are compilation errors
j
jβ€’2mo ago
Yes
dan1st
dan1stβ€’2mo ago
Essentially the following: There is no this.plugin and event
j
jβ€’2mo ago
he is attempting to access methods from this.plugin. but cant because there isnt a JavaPlugin to be instantiated correct
dan1st
dan1stβ€’2mo ago
I just wanted to make sure it's clearly written down so that shazim can see it - feel free to continue ;)
shazim
shazimOPβ€’2mo ago
Yo I was asleep 😭 For the β€œjoin-message”, I may or may not have forgotten to change that (mb chat) For clarification, I am trying to have a /gmc command in which you can edit the message through config.
dan1st
dan1stβ€’2mo ago
.
shazim
shazimOPβ€’2mo ago
So how would I exactly fix it? Sorry I am a bit confused about what yall are saying
dan1st
dan1stβ€’2mo ago
What is event supposed to be? What exactly is Command?
shazim
shazimOPβ€’2mo ago
event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', creativeMessage));
In this sectioin, it is suppose to send the message that is found in config. I copied this from an JoinEvent class, so that is why you see "events"
dan1st
dan1stβ€’2mo ago
well ig that's the issue Can you show the content of the CommandSender class?
shazim
shazimOPβ€’2mo ago
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available

package org.bukkit.command;

public interface CommandSender extends org.bukkit.permissions.Permissible {
void sendMessage(@org.jetbrains.annotations.NotNull java.lang.String s);

void sendMessage(@org.jetbrains.annotations.NotNull java.lang.String... strings);

void sendMessage(@org.jetbrains.annotations.Nullable java.util.UUID uuid, @org.jetbrains.annotations.NotNull java.lang.String s);

void sendMessage(@org.jetbrains.annotations.Nullable java.util.UUID uuid, @org.jetbrains.annotations.NotNull java.lang.String... strings);

@org.jetbrains.annotations.NotNull
org.bukkit.Server getServer();

@org.jetbrains.annotations.NotNull
java.lang.String getName();

@org.jetbrains.annotations.NotNull
org.bukkit.command.CommandSender.Spigot spigot();

static class Spigot {
public Spigot() { /* compiled code */ }

public void sendMessage(@org.jetbrains.annotations.NotNull net.md_5.bungee.api.chat.BaseComponent component) { /* compiled code */ }

public void sendMessage(@org.jetbrains.annotations.NotNull net.md_5.bungee.api.chat.BaseComponent... components) { /* compiled code */ }

public void sendMessage(@org.jetbrains.annotations.Nullable java.util.UUID sender, @org.jetbrains.annotations.NotNull net.md_5.bungee.api.chat.BaseComponent component) { /* compiled code */ }

public void sendMessage(@org.jetbrains.annotations.Nullable java.util.UUID sender, @org.jetbrains.annotations.NotNull net.md_5.bungee.api.chat.BaseComponent... components) { /* compiled code */ }
}
}
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available

package org.bukkit.command;

public interface CommandSender extends org.bukkit.permissions.Permissible {
void sendMessage(@org.jetbrains.annotations.NotNull java.lang.String s);

void sendMessage(@org.jetbrains.annotations.NotNull java.lang.String... strings);

void sendMessage(@org.jetbrains.annotations.Nullable java.util.UUID uuid, @org.jetbrains.annotations.NotNull java.lang.String s);

void sendMessage(@org.jetbrains.annotations.Nullable java.util.UUID uuid, @org.jetbrains.annotations.NotNull java.lang.String... strings);

@org.jetbrains.annotations.NotNull
org.bukkit.Server getServer();

@org.jetbrains.annotations.NotNull
java.lang.String getName();

@org.jetbrains.annotations.NotNull
org.bukkit.command.CommandSender.Spigot spigot();

static class Spigot {
public Spigot() { /* compiled code */ }

public void sendMessage(@org.jetbrains.annotations.NotNull net.md_5.bungee.api.chat.BaseComponent component) { /* compiled code */ }

public void sendMessage(@org.jetbrains.annotations.NotNull net.md_5.bungee.api.chat.BaseComponent... components) { /* compiled code */ }

public void sendMessage(@org.jetbrains.annotations.Nullable java.util.UUID sender, @org.jetbrains.annotations.NotNull net.md_5.bungee.api.chat.BaseComponent component) { /* compiled code */ }

public void sendMessage(@org.jetbrains.annotations.Nullable java.util.UUID sender, @org.jetbrains.annotations.NotNull net.md_5.bungee.api.chat.BaseComponent... components) { /* compiled code */ }
}
}
This?
dan1st
dan1stβ€’2mo ago
In this case: Can't you just use commandSender.sendMessage(ChatColor.translateAlternateColorCodes('&', creativeMessage));?
j
jβ€’2mo ago
indeed you can :) but i believe he would want it to send the message to everyone online but if that is the case he should create a loop and then just loop through every player online and then send the message from within the loop
shazim
shazimOPβ€’2mo ago
nah
shazim
shazimOPβ€’2mo ago
Should I change it to just "sender"?
No description
dan1st
dan1stβ€’2mo ago
yes
j
jβ€’2mo ago
for (Player online : Bukkit.getOnlinePlayers()) { online.sendMessage(ChatColor.translateAlternateColorCodes('&', creativeMessage)); }
dan1st
dan1stβ€’2mo ago
Can't you just send a global message? instead of sending a message to all players
j
jβ€’2mo ago
yeah you could also do Bukkit.broadcastMessage but i dont think broadcast sends in chatcolor... if not mistakened unless they fixed it i know a long time ago that was an issue
shazim
shazimOPβ€’2mo ago
@dan1st | Daniel What should I do regarding this?
No description
dan1st
dan1stβ€’2mo ago
How are you handling configuration? (Note: I don't know much about Minecraft - Jordan is probably the better person for that)
j
jβ€’2mo ago
Create your main class singleton and use that to retrieve your getConfig method so in your main class (because you would quite never need to use more than one instance of the main) just create a getInstance method
shazim
shazimOPβ€’2mo ago
join-message: '&aWelcome back to the server &e&l%player%&a! We missed you.'
first-join-message: '&9&l%player%&9, welcome to our server, we hope you have a wonderful time!'
quit-message: '&c%player% has left the server!'

creative-message: '&cYour gamemode has been set to creative.'
join-message: '&aWelcome back to the server &e&l%player%&a! We missed you.'
first-join-message: '&9&l%player%&9, welcome to our server, we hope you have a wonderful time!'
quit-message: '&c%player% has left the server!'

creative-message: '&cYour gamemode has been set to creative.'
This is what my config looks like.
j
jβ€’2mo ago
.
dan1st
dan1stβ€’2mo ago
singleton ugh
j
jβ€’2mo ago
Yeahhh everything would be instantiated within your main class so no need to create more than one instance of it :P gotta love singletons Without judge, I also take it you're pretty new to java
shazim
shazimOPβ€’2mo ago
yea
j
jβ€’2mo ago
I do recommend brushing up on the bare basics of it :P it could help out a lot
dan1st
dan1stβ€’2mo ago
until you want to create unit tests
j
jβ€’2mo ago
lmaooo is it safe to say we should ban all instances of the word singleton from this thread
dan1st
dan1stβ€’2mo ago
lol nah it's fine I mean do Minecraft plugin devs really test their code?
j
jβ€’2mo ago
unit testing wouldnt be necessary tbh All you gotta simply do is...launch server, test plugin functionality...doesnt work? Input System.out.println lines to solve your issue. :SadMan:
shazim
shazimOPβ€’2mo ago
I am following a playlist for it, and trying to distance a bit from it for my own things, like this, so I'm not fully reliate on the playlist and try to learn some stuff myself (clearly you can see this is a disaster lol
j
jβ€’2mo ago
Best way you ever get fully developed into this world called software engineering/programming is to do it as you go. You have a playlist that teaches you things chapter by chapter learn from each chapter and implement from there rense and repeat it over and over until you fully grasp the concept of what it is they are teaching I made the mistake of jumping STRAIGHT into Minecraft Spigot development without knowing a thing of the Java language itself. Wasn't until I got a little older of course and had a want for software engineering as a whole that is when I told myself I need to learn how the language works. Ever since then, programming as a whole became pretty easy. Another rule of thumb: If you can read Oracle's Java Tutorial Docs...you are a powerful student (fyi) :SadMan:
shazim
shazimOPβ€’2mo ago
Im doing the same thing lol ehh you became smart so I should be fine πŸ’€ as a person with 0 iq (me), how should I do this 😭
j
jβ€’2mo ago
Whats your main class called?
shazim
shazimOPβ€’2mo ago
Also off topic question, how do I format my document in IntellJ IDEA Community Edition ShazimsPlugin.java (the name is bound to change lol)
j
jβ€’2mo ago
I assume you mean indent
shazim
shazimOPβ€’2mo ago
I mean how "Format Document" is on Visual Studio, is there a way to do that on IntellJ IDEA Community Edition
No description
j
jβ€’2mo ago
public class ShazimsPlugin extends JavaPlugin {

private static ShazimsPlugin instance;

private ShazimsPlugin() {
}

@Override
public void onEnable() {
instance = this;
}

@Override
public void onDisable() {

}

public static ShazimsPlugin getInstance() {
if (instance == null) {
instance = new ShazimsPlugin();
}
return instance;
}
}
public class ShazimsPlugin extends JavaPlugin {

private static ShazimsPlugin instance;

private ShazimsPlugin() {
}

@Override
public void onEnable() {
instance = this;
}

@Override
public void onDisable() {

}

public static ShazimsPlugin getInstance() {
if (instance == null) {
instance = new ShazimsPlugin();
}
return instance;
}
}
j
jβ€’2mo ago
No description
shazim
shazimOPβ€’2mo ago
No description
j
jβ€’2mo ago
go to Code tab on the top taskbar and then you'll see "Reformat Code" you dont right click you just select the whole file and then go to the section Code and reformat from there
shazim
shazimOPβ€’2mo ago
I'm on windows 😭
shazim
shazimOPβ€’2mo ago
No description
shazim
shazimOPβ€’2mo ago
There is no "Code" tab
j
jβ€’2mo ago
whats the 3 lines at the top left
shazim
shazimOPβ€’2mo ago
Oh nvm when you click the 3 lines it gives you those tabs ty
j
jβ€’2mo ago
No problem
shazim
shazimOPβ€’2mo ago
(Also in the screenshot you can see I am getting errors for the code you sent)
j
jβ€’2mo ago
I see that its because you copied and pasted it straight into there :P I truly do recommend you learn the Java language before you continue :P
shazim
shazimOPβ€’2mo ago
You got any playlists that can help without breaking my brain? Or something that can help
shazim
shazimOPβ€’2mo ago
Also @dan1st | Daniel am I suppose to be able to ping these roles? @
No description
j
jβ€’2mo ago
Depends on the playlist you watch but ever heard of a well known youtuber named "Bro Code" ?
shazim
shazimOPβ€’2mo ago
Nope lol\
dan1st
dan1stβ€’2mo ago
these are moderation roles so yeah you being able to ping these roles is fine
j
jβ€’2mo ago
I do recommend checking him out but be mindful not to fall into "Tutorial Hell" :P
dan1st
dan1stβ€’2mo ago
but doing stupid stuff and pinging these roles as part of it might be a good way to get banned
JavaBot
JavaBotβ€’2mo ago
πŸ’€ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
JavaBot
JavaBotβ€’2mo ago
Post Closed
This post has been closed by <@810632160418988053>.

Did you find this page helpful?