Add basic sleep command to ScriptSystem
Dieser Commit ist enthalten in:
Ursprung
879585e542
Commit
ee94b51612
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
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;
|
||||||
@ -30,10 +31,94 @@ 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.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class ScriptListener implements Listener {
|
public class ScriptListener implements Listener {
|
||||||
|
|
||||||
|
private Map<BookMeta, Script> scriptMap = new HashMap<>();
|
||||||
|
|
||||||
|
private class Script {
|
||||||
|
|
||||||
|
private BookMeta bookMeta;
|
||||||
|
private List<String> commands = new ArrayList<>();
|
||||||
|
private int uses = 0;
|
||||||
|
|
||||||
|
public Script(BookMeta bookMeta) {
|
||||||
|
this.bookMeta = bookMeta;
|
||||||
|
|
||||||
|
for(String page : bookMeta.getPages()) {
|
||||||
|
for (String command : page.split("\n")) {
|
||||||
|
if (command.startsWith("#")) continue;
|
||||||
|
commands.add(command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void keep() {
|
||||||
|
uses++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void free() {
|
||||||
|
uses--;
|
||||||
|
if (uses < 0) {
|
||||||
|
scriptMap.remove(bookMeta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ScriptExecutor {
|
||||||
|
|
||||||
|
private Player player;
|
||||||
|
private Script script;
|
||||||
|
private int index = 0;
|
||||||
|
|
||||||
|
public ScriptExecutor(BookMeta bookMeta, Player player) {
|
||||||
|
this.player = player;
|
||||||
|
|
||||||
|
if (!scriptMap.containsKey(bookMeta)) {
|
||||||
|
scriptMap.put(bookMeta, new Script(bookMeta));
|
||||||
|
}
|
||||||
|
script = scriptMap.get(bookMeta);
|
||||||
|
script.keep();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resume() {
|
||||||
|
while (index < script.commands.size()) {
|
||||||
|
String command = script.commands.get(index++);
|
||||||
|
|
||||||
|
if (command.startsWith("sleep")) {
|
||||||
|
String sleepTimeString = command.substring(5).trim();
|
||||||
|
int sleepTime = 1;
|
||||||
|
try {
|
||||||
|
Integer.parseInt(sleepTimeString);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// Ignored
|
||||||
|
}
|
||||||
|
Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onLeftClick(PlayerInteractEvent event) {
|
public void onLeftClick(PlayerInteractEvent event) {
|
||||||
if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK)
|
if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK)
|
||||||
@ -46,19 +131,7 @@ public class ScriptListener implements Listener {
|
|||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
BookMeta meta = (BookMeta) item.getItemMeta();
|
BookMeta meta = (BookMeta) item.getItemMeta();
|
||||||
for(String page : meta.getPages()){
|
new ScriptExecutor(meta, player).resume();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNoBook(ItemStack item){
|
private boolean isNoBook(ItemStack item){
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren