Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.beshographix.tsigns;
- import java.io.ByteArrayOutputStream;
- import java.io.DataOutputStream;
- import java.util.ArrayList;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import org.bukkit.block.Sign;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.ConfigurationSection;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.Action;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Tsigns extends JavaPlugin implements Listener{
- private ArrayList<Signs> signs;
- public void onEnable(){
- this.signs = new ArrayList<Signs>();
- saveDefaultConfig();
- for ( String str : getConfig().getKeys(false)){
- ConfigurationSection s = getConfig().getConfigurationSection(str);
- ConfigurationSection l = s.getConfigurationSection("loc");
- World w = Bukkit.getServer().getWorld(l.getString("world"));
- double x = l.getDouble("x"), y = l.getDouble("y"), z = l.getDouble("z");
- Location loc = new Location(w, x, y, z);
- if (loc.getBlock() == null){
- getConfig().set(str, null);
- }
- else{
- signs.add(new Signs(loc, s.getString("name"), s.getString("ip"), s.getInt("port")));
- }
- }
- Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
- public void run() {
- for (Signs s : signs){
- s.update();
- }
- }
- }, 0, 20);
- Bukkit.getServer().getPluginManager().registerEvents(this, this);
- Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
- }
- @EventHandler
- public void onPlayerInteract(PlayerInteractEvent e){
- if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
- Block block = e.getClickedBlock();
- if (block.getType() != Material.SIGN && block.getType() != Material.SIGN_POST && block.getType() != Material.WALL_SIGN) return;
- for (Signs s : signs) {
- if (s.getLocation().equals(block.getLocation())){
- try {
- ByteArrayOutputStream b = new ByteArrayOutputStream();
- DataOutputStream out = new DataOutputStream(b);
- out.writeUTF("Connect");
- out.writeUTF(s.getName());
- e.getPlayer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- }
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if (!(sender instanceof Player)){
- sender.sendMessage("§3Only Players can do that !");
- return true;
- }
- Player p = (Player) sender;
- if (cmd.getName().equalsIgnoreCase("titanssigns")){
- if (args.length < 3){
- p.sendMessage("§3/titanssigns §a<ip> <port> <name>");
- return true;
- }
- String ip = args[0];
- int port;
- String name = args[2];
- try {
- port = Integer.valueOf(args[1]);
- } catch (Exception e) {
- p.sendMessage(ChatColor.RED + "Port is not a number.");
- return true;
- }
- Block b = p.getTargetBlock(null, 10);
- if (b == null){
- p.sendMessage("§3Please look at a Sign !");
- return true;
- }
- if (b.getType() != Material.SIGN && b.getType() != Material.SIGN_POST && b.getType() != Material.WALL_SIGN){
- p.sendMessage("§3Please look at a Sign !");
- return true;
- }
- Signs tsigns = new Signs(b.getLocation(), name, ip, port);
- signs.add(tsigns);
- save(tsigns);
- }
- return true;
- }
- private void save(Signs tsigns) {
- int size = getConfig().getKeys(false).size() + 1;
- getConfig().set(size + ".loc.world", tsigns.getLocation().getWorld().getName());
- getConfig().set(size + ".loc.x", tsigns.getLocation().getX());
- getConfig().set(size + ".loc.y", tsigns.getLocation().getY());
- getConfig().set(size + ".loc.z", tsigns.getLocation().getZ());
- getConfig().set(size + ".name", tsigns.getName());
- getConfig().set(size + ".ip", tsigns.getIP());
- getConfig().set(size + ".port", tsigns.getPort());
- saveConfig();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment