Search code examples
javaminecraft

How do I make a plugin that sends a message to specific people?


I want to make a plugin which sends a message to people who I specify in config.yml.

MinePlayers:
-me
-someone_else

But I can't think of a way to do this. My current code doesn't run, but I will paste it:

public class antixray implements Listener {

    Plugin plugin = TestPlugin.getPlugin(TestPlugin.class);
    
    @EventHandler
    public void blockBreak(BlockBreakEvent e){
        Player p = e.getPlayer();
        Block block = e.getBlock();
        String minedetector1 = plugin.getConfig().getStringList("MinePlayers").get(0); //gets the first player name in the config
        Player m1 = Bukkit.getServer().getPlayerExact(minedetector1);    //tries to find player in game
        String minedetector2 = plugin.getConfig().getStringList("MinePlayers").get(1);
        Player m2 = Bukkit.getServer().getPlayerExact(minedetector2);
        String minedetector3 = plugin.getConfig().getStringList("MinePlayers").get(2);
        Player m3 = Bukkit.getServer().getPlayerExact(minedetector3);
    
        Player tm1 = null;  //intelliJ IDEA told me to put these here
        Player tm2 = null;
        Player tm3 = null;
    
    
        if(m1!=null){                  //i though null players caused the error, so i put this here
            Player tm1=m1;
        }
        if(m2!=null){
            Player tm2=m2;
        }
        if(m3!=null){
            Player tm3=m3;
        }
        if(m1 != null || m2 != null || m3 != null){            //this would make a message print
            printmsg(block.getType().toString(),tm1,tm2,tm3);
        }

}
public static void printmsg(String type, Player m1, Player m2, Player m3) {
//this would print the messages, there would be code here
}
[20:14:48] [Server thread/ERROR]: Could not pass event BlockBreakEvent to TestPlugin v1.0-SNAPSHOT
org.bukkit.event.EventException: null
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-api-1.19.2-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-api-1.19.2-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-api-1.19.2-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-api-1.19.2-R0.1-SNAPSHOT.jar:?]
at net.minecraft.server.level.PlayerInteractManager.a(PlayerInteractManager.java:352) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.level.PlayerInteractManager.a(PlayerInteractManager.java:311) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.level.PlayerInteractManager.a(PlayerInteractManager.java:279) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1586) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.network.protocol.game.PacketPlayInBlockDig.a(SourceFile:42) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.network.protocol.game.PacketPlayInBlockDig.a(SourceFile:8) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:31) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.TickTask.run(SourceFile:18) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.util.thread.IAsyncTaskHandler.d(SourceFile:157) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.d(SourceFile:23) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:1150) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:1) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.util.thread.IAsyncTaskHandler.x(SourceFile:131) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.MinecraftServer.bh(MinecraftServer.java:1129) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1122) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.util.thread.IAsyncTaskHandler.c(SourceFile:140) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1106) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.MinecraftServer.v(MinecraftServer.java:1017) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:293) ~[spigot-1.19.2-R0.1-SNAPSHOT.jar:3604-Spigot-1aec3fc-f43634a]
at java.lang.Thread.run(Thread.java:833) [?:?]
Caused by: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) ~[?:?]
at jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) ~[?:?]
at jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266) ~[?:?]
at java.util.Objects.checkIndex(Objects.java:359) ~[?:?]
at java.util.ArrayList.get(ArrayList.java:427) ~[?:?]
at play.ourcraft.testplugin.listeners.antixray.blockBreak(antixray.java:23) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-api-1.19.2-R0.1-SNAPSHOT.jar:?]
... 23 more

^ This is returned by the console.

My code doesn't reflect what I want to do fully, What I want is: Event is called -> It checks if block broken is in (list of blocks) -> If it is send message of name of the block and the person who triggered the event to (list of people).


Solution

  • You save as list, and check for all index is not good idea, mostly when you will add/remove players from list.

    The error comes from the list content. It seems to be empty, and you are getting index from it. Maybe you should check also how you load config file.

    To don't have field for each player name in string list, use for loop like that:

    @EventHandler
    public void blockBreak(BlockBreakEvent e){
        Player p = e.getPlayer();
        Block block = e.getBlock();
        List<String> minePlayers = plugin.getConfig().getStringList("MinePlayers");
        for(String name : minePlayers) {
            Player cible = Bukkit.getPlayerExact(name);
            if(cible != null){
                printmsg(block.getType(), cible);
            }
        }
    }
    
    public static void printmsg(Material type, Player p) {
       // This would print the messages, there would be code here
       p.sendMessage("Some type: " + type.name());
    }