2016-01-06 14:40:27 +01:00
|
|
|
package com.gamingmesh.jobs.Signs;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.block.Block;
|
2016-06-09 17:35:24 +02:00
|
|
|
import org.bukkit.block.BlockFace;
|
2016-01-06 14:40:27 +01:00
|
|
|
import org.bukkit.block.Skull;
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
|
|
|
import com.gamingmesh.jobs.Jobs;
|
|
|
|
import com.gamingmesh.jobs.JobsPlugin;
|
|
|
|
import com.gamingmesh.jobs.config.CommentedYamlConfiguration;
|
|
|
|
import com.gamingmesh.jobs.container.TopList;
|
|
|
|
|
|
|
|
public class SignUtil {
|
2016-03-30 15:42:36 +02:00
|
|
|
|
2016-01-06 14:40:27 +01:00
|
|
|
public SignInfo Signs = new SignInfo();
|
|
|
|
private JobsPlugin plugin;
|
|
|
|
|
|
|
|
public SignUtil(JobsPlugin plugin) {
|
|
|
|
this.plugin = plugin;
|
|
|
|
}
|
2016-03-30 15:42:36 +02:00
|
|
|
|
|
|
|
public SignInfo getSigns() {
|
2016-01-06 14:40:27 +01:00
|
|
|
return Signs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign file
|
|
|
|
public void LoadSigns() {
|
2016-03-30 15:42:36 +02:00
|
|
|
Signs.GetAllSigns().clear();
|
|
|
|
File file = new File(plugin.getDataFolder(), "Signs.yml");
|
|
|
|
YamlConfiguration f = YamlConfiguration.loadConfiguration(file);
|
|
|
|
|
|
|
|
if (!f.isConfigurationSection("Signs"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ConfigurationSection ConfCategory = f.getConfigurationSection("Signs");
|
|
|
|
ArrayList<String> categoriesList = new ArrayList<String>(ConfCategory.getKeys(false));
|
|
|
|
if (categoriesList.size() == 0)
|
|
|
|
return;
|
|
|
|
for (String category : categoriesList) {
|
|
|
|
ConfigurationSection NameSection = ConfCategory.getConfigurationSection(category);
|
|
|
|
com.gamingmesh.jobs.Signs.Sign newTemp = new com.gamingmesh.jobs.Signs.Sign();
|
|
|
|
newTemp.setCategory(Integer.valueOf(category));
|
|
|
|
newTemp.setWorld(NameSection.getString("World"));
|
|
|
|
newTemp.setX(NameSection.getDouble("X"));
|
|
|
|
newTemp.setY(NameSection.getDouble("Y"));
|
|
|
|
newTemp.setZ(NameSection.getDouble("Z"));
|
|
|
|
newTemp.setNumber(NameSection.getInt("Number"));
|
|
|
|
newTemp.setJobName(NameSection.getString("JobName"));
|
|
|
|
newTemp.setSpecial(NameSection.getBoolean("Special"));
|
|
|
|
Signs.addSign(newTemp);
|
|
|
|
}
|
|
|
|
return;
|
2016-01-06 14:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Signs save file
|
|
|
|
public void saveSigns() {
|
2016-03-30 15:42:36 +02:00
|
|
|
File f = new File(plugin.getDataFolder(), "Signs.yml");
|
|
|
|
YamlConfiguration conf = YamlConfiguration.loadConfiguration(f);
|
|
|
|
|
|
|
|
CommentedYamlConfiguration writer = new CommentedYamlConfiguration();
|
|
|
|
conf.options().copyDefaults(true);
|
|
|
|
|
|
|
|
writer.addComment("Signs", "DO NOT EDIT THIS FILE BY HAND!");
|
|
|
|
|
|
|
|
if (!conf.isConfigurationSection("Signs"))
|
|
|
|
conf.createSection("Signs");
|
|
|
|
|
|
|
|
for (com.gamingmesh.jobs.Signs.Sign one : Signs.GetAllSigns()) {
|
|
|
|
String path = "Signs." + String.valueOf(one.GetCategory());
|
|
|
|
writer.set(path + ".World", one.GetWorld());
|
|
|
|
writer.set(path + ".X", one.GetX());
|
|
|
|
writer.set(path + ".Y", one.GetY());
|
|
|
|
writer.set(path + ".Z", one.GetZ());
|
|
|
|
writer.set(path + ".Number", one.GetNumber());
|
|
|
|
writer.set(path + ".JobName", one.GetJobName());
|
|
|
|
writer.set(path + ".Special", one.isSpecial());
|
|
|
|
}
|
2016-01-06 14:40:27 +01:00
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
try {
|
|
|
|
writer.save(f);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return;
|
2016-01-06 14:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean SignUpdate(String JobName) {
|
|
|
|
List<com.gamingmesh.jobs.Signs.Sign> Copy = new ArrayList<com.gamingmesh.jobs.Signs.Sign>(Signs.GetAllSigns().size());
|
|
|
|
for (com.gamingmesh.jobs.Signs.Sign foo : Signs.GetAllSigns()) {
|
|
|
|
Copy.add(foo);
|
|
|
|
}
|
|
|
|
int timelapse = 1;
|
|
|
|
for (com.gamingmesh.jobs.Signs.Sign one : Copy) {
|
|
|
|
String SignJobName = one.GetJobName();
|
|
|
|
if (JobName.equalsIgnoreCase(SignJobName)) {
|
|
|
|
String SignsWorld = one.GetWorld();
|
|
|
|
double SignsX = one.GetX();
|
|
|
|
double SignsY = one.GetY();
|
|
|
|
double SignsZ = one.GetZ();
|
|
|
|
int number = one.GetNumber() - 1;
|
|
|
|
|
|
|
|
List<TopList> PlayerList = new ArrayList<TopList>();
|
|
|
|
if (!JobName.equalsIgnoreCase("gtoplist")) {
|
|
|
|
PlayerList = Jobs.getJobsDAO().toplist(SignJobName, number);
|
|
|
|
} else {
|
|
|
|
PlayerList = Jobs.getJobsDAO().getGlobalTopList(number);
|
|
|
|
}
|
|
|
|
if (PlayerList.size() != 0) {
|
|
|
|
World world = Bukkit.getWorld(SignsWorld);
|
|
|
|
if (world == null)
|
|
|
|
continue;
|
|
|
|
Location nloc = new Location(world, SignsX, SignsY, SignsZ);
|
|
|
|
Block block = nloc.getBlock();
|
|
|
|
if (!(block.getState() instanceof org.bukkit.block.Sign)) {
|
|
|
|
Signs.GetAllSigns().remove(one);
|
|
|
|
saveSigns();
|
|
|
|
} else {
|
|
|
|
org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState();
|
|
|
|
if (!one.isSpecial()) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (i >= PlayerList.size()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
String PlayerName = ((TopList) PlayerList.get(i)).getPlayerName();
|
|
|
|
|
|
|
|
if (PlayerName != null && PlayerName.length() > 8) {
|
|
|
|
String PlayerNameStrip = PlayerName.split("(?<=\\G.{7})")[0];
|
|
|
|
PlayerName = PlayerNameStrip + "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PlayerName == null)
|
|
|
|
PlayerName = "Unknown";
|
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
String line = Jobs.getLanguage().getMessage("signs.List");
|
2016-01-06 14:40:27 +01:00
|
|
|
line = line.replace("[number]", String.valueOf(i + number + 1));
|
|
|
|
line = line.replace("[player]", PlayerName);
|
|
|
|
line = line.replace("[level]", String.valueOf(((TopList) PlayerList.get(i)).getLevel()));
|
|
|
|
|
|
|
|
sign.setLine(i, line);
|
|
|
|
}
|
|
|
|
sign.update();
|
2016-06-09 17:35:24 +02:00
|
|
|
UpdateHead(sign, ((TopList) PlayerList.get(0)).getPlayerName(), timelapse);
|
2016-01-06 14:40:27 +01:00
|
|
|
} else {
|
|
|
|
String PlayerName = ((TopList) PlayerList.get(0)).getPlayerName();
|
2016-01-10 14:25:14 +01:00
|
|
|
if (PlayerName.length() > 8) {
|
|
|
|
String PlayerNameStrip = PlayerName.split("(?<=\\G.{7})")[0];
|
2016-01-06 14:40:27 +01:00
|
|
|
PlayerName = PlayerNameStrip + "~";
|
|
|
|
}
|
2016-03-30 15:42:36 +02:00
|
|
|
String line1 = Jobs.getLanguage().getMessage("signs.SpecialList." + one.GetNumber() + ".1");
|
2016-01-06 14:40:27 +01:00
|
|
|
line1 = line1.replace("[number]", String.valueOf(one.GetNumber() + number + 1));
|
|
|
|
line1 = line1.replace("[player]", PlayerName);
|
|
|
|
line1 = line1.replace("[level]", String.valueOf(((TopList) PlayerList.get(0)).getLevel()));
|
|
|
|
|
|
|
|
sign.setLine(0, line1);
|
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
line1 = Jobs.getLanguage().getMessage("signs.SpecialList." + one.GetNumber() + ".2");
|
2016-01-06 14:40:27 +01:00
|
|
|
line1 = line1.replace("[number]", String.valueOf(one.GetNumber() + number + 1));
|
|
|
|
line1 = line1.replace("[player]", PlayerName);
|
|
|
|
line1 = line1.replace("[level]", String.valueOf(((TopList) PlayerList.get(0)).getLevel()));
|
|
|
|
|
|
|
|
sign.setLine(1, line1);
|
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
line1 = Jobs.getLanguage().getMessage("signs.SpecialList." + one.GetNumber() + ".3");
|
2016-01-06 14:40:27 +01:00
|
|
|
line1 = line1.replace("[number]", String.valueOf(one.GetNumber() + number + 1));
|
|
|
|
line1 = line1.replace("[player]", PlayerName);
|
|
|
|
line1 = line1.replace("[level]", String.valueOf(((TopList) PlayerList.get(0)).getLevel()));
|
|
|
|
|
|
|
|
sign.setLine(2, line1);
|
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
line1 = Jobs.getLanguage().getMessage("signs.SpecialList." + one.GetNumber() + ".4");
|
2016-01-06 14:40:27 +01:00
|
|
|
line1 = line1.replace("[number]", String.valueOf(one.GetNumber() + number + 1));
|
|
|
|
line1 = line1.replace("[player]", PlayerName);
|
|
|
|
line1 = line1.replace("[level]", String.valueOf(((TopList) PlayerList.get(0)).getLevel()));
|
|
|
|
|
|
|
|
sign.setLine(3, line1);
|
|
|
|
sign.update();
|
2016-06-09 17:35:24 +02:00
|
|
|
UpdateHead(sign, ((TopList) PlayerList.get(0)).getPlayerName(), timelapse);
|
2016-01-06 14:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
timelapse++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:35:24 +02:00
|
|
|
public void UpdateHead(final org.bukkit.block.Sign sign, final String Playername, final int timelapse) {
|
2016-01-06 14:40:27 +01:00
|
|
|
|
|
|
|
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
|
2016-06-09 17:35:24 +02:00
|
|
|
org.bukkit.material.Sign signMat = (org.bukkit.material.Sign) sign.getData();
|
|
|
|
BlockFace directionFacing = signMat.getFacing();
|
|
|
|
|
|
|
|
Location loc = sign.getLocation().clone();
|
|
|
|
loc.add(0, 1, 0);
|
2016-01-06 14:40:27 +01:00
|
|
|
|
|
|
|
if (Playername == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Block block = loc.getBlock();
|
|
|
|
|
2016-06-09 17:35:24 +02:00
|
|
|
if (block == null || !(block.getState() instanceof Skull))
|
|
|
|
loc.add(directionFacing.getOppositeFace().getModX(), 0, directionFacing.getOppositeFace().getModZ());
|
|
|
|
|
|
|
|
block = loc.getBlock();
|
2016-01-06 14:40:27 +01:00
|
|
|
|
2016-06-09 17:35:24 +02:00
|
|
|
if (block == null || !(block.getState() instanceof Skull))
|
2016-01-06 14:40:27 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
Skull skull = (Skull) block.getState();
|
|
|
|
|
|
|
|
if (skull == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
skull.setOwner(Playername);
|
|
|
|
skull.update();
|
|
|
|
return;
|
|
|
|
}
|
2016-03-30 15:42:36 +02:00
|
|
|
}, timelapse * Jobs.getGCManager().InfoUpdateInterval * 20L);
|
2016-01-06 14:40:27 +01:00
|
|
|
}
|
|
|
|
}
|