Merge pull request 'Add basic sleep command to ScriptSystem' (#142) from ScriptSleep into master
Reviewed-by: Lixfel <lixfel@steamwar.de>
Dieser Commit ist enthalten in:
Commit
57f26874a5
@ -19,10 +19,12 @@
|
|||||||
|
|
||||||
package de.steamwar.bausystem.world;
|
package de.steamwar.bausystem.world;
|
||||||
|
|
||||||
|
import de.steamwar.bausystem.BauSystem;
|
||||||
import de.steamwar.core.Core;
|
import de.steamwar.core.Core;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.Action;
|
import org.bukkit.event.block.Action;
|
||||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||||
@ -30,35 +32,38 @@ import org.bukkit.event.player.PlayerInteractEvent;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class ScriptListener implements Listener {
|
public class ScriptListener implements Listener {
|
||||||
|
|
||||||
@EventHandler
|
private Set<Player> playerSet = new HashSet<>();
|
||||||
public void onLeftClick(PlayerInteractEvent event) {
|
|
||||||
if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK)
|
|
||||||
return;
|
|
||||||
|
|
||||||
|
public ScriptListener() {
|
||||||
|
Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), playerSet::clear, 5, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler(priority = EventPriority.HIGH)
|
||||||
|
public void onLeftClick(PlayerInteractEvent event) {
|
||||||
ItemStack item = event.getItem();
|
ItemStack item = event.getItem();
|
||||||
if(item == null || isNoBook(item) || item.getItemMeta() == null)
|
if(item == null || isNoBook(item) || item.getItemMeta() == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
event.setCancelled(true);
|
if (event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) {
|
||||||
Player player = event.getPlayer();
|
if (event.getAction() == Action.RIGHT_CLICK_AIR) {
|
||||||
BookMeta meta = (BookMeta) item.getItemMeta();
|
playerSet.add(event.getPlayer());
|
||||||
for(String page : meta.getPages()){
|
|
||||||
for(String command : page.split("\n")){
|
|
||||||
if (command.startsWith("#")) continue;
|
|
||||||
|
|
||||||
PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command);
|
|
||||||
Bukkit.getServer().getPluginManager().callEvent(preprocessEvent);
|
|
||||||
if(preprocessEvent.isCancelled())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Bukkit.getLogger().log(Level.INFO, player.getName() + " dispatched command: " + command);
|
|
||||||
Bukkit.getServer().dispatchCommand(player, command);
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
if (playerSet.remove(event.getPlayer())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
event.setCancelled(true);
|
||||||
|
new ScriptExecutor((BookMeta) item.getItemMeta(), event.getPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNoBook(ItemStack item){
|
private boolean isNoBook(ItemStack item){
|
||||||
@ -71,4 +76,69 @@ public class ScriptListener implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class ScriptExecutor {
|
||||||
|
|
||||||
|
private final Player player;
|
||||||
|
private final List<String> commands = new ArrayList<>();
|
||||||
|
private int index = 0;
|
||||||
|
|
||||||
|
public ScriptExecutor(BookMeta bookMeta, Player player) {
|
||||||
|
this.player = player;
|
||||||
|
|
||||||
|
for(String page : bookMeta.getPages()) {
|
||||||
|
for (String command : page.split("\n")) {
|
||||||
|
if (command.startsWith("#") || command.trim().isEmpty()) continue;
|
||||||
|
commands.add(command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (commands.isEmpty()) return;
|
||||||
|
resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resume() {
|
||||||
|
if (!player.isOnline()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (index < commands.size()) {
|
||||||
|
String command = commands.get(index++);
|
||||||
|
|
||||||
|
if (command.toLowerCase().startsWith("sleep")) {
|
||||||
|
ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command);
|
||||||
|
Bukkit.getServer().getPluginManager().callEvent(preprocessEvent);
|
||||||
|
if (preprocessEvent.isCancelled()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Bukkit.getLogger().log(Level.INFO, player.getName() + " dispatched command: " + command);
|
||||||
|
Bukkit.getServer().dispatchCommand(player, command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String[] generateArgumentArray(String command, String fullCommand) {
|
||||||
|
return fullCommand.substring(command.length()).trim().split(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sleepCommand(ScriptExecutor scriptExecutor, String[] args) {
|
||||||
|
int sleepTime = 1;
|
||||||
|
if (args.length > 0) {
|
||||||
|
try {
|
||||||
|
sleepTime = Integer.parseInt(args[0]);
|
||||||
|
if (sleepTime <= 0) {
|
||||||
|
scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie Zeit muss eine Zahl großer 0 sein.");
|
||||||
|
sleepTime = 1;
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie Zeit darf nur aus Zahlen bestehen.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren