You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 lines
14 KiB

package cx.sfy.TheBridge;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import cx.sfy.TheBridge.cmds.SetupCMD;
import cx.sfy.TheBridge.controllers.WorldController;
import cx.sfy.TheBridge.database.Cosmeticbase;
import cx.sfy.TheBridge.database.Database;
import cx.sfy.TheBridge.database.PlayerStat;
import cx.sfy.TheBridge.game.Game;
import cx.sfy.TheBridge.game.Game.State;
import cx.sfy.TheBridge.game.GameFour;
import cx.sfy.TheBridge.game.GameFour.FState;
import cx.sfy.TheBridge.game.InventoryData;
import cx.sfy.TheBridge.game.PlayerData;
import cx.sfy.TheBridge.hooks.PlaceholderHook;
import cx.sfy.TheBridge.kit.Hotbar;
import cx.sfy.TheBridge.kit.Kit;
import cx.sfy.TheBridge.leaderheads.TheBridgeFourGoals;
import cx.sfy.TheBridge.leaderheads.TheBridgeFourKills;
import cx.sfy.TheBridge.leaderheads.TheBridgeFourWins;
import cx.sfy.TheBridge.leaderheads.TheBridgeNormalGoals;
import cx.sfy.TheBridge.leaderheads.TheBridgeNormalKills;
import cx.sfy.TheBridge.leaderheads.TheBridgeNormalWins;
import cx.sfy.TheBridge.listeners.PlayerListener;
import cx.sfy.TheBridge.listeners.SetupListener;
import cx.sfy.TheBridge.listeners.SpectatorListener;
import cx.sfy.TheBridge.managers.ArchiManager;
import cx.sfy.TheBridge.managers.CageManager;
import cx.sfy.TheBridge.managers.FileManager;
import cx.sfy.TheBridge.managers.GameManager;
import cx.sfy.TheBridge.managers.GlassManager;
import cx.sfy.TheBridge.managers.LocationManager;
import cx.sfy.TheBridge.managers.ScoreboardManager;
import cx.sfy.TheBridge.managers.SetupManager;
import cx.sfy.TheBridge.managers.SignManager;
import cx.sfy.TheBridge.managers.TitleManager;
import cx.sfy.TheBridge.managers.TopManager;
import cx.sfy.TheBridge.menus.AchievementsMenu;
import cx.sfy.TheBridge.menus.GameMenu;
import cx.sfy.TheBridge.menus.GlassMenu;
import cx.sfy.TheBridge.menus.ShopMenu;
import cx.sfy.TheBridge.menus.SpectOptionsMenu;
import cx.sfy.TheBridge.menus.SpectPlayerMenu;
import cx.sfy.TheBridge.menus.TeamMenu;
import cx.sfy.TheBridge.nms.GenericNMS;
import cx.sfy.TheBridge.nms.NMS;
import cx.sfy.TheBridge.packets.PacketMain;
public class Main extends JavaPlugin {
private static Main instance;
private PacketMain refl;
private SpectOptionsMenu som;
private SpectPlayerMenu spm;
private TeamMenu tem;
private boolean placeholder;
private TitleManager tm;
private GameManager gm;
private GameMenu gmu;
private WorldController wc;
private Settings lang;
private Settings cages;
private Settings signs;
private Settings sounds;
private Settings achievement;
private Settings killsounds;
private NMS nms;
private FileManager fm;
private SetupManager sm;
private GlassManager glm;
private LocationManager lm;
private Location mainLobby;
private Kit kit;
private Database db;
private TopManager top;
private SignManager sim;
private Hotbar hotbar;
private boolean stop;
private ScoreboardManager sb;
private ArchiManager am;
private AchievementsMenu archimenu;
private CageManager cm;
private GlassMenu glam;
private boolean isCage = false;
private boolean archiDisabled = false;
private ShopMenu shop;
private Cosmeticbase cb;
public static Main get() {
return instance;
}
@Override
public void onEnable() {
refl = new PacketMain(this);
instance = this;
stop = false;
getConfig().options().copyDefaults(true);
saveConfig();
File s = new File(getDataFolder() + "/cages");
if (!s.exists()) {
s.mkdirs();
}
saveResources();
lang = new Settings(this, "lang");
signs = new Settings(this, "signs");
cages = new Settings(this, "cages");
sounds = new Settings(this, "sounds");
achievement = new Settings(this, "achievement");
//killsounds = new Settings(this, "killsounds");
if (getServer().getPluginManager().isPluginEnabled("FastAsyncWorldEdit")) {
if (!getConfig().getBoolean("cages")) {
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "You have " + ChatColor.RED + "FAWE installed." + ChatColor.GREEN + "Cage System Enabled");
} else {
cm = new CageManager(this);
isCage = true;
}
} else {
if (getConfig().getBoolean("cages")) {
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "You don't have " + ChatColor.YELLOW + "FAWE installed." + ChatColor.RED + "Cage System Disabled");
isCage = false;
}
}
archiDisabled = getConfig().getBoolean("archievements.disable");
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
placeholder = true;
new PlaceholderHook(this).register();
}
loadNMS();
File a = new File(getDataFolder() + "/arenas");
if (!a.exists()) {
a.mkdirs();
}
File a1 = new File(getDataFolder() + "/maps");
if (!a1.exists()) {
a1.mkdirs();
}
db = new Database(this);
cb = new Cosmeticbase(this);
kit = new Kit();
lm = new LocationManager(this);
fm = new FileManager(this);
wc = new WorldController(this);
tm = new TitleManager(this);
sim = new SignManager(this);
gm = new GameManager(this);
gmu = new GameMenu(this);
sm = new SetupManager(this);
glm = new GlassManager();
top = new TopManager(this);
tem = new TeamMenu(this);
som = new SpectOptionsMenu(this);
spm = new SpectPlayerMenu(this);
hotbar = new Hotbar(this);
sb = new ScoreboardManager(this);
am = new ArchiManager(this);
archimenu = new AchievementsMenu(this);
glam = new GlassMenu(this);
shop = new ShopMenu(this);
lm.reloadLocations();
top.updateTops();
if (getConfig().getString("mainLobby") == null) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "The main lobby hasn't been set up. Please use " + ChatColor.YELLOW + "/bridges setmainlobby");
} else {
mainLobby = getStringLocation(getConfig().getString("mainLobby"));
}
getCommand("bridges").setExecutor(new SetupCMD(this));
Bukkit.getServer().getPluginManager().registerEvents(new SetupListener(this), this);
Bukkit.getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
Bukkit.getServer().getPluginManager().registerEvents(new SpectatorListener(this), this);
new BukkitRunnable() {
@Override
public void run() {
top.updateTops();
}
}.runTaskTimer(this, 6000, 6000);
new BukkitRunnable() {
@Override
public void run() {
for (Game game : getGM().getGames()) {
if (game.isState(State.WAITING) || game.isState(State.STARTING) || game.isState(State.PREGAME)) {
game.updateGame();
}
}
for (GameFour game : getGM().getGamesFour()) {
if (game.isState(FState.WAITING) || game.isState(FState.STARTING) || game.isState(FState.PREGAME)) {
game.updateGame();
}
}
}
}.runTaskTimer(this, 20, 20);
if (getServer().getPluginManager().isPluginEnabled("LeaderHeads")) {
new TheBridgeNormalKills();
new TheBridgeNormalWins();
new TheBridgeNormalGoals();
new TheBridgeFourKills();
new TheBridgeFourWins();
new TheBridgeFourGoals();
}
}
@Override
public void onDisable() {
stop = true;
getTOP().removeHolo();
getServer().getScheduler().cancelTasks(this);
for (InventoryData inv : InventoryData.getInventoryData().values()) {
inv.restore();
}
for (GameFour gamef : getGM().getGamesFour()) {
for (PlayerData pd : gamef.getPD().values()) {
pd.restore();
}
}
for (Game gamef : getGM().getGames()) {
for (PlayerData pd : gamef.getPD().values()) {
pd.restore();
}
}
for (Player on : Bukkit.getOnlinePlayers()) {
getDB().saveData(PlayerStat.getPlayerStat(on));
getCB().saveData(PlayerStat.getPlayerStat(on));
}
}
public boolean isArchiDisabled() {
return archiDisabled;
}
public Settings getSounds() {
return sounds;
}
public Cosmeticbase getCB() {
return cb;
}
public boolean isCage() {
return isCage;
}
public Settings getKS() {
return killsounds;
}
public ShopMenu getShop() {
return shop;
}
public GlassMenu getGLAM() {
return glam;
}
public CageManager getCM() {
return cm;
}
public boolean isStop() {
return stop;
}
public AchievementsMenu getArchiMenu() {
return archimenu;
}
public ArchiManager getAM() {
return am;
}
public Hotbar getHotbar() {
return hotbar;
}
public ScoreboardManager getSB() {
return sb;
}
public SpectPlayerMenu getSPM() {
return spm;
}
public SpectOptionsMenu getSOM() {
return som;
}
public TeamMenu getTEM() {
return tem;
}
public void loadNMS() {
// Replaced with reflection.
nms = new GenericNMS();
}
public void saveResources() {
saveResource("clear.schematic", false);
saveResource("normal-red.schematic", false);
saveResource("normal-blue.schematic", false);
saveResource("normal-green.schematic", false);
saveResource("normal-yellow.schematic", false);
File cage = new File(getDataFolder(), "cages");
File c = new File(getDataFolder(), "clear.schematic");
File r = new File(getDataFolder(), "normal-red.schematic");
File b = new File(getDataFolder(), "normal-blue.schematic");
File g = new File(getDataFolder(), "normal-green.schematic");
File y = new File(getDataFolder(), "normal-yellow.schematic");
copyFiles(c, new File(cage, "clear.schematic"));
copyFiles(r, new File(cage, "normal-red.schematic"));
copyFiles(b, new File(cage, "normal-blue.schematic"));
copyFiles(g, new File(cage, "normal-green.schematic"));
copyFiles(y, new File(cage, "normal-yellow.schematic"));
c.delete();
r.delete();
g.delete();
b.delete();
y.delete();
}
public void copyFiles(File file, File file2) {
try {
if (!new ArrayList<String>(Arrays.asList("uid.dat", "session.dat")).contains(file.getName())) {
if (file.isDirectory()) {
if (!file2.exists()) {
file2.mkdirs();
}
String[] list;
for (int length = (list = file.list()).length, i = 0; i < length; ++i) {
String s = list[i];
copyFiles(new File(file, s), new File(file2, s));
}
}
else {
FileOutputStream fileOutputStream;
try (FileInputStream fileInputStream = new FileInputStream(file)) {
fileOutputStream = new FileOutputStream(file2);
byte[] array = new byte[1024];
int read;
while ((read = fileInputStream.read(array)) > 0) {
fileOutputStream.write(array, 0, read);
}
}
fileOutputStream.close();
}
}
}
catch (IOException ex) {}
}
/*public boolean isOnePointNine() {
return onepointnine;
}*/
public SignManager getSIM() {
return sim;
}
public Settings getSigns() {
return signs;
}
public TopManager getTOP() {
return top;
}
public Kit getKit() {
return kit;
}
public void reloadMainLobby() {
mainLobby = getStringLocation(getConfig().getString("mainLobby"));
}
public Database getDB() {
return db;
}
public LocationManager getLM() {
return lm;
}
public Location getMainLobby() {
return mainLobby;
}
public GlassManager getGLM() {
return glm;
}
public FileManager getFM() {
return fm;
}
public SetupManager getSM() {
return sm;
}
public WorldController getWC() {
return wc;
}
public NMS getNMS() {
return nms;
}
public GameMenu getGMU() {
return gmu;
}
public GameManager getGM() {
return gm;
}
public TitleManager getTM() {
return tm;
}
public Settings getAchievement() {
return achievement;
}
public Settings getLang() {
return lang;
}
public Settings getCages() {
return cages;
}
public boolean getPlaceholder() {
return placeholder;
}
public Location getStringLocation(String location) {
String[] l = location.split(";");
World world = Bukkit.getWorld(l[0]);
double x = Double.parseDouble(l[1]);
double y = Double.parseDouble(l[2]);
double z = Double.parseDouble(l[3]);
float yaw = Float.parseFloat(l[4]);
float pitch = Float.parseFloat(l[5]);
return new Location(world, x, y, z, yaw, pitch);
}
public PacketMain getRefl() {
return refl;
}
public void setRefl(PacketMain refl) {
this.refl = refl;
}
}