1
0
mirror of https://github.com/Zrips/Jobs.git synced 2025-02-27 17:52:17 +01:00

Sign check fix

This commit is contained in:
Zrips 2023-04-24 12:57:57 +03:00
parent 6294aece85
commit 9b291b299b

View File

@ -21,7 +21,6 @@ import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.TopList; import com.gamingmesh.jobs.container.TopList;
import net.Zrips.CMILib.FileHandler.ConfigReader; import net.Zrips.CMILib.FileHandler.ConfigReader;
import net.Zrips.CMILib.Items.CMIMaterial;
import net.Zrips.CMILib.Messages.CMIMessages; import net.Zrips.CMILib.Messages.CMIMessages;
import net.Zrips.CMILib.Version.Version; import net.Zrips.CMILib.Version.Version;
@ -33,345 +32,342 @@ public class SignUtil {
private Jobs plugin; private Jobs plugin;
public SignUtil(Jobs plugin) { public SignUtil(Jobs plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
public Map<String, Map<String, jobsSign>> getSigns() { public Map<String, Map<String, jobsSign>> getSigns() {
return signsByType; return signsByType;
} }
public boolean removeSign(Location loc) { public boolean removeSign(Location loc) {
jobsSign jSign = signsByLocation.remove(jobsSign.locToBlockString(loc)); jobsSign jSign = signsByLocation.remove(jobsSign.locToBlockString(loc));
if (jSign == null) if (jSign == null)
return false; return false;
Map<String, jobsSign> sub = signsByType.get(jSign.getIdentifier().toLowerCase()); Map<String, jobsSign> sub = signsByType.get(jSign.getIdentifier().toLowerCase());
if (sub != null) { if (sub != null) {
sub.remove(jSign.locToBlockString()); sub.remove(jSign.locToBlockString());
} }
return true; return true;
} }
public jobsSign getSign(Location loc) { public jobsSign getSign(Location loc) {
return loc == null ? null : signsByLocation.get(jobsSign.locToBlockString(loc)); return loc == null ? null : signsByLocation.get(jobsSign.locToBlockString(loc));
} }
public void addSign(jobsSign jSign) { public void addSign(jobsSign jSign) {
if (jSign == null) if (jSign == null)
return; return;
String locToBlockString = jSign.locToBlockString(); String locToBlockString = jSign.locToBlockString();
signsByLocation.put(locToBlockString, jSign); signsByLocation.put(locToBlockString, jSign);
String identifier = jSign.getIdentifier().toLowerCase(); String identifier = jSign.getIdentifier().toLowerCase();
Map<String, jobsSign> old = signsByType.get(identifier); Map<String, jobsSign> old = signsByType.get(identifier);
if (old == null) { if (old == null) {
old = new HashMap<>(); old = new HashMap<>();
signsByType.put(identifier, old); signsByType.put(identifier, old);
} }
old.put(locToBlockString, jSign); old.put(locToBlockString, jSign);
signsByType.put(identifier, old); signsByType.put(identifier, old);
} }
public void loadSigns() { public void loadSigns() {
if (!Jobs.getGCManager().SignsEnabled) if (!Jobs.getGCManager().SignsEnabled)
return; return;
signsByType.clear(); signsByType.clear();
signsByLocation.clear(); signsByLocation.clear();
File file = new File(Jobs.getFolder(), "Signs.yml"); File file = new File(Jobs.getFolder(), "Signs.yml");
ConfigurationSection confCategory = YamlConfiguration.loadConfiguration(file).getConfigurationSection("Signs"); ConfigurationSection confCategory = YamlConfiguration.loadConfiguration(file).getConfigurationSection("Signs");
if (confCategory == null) if (confCategory == null)
return; return;
List<String> categoriesList = new ArrayList<>(confCategory.getKeys(false)); List<String> categoriesList = new ArrayList<>(confCategory.getKeys(false));
if (categoriesList.isEmpty()) if (categoriesList.isEmpty())
return; return;
for (String category : categoriesList) { for (String category : categoriesList) {
ConfigurationSection nameSection = confCategory.getConfigurationSection(category); ConfigurationSection nameSection = confCategory.getConfigurationSection(category);
if (nameSection == null) if (nameSection == null)
continue; continue;
jobsSign newTemp = new jobsSign(); jobsSign newTemp = new jobsSign();
if (nameSection.isString("World")) { if (nameSection.isString("World")) {
newTemp.setWorldName(nameSection.getString("World")); newTemp.setWorldName(nameSection.getString("World"));
newTemp.setX((int) nameSection.getDouble("X")); newTemp.setX((int) nameSection.getDouble("X"));
newTemp.setY((int) nameSection.getDouble("Y")); newTemp.setY((int) nameSection.getDouble("Y"));
newTemp.setZ((int) nameSection.getDouble("Z")); newTemp.setZ((int) nameSection.getDouble("Z"));
} else { } else {
newTemp.setLoc(nameSection.getString("Loc")); newTemp.setLoc(nameSection.getString("Loc"));
} }
if (nameSection.isString("Type")) if (nameSection.isString("Type"))
newTemp.setType(SignTopType.getType(nameSection.getString("Type"))); newTemp.setType(SignTopType.getType(nameSection.getString("Type")));
newTemp.setNumber(nameSection.getInt("Number")); newTemp.setNumber(nameSection.getInt("Number"));
if (nameSection.isString("JobName")) { if (nameSection.isString("JobName")) {
SignTopType t = SignTopType.getType(nameSection.getString("JobName")); SignTopType t = SignTopType.getType(nameSection.getString("JobName"));
if (t == null) if (t == null)
newTemp.setJobName(nameSection.getString("JobName")); newTemp.setJobName(nameSection.getString("JobName"));
} }
newTemp.setSpecial(nameSection.getBoolean("Special")); newTemp.setSpecial(nameSection.getBoolean("Special"));
String identifier = newTemp.getIdentifier().toLowerCase(); String identifier = newTemp.getIdentifier().toLowerCase();
Map<String, jobsSign> old = signsByType.get(identifier); Map<String, jobsSign> old = signsByType.get(identifier);
if (old == null) { if (old == null) {
old = new HashMap<>(); old = new HashMap<>();
signsByType.put(identifier, old); signsByType.put(identifier, old);
} }
String loc = newTemp.locToBlockString(); String loc = newTemp.locToBlockString();
old.put(loc, newTemp); old.put(loc, newTemp);
signsByLocation.put(loc, newTemp); signsByLocation.put(loc, newTemp);
} }
if (!signsByLocation.isEmpty()) { if (!signsByLocation.isEmpty()) {
CMIMessages.consoleMessage("&e[Jobs] Loaded " + signsByLocation.size() + " top list signs"); CMIMessages.consoleMessage("&e[Jobs] Loaded " + signsByLocation.size() + " top list signs");
} }
} }
public void saveSigns() { public void saveSigns() {
File f = new File(Jobs.getFolder(), "Signs.yml"); File f = new File(Jobs.getFolder(), "Signs.yml");
YamlConfiguration conf = YamlConfiguration.loadConfiguration(f); YamlConfiguration conf = YamlConfiguration.loadConfiguration(f);
ConfigReader reader = null; ConfigReader reader = null;
try { try {
reader = new ConfigReader(f); reader = new ConfigReader(f);
} catch (Exception e1) { } catch (Exception e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
if (reader == null) if (reader == null)
return; return;
conf.options().copyDefaults(true); conf.options().copyDefaults(true);
reader.addComment("Signs", "DO NOT EDIT THIS FILE BY HAND!"); reader.addComment("Signs", "DO NOT EDIT THIS FILE BY HAND!");
if (!conf.isConfigurationSection("Signs")) if (!conf.isConfigurationSection("Signs"))
conf.createSection("Signs"); conf.createSection("Signs");
int i = 0; int i = 0;
for (jobsSign sign : signsByLocation.values()) { for (jobsSign sign : signsByLocation.values()) {
String path = "Signs." + ++i; String path = "Signs." + ++i;
reader.set(path + ".Loc", sign.locToBlockString()); reader.set(path + ".Loc", sign.locToBlockString());
reader.set(path + ".Number", sign.getNumber()); reader.set(path + ".Number", sign.getNumber());
reader.set(path + ".Type", sign.getType().toString()); reader.set(path + ".Type", sign.getType().toString());
reader.set(path + ".JobName", sign.getJobName()); reader.set(path + ".JobName", sign.getJobName());
reader.set(path + ".Special", sign.isSpecial()); reader.set(path + ".Special", sign.isSpecial());
} }
try { try {
reader.save(f); reader.save(f);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void updateAllSign(Job job) { public void updateAllSign(Job job) {
for (SignTopType types : SignTopType.values()) { for (SignTopType types : SignTopType.values()) {
if (types != SignTopType.questtoplist) if (types != SignTopType.questtoplist)
signUpdate(job, types); signUpdate(job, types);
} }
} }
public boolean signUpdate(Job job) { public boolean signUpdate(Job job) {
return signUpdate(job, SignTopType.toplist); return signUpdate(job, SignTopType.toplist);
} }
public boolean signUpdate(Job job, SignTopType type) { public boolean signUpdate(Job job, SignTopType type) {
if (!Jobs.getGCManager().SignsEnabled) if (!Jobs.getGCManager().SignsEnabled)
return true; return true;
if (type == null) if (type == null)
type = SignTopType.toplist; type = SignTopType.toplist;
String jobNameOrType = jobsSign.getIdentifier(job, type).toLowerCase(); String jobNameOrType = jobsSign.getIdentifier(job, type).toLowerCase();
Map<String, jobsSign> signs = signsByType.get(jobNameOrType); Map<String, jobsSign> signs = signsByType.get(jobNameOrType);
if (signs == null || signs.isEmpty()) if (signs == null || signs.isEmpty())
return false; return false;
List<TopList> playerList = new ArrayList<>(); List<TopList> playerList = new ArrayList<>();
switch (type) { switch (type) {
case gtoplist: case gtoplist:
playerList = Jobs.getJobsDAO().getGlobalTopList(0); playerList = Jobs.getJobsDAO().getGlobalTopList(0);
break; break;
case questtoplist: case questtoplist:
playerList = Jobs.getJobsDAO().getQuestTopList(0); playerList = Jobs.getJobsDAO().getQuestTopList(0);
break; break;
default: default:
break; break;
} }
int timelapse = 1; int timelapse = 1;
Map<String, List<TopList>> temp = new HashMap<>(); Map<String, List<TopList>> temp = new HashMap<>();
boolean save = false; boolean save = false;
for (jobsSign jSign : new HashMap<>(signs).values()) { for (jobsSign jSign : new HashMap<>(signs).values()) {
Location loc = jSign.getLocation(); Location loc = jSign.getLocation();
if (loc == null) if (loc == null)
continue; continue;
Block block = loc.getBlock(); Block block = loc.getBlock();
if (!(block.getState() instanceof Sign)) { if (!(block.getState() instanceof Sign)) {
if (!jobNameOrType.isEmpty()) { if (!jobNameOrType.isEmpty()) {
Map<String, jobsSign> tt = signsByType.get(jobNameOrType); Map<String, jobsSign> tt = signsByType.get(jobNameOrType);
if (tt != null) { if (tt != null) {
tt.remove(jSign.locToBlockString()); tt.remove(jSign.locToBlockString());
} }
} }
signsByLocation.remove(jSign.locToBlockString()); signsByLocation.remove(jSign.locToBlockString());
save = true; save = true;
continue; continue;
} }
String signJobName = jSign.getJobName(); String signJobName = jSign.getJobName();
if (type == SignTopType.toplist && (playerList = temp.get(signJobName)) == null) { if (type == SignTopType.toplist && (playerList = temp.get(signJobName)) == null) {
playerList = Jobs.getJobsDAO().toplist(signJobName); playerList = Jobs.getJobsDAO().toplist(signJobName);
temp.put(signJobName, playerList); temp.put(signJobName, playerList);
} }
if (Jobs.getJob(jSign.getJobName()) != null) if (Jobs.getJob(jSign.getJobName()) != null)
signJobName = Jobs.getJob(jSign.getJobName()).getDisplayName(); signJobName = Jobs.getJob(jSign.getJobName()).getDisplayName();
if (playerList.isEmpty()) if (playerList.isEmpty())
continue; continue;
int number = jSign.getNumber() - 1; int number = jSign.getNumber() - 1;
Sign sign = (Sign) block.getState(); Sign sign = (Sign) block.getState();
if (!jSign.isSpecial()) { if (!jSign.isSpecial()) {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
if (i + number >= playerList.size()) { if (i + number >= playerList.size()) {
plugin.getComplement().setLine(sign, i, ""); plugin.getComplement().setLine(sign, i, "");
continue; continue;
} }
TopList pl = playerList.get(i + number); TopList pl = playerList.get(i + number);
String playerName = pl.getPlayerInfo().getName(); String playerName = pl.getPlayerInfo().getName();
if (playerName.length() > 15) { if (playerName.length() > 15) {
// We need to split 10 char of name, because of sign rows // We need to split 10 char of name, because of sign rows
playerName = playerName.split("(?<=\\G.{10})", 2)[0] + "~"; playerName = playerName.split("(?<=\\G.{10})", 2)[0] + "~";
} }
String line = ""; String line = "";
switch (type) { switch (type) {
case toplist: case toplist:
case gtoplist: case gtoplist:
line = Jobs.getLanguage().getMessage("signs.List", "[number]", i + number + 1, "[player]", playerName, "[level]", pl.getLevel()); line = Jobs.getLanguage().getMessage("signs.List", "[number]", i + number + 1, "[player]", playerName, "[level]", pl.getLevel());
break; break;
case questtoplist: case questtoplist:
line = Jobs.getLanguage().getMessage("signs.questList", "[number]", i + number + 1, "[player]", playerName, "[quests]", pl.getLevel()); line = Jobs.getLanguage().getMessage("signs.questList", "[number]", i + number + 1, "[player]", playerName, "[quests]", pl.getLevel());
break; break;
default: default:
break; break;
} }
if (!line.isEmpty()) if (!line.isEmpty())
sign.setLine(i, line); sign.setLine(i, line);
} }
sign.update(); sign.update();
if (!updateHead(sign, playerList.get(0).getPlayerInfo().getName(), timelapse)) { if (!updateHead(sign, playerList.get(0).getPlayerInfo().getName(), timelapse)) {
timelapse--; timelapse--;
} }
} else { } else {
if (jSign.getNumber() > playerList.size()) if (jSign.getNumber() > playerList.size())
continue; continue;
TopList pl = playerList.get(jSign.getNumber() - 1); TopList pl = playerList.get(jSign.getNumber() - 1);
String playerName = pl.getPlayerInfo().getName(); String playerName = pl.getPlayerInfo().getName();
if (playerName.length() > 15) { if (playerName.length() > 15) {
playerName = playerName.split("(?<=\\G.{10})", 2)[0] + "~"; playerName = playerName.split("(?<=\\G.{10})", 2)[0] + "~";
} }
int no = jSign.getNumber() + number + 1; int no = jSign.getNumber() + number + 1;
sign.setLine(0, translateSignLine("signs.SpecialList.p" + jSign.getNumber(), no, playerName, pl.getLevel(), signJobName)); sign.setLine(0, translateSignLine("signs.SpecialList.p" + jSign.getNumber(), no, playerName, pl.getLevel(), signJobName));
sign.setLine(1, translateSignLine("signs.SpecialList.name", no, playerName, pl.getLevel(), signJobName)); sign.setLine(1, translateSignLine("signs.SpecialList.name", no, playerName, pl.getLevel(), signJobName));
switch (type) { switch (type) {
case toplist: case toplist:
case gtoplist: case gtoplist:
sign.setLine(2, translateSignLine("signs.SpecialList.level", no, playerName, pl.getLevel(), signJobName)); sign.setLine(2, translateSignLine("signs.SpecialList.level", no, playerName, pl.getLevel(), signJobName));
break; break;
case questtoplist: case questtoplist:
sign.setLine(2, Jobs.getLanguage().getMessage("signs.SpecialList.quests", "[number]", no, "[player]", playerName, "[quests]", pl.getLevel(), "[job]", signJobName)); sign.setLine(2, Jobs.getLanguage().getMessage("signs.SpecialList.quests", "[number]", no, "[player]", playerName, "[quests]", pl.getLevel(), "[job]", signJobName));
break; break;
default: default:
break; break;
} }
sign.setLine(3, translateSignLine("signs.SpecialList.bottom", no, playerName, pl.getLevel(), signJobName)); sign.setLine(3, translateSignLine("signs.SpecialList.bottom", no, playerName, pl.getLevel(), signJobName));
sign.update(); sign.update();
if (!updateHead(sign, pl.getPlayerInfo().getName(), timelapse)) { if (!updateHead(sign, pl.getPlayerInfo().getName(), timelapse)) {
timelapse--; timelapse--;
} }
} }
timelapse++; timelapse++;
} }
if (save) if (save)
saveSigns(); saveSigns();
return true; return true;
} }
private static String translateSignLine(String path, int number, String playerName, int level, String jobname) { private static String translateSignLine(String path, int number, String playerName, int level, String jobname) {
return Jobs.getLanguage().getMessage(path, return Jobs.getLanguage().getMessage(path,
"[number]", number, "[number]", number,
"[player]", playerName, "[player]", playerName,
"[level]", level, "[level]", level,
"[job]", jobname); "[job]", jobname);
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public boolean updateHead(final Sign sign, final String playerName, int timelapse) { public boolean updateHead(final Sign sign, final String playerName, int timelapse) {
if (playerName == null) if (playerName == null)
return false; return false;
if (timelapse < 1) { if (timelapse < 1) {
timelapse = 1; timelapse = 1;
} }
BlockFace directionFacing = null; BlockFace directionFacing = null;
if (Version.isCurrentEqualOrLower(Version.v1_13_R2)) { if (Version.isCurrentEqualOrLower(Version.v1_13_R2)) {
org.bukkit.material.Sign signMat = (org.bukkit.material.Sign) sign.getData(); org.bukkit.material.Sign signMat = (org.bukkit.material.Sign) sign.getData();
directionFacing = signMat.getFacing(); directionFacing = signMat.getFacing();
} else { } else {
if (CMIMaterial.isWallSign(sign.getType())) { if (sign.getBlockData() instanceof org.bukkit.block.data.type.WallSign)
org.bukkit.block.data.type.WallSign data = (org.bukkit.block.data.type.WallSign) sign.getBlockData(); directionFacing = ((org.bukkit.block.data.type.WallSign) sign.getBlockData()).getFacing();
directionFacing = data.getFacing(); else if (sign.getBlockData() instanceof org.bukkit.block.data.type.Sign)
} else { directionFacing = ((org.bukkit.block.data.type.Sign) sign.getBlockData()).getRotation();
org.bukkit.block.data.type.Sign data = (org.bukkit.block.data.type.Sign) sign.getBlockData(); }
directionFacing = data.getRotation();
}
}
final Location loc = sign.getLocation().clone(); final Location loc = sign.getLocation().clone();
loc.add(0, 1, 0); loc.add(0, 1, 0);
if (directionFacing != null && !(loc.getBlock().getState() instanceof Skull)) if (directionFacing != null && !(loc.getBlock().getState() instanceof Skull))
loc.add(directionFacing.getOppositeFace().getModX(), 0, directionFacing.getOppositeFace().getModZ()); loc.add(directionFacing.getOppositeFace().getModX(), 0, directionFacing.getOppositeFace().getModZ());
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
if (!(loc.getBlock().getState() instanceof Skull)) if (!(loc.getBlock().getState() instanceof Skull))
return; return;
Skull skull = (Skull) loc.getBlock().getState(); Skull skull = (Skull) loc.getBlock().getState();
if (playerName.equalsIgnoreCase(skull.getOwner())) if (playerName.equalsIgnoreCase(skull.getOwner()))
return; return;
skull.setOwner(playerName); skull.setOwner(playerName);
skull.update(); skull.update();
} }
}, timelapse * Jobs.getGCManager().InfoUpdateInterval * 20L); }, timelapse * Jobs.getGCManager().InfoUpdateInterval * 20L);
return true; return true;
} }
} }