Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/org/sk/skMinecraft/SkMinecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;

public final class SkMinecraft extends JavaPlugin implements Listener {

Expand Down Expand Up @@ -51,11 +54,36 @@ public static String joinWithSeperator(Object... args) {
return result;
}

public static String getSuccessMessage(){
return joinWithSeperator(seperator + "success" + seperator);
}

public static String getFailMessage(){
return joinWithSeperator(seperator + "failed" + seperator);
}

public static StringCommand splitCommand(String command) {
String[] parts = command.split(SkMinecraft.seperator);
return new StringCommand(parts[0], Arrays.copyOfRange(parts, 1, parts.length));
}

public static String runBukkitTaskSync(JavaPlugin plugin, Supplier<String> task) {
CompletableFuture<String> future = new CompletableFuture<>();

Bukkit.getScheduler().runTask(plugin, () -> {
String result;
try {
result = task.get();
} catch(Exception e) {
result = SkMinecraft.getFailMessage();
}

future.complete(result);
});

return future.join();
}

public static int playerIndexFromName(String name) {
Player[] players = Bukkit.getOnlinePlayers().toArray(new Player[0]);

Expand Down Expand Up @@ -144,12 +172,18 @@ private void handleClient(Socket client) {

if (!commandObject.isValid()) {
System.out.println("Invalid");
writer.println("Received command is invalid: " + command);
continue;
}

commandObject.setParameters(writer, this);

commandObject.apply();

// This output would be provided by .apply();
String output = "";

writer.println(output);
}

client.close();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/AddInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;


Expand Down Expand Up @@ -80,5 +81,7 @@ public void apply() {
inv.addItem(stack);
}
});
this.writer.println(SkMinecraft.getSuccessMessage());

}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/ChatCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;

public class ChatCommand extends Command {
Expand All @@ -19,5 +20,6 @@ public void apply() {
Bukkit.getScheduler().runTask(this.plugin, () -> {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), this.command);
});
this.writer.println(SkMinecraft.getSuccessMessage());
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/DeleteBossBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.boss.KeyedBossBar;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;

Expand Down Expand Up @@ -38,5 +39,6 @@ public void apply() {
bossBar.removeAll();
}
});
this.writer.println(SkMinecraft.getSuccessMessage());
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/EditBossBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.KeyedBossBar;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;

Expand Down Expand Up @@ -88,5 +89,6 @@ public void apply() {
}

});
this.writer.println(SkMinecraft.getSuccessMessage());
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/EditEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;
import org.sk.skMinecraft.data.Position;
Expand Down Expand Up @@ -107,5 +108,6 @@ public void apply() {
ent.setCustomName(this.name);
}
});
this.writer.println(SkMinecraft.getSuccessMessage());
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/PostChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;

public class PostChat extends Command {
Expand All @@ -17,5 +18,6 @@ public PostChat(StringCommand command) {
@Override
public void apply() {
Bukkit.broadcastMessage(this.message);
this.writer.println(SkMinecraft.getSuccessMessage());
}
}
11 changes: 8 additions & 3 deletions src/main/java/org/sk/skMinecraft/commands/SetPlayerPos.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;

Expand Down Expand Up @@ -52,11 +53,11 @@ public SetPlayerPos(StringCommand command ){

@Override
public void apply() {
Bukkit.getScheduler().runTask(plugin, () -> {
String result = SkMinecraft.runBukkitTaskSync(plugin, () -> {
Player[] players = Bukkit.getOnlinePlayers().toArray(new Player[0]);
if(playerIndex < 0 || playerIndex >= players.length) {
writer.println("error invalid_index");
return;
// writer.println("error invalid_index");
return SkMinecraft.getFailMessage();
}

Player player = players[playerIndex];
Expand All @@ -67,7 +68,11 @@ public void apply() {
loc.setYaw(this.rot);
}
player.teleport(loc);

return SkMinecraft.getSuccessMessage();
});

// Hier könnte man das dann returnen
// return result
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/SetPlayerStat.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Player;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;

Expand Down Expand Up @@ -69,5 +70,6 @@ public void apply() {
case XP_PROGRESS -> target.setExp((float)this.value);
}
});
this.writer.println(SkMinecraft.getSuccessMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;

Expand Down Expand Up @@ -64,5 +65,6 @@ public void apply() {
case LOOKING -> target.setVelocity(target.getLocation().getDirection().normalize().multiply(this.strength));
}
});
this.writer.println(SkMinecraft.getSuccessMessage());
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/ShowTitle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;

Expand Down Expand Up @@ -54,6 +55,7 @@ public void apply() {

players[this.player].sendTitle(this.title, this.subtitle, this.fadeIn, this.stay, this.fadeOut);
}
this.writer.println(SkMinecraft.getSuccessMessage());

}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/SpawnBossBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.KeyedBossBar;
import org.bukkit.entity.Player;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;

Expand Down Expand Up @@ -54,5 +55,6 @@ public void apply() {

bossBar.setVisible(true);
});
this.writer.println(SkMinecraft.getSuccessMessage());
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sk/skMinecraft/commands/SpawnEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.sk.skMinecraft.CentralResourceHandler;
import org.sk.skMinecraft.SkMinecraft;
import org.sk.skMinecraft.SkMinecraft.StringCommand;
import org.sk.skMinecraft.commands.ArgumentParser.ParseResult;

Expand Down Expand Up @@ -55,5 +56,6 @@ public void apply() {
String output = GetEntity.informationString(uuid, entity);
writer.println(output);
});
this.writer.println(SkMinecraft.getSuccessMessage());
}
}