From a35d685f33d1fa3412e622ebc4d3a63e47cd9b49 Mon Sep 17 00:00:00 2001 From: Simon Giesel Date: Fri, 20 Sep 2019 06:28:19 +0200 Subject: [PATCH] Added portal and sign functionality --- .idea/encodings.xml | 6 ++ .idea/saveactions_settings.xml | 13 +++ .../de/cliffbreak/core/CliffbreakCore.java | 52 ++++++++++- .../cliffbreak/core/classes/StatusSign.java | 83 +++++++++++++++++ .../cliffbreak/core/commands/CommandSign.java | 88 +++++++++++++++++++ .../core/listeners/PlayerPortalListener.java | 29 ++++++ .../core/listeners/PlayerQuitListener.java | 12 +++ src/main/resources/plugin.yml | 5 +- 8 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 .idea/encodings.xml create mode 100644 .idea/saveactions_settings.xml create mode 100644 src/main/java/de/cliffbreak/core/classes/StatusSign.java create mode 100644 src/main/java/de/cliffbreak/core/commands/CommandSign.java create mode 100644 src/main/java/de/cliffbreak/core/listeners/PlayerPortalListener.java create mode 100644 src/main/java/de/cliffbreak/core/listeners/PlayerQuitListener.java diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..b26911b --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/saveactions_settings.xml b/.idea/saveactions_settings.xml new file mode 100644 index 0000000..6025467 --- /dev/null +++ b/.idea/saveactions_settings.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/de/cliffbreak/core/CliffbreakCore.java b/src/main/java/de/cliffbreak/core/CliffbreakCore.java index 4b0f0af..b60aecc 100644 --- a/src/main/java/de/cliffbreak/core/CliffbreakCore.java +++ b/src/main/java/de/cliffbreak/core/CliffbreakCore.java @@ -1,27 +1,77 @@ package de.cliffbreak.core; +import de.cliffbreak.core.classes.StatusSign; import de.cliffbreak.core.commands.CommandGM; import de.cliffbreak.core.commands.CommandHub; +import de.cliffbreak.core.commands.CommandSign; import de.cliffbreak.core.commands.CommandSpeed; import de.cliffbreak.core.listeners.ChatListener; import de.cliffbreak.core.listeners.PlayerJoinListener; +import de.cliffbreak.core.listeners.PlayerPortalListener; +import de.cliffbreak.core.listeners.PlayerQuitListener; +import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Map; public class CliffbreakCore extends JavaPlugin { + public ArrayList statusSigns = new ArrayList(); + public File cfgFile = new File("plugins/Core", "config.yml"); public FileConfiguration cfg = YamlConfiguration.loadConfiguration(cfgFile); + @SuppressWarnings("unchecked") @Override public void onEnable() { + + // Enable BungeeCord Messaging System + this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); + getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this); + getServer().getPluginManager().registerEvents(new PlayerQuitListener(), this); getServer().getPluginManager().registerEvents(new ChatListener(), this); + getServer().getPluginManager().registerEvents(new PlayerPortalListener(this), this); this.getCommand("gm").setExecutor(new CommandGM()); this.getCommand("speed").setExecutor(new CommandSpeed()); this.getCommand("hub").setExecutor(new CommandHub(this)); + this.getCommand("sign").setExecutor(new CommandSign(this)); + + if (cfg.getList("Signs") == null || cfg.getList("Signs").isEmpty()) + getLogger().info("No Signs registered. Skipping..."); + else + for (Iterator i = cfg.getList("Signs").iterator(); i.hasNext(); ) { + Map sign = (Map) i.next(); + getLogger().info("" + sign); + + Map locationMap = ((Map) sign.get("location")); + Location location = new Location( + Bukkit.getServer().getWorld((String) locationMap.get("world")), + (double) (Integer) locationMap.get("x"), + (double) (Integer) locationMap.get("y"), + (double) (Integer) locationMap.get("z") + ); + /* TODO: Add autoremoval + if (location.getBlock() == null || (location.getBlock().getState() instanceof Sign)) { + getLogger().info("Nicht vorhandenes Schild wurde aus der Konfiguration entfernt. Position: " + location); + }*/ + + statusSigns.add(new StatusSign(location, (String) sign.get("host"), (Integer) sign.get("port"), (String) sign.get("name"))); + + Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { + @Override + public void run() { + for (StatusSign s : statusSigns) { + s.update(); + } + } + }, 0L, 5 * 20L); + } } -} +} \ No newline at end of file diff --git a/src/main/java/de/cliffbreak/core/classes/StatusSign.java b/src/main/java/de/cliffbreak/core/classes/StatusSign.java new file mode 100644 index 0000000..5130522 --- /dev/null +++ b/src/main/java/de/cliffbreak/core/classes/StatusSign.java @@ -0,0 +1,83 @@ +package de.cliffbreak.core.classes; + +import org.bukkit.Location; +import org.bukkit.block.Sign; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; + +public class StatusSign { + + private Location location; + private Sign sign; + private String name, host; + private int port; + + public StatusSign(Location location, String host, int port, String name) { + this.location = location; + this.sign = (Sign) location.getBlock().getState(); + this.host = host; + this.port = port; + this.name = name; + } + + public void update() { + try { + Socket socket = new Socket(); + socket.connect(new InetSocketAddress(this.host, this.port), 500); + socket.setSoTimeout(500); + + DataOutputStream out = new DataOutputStream(socket.getOutputStream()); + DataInputStream in = new DataInputStream(socket.getInputStream()); + + out.write(0xFE); + + StringBuilder str = new StringBuilder(); + + int b; + while ((b = in.read()) != -1) { + if (b != 0 && b > 16 && b != 255 && b != 23 && b != 24) { + str.append((char) b); + } + } + + String[] data = str.toString().split("§"); + //String motd = data[0]; + int onlinePlayers = Integer.parseInt(data[1]); + int maxPlayers = Integer.parseInt(data[2]); + + sign.setLine(0, "§2 ▒▒▒▒▒▒▒▒"); + sign.setLine(1, "§b§l§o" + this.name.substring(0, 1).toUpperCase() + this.name.substring(1)); + sign.setLine(2, "§f" + onlinePlayers + "§a/§f" + maxPlayers); + sign.setLine(3, "§2 ▒▒▒▒▒▒▒▒"); + + sign.update(); + socket.close(); + } catch (Exception e) { + sign.setLine(0, "§4 ▒▒▒▒▒▒▒▒"); + sign.setLine(1, "§b§l§o" + this.name.substring(0, 1).toUpperCase() + this.name.substring(1)); + sign.setLine(2, "§c§lOFFLINE"); + sign.setLine(3, "§4 ▒▒▒▒▒▒▒▒"); + sign.update(); + } + + } + + public Location getLocation() { + return location; + } + + public String getName() { + return name; + } + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } +} diff --git a/src/main/java/de/cliffbreak/core/commands/CommandSign.java b/src/main/java/de/cliffbreak/core/commands/CommandSign.java new file mode 100644 index 0000000..5d55f60 --- /dev/null +++ b/src/main/java/de/cliffbreak/core/commands/CommandSign.java @@ -0,0 +1,88 @@ +package de.cliffbreak.core.commands; + +import de.cliffbreak.core.CliffbreakCore; +import de.cliffbreak.core.classes.StatusSign; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.data.type.Sign; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.io.IOException; +import java.util.*; + +public class CommandSign implements CommandExecutor { + private CliffbreakCore plugin; + + public CommandSign(CliffbreakCore plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { + if (commandSender instanceof Player) { + Player p = (Player) commandSender; + if (!p.hasPermission("cliffbreak.command.sign")) { + p.sendMessage("§cNope"); + return true; + } + if (args.length > 0 && args[0].equalsIgnoreCase("add")) { + if (args.length < 4) { + p.sendMessage("§c/sign add "); + return true; + } + + String host = args[1]; + int port = Integer.parseInt(args[2]); + String name = args[3]; + + Set set = new HashSet(); + set.add(Material.AIR); + Block block = p.getTargetBlock(set, 10); + + if (block == null || (block.getState() instanceof Sign)) { + p.sendMessage("§cBitte schaue auf ein Schild!"); + return true; + } + + StatusSign statusSign = new StatusSign(block.getLocation(), host, port, name); + saveSign(statusSign, p); + plugin.statusSigns.add(statusSign); + } + } + return true; + } + + @SuppressWarnings("unchecked") + private void saveSign(StatusSign statusSign, Player p) { + List signs; + if (plugin.cfg.get("Signs") == null) + signs = new ArrayList(); + else + signs = (List) plugin.cfg.get("Signs"); + + Map sign = new HashMap(); + sign.put("host", statusSign.getHost()); + sign.put("port", statusSign.getPort()); + sign.put("name", statusSign.getName()); + Map location = new HashMap(); + location.put("world", statusSign.getLocation().getWorld().getName()); + location.put("x", statusSign.getLocation().getBlockX()); + location.put("y", statusSign.getLocation().getBlockY()); + location.put("z", statusSign.getLocation().getBlockZ()); + + sign.put("location", location); + signs.add(sign); + plugin.cfg.set("Signs", signs); + + try { + plugin.cfg.save(plugin.cfgFile); + p.sendMessage("§aDas §lSchild §r§awurde erfolgreich erstellt."); + } catch (IOException e) { + p.sendMessage("§cDas §lSchild §r§cwurde NICHT erstellt."); + e.printStackTrace(); + } + } +} diff --git a/src/main/java/de/cliffbreak/core/listeners/PlayerPortalListener.java b/src/main/java/de/cliffbreak/core/listeners/PlayerPortalListener.java new file mode 100644 index 0000000..aeb60dc --- /dev/null +++ b/src/main/java/de/cliffbreak/core/listeners/PlayerPortalListener.java @@ -0,0 +1,29 @@ +package de.cliffbreak.core.listeners; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import de.cliffbreak.core.CliffbreakCore; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerPortalEvent; + +public class PlayerPortalListener implements Listener { + + private CliffbreakCore plugin; + + public PlayerPortalListener(CliffbreakCore plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onPlayerPortal(PlayerPortalEvent e) { + e.setCancelled(true); + Player p = e.getPlayer(); + + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF("Connect"); + out.writeUTF("vanilla"); + p.sendPluginMessage(this.plugin, "BungeeCord", out.toByteArray()); + } +} diff --git a/src/main/java/de/cliffbreak/core/listeners/PlayerQuitListener.java b/src/main/java/de/cliffbreak/core/listeners/PlayerQuitListener.java new file mode 100644 index 0000000..76e274c --- /dev/null +++ b/src/main/java/de/cliffbreak/core/listeners/PlayerQuitListener.java @@ -0,0 +1,12 @@ +package de.cliffbreak.core.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; + +public class PlayerQuitListener implements Listener { + @EventHandler + public void onPlayerQuit(PlayerQuitEvent e) { + e.setQuitMessage(null); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index b161c6b..73fa988 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -11,4 +11,7 @@ commands: usage: /speed hub: description: Default Hub Command - usage: /hub \ No newline at end of file + usage: /hub + sign: + description: Default Sign Command + usage: /sign \ No newline at end of file