Bump version, and cleanup code in main

This commit is contained in:
Shevchik 2016-12-25 03:55:53 +03:00
parent 71140efa0f
commit eed1f18233
2 changed files with 42 additions and 76 deletions

View File

@ -1,17 +1,14 @@
package com.cnaude.chairs.core; package com.cnaude.chairs.core;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -50,21 +47,30 @@ public class Chairs extends JavaPlugin {
public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled, msgCommandRestricted; public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled, msgCommandRestricted;
private PlayerSitData psitdata; private final PlayerSitData psitdata = new PlayerSitData(this);
public PlayerSitData getPlayerSitData() { public PlayerSitData getPlayerSitData() {
return psitdata; return psitdata;
} }
private NMSAccess nmsaccess = new NMSAccess(); private final NMSAccess nmsaccess = new NMSAccess();
protected NMSAccess getNMSAccess() { protected NMSAccess getNMSAccess() {
return nmsaccess; return nmsaccess;
} }
public ChairEffects chairEffects; public final ChairEffects chairEffects = new ChairEffects(this);
@Override @Override
public void onEnable() { public void onEnable() {
log = this.getLogger(); getConfig().options().copyDefaults(true);
saveConfig();
loadConfig();
loadSitDisabled();
if (sitHealEnabled) {
chairEffects.startHealing();
}
if (sitPickupEnabled) {
chairEffects.startPickup();
}
try { try {
nmsaccess.setupChairsArrow(); nmsaccess.setupChairsArrow();
} catch (Exception e) { } catch (Exception e) {
@ -72,17 +78,6 @@ public class Chairs extends JavaPlugin {
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
return; return;
} }
chairEffects = new ChairEffects(this);
psitdata = new PlayerSitData(this);
getConfig().options().copyDefaults(true);
saveConfig();
loadConfig();
if (sitHealEnabled) {
chairEffects.startHealing();
}
if (sitPickupEnabled) {
chairEffects.startPickup();
}
getServer().getPluginManager().registerEvents(new NANLoginListener(), this); getServer().getPluginManager().registerEvents(new NANLoginListener(), this);
getServer().getPluginManager().registerEvents(new TrySitEventListener(this), this); getServer().getPluginManager().registerEvents(new TrySitEventListener(this), this);
getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this); getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this);
@ -93,21 +88,13 @@ public class Chairs extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
if (psitdata != null) {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
if (psitdata.isSitting(player)) { if (psitdata.isSitting(player)) {
psitdata.unsitPlayerForce(player); psitdata.unsitPlayerForce(player);
} }
} }
}
if (chairEffects != null) {
chairEffects.cancelHealing(); chairEffects.cancelHealing();
chairEffects.cancelPickup(); chairEffects.cancelPickup();
chairEffects = null;
}
log = null;
nmsaccess = null;
psitdata = null;
saveSitDisabled(); saveSitDisabled();
} }
@ -145,48 +132,37 @@ public class Chairs extends JavaPlugin {
validChairs.clear(); validChairs.clear();
for (String s : config.getStringList("sit-blocks")) { for (String s : config.getStringList("sit-blocks")) {
String type;
double sh = 0.7;
String tmp[] = s.split("[:]"); String tmp[] = s.split("[:]");
type = tmp[0]; Material mat = Material.matchMaterial(tmp[0]);
if (mat != null) {
double sh = 0.7;
if (tmp.length == 2) { if (tmp.length == 2) {
sh = Double.parseDouble(tmp[1]); sh = Double.parseDouble(tmp[1]);
} }
Material mat = Material.matchMaterial(type);
if (mat != null) {
logInfo("Allowed block: " + mat.toString() + " => " + sh);
validChairs.put(mat, sh); validChairs.put(mat, sh);
} else {
logError("Invalid block: " + type);
} }
} }
validSigns.clear(); validSigns.clear();
for (String type : config.getStringList("valid-signs")) { for (String type : config.getStringList("valid-signs")) {
try { Material mat = Material.matchMaterial(type);
validSigns.add(Material.matchMaterial(type)); if (mat != null) {
} catch (Exception e) { validSigns.add(mat);
logError(e.getMessage());
} }
} }
}
public void loadSitDisabled() {
try { try {
File sitDisabledFile = new File(getDataFolder(), "sit-disabled.txt");
if (sitDisabledFile.exists()) {
String line = null;
sitDisabled.clear(); sitDisabled.clear();
BufferedReader br = new BufferedReader(new FileReader(sitDisabledFile)); for (String line: Files.readAllLines(new File(getDataFolder(), "sit-disabled.txt").toPath())) {
while ((line = br.readLine()) != null) {
try { try {
sitDisabled.add(UUID.fromString(line)); sitDisabled.add(UUID.fromString(line));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
/* Not a UUID */
} }
} }
br.close();
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
} }
} }
@ -195,24 +171,14 @@ public class Chairs extends JavaPlugin {
File sitDisabledFile = new File(getDataFolder(), "sit-disabled.txt"); File sitDisabledFile = new File(getDataFolder(), "sit-disabled.txt");
if (!sitDisabledFile.exists()) if (!sitDisabledFile.exists())
sitDisabledFile.createNewFile(); sitDisabledFile.createNewFile();
PrintWriter writer = new PrintWriter(sitDisabledFile, "UTF-8"); try (PrintWriter writer = new PrintWriter(sitDisabledFile, "UTF-8")) {
writer.println("# The following players disabled Chairs for themselves"); writer.println("# The following players disabled Chairs for themselves");
for (UUID uuid : sitDisabled) for (UUID uuid : sitDisabled) {
writer.println(uuid.toString()); writer.println(uuid.toString());
writer.close(); }
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
} }
} }
private Logger log;
public void logInfo(String _message) {
log.log(Level.INFO, _message);
}
public void logError(String _message) {
log.log(Level.SEVERE, _message);
}
} }

View File

@ -1,5 +1,5 @@
name: Chairs name: Chairs
version: 4.9 version: 4.10
description: Let players sit on blocks. description: Let players sit on blocks.
website: http://dev.bukkit.org/bukkit-plugins/chairsreloaded/ website: http://dev.bukkit.org/bukkit-plugins/chairsreloaded/
authors: authors: