MySQL system implemented.

This commit is contained in:
Brianna O'Keefe 2018-09-27 00:56:23 -04:00
parent 409cb860f0
commit f3831a037e
14 changed files with 506 additions and 100 deletions

View File

@ -1,22 +1,22 @@
package com.songoda.epichoppers.api.hopper;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public interface Filter {
List<ItemStack> getWhiteList();
List<Material> getWhiteList();
void setWhiteList(List<ItemStack> whiteList);
void setWhiteList(List<Material> whiteList);
List<ItemStack> getBlackList();
List<Material> getBlackList();
void setBlackList(List<ItemStack> blackList);
void setBlackList(List<Material> blackList);
List<ItemStack> getVoidList();
List<Material> getVoidList();
void setVoidList(List<ItemStack> voidList);
void setVoidList(List<Material> voidList);
Block getEndPoint();

View File

@ -2,6 +2,7 @@ package com.songoda.epichoppers;
import com.google.common.base.Preconditions;
import com.songoda.arconix.api.mcupdate.MCUpdate;
import com.songoda.arconix.api.methods.serialize.Serialize;
import com.songoda.arconix.api.utils.ConfigWrapper;
import com.songoda.arconix.plugin.Arconix;
import com.songoda.epichoppers.api.EpicHoppers;
@ -31,6 +32,11 @@ import com.songoda.epichoppers.listeners.HopperListeners;
import com.songoda.epichoppers.listeners.InteractListeners;
import com.songoda.epichoppers.listeners.InventoryListeners;
import com.songoda.epichoppers.player.PlayerDataManager;
import com.songoda.epichoppers.storage.Storage;
import com.songoda.epichoppers.storage.StorageItem;
import com.songoda.epichoppers.storage.StorageRow;
import com.songoda.epichoppers.storage.types.StorageMysql;
import com.songoda.epichoppers.storage.types.StorageYaml;
import com.songoda.epichoppers.utils.Methods;
import com.songoda.epichoppers.utils.SettingsManager;
import org.bukkit.Bukkit;
@ -62,7 +68,6 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
private ClaimableProtectionPluginHook factionsHook, townyHook, aSkyblockHook, uSkyblockHook;
private SettingsManager settingsManager;
private ConfigWrapper hooksFile = new ConfigWrapper(this, "", "hooks.yml");
private ConfigWrapper dataFile = new ConfigWrapper(this, "", "data.yml");
private Locale locale;
private HopperManager hopperManager;
@ -73,6 +78,8 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
private TeleportHandler teleportHandler;
private Storage storage;
public static EpicHoppersPlugin getInstance() {
return INSTANCE;
}
@ -123,35 +130,37 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
loadLevelManager();
checkStorage();
/*
* Register hoppers into HopperManger from configuration
*/
Bukkit.getScheduler().runTaskLater(this, () -> {
if (dataFile.getConfig().contains("data.sync")) {
for (String locationStr : dataFile.getConfig().getConfigurationSection("data.sync").getKeys(false)) {
Location location = Arconix.pl().getApi().serialize().unserializeLocation(locationStr);
if (storage.containsGroup("sync")) {
for (StorageRow row : storage.getRowsByGroup("sync")) {
Location location = Serialize.getInstance().unserializeLocation(row.getKey());
if (location == null || location.getBlock() == null) return;
int level = dataFile.getConfig().getInt("data.sync." + locationStr + ".level");
int level = row.get("level").asInt();
String blockLoc = dataFile.getConfig().getString("data.sync." + locationStr + ".block");
Block block = blockLoc == null ? null : Arconix.pl().getApi().serialize().unserializeLocation(dataFile.getConfig().getString("data.sync." + locationStr + ".block")).getBlock();
String blockLoc = row.get("block").asString();
Block block = blockLoc == null ? null : Arconix.pl().getApi().serialize().unserializeLocation(blockLoc).getBlock();
TeleportTrigger teleportTrigger = TeleportTrigger.valueOf(dataFile.getConfig().getString("data.sync." + locationStr + ".teleportTrigger"));
TeleportTrigger teleportTrigger = TeleportTrigger.valueOf(row.get("teleporttrigger").asString() == null ? "DISABLED" : row.get("teleporttrigger").asString());
String playerStr = dataFile.getConfig().getString("data.sync." + locationStr + ".player");
String placedByStr = dataFile.getConfig().getString("data.sync." + locationStr + ".placedBy");
String playerStr = row.get("player").asString();
String placedByStr = row.get("placedby").asString();
UUID lastPlayer = playerStr == null ? null : UUID.fromString(playerStr);
UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr);
List<ItemStack> whiteList = (ArrayList<ItemStack>) dataFile.getConfig().getList("data.sync." + locationStr + ".whitelist");
List<ItemStack> blackList = (ArrayList<ItemStack>) dataFile.getConfig().getList("data.sync." + locationStr + ".blacklist");
List<ItemStack> voidList = (ArrayList<ItemStack>) dataFile.getConfig().getList("data.sync." + locationStr + ".void");
List<Material> whiteList = row.get("whitelist").asMaterialList();
List<Material> blackList = row.get("blacklist").asMaterialList();
List<Material> voidList = row.get("void").asMaterialList();
Material autoCrafting = Material.valueOf(dataFile.getConfig().getString("data.sync." + locationStr + ".autoCrafting", "AIR"));
Material autoCrafting = Material.valueOf(row.get("autocrafting").asString() == null ? "AIR" : row.get("autocrafting").asString());
String blackLoc = dataFile.getConfig().getString("data.sync." + locationStr + ".black");
Block black = blackLoc == null ? null : Arconix.pl().getApi().serialize().unserializeLocation(dataFile.getConfig().getString("data.sync." + locationStr + ".black")).getBlock();
String blackLoc = row.get("black").asString();
Block black = blackLoc == null ? null : Arconix.pl().getApi().serialize().unserializeLocation(blackLoc).getBlock();
EFilter filter = new EFilter();
@ -167,13 +176,15 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
}
// Adding in Boosts
if (dataFile.getConfig().contains("data.boosts")) {
for (String key : dataFile.getConfig().getConfigurationSection("data.boosts").getKeys(false)) {
if (!dataFile.getConfig().contains("data.boosts." + key + ".Player")) continue;
if (storage.containsGroup("boosts")) {
for (StorageRow row : storage.getRowsByGroup("boosts")) {
if (!row.getItems().containsKey("player") || row.get("player").asString().equals(""))
continue;
BoostData boostData = new BoostData(
dataFile.getConfig().getInt("data.boosts." + key + ".Amount"),
Long.parseLong(key),
UUID.fromString(dataFile.getConfig().getString("data.boosts." + key + ".Player")));
row.get("amount").asInt(),
Long.parseLong(row.getKey()),
UUID.fromString(row.get("uuid").asString()));
this.boostManager.addBoostToPlayer(boostData);
}
@ -216,21 +227,32 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
public void onDisable() {
saveToFile();
this.storage.closeConnection();
this.protectionHooks.clear();
console.sendMessage(Arconix.pl().getApi().format().formatText("&a============================="));
console.sendMessage(Arconix.pl().getApi().format().formatText("&7EpicHoppers " + this.getDescription().getVersion() + " by &5Brianna <3!"));
console.sendMessage(Arconix.pl().getApi().format().formatText("&7Action: &cDisabling&7..."));
console.sendMessage(Arconix.pl().getApi().format().formatText("&a============================="));
dataFile.saveConfig();
}
private void checkStorage() {
if (getConfig().getBoolean("Database.Activate Mysql Support")) {
this.storage = new StorageMysql(this);
} else {
this.storage = new StorageYaml(this);
}
}
/*
* Saves registered hopper to file.
*/
private void saveToFile() {
this.storage.closeConnection();
checkStorage();
// Wipe old hopper information
dataFile.getConfig().set("data.sync", null);
storage.clearFile();
/*
* Dump HopperManager to file.
@ -240,32 +262,28 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
continue;
String locationStr = Arconix.pl().getApi().serialize().serializeLocation(hopper.getLocation());
ConfigurationSection sync = dataFile.getConfig().createSection("data.sync." + locationStr);
storage.saveItem("sync", new StorageItem("location", locationStr),
new StorageItem("level", hopper.getLevel().getLevel()),
new StorageItem("block", hopper.getSyncedBlock() == null ? null : Arconix.pl().getApi().serialize().serializeLocation(hopper.getSyncedBlock().getLocation())),
new StorageItem("placedby", hopper.getPlacedBy() == null ? null : hopper.getPlacedBy().toString()),
new StorageItem("player", hopper.getLastPlayer() == null ? null : hopper.getLastPlayer().toString()),
new StorageItem("teleporttrigger", hopper.getTeleportTrigger().toString()),
sync.set(".level", hopper.getLevel().getLevel());
sync.set(".block", hopper.getSyncedBlock() == null ? null : Arconix.pl().getApi().serialize().serializeLocation(hopper.getSyncedBlock().getLocation()));
sync.set(".player", hopper.getLastPlayer() == null ? null : hopper.getLastPlayer().toString());
sync.set(".placedBy", hopper.getPlacedBy() == null ? null : hopper.getPlacedBy().toString());
sync.set(".teleportTrigger", hopper.getTeleportTrigger().toString());
sync.set(".autoCrafting", hopper.getAutoCrafting() == null || hopper.getAutoCrafting() == Material.AIR ? null : hopper.getAutoCrafting().name());
sync.set(".whitelist", hopper.getFilter().getWhiteList());
sync.set(".blacklist", hopper.getFilter().getBlackList());
sync.set(".void", hopper.getFilter().getVoidList());
sync.set(".black", hopper.getFilter().getEndPoint() == null ? null : Arconix.pl().getApi().serialize().serializeLocation(hopper.getFilter().getEndPoint().getLocation()));
new StorageItem("autocrafting", hopper.getAutoCrafting() == null || hopper.getAutoCrafting() == Material.AIR ? null : hopper.getAutoCrafting().name()),
new StorageItem("whitelist", hopper.getFilter().getWhiteList()),
new StorageItem("blacklist", hopper.getFilter().getBlackList()),
new StorageItem("void", hopper.getFilter().getVoidList()),
new StorageItem("black", hopper.getFilter().getEndPoint() == null ? null : Arconix.pl().getApi().serialize().serializeLocation(hopper.getFilter().getEndPoint().getLocation())));
}
/*
* Dump BoostManager to file.
*/
for (BoostData boostData : boostManager.getBoosts()) {
String endTime = String.valueOf(boostData.getEndTime());
dataFile.getConfig().set("data.boosts." + endTime + ".Player", boostData.getPlayer().toString());
dataFile.getConfig().set("data.boosts." + endTime + ".Amount", boostData.getMultiplier());
storage.saveItem("boosts", new StorageItem("endtime", String.valueOf(boostData.getEndTime())),
new StorageItem("amount", boostData.getMultiplier()),
new StorageItem("uuid", boostData.getPlayer().toString()));
}
//Save to file
dataFile.saveConfig();
}
private void loadLevelManager() {
@ -462,10 +480,6 @@ public class EpicHoppersPlugin extends JavaPlugin implements EpicHoppers {
this.registerProtectionHook(hookSupplier.get());
}
public ConfigWrapper getDataFile() {
return dataFile;
}
@Override
public void registerProtectionHook(ProtectionPluginHook hook) {
Preconditions.checkNotNull(hook, "Cannot register null hook");

View File

@ -74,11 +74,11 @@ public class CommandBoost extends AbstractCommand {
@Override
public String getSyntax() {
return "/es boost <player> <multiplier> [m:minute, h:hour, d:day, y:year]";
return "/eh boost <player> <multiplier> [m:minute, h:hour, d:day, y:year]";
}
@Override
public String getDescription() {
return "This allows you to boost a players hoppers be a multiplier (Put 2 for double, 3 for triple and so on).";
return "This allows you to boost a players hoppers transfer speeds by a multiplier (Put 2 for double, 3 for triple and so on).";
}
}

View File

@ -110,9 +110,9 @@ public class HopHandler {
int amt = hopper.getLevel().getAmount() * (boostData == null ? 1 : boostData.getMultiplier());
List<ItemStack> whiteList = hopper.getFilter().getWhiteList();
List<Material> whiteList = hopper.getFilter().getWhiteList();
List<ItemStack> blackList = hopper.getFilter().getBlackList();
List<Material> blackList = hopper.getFilter().getBlackList();
int num = 0;
while (num != 5) {
@ -130,13 +130,13 @@ public class HopHandler {
if (is[num] != null
&& !whiteList.isEmpty()
&& !whiteList.contains(it)) {
&& !whiteList.contains(it.getType())) {
doBlacklist(hopperBlock, hopper, is[num].clone(), is, amt, num);
} else if (is[num] != null && !blackList.contains(it)) {
} else if (is[num] != null && !blackList.contains(it.getType())) {
int numm = addItem(hopperBlock, hopper, b2, is[num], is, amt, num);
if (numm != 10)
num = numm;
} else if (is[num] != null && blackList.contains(it)) {
} else if (is[num] != null && blackList.contains(it.getType())) {
doBlacklist(hopperBlock, hopper, is[num].clone(), is, amt, num);
}
num++;
@ -179,8 +179,8 @@ public class HopHandler {
List<Material> ovoid = new ArrayList<>();
for (ItemStack iss : hopper.getFilter().getVoidList()) {
ovoid.add(iss.getType());
for (Material iss : hopper.getFilter().getVoidList()) {
ovoid.add(iss);
}
if (is.getType() == Material.AIR) {
@ -205,7 +205,7 @@ public class HopHandler {
ih = (InventoryHolder) b2.getState();
}
if (b2.getType().equals(Material.ENDER_CHEST)) {
if (b2.getType().equals(Material.ENDER_CHEST)) {/*
try {
OfflinePlayer op = Bukkit.getOfflinePlayer(UUID.fromString(instance.getDataFile().getConfig().getString("data.enderTracker." + Arconix.pl().getApi().serialize().serializeLocation(b2))));
if (op.isOnline() && canHop(op.getPlayer().getEnderChest(), newItem, amt)) {
@ -217,7 +217,7 @@ public class HopHandler {
}
} catch (Exception ignore) {
}
} */
} else {
if (!canHop(ih.getInventory(), newItem, amt) || b2.getType() == Material.BREWING_STAND) {
return 4;

View File

@ -1,50 +1,50 @@
package com.songoda.epichoppers.hopper;
import com.songoda.epichoppers.api.hopper.Filter;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
public class EFilter implements Filter {
private List<ItemStack> whiteList = new ArrayList<>();
private List<ItemStack> blackList = new ArrayList<>();
private List<ItemStack> voidList = new ArrayList<>();
private List<Material> whiteList = new ArrayList<>();
private List<Material> blackList = new ArrayList<>();
private List<Material> voidList = new ArrayList<>();
private Block endPoint;
@Override
public List<ItemStack> getWhiteList() {
public List<Material> getWhiteList() {
if (whiteList == null) return new ArrayList<>();
return whiteList;
}
@Override
public void setWhiteList(List<ItemStack> whiteList) {
public void setWhiteList(List<Material> whiteList) {
this.whiteList = whiteList;
}
@Override
public List<ItemStack> getBlackList() {
public List<Material> getBlackList() {
if (blackList == null) return new ArrayList<>();
return blackList;
}
@Override
public void setBlackList(List<ItemStack> blackList) {
public void setBlackList(List<Material> blackList) {
this.blackList = blackList;
}
@Override
public List<ItemStack> getVoidList() {
public List<Material> getVoidList() {
if (voidList == null) return new ArrayList<>();
return voidList;
}
@Override
public void setVoidList(List<ItemStack> voidList) {
public void setVoidList(List<Material> voidList) {
this.voidList = voidList;
}

View File

@ -312,9 +312,9 @@ public class EHopper implements Hopper {
}
int num = 0;
for (ItemStack o : filter.getWhiteList()) {
if (o != null) {
i.setItem(whiteSlots[num], o);
for (Material m : filter.getWhiteList()) {
if (m != null) {
i.setItem(whiteSlots[num], new ItemStack(m));
num++;
}
}
@ -329,9 +329,9 @@ public class EHopper implements Hopper {
}
num = 0;
for (ItemStack o : filter.getBlackList()) {
if (o != null) {
i.setItem(blackSlots[num], o);
for (Material m : filter.getBlackList()) {
if (m != null) {
i.setItem(blackSlots[num], new ItemStack(m));
num++;
}
}
@ -346,9 +346,9 @@ public class EHopper implements Hopper {
}
num = 0;
for (ItemStack o : filter.getVoidList()) {
if (o != null) {
i.setItem(avoid[num], o);
for (Material m : filter.getVoidList()) {
if (m != null) {
i.setItem(avoid[num], new ItemStack(m));
num++;
}
@ -392,9 +392,9 @@ public class EHopper implements Hopper {
try {
ItemStack[] items2 = p.getOpenInventory().getTopInventory().getContents();
List<ItemStack> owhite = new ArrayList<>();
List<ItemStack> oblack = new ArrayList<>();
List<ItemStack> ovoid = new ArrayList<>();
List<Material> owhite = new ArrayList<>();
List<Material> oblack = new ArrayList<>();
List<Material> ovoid = new ArrayList<>();
int[] awhite = {0, 1, 9, 10, 18, 19};
int[] ablack = {27, 28, 36, 37, 45, 46};
@ -405,19 +405,19 @@ public class EHopper implements Hopper {
for (int aa : awhite) {
if (aa == num) {
if (items2[num] != null && !items2[num].getType().name().contains("STAINED_GLASS") && items2[num].getType() != Material.AIR)
owhite.add(items2[num]);
owhite.add(items2[num].getType());
}
}
for (int aa : ablack) {
if (aa == num) {
if (items2[num] != null && !items2[num].getType().name().contains("STAINED_GLASS") && items2[num].getType() != Material.AIR)
oblack.add(items2[num]);
oblack.add(items2[num].getType());
}
}
for (int aa : avoid) {
if (aa == num) {
if (items2[num] != null && !items2[num].getType().equals(Material.BARRIER) && items2[num].getType() != Material.AIR)
ovoid.add(items2[num]);
ovoid.add(items2[num].getType());
}
}
num++;

View File

@ -43,7 +43,7 @@ public class BlockListeners implements Listener {
try {
Player player = e.getPlayer();
if (e.getBlock().getType().equals(Material.ENDER_CHEST)) {
instance.getDataFile().getConfig().set("data.enderTracker." + Arconix.pl().getApi().serialize().serializeLocation(e.getBlock()), player.getUniqueId().toString());
//instance.getDataFile().getConfig().set("data.enderTracker." + Arconix.pl().getApi().serialize().serializeLocation(e.getBlock()), player.getUniqueId().toString());
return;
}
@ -101,7 +101,7 @@ public class BlockListeners implements Listener {
public void onBlockBreak(BlockBreakEvent event) {
try {
if (event.getBlock().getType().equals(Material.ENDER_CHEST)) {
instance.getDataFile().getConfig().set("data.enderTracker." + Arconix.pl().getApi().serialize().serializeLocation(event.getBlock()), null);
//instance.getDataFile().getConfig().set("data.enderTracker." + Arconix.pl().getApi().serialize().serializeLocation(event.getBlock()), null);
}
Block block = event.getBlock();
@ -125,18 +125,18 @@ public class BlockListeners implements Listener {
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), item);
}
for (ItemStack i : hopper.getFilter().getWhiteList()) {
if (i != null)
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), i);
for (Material m : hopper.getFilter().getWhiteList()) {
if (m != null)
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(m));
}
for (ItemStack i : hopper.getFilter().getBlackList()) {
if (i != null)
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), i);
for (Material m : hopper.getFilter().getBlackList()) {
if (m != null)
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(m));
}
for (ItemStack i : hopper.getFilter().getVoidList()) {
if (i != null)
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), i);
for (Material m : hopper.getFilter().getVoidList()) {
if (m != null)
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(m));
}
instance.getHopperManager().removeHopper(block.getLocation());

View File

@ -0,0 +1,31 @@
package com.songoda.epichoppers.storage;
import com.songoda.arconix.api.utils.ConfigWrapper;
import com.songoda.epichoppers.EpicHoppersPlugin;
import java.util.List;
public abstract class Storage {
protected final EpicHoppersPlugin instance;
protected final ConfigWrapper dataFile;
public Storage(EpicHoppersPlugin instance) {
this.instance = instance;
this.dataFile = new ConfigWrapper(instance, "", "data.yml");
this.dataFile.createNewFile("Loading Data File", "EpicHoppers Data File");
this.dataFile.getConfig().options().copyDefaults(true);
this.dataFile.saveConfig();
}
public abstract boolean containsGroup(String group);
public abstract List<StorageRow> getRowsByGroup(String group);
public abstract void clearFile();
public abstract void saveItem(String group, StorageItem... items);
public abstract void closeConnection();
}

View File

@ -0,0 +1,65 @@
package com.songoda.epichoppers.storage;
import org.bukkit.Material;
import java.util.ArrayList;
import java.util.List;
public class StorageItem {
private String key = null;
private final Object object;
public StorageItem(Object object) {
this.object = object;
}
public StorageItem(String key, Object object) {
this.key = key;
this.object = object;
}
public StorageItem(String key, List<Material> material) {
String object = "";
for (Material m : material) {
object += m.name() + ";";
}
this.key = key;
this.object = object;
}
public String getKey() {
return key;
}
public String asString() {
if (object == null) return null;
return (String)object;
}
public boolean asBoolean() {
if (object == null) return false;
return (boolean)object;
}
public int asInt() {
if (object == null) return 0;
return (int)object;
}
public Object asObject() {
return object;
}
public List<Material> asMaterialList() {
List<Material> list = new ArrayList<>();
if (object == null) return list;
String[] stack = ((String)object).split(";");
for (String item : stack) {
if (item.equals("")) continue;
list.add(Material.valueOf(item));
}
return list;
}
}

View File

@ -0,0 +1,29 @@
package com.songoda.epichoppers.storage;
import java.util.Collections;
import java.util.Map;
public class StorageRow {
private final String key;
private final Map<String, StorageItem> items;
public StorageRow(String key, Map<String, StorageItem> items) {
this.key = key;
this.items = items;
}
public String getKey() {
return key;
}
public Map<String, StorageItem> getItems() {
return Collections.unmodifiableMap(items);
}
public StorageItem get(String key) {
if (!items.containsKey(key)) return new StorageItem(null);
return items.get(key);
}
}

View File

@ -0,0 +1,116 @@
package com.songoda.epichoppers.storage.types;
import com.songoda.epichoppers.EpicHoppersPlugin;
import com.songoda.epichoppers.storage.Storage;
import com.songoda.epichoppers.storage.StorageItem;
import com.songoda.epichoppers.storage.StorageRow;
import com.songoda.epichoppers.utils.MySQLDatabase;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StorageMysql extends Storage {
private MySQLDatabase database;
public StorageMysql(EpicHoppersPlugin instance) {
super(instance);
this.database = new MySQLDatabase(instance);
}
@Override
public boolean containsGroup(String group) {
try {
DatabaseMetaData dbm = database.getConnection().getMetaData();
ResultSet rs = dbm.getTables(null, null, group, null);
if (rs.next()) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
@Override
public List<StorageRow> getRowsByGroup(String group) {
List<StorageRow> rows = new ArrayList<>();
try {
ResultSet set = database.getConnection().createStatement().executeQuery(String.format("SELECT * FROM `%s`", group));
while (set.next()) {
Map<String, StorageItem> items = new HashMap<>();
String key = set.getString(1);
for (int i = 2; i <= set.getMetaData().getColumnCount(); i++) {
if (set.getObject(i) == null || set.getObject(i) == "") continue;
StorageItem item = new StorageItem(set.getObject(i));
items.put(set.getMetaData().getColumnName(i), item);
}
StorageRow row = new StorageRow(key, items);
rows.add(row);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
@Override
public void clearFile() {
try {
database.getConnection().createStatement().execute("TRUNCATE `sync`");
database.getConnection().createStatement().execute("TRUNCATE `boosts`");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void saveItem(String group, StorageItem... items) {
try {
StringBuilder sql = new StringBuilder(String.format("INSERT INTO `%s`", group));
sql.append(" (");
for (StorageItem item : items) {
if (item == null || item.asObject() == null) continue;
sql.append(String.format("`%s`, ", item.getKey()));
}
sql = new StringBuilder(sql.substring(0, sql.length() - 2));
sql.append(")");
sql.append(" VALUES (");
for (StorageItem item : items) {
if (item == null || item.asObject() == null) continue;
sql.append(String.format("'%s', ", item.asObject().toString()));
}
sql = new StringBuilder(sql.substring(0, sql.length() - 2));
sql.append(");");
database.getConnection().createStatement().execute(sql.toString());
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void closeConnection() {
try {
database.getConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,71 @@
package com.songoda.epichoppers.storage.types;
import com.songoda.epichoppers.EpicHoppersPlugin;
import com.songoda.epichoppers.storage.Storage;
import com.songoda.epichoppers.storage.StorageItem;
import com.songoda.epichoppers.storage.StorageRow;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemorySection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StorageYaml extends Storage {
public StorageYaml(EpicHoppersPlugin instance) {
super(instance);
}
@Override
public boolean containsGroup(String group) {
return dataFile.getConfig().contains("data." + group);
}
@Override
public List<StorageRow> getRowsByGroup(String group) {
List<StorageRow> rows = new ArrayList<>();
ConfigurationSection currentSection = dataFile.getConfig().getConfigurationSection("data." + group);
for (String key : currentSection.getKeys(false)) {
Map<String, StorageItem> items = new HashMap<>();
ConfigurationSection currentSection2 = dataFile.getConfig().getConfigurationSection("data." + group + "." + key);
for (String key2 : currentSection2.getKeys(false)) {
String path = "data." + group + "." + key + "." + key2;
items.put(key2, new StorageItem(dataFile.getConfig().get(path) instanceof MemorySection
? convertToInLineList(path) : dataFile.getConfig().get(path)));
}
if (items.isEmpty()) continue;
StorageRow row = new StorageRow(key, items);
rows.add(row);
}
return rows;
}
private String convertToInLineList(String path) {
String converted = "";
for (String key : dataFile.getConfig().getConfigurationSection(path).getKeys(false)) {
converted += key + ":" + dataFile.getConfig().getInt(path + "." + key) + ";";
}
return converted;
}
@Override
public void clearFile() {
dataFile.getConfig().set("data", null);
}
@Override
public void saveItem(String group, StorageItem... items) {
for (int i = 0; i < items.length; i++) {
if (items[i] == null || items[i].asObject() == null) continue;
dataFile.getConfig().set("data." + group + "." + items[0].asString() + "." + items[i].getKey(), items[i].asObject());
}
}
@Override
public void closeConnection() {
dataFile.saveConfig();
}
}

View File

@ -0,0 +1,73 @@
package com.songoda.epichoppers.utils;
import com.songoda.epichoppers.EpicHoppersPlugin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.CompletableFuture;
/**
* Represents a MySQL database source which directly connects to, queries and
* executes statements towards the database found at the constructed location.
* Operations performed on this object are done async (with the help of the
* {@link CompletableFuture} API) and uses a connection pool as to not have to
* constantly connect to and disconnect from the database
*/
public class MySQLDatabase {
private final EpicHoppersPlugin instance;
private Connection connection;
/**
* Construct a new instance of a MySQLDatabase given the specified database
* credentials file. The file should be under the following format:
* <p>
* <code>
* host:127.0.0.1<br>
* user:database_username<br>
* password:database_password
* </code>
*
* @param instance an instance of the plugin
*/
public MySQLDatabase(EpicHoppersPlugin instance) {
this.instance = instance;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://" + instance.getConfig().getString("Database.IP") + ":" + instance.getConfig().getString("Database.Port") + "/" + instance.getConfig().getString("Database.Database Name");
this.connection = DriverManager.getConnection(url, instance.getConfig().getString("Database.Username"), instance.getConfig().getString("Database.Password"));
//ToDo: This is sloppy
connection.createStatement().execute(
"CREATE TABLE IF NOT EXISTS `sync` (\n" +
"\t`location` TEXT NULL,\n" +
"\t`level` INT NULL,\n" +
"\t`block` TEXT NULL,\n" +
"\t`placedby` TEXT NULL,\n" +
"\t`player` TEXT NULL,\n" +
"\t`teleporttrigger` TEXT NULL,\n" +
"\t`autocrafting` TEXT NULL,\n" +
"\t`whitelist` TEXT NULL,\n" +
"\t`blacklist` TEXT NULL,\n" +
"\t`void` TEXT NULL,\n" +
"\t`black` TEXT NULL\n" +
")");
connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `boosts` (\n" +
"\t`endtime` TEXT NULL,\n" +
"\t`amount` INT NULL,\n" +
"\t`uuid` TEXT NULL\n" +
")");
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Database connection failed.");
}
}
public Connection getConnection() {
return connection;
}
}

View File

@ -217,6 +217,13 @@ public class SettingsManager implements Listener {
o20("Glass-Type-2", "Interfaces.Glass Type 2", 11),
o21("Glass-Type-3", "Interfaces.Glass Type 3", 3),
DATABASE_SUPPORT("-", "Database.Activate Mysql Support", false),
DATABASE_IP("-", "Database.IP", "127.0.0.1"),
DATABASE_PORT("-", "Database.Port", 3306),
DATABASE_NAME("-", "Database.Database Name", "EpicHoppers"),
DATABASE_USERNAME("-", "Database.Username", "PUT_USERNAME_HERE"),
DATABASE_PASSWORD("-", "Database.Password", "PUT_PASSWORD_HERE"),
o22("Debug-Mode", "System.Debugger Enabled", false);
private String setting;