LagAssist is a plugin that provides tools to prevent, analyse or resolve lag.
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.

214 lines
5.3 KiB

package com.entryrise.lagassist;
4 years ago
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Hopper;
import org.bukkit.command.CommandSender;
4 years ago
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.map.MapView;
import com.entryrise.lagassist.hoppers.SellHoppers;
import com.entryrise.lagassist.packets.Reflection;
import com.entryrise.lagassist.utils.WorldMgr;
4 years ago
public class Data {
private static File dataf = new File(Main.p.getDataFolder(), "data.yml");
private static FileConfiguration data = new YamlConfiguration();
public static void Enabler() {
try {
if (!dataf.exists()) {
dataf.createNewFile();
}
data.load(dataf);
} catch (Exception e) {
e.printStackTrace();
}
4 years ago
if (!data.contains("version")) {
4 years ago
if (data.contains("hoppers")) {
for (String rawh : data.getConfigurationSection("hoppers").getKeys(false)) {
String loc = "hoppers." + rawh;
4 years ago
List<String> values = data.getStringList(loc);
data.set(loc, null);
data.set(loc + ".materials", values);
}
4 years ago
}
4 years ago
data.set("version", 1);
4 years ago
saveData();
4 years ago
}
}
private static long last;
4 years ago
private static void saveData() {
if (System.currentTimeMillis() - last < 3000) {
return;
4 years ago
}
last = System.currentTimeMillis();
Bukkit.getScheduler().runTaskAsynchronously(Main.p, () -> {
try {
data.save(dataf);
} catch (IOException e) {
e.printStackTrace();
}
});
4 years ago
}
private static short genMapId() {
MapView newe = Bukkit.createMap(Bukkit.getWorlds().get(0));
short mapid = (short) Reflection.getId(newe);
data.set("data.mapid", mapid);
saveData();
return mapid;
}
public static short getMapId() {
if (data.contains("data.mapid")) {
int mapid = data.getInt("data.mapid");
// if (Reflection.getMapView(mapid) == null) {
// return genMapId();
// } else {
// return mapid;
// }
return (short) mapid;
} else {
return genMapId();
}
}
4 years ago
public static void deleteHopper(Hopper h) {
String cloc = "hoppers." + WorldMgr.serializeLocation(h.getLocation());
4 years ago
data.set(cloc, null);
saveData();
}
4 years ago
public static boolean isSellHopper(Location loc) {
String cloc = "hoppers." + WorldMgr.serializeLocation(loc);
4 years ago
return data.getBoolean(cloc + ".sellhopper", false);
}
4 years ago
public static boolean toggleSellHopper(Player p, Location loc) {
String cloc = "hoppers." + WorldMgr.serializeLocation(loc);
String owner = data.getString(cloc + ".owner", "NONE");
4 years ago
if (!(p.getUniqueId().toString().equals(owner) || p.hasPermission("lagassist.hopper.bypass"))) {
p.sendMessage(Main.PREFIX + "You can't toggle selling for a hopper not owned by you");
return false;
}
4 years ago
String percentage = "§e" + SellHoppers.getMultiplierPercentage(Bukkit.getOfflinePlayer(p.getUniqueId())) + "%";
4 years ago
if (isSellHopper(loc)) {
data.set(cloc + ".sellhopper", false);
p.sendMessage(Main.PREFIX + "This sellhopper has been §2disabled§f at " + percentage + "§f.");
4 years ago
saveData();
return true;
} else {
data.set(cloc + ".sellhopper", true);
p.sendMessage(Main.PREFIX + "This sellhopper has been §aenabled§f at " + percentage + "§f.");
saveData();
return true;
}
}
public static OfflinePlayer getOwningPlayer(Location loc) {
4 years ago
String cloc = "hoppers." + WorldMgr.serializeLocation(loc);
if (data.contains(cloc + ".owner")) {
return Bukkit.getOfflinePlayer(UUID.fromString(data.getString(cloc + ".owner")));
}
return null;
}
public static void setOwningPlayer(Location loc, OfflinePlayer owner) {
String cloc = "hoppers." + WorldMgr.serializeLocation(loc);
data.set(cloc + ".owner", owner.getUniqueId().toString());
saveData();
}
public static Set<Material> getFilterWhitelist(Location loc) {
String cloc = "hoppers." + WorldMgr.serializeLocation(loc);
List<String> found = null;
if (data.contains(cloc + ".materials")) {
found = data.getStringList(cloc + ".materials");
}
Set<Material> allowed = new HashSet<Material>();
if (found == null) {
return allowed;
}
for (String stg : found) {
allowed.add(Material.getMaterial(stg));
}
return allowed;
}
public static void saveFilterWhitelist(Location loc, Set<Material> mats) {
String cloc = "hoppers." + WorldMgr.serializeLocation(loc) + ".materials";
if (mats == null) {
data.set(cloc, null);
saveData();
return;
}
List<String> allowed = new ArrayList<String>();
for (Material mat : mats) {
allowed.add(mat.name());
}
data.set(cloc, allowed);
saveData();
}
public static void toggleAdvertising(CommandSender sender) {
boolean advertising = !data.getBoolean("disable-advertising", false);
data.set("disable-advertising", advertising);
sender.sendMessage(Main.PREFIX + "Advertising disabled: " + advertising);
saveData();
}
public static boolean isAdvertising() {
return !data.getBoolean("disable-advertising");
}
4 years ago
}