Added portal and sign functionality

This commit is contained in:
Simon Giesel 2019-09-20 06:28:19 +02:00 committed by Simon
parent 1de9dc0600
commit a35d685f33
8 changed files with 286 additions and 2 deletions

6
.idea/encodings.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
</component>
</project>

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SaveActionSettings">
<option name="actions">
<set>
<option value="activate" />
<option value="organizeImports" />
<option value="reformat" />
</set>
</option>
<option name="configurationPath" value="" />
</component>
</project>

View file

@ -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<StatusSign> statusSigns = new ArrayList<StatusSign>();
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<String, Object> sign = (Map<String, Object>) i.next();
getLogger().info("" + sign);
Map<String, Object> locationMap = ((Map<String, Object>) 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);
}
}
}

View file

@ -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;
}
}

View file

@ -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 <host> <port> <name>");
return true;
}
String host = args[1];
int port = Integer.parseInt(args[2]);
String name = args[3];
Set<Material> set = new HashSet<Material>();
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<Object> signs;
if (plugin.cfg.get("Signs") == null)
signs = new ArrayList<Object>();
else
signs = (List<Object>) plugin.cfg.get("Signs");
Map<String, Object> sign = new HashMap<String, Object>();
sign.put("host", statusSign.getHost());
sign.put("port", statusSign.getPort());
sign.put("name", statusSign.getName());
Map<String, Object> location = new HashMap<String, Object>();
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();
}
}
}

View file

@ -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());
}
}

View file

@ -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);
}
}

View file

@ -12,3 +12,6 @@ commands:
hub:
description: Default Hub Command
usage: /hub
sign:
description: Default Sign Command
usage: /sign