Jump to content

Simple /setspawn | /spawn Command


Recommended Posts

Posted

Hey leute,

Ich bin neu in dem ganzen Programmier Business und übe zur Zeit, indem ich Plugins programmiere.

Im Moment ist mein Projekt ein simples /setspawn zum setzten des spawnpoints und ein /spawn Command um dort hin zu gelangen.

Mein Code sieht wie folgt aus:

public class spawn implements CommandExecutor {

    Location spawn;


    public boolean onCommand(CommandSender cs, Command c, String s, String[] args) {

        if (cs instanceof Player){
            Player p = (Player)cs;
            if (p.isOp()){
                if (s.equalsIgnoreCase("setspawn")){

                    double xcordinate = p.getLocation().getX();
                    double ycordinate = p.getLocation().getY() + 3;
                    double zcordinate = p.getLocation().getZ();
                    spawn = new Location(Bukkit.getWorld("Lobby"), xcordinate, ycordinate, zcordinate);

                    p.sendMessage(ChatColor.GOLD + "Die neuen Spawn coordinaten sind: " + xcordinate + ycordinate + zcordinate);
                }




            }else {cs.sendMessage(ChatColor.RED + "Du besitzt nicht die benötigten Rechte!");}


            if (s.equalsIgnoreCase("spawn")){

                p.teleport(spawn);

            }

        }else {System.out.println("Der Command ist ausschließlich für Spieler geeignet");}


        return false;
    }
}

Wenn ich /setspawn eingebe klappt alles, jedoch bekomme ich dann bei /spawn ein Internal Error.

Wo ist mein Denkfehler?

Liebe Grüße

Posted (edited)
[20:14:45] [Server thread/INFO]: Dinozaurier issued server command: /spawn
[20:14:45] [Server thread/ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'spawn' in plugin BasicTees v0.2
	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:909) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:902) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:886) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:819) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at java.lang.Thread.run(Thread.java:830) [?:?]
Caused by: java.lang.IllegalArgumentException: location
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at org.bukkit.craftbukkit.v1_14_R1.entity.CraftPlayer.teleport(CraftPlayer.java:616) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity.teleport(CraftEntity.java:451) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	at com.Commands.spawn.onCommand(spawn.java:40) ~[?:?]
	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
	... 17 more

@Bedrock

 

Edited by tasse
Posted (edited)

Also die Ursache ist, dass Location im spawn-Command null ist.

setspawn und spawn sind ja zwei verschiedene Befehle - sozusagen weiß der spawn-Befehl nicht, was der setspawn-Befehl gemacht hat. Daher wird Location im spawn-Befehl nicht aktualisiert und bleibt daher null.

Die einfachste und geläufigste Methode wäre, die Location über den setspawn-Befehl in eine Config zu speichern und mit dem spawn-Befehl wieder abzurufen. Ohne Config wäre der setspawn nach einem Serverneustart auch wieder weg.

 

Zuerst legt man eine Config-Datei an - im Code einfach unter "Location spawn;" packen

public void saveLocation (Player p) {
    	
    	UUID id = p.getUniqueId();
    	
    	File locationData = new File (Bukkit.getServer().getPluginManager().getPlugin("PLUGINNAME").getDataFolder(), File.separator + "PlayerLocation");
        File file = new File(locationData, File.separator + id + ".yml");
        FileConfiguration fc = YamlConfiguration.loadConfiguration(file);
        
        if (!file.exists()) {
        	try {
        		fc.set("location", spawn);
        		fc.save(file);
        	} catch (IOException exception) {
        		exception.printStackTrace();
        	}
        } else {
        	try {
        		fc.set("location", spawn);
        		fc.save(file);
        	} catch (IOException exception) {
        		exception.printStackTrace();
        	}
        }
    }

Nachdem du die Koordinaten mit dem setspawn-Befehl abgegriffen hast, führst du saveLocation(p) aus - damit wird die "Location spawn" in eine .yml-Datei gespeichert. Die wird sich dann in deinem Plugin-Config-Ordner unter "PlayerLocation" befinden.

...
spawn = new Location(Bukkit.getWorld("WELTNAME"), xcordinate, ycordinate, zcordinate);
saveLocation(p);

Über die UUID des Spielers wird sozusagen für jeden, der den Befehl nutzen kann, eine eigene Datei im Ordner angelegt.

Im spawn-Befehl holt man dann die Location zurück:

if (s.equalsIgnoreCase("spawn")){
            	
	Location spawn = null;
	UUID id = p.getUniqueId();
            	
	File locationData = new File (Bukkit.getServer().getPluginManager().getPlugin("PLUGINNAME").getDataFolder(), File.separator + "PlayerLocation");
	File file = new File(locationData, File.separator + id + ".yml");
	FileConfiguration fc = YamlConfiguration.loadConfiguration(file);
                
	spawn = (Location) fc.get("location");
	p.teleport(spawn);
}

 

Das war es dann auch schon 🙂 Im Code musst du noch PLUGINNAME und WELTNAME ersetzen. Dann sollte alles funktionieren.

 

Wegen der Chatausgabe vom setspawn-Befehl:

Da würde es sich anbieten, die Koordinaten als Integer abzugreifen - es sieht im Chat besser aus, als die ewig langen Zahlen 😉

int xcordinate = (int) p.getLocation().getX();
int ycordinate = (int) (p.getLocation().getY() + 3);
int zcordinate = (int) p.getLocation().getZ();
spawn = new Location(Bukkit.getWorld("WELTNAME"), xcordinate, ycordinate, zcordinate);
saveLocation(p);
                    
p.sendMessage(ChatColor.GOLD + "Die neuen Spawn-Koordinaten sind: " + ChatColor.GREEN + xcordinate + " " + ycordinate + " " + zcordinate);

 

Edited by Bedrock

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...
B
B