Skip to content

Commit

Permalink
2.6.1 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP committed Jul 5, 2020
1 parent 07858cf commit 0645932
Show file tree
Hide file tree
Showing 19 changed files with 601 additions and 327 deletions.
83 changes: 50 additions & 33 deletions src/com/loohp/bookshelf/Bookshelf.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -77,8 +80,8 @@ public class Bookshelf extends JavaPlugin {
public static boolean EnableDropperSupport = true;
public static int HopperTaskID = -1;
public static int HopperMinecartTaskID = -1;
public static long HopperTicksPerTransfer = 8;
public static long HopperAmount = 1;
public static int HopperTicksPerTransfer = 8;
public static int HopperAmount = 1;

public static ConcurrentHashMap<String, Inventory> bookshelfContent = new ConcurrentHashMap<String, Inventory>();

Expand All @@ -90,10 +93,10 @@ public class Bookshelf extends JavaPlugin {

public static ConcurrentHashMap<Player, String> requestOpen = new ConcurrentHashMap<Player, String>();

public static long BookShelfRows = 2;
public static int BookShelfRows = 2;
public static boolean UseWhitelist = true;
public static String Title = "Bookshelf";
public static List<String> Whitelist = new CopyOnWriteArrayList<String>();
public static Set<String> Whitelist = new HashSet<String>();
public static boolean particlesEnabled = true;

public static String NoPermissionToReloadMessage = "&cYou do not have permission use this command!";
Expand All @@ -108,8 +111,8 @@ public class Bookshelf extends JavaPlugin {

public static ConcurrentHashMap<Player, Long> enchantSeed = new ConcurrentHashMap<Player, Long>();

private static long spawnchunks = 0;
private static long done = 0;
private static int spawnchunks = 0;
private static int done = 0;
private static String currentWorld = "world";

public static long lastHopperTime = 0;
Expand Down Expand Up @@ -146,84 +149,84 @@ public void onEnable() {
//------

String GriefPrevention = "GriefPrevention";
if (Bukkit.getServer().getPluginManager().getPlugin("GriefPrevention") != null) {
if (getServer().getPluginManager().getPlugin("GriefPrevention") != null) {
hookMessage(GriefPrevention);
getServer().getPluginManager().registerEvents(new GriefPreventionEvents(), this);
GriefPreventionHook = true;
}

String LWC = "LWC";
if (Bukkit.getServer().getPluginManager().getPlugin(LWC) != null) {
if (getServer().getPluginManager().getPlugin(LWC) != null) {
hookMessage(LWC);
LWCEvents.hookLWC();
LWCHook = true;
}

String BlockLocker = "BlockLocker";
if (Bukkit.getServer().getPluginManager().getPlugin(BlockLocker) != null) {
if (getServer().getPluginManager().getPlugin(BlockLocker) != null) {
hookMessage(BlockLocker);
BlockLockerHook = true;
}

String WorldGuard = "WorldGuard";
if (Bukkit.getServer().getPluginManager().getPlugin(WorldGuard) != null) {
if (getServer().getPluginManager().getPlugin(WorldGuard) != null) {
hookMessage(WorldGuard);
getServer().getPluginManager().registerEvents(new WorldGuardEvents(), this);
WorldGuardHook = true;
}

String RedProtect = "RedProtect";
if (Bukkit.getServer().getPluginManager().getPlugin(RedProtect) != null) {
if (getServer().getPluginManager().getPlugin(RedProtect) != null) {
hookMessage(RedProtect);
getServer().getPluginManager().registerEvents(new RedProtectEvents(), this);
RedProtectHook = true;
}

String BentoBox = "BentoBox";
if (Bukkit.getServer().getPluginManager().getPlugin(BentoBox) != null) {
if (getServer().getPluginManager().getPlugin(BentoBox) != null) {
hookMessage(BentoBox);
getServer().getPluginManager().registerEvents(new BentoBoxEvents(), this);
BentoBoxHook = true;
}

String ASkyBlock = "ASkyBlock";
if (Bukkit.getServer().getPluginManager().getPlugin(ASkyBlock) != null) {
if (getServer().getPluginManager().getPlugin(ASkyBlock) != null) {
hookMessage(ASkyBlock);
getServer().getPluginManager().registerEvents(new ASkyBlockEvents(), this);
ASkyBlockHook = true;
}

String Residence = "Residence";
if (Bukkit.getServer().getPluginManager().getPlugin(Residence) != null) {
if (getServer().getPluginManager().getPlugin(Residence) != null) {
hookMessage(Residence);
getServer().getPluginManager().registerEvents(new ResidenceEvents(), this);
ResidenceHook = true;
}

String Towny = "Towny";
if (Bukkit.getServer().getPluginManager().getPlugin(Towny) != null) {
if (getServer().getPluginManager().getPlugin(Towny) != null) {
hookMessage(Towny);
getServer().getPluginManager().registerEvents(new TownyEvents(), this);
TownyHook = true;
}

String SuperiorSkyblock2 = "SuperiorSkyblock2";
if (Bukkit.getServer().getPluginManager().getPlugin(SuperiorSkyblock2) != null) {
if (getServer().getPluginManager().getPlugin(SuperiorSkyblock2) != null) {
hookMessage(SuperiorSkyblock2);
getServer().getPluginManager().registerEvents(new SuperiorSkyblock2Events(), this);
SuperiorSkyblock2Hook = true;
}

String Lands = "Lands";
if (Bukkit.getServer().getPluginManager().getPlugin(Lands) != null) {
if (getServer().getPluginManager().getPlugin(Lands) != null) {
hookMessage(Lands);
getServer().getPluginManager().registerEvents(new LandEvents(), this);
LandEvents.setup();
LandHook = true;
}

String PlotSquared = "PlotSquared";
if (Bukkit.getServer().getPluginManager().getPlugin(PlotSquared) != null) {
if (getServer().getPluginManager().getPlugin(PlotSquared) != null) {
hookMessage(PlotSquared);
getServer().getPluginManager().registerEvents(new PlotSquaredEvents(), this);
PlotSquaredHook = true;
Expand Down Expand Up @@ -251,7 +254,8 @@ public void onEnable() {
intervalLoad();
intervalRemove();
particles();
loadBookshelf();

loadBookshelf(getServer().getWorlds());

Charts.loadCharts(metrics);

Expand All @@ -277,9 +281,9 @@ private static void hookMessage(String name) {
}

public static void loadConfig() {
BookShelfRows = plugin.getConfig().getLong("Options.BookShelfRows");
BookShelfRows = plugin.getConfig().getInt("Options.BookShelfRows");
UseWhitelist = plugin.getConfig().getBoolean("Options.UseWhitelist");
Whitelist = plugin.getConfig().getStringList("Options.Whitelist");
Whitelist = plugin.getConfig().getStringList("Options.Whitelist").stream().collect(Collectors.toSet());
Title = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Options.Title"));
NoPermissionToReloadMessage = plugin.getConfig().getString("Options.NoPermissionToReloadMessage");
NoPermissionToUpdateMessage = plugin.getConfig().getString("Options.NoPermissionToUpdateMessage");
Expand All @@ -304,8 +308,8 @@ public static void loadConfig() {
Bukkit.getScheduler().cancelTask(HopperMinecartTaskID);
}
if (EnableHopperSupport == true) {
HopperTicksPerTransfer = Bukkit.spigot().getConfig().getLong("world-settings.default.ticks-per.hopper-transfer");
HopperAmount = Bukkit.spigot().getConfig().getLong("world-settings.default.hopper-amount");
HopperTicksPerTransfer = Bukkit.spigot().getConfig().getInt("world-settings.default.ticks-per.hopper-transfer");
HopperAmount = Bukkit.spigot().getConfig().getInt("world-settings.default.hopper-amount");
HopperUtils.hopperCheck();
HopperUtils.hopperMinecartCheck();
}
Expand All @@ -319,47 +323,60 @@ public static void loadConfig() {
}
}

public void loadBookshelf() {
public static void loadBookshelf(World world) {
List<World> worlds = new ArrayList<World>(1);
worlds.add(world);
loadBookshelf(worlds);
}

public synchronized static void loadBookshelf(List<World> worlds) {
long start = System.currentTimeMillis();
for (World world : Bukkit.getWorlds()) {
for (World world : worlds) {
spawnchunks = world.getLoadedChunks().length;
done = 0;
currentWorld = world.getName();
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Bookshelf] Loading bookshelves in spawn chunks in " + world.getName());
loadBookshelfProgress();
for (Chunk chunk: world.getLoadedChunks()) {
for (Chunk chunk : world.getLoadedChunks()) {
for (Block block : BookshelfUtils.getAllBookshelvesInChunk(chunk)) {
String loc = BookshelfUtils.locKey(block.getLocation());
if (!bookshelfContent.containsKey(loc)) {
if (!BookshelfManager.contains(loc)) {
String bsTitle = Title;
bookshelfContent.put(loc , Bukkit.createInventory(null, (int) (BookShelfRows * 9), bsTitle));
bookshelfContent.put(loc, Bukkit.createInventory(null, (int) (BookShelfRows * 9), bsTitle));
BookshelfManager.setTitle(loc, bsTitle);
BookshelfUtils.saveBookShelf(loc);
} else {
BookshelfUtils.loadBookShelf(loc);
}
}
}
done = done + 1;
done++;
}
Bukkit.getConsoleSender().sendMessage("[Bookshelf] Preparing bookshelves in spawn chunks in " + currentWorld + ": 100%");
}
BookshelfManager.save();
BookshelfManager.intervalSaveToFile();
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[Bookshelf] Bookshelves loaded! (" + (System.currentTimeMillis() - start) + "ms)");
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[Bookshelf] Bookshelves loaded in " + worlds.size() + (worlds.size() == 1 ? " world" : " worlds") + "! (" + (System.currentTimeMillis() - start) + "ms)");
}

public void loadBookshelfProgress() {
public static void loadBookshelfProgress() {
CompletableFuture.runAsync(()->{
long start = System.currentTimeMillis();
String thisWorld = currentWorld;
long lastDone = 0;
while (done < spawnchunks && thisWorld == currentWorld) {
Bukkit.getConsoleSender().sendMessage("[Bookshelf] Preparing bookshelves in spawn chunks in " + currentWorld + ": " + Math.round((double) ((double) done / (double) spawnchunks) * 100) + "%");
if ((System.currentTimeMillis() - start) > 30000) {
return;
}
if (lastDone != done) {
start = System.currentTimeMillis();
lastDone = done;
}
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error");
}
} catch (InterruptedException ignore) {}
}
});
}
Expand Down
45 changes: 37 additions & 8 deletions src/com/loohp/bookshelf/BookshelfManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,40 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap;

import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.Bukkit;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import net.md_5.bungee.api.ChatColor;

public class BookshelfManager {

private static File file;
private static JSONObject json;
private static JSONParser parser = new JSONParser();
private static File BackupFolder = new File(Bookshelf.plugin.getDataFolder().getPath() + "/Backup", "bookshelf");

public static void intervalSaveToFile() {
new BukkitRunnable() {
public void run() {
BookshelfManager.save();
}
}.runTaskTimerAsynchronously(Bookshelf.plugin, 200, 600);
Bukkit.getScheduler().runTaskTimerAsynchronously(Bookshelf.plugin, () -> {
BookshelfManager.save();
}, 200, 600);
}

public static void reload() {
public synchronized static void reload() {
try {
if (!Bookshelf.plugin.getDataFolder().exists()) {
Bookshelf.plugin.getDataFolder().mkdir();
Expand All @@ -42,6 +48,29 @@ public static void reload() {
pw.print("}");
pw.flush();
pw.close();
} else {
String fileName = new SimpleDateFormat("yyyy'-'MM'-'dd'_'HH'-'mm'-'ss'_'zzz'_bookshelfdata.json'").format(new Date());
BackupFolder.mkdirs();
File outputfile = new File(BackupFolder, fileName);
try (InputStream in = new FileInputStream(file)) {
Files.copy(in, outputfile.toPath());
} catch (IOException e) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Bookshelf] Failed to make backup for bookshelfdata.json");
}
}
if (BackupFolder.exists()) {
for (File file : BackupFolder.listFiles()) {
try {
String fileName = file.getName();
if (fileName.matches("^[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}_.*_bookshelfdata\\.json$")) {
Date timestamp = new SimpleDateFormat("yyyy'-'MM'-'dd'_'HH'-'mm'-'ss'_'zzz'_bookshelfdata.json'").parse(fileName);
if ((System.currentTimeMillis() - timestamp.getTime()) > 2592000000L) {
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Bookshelf] Removing Backup/Backup/" + fileName + " as it is from 30 days ago.");
file.delete();
}
}
} catch (Exception ignore) {}
}
}
json = (JSONObject) parser.parse(new InputStreamReader(new FileInputStream(file), "UTF-8"));
} catch (Exception ex) {
Expand All @@ -50,7 +79,7 @@ public static void reload() {
}

@SuppressWarnings("unchecked")
public static boolean save() {
public synchronized static boolean save() {
try {
JSONObject toSave = json;

Expand Down
4 changes: 2 additions & 2 deletions src/com/loohp/bookshelf/Listeners/ASkyBlockEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ASkyBlockEvents implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onASkyBlockCheck(PlayerInteractEvent event) {

if (Bookshelf.ASkyBlockHook == false) {
if (!Bookshelf.ASkyBlockHook) {
return;
}

Expand All @@ -44,7 +44,7 @@ public void onASkyBlockCheck(PlayerInteractEvent event) {
if (Bookshelf.cancelOpen.contains(event.getPlayer())) {
return;
}
if (player.isSneaking() == true) {
if (player.isSneaking()) {
return;
}
if (event.getClickedBlock() == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/loohp/bookshelf/Listeners/BentoBoxEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BentoBoxEvents implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onBentoBoxCheck(PlayerInteractEvent event) {

if (Bookshelf.BentoBoxHook == false) {
if (!Bookshelf.BentoBoxHook) {
return;
}

Expand All @@ -45,7 +45,7 @@ public void onBentoBoxCheck(PlayerInteractEvent event) {
if (Bookshelf.cancelOpen.contains(event.getPlayer())) {
return;
}
if (player.isSneaking() == true) {
if (player.isSneaking()) {
return;
}
if (event.getClickedBlock() == null) {
Expand Down
Loading

0 comments on commit 0645932

Please sign in to comment.