You are not logged in.

  • Login

[Bukkit-Plugin] Server-Signs

Bitte besucht unser neues Forum unter https://forum.nitrado.net | Please visit our new Forum at https://forum.nitrado.net

Dear visitor, welcome to Nitrado.net Prepaid Gameserver Community-Support - Archiv. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

lehu

User / Kunde

  • "lehu" started this thread

Posts: 110

  • Send private message

1

Wednesday, December 28th 2016, 12:25pm

Server-Signs

Hey :) ,

ich bin gerade an einem Plugin für Server-Schilder und nun habe ich das Problem, dass ich das Schild an einer Wand von einer Location bekommen will.
ich speichere im SignChangeEvent die Location des Schildes ab, doch später wenn ich aus einer File die Location mir neu instanziiere und dort den Block haben will,
tritt eine Exception auf.
Ich habe herausgefunden, dass der Typ des Blocks Air ist, also kann ich ihn schlecht zum Sign casten.
Kann einer mir helfen?

Listener:

Spoiler Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package de.leo.serversigns.listener;

import java.io.File;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

import de.leo.serversigns.ConfigFile;
import de.leo.serversigns.Main;
import de.leo.serversigns.server.Server;
import de.leo.serversigns.server.ServerUnit;

public class SignListener implements Listener {
	
	public static BukkitTask run;
	public static boolean isrunning = false;
	
	@EventHandler
	public void onSign(SignChangeEvent e) {
		Player p = e.getPlayer();
		Location loc = p.getLocation();
		
		if(e.getLine(1).contains("join")) {
			
			ConfigFile.addSign(loc, e.getLine(2), e.getLine(3));
			
			ServerUnit sunit = null;
			try{
				sunit = ServerUnit.valueOf(e.getLine(0).toUpperCase());
			}catch(NumberFormatException ex) {
				ex.printStackTrace();
				p.sendMessage("Error");
				return;
			}
			
			e.setLine(0, sunit.getOutput());
			e.setLine(1, "§cPinging");
			e.setLine(2, "§c...");
			e.setLine(3, "§c...");
			
			Sign s = (Sign) e.getBlock().getState();
			s.update();
			
			p.sendMessage("§aSchild erschaffen");
			
			try {
				if(isrunning) {
					run.wait(1000);
				}
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			
		}
		
	}
	
	public static void update() {
		
		isrunning = true;
		
		run = new BukkitRunnable() {
						
			@Override
			public void run() {
				
				File file = new File("plugins/ServerSigns", "config.yml");
				FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
				
				List<String> servers = cfg.getStringList("signs");
				
				if(servers.size() == 0) {
					SignListener.isrunning = false;
					cancel();
					return;
				}
				
				for(String s : servers) {
					
					System.out.println("test");
					
					String[] a = s.split(", ");
					
					World world = Bukkit.getWorld(a[0]);
					double x = Double.valueOf(a[1]);
					double y = Double.valueOf(a[2]);
					double z = Double.valueOf(a[3]);
					
					String host = a[4];
					String port = a[5];
					
					Server server = new Server(host, Integer.valueOf(port));
					
					Location loc = new Location(world, x, y, z);
					Block b = loc.getBlock();
					Sign sign = (Sign) b.getState();
					
					if(server.isOnline()) {
						
						if(server.getMaxPlayers() == server.getPlayers()) {
							
							sign.setLine(1, "§7[§6Premium§7]");
							sign.setLine(2, server.getMOTD());
							sign.setLine(3, "§7" + server.getPlayers() + "/" + server.getMaxPlayers());
							sign.update();
							
						} else {
							
							sign.setLine(1, "§7[§aJoin§7]");
							sign.setLine(2, server.getMOTD());
							sign.setLine(3, "§7" + server.getPlayers() + "/" + server.getMaxPlayers());
							sign.update();
							
						}
						
					} else {
						
						sign.setLine(1, "§7[§cOffline§7]");
						sign.setLine(2, "§crestarting");
						sign.setLine(3, "§c....");
						sign.update();
						
					}
					
				}
			}
			
		}.runTaskTimerAsynchronously(Main.main, 200L, 20L);
		
	}
	
}



Hier die File-Klasse:

Spoiler Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package de.leo.serversigns;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

public class ConfigFile {
	
	public static void createConfig() {
		
		File ordner = new File("plugins", "ServerSigns");
		File file = new File("plugins/ServerSigns", "config.yml");
		FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
		
		if(!ordner.exists()) {
			ordner.mkdirs();
		}
		
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		try {
			cfg.save(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void addSign(Location loc, String host, String port) {
		
		File file = new File("plugins/ServerSigns", "config.yml");
		FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
		
		String string = loc.getWorld().getName() + ", " + loc.getX() + ", " + loc.getY() + ", " + loc.getZ() + ", " + host + ", " + port;
		
		if(cfg.isSet("signs")) {
			
			System.out.println("test2");
			cfg.set("signs", cfg.getStringList("signs").add(string));
			
		} else {
			
			System.out.println("test1");
			List<String> list = new ArrayList<>();
			list.add(string);
			cfg.set("signs", list);
			
		}
		
		try {
			cfg.save(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}



Danke schonmal im vorraus :thumbsup:

//EDIT
gelößt habe die Location vom Spieler genommen :D nicht die vom schild

This post has been edited 1 times, last edit by "lehu" (Dec 28th 2016, 12:59pm)


Bitte besucht unser neues Forum unter https://forum.nitrado.net| Please visit our new Forum at https://forum.nitrado.net

Similar threads