mirror of
https://github.com/RonanPlugins/BetterRTP.git
synced 2025-08-17 17:15:47 +00:00
cooldowns database + rtp loader
This commit is contained in:
parent
bba7e70e8c
commit
9499d36482
8
pom.xml
8
pom.xml
@ -7,7 +7,7 @@
|
||||
<groupId>me.SuperRonanCraft</groupId>
|
||||
<artifactId>BetterRTP</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>3.1.0-5</version>
|
||||
<version>3.1.1</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
@ -69,7 +69,7 @@
|
||||
<file>${basedir}/LocalJars/hClaims.jar</file>
|
||||
<repositoryLayout>default</repositoryLayout>
|
||||
<groupId>com.hakan</groupId>
|
||||
<artifactId>claimsystem</artifactId>
|
||||
<artifactId>claimapi</artifactId>
|
||||
<version>2.1.8</version>
|
||||
<packaging>jar</packaging>
|
||||
<generatePom>true</generatePom>
|
||||
@ -220,7 +220,7 @@
|
||||
<dependency>
|
||||
<groupId>com.bekvon.bukkit</groupId>
|
||||
<artifactId>Residence</artifactId>
|
||||
<version>LATEST</version>
|
||||
<version>4.9.1.9</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- ProtocolLib -->
|
||||
@ -261,7 +261,7 @@
|
||||
<dependency>
|
||||
<groupId>com.hakan</groupId>
|
||||
<artifactId>claimapi</artifactId>
|
||||
<version>LATEST</version>
|
||||
<version>2.1.8</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- GriefDefender (https://www.spigotmc.org/resources/griefdefender.68900/) -->
|
||||
|
@ -52,7 +52,7 @@ public class CmdPlayer implements RTPCommand, RTPCommandHelpable {
|
||||
list.add(p.getName());
|
||||
} else if (args.length == 3) {
|
||||
for (World w : Bukkit.getWorlds())
|
||||
if (w.getName().startsWith(args[2]) && !BetterRTP.getInstance().getRTP().disabledWorlds().contains(w.getName()))
|
||||
if (w.getName().startsWith(args[2]) && !BetterRTP.getInstance().getRTP().getDisabledWorlds().contains(w.getName()))
|
||||
list.add(w.getName());
|
||||
} else if (args.length > 3) {
|
||||
if (RTPCommandType.BIOME.getCmd().permission(sendi))
|
||||
|
@ -31,7 +31,7 @@ public class CmdWorld implements RTPCommand, RTPCommandHelpable {
|
||||
if (args.length == 2) {
|
||||
for (World w : Bukkit.getWorlds()) {
|
||||
String _wName = w.getName().replace(" ", "_");
|
||||
if (w.getName().startsWith(args[1]) && !BetterRTP.getInstance().getRTP().disabledWorlds().contains(_wName)
|
||||
if (w.getName().startsWith(args[1]) && !BetterRTP.getInstance().getRTP().getDisabledWorlds().contains(_wName)
|
||||
&& BetterRTP.getInstance().getPerms().getAWorld(sendi, _wName))
|
||||
list.add(_wName);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package me.SuperRonanCraft.BetterRTP.player.rtp;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import me.SuperRonanCraft.BetterRTP.references.worlds.*;
|
||||
@ -17,14 +18,14 @@ public class RTP {
|
||||
final RTPPluginValidation softDepends = new RTPPluginValidation();
|
||||
public final RTPPermissionGroup permConfig = new RTPPermissionGroup();
|
||||
//Cache
|
||||
public HashMap<String, RTPWorld> customWorlds = new HashMap<>();
|
||||
public HashMap<String, String> overriden = new HashMap<>();
|
||||
public WorldDefault defaultWorld = new WorldDefault();
|
||||
public List<String> disabledWorlds, blockList;
|
||||
public final HashMap<String, RTPWorld> customWorlds = new HashMap<>();
|
||||
public final HashMap<String, String> overriden = new HashMap<>();
|
||||
public final WorldDefault defaultWorld = new WorldDefault();
|
||||
@Getter List<String> disabledWorlds, blockList;
|
||||
int maxAttempts, delayTime;
|
||||
boolean cancelOnMove, cancelOnDamage;
|
||||
public HashMap<String, WORLD_TYPE> world_type = new HashMap<>();
|
||||
public HashMap<String, RTPWorld> worldLocations = new HashMap<>();
|
||||
public final HashMap<String, WORLD_TYPE> world_type = new HashMap<>();
|
||||
public final HashMap<String, RTPWorld> worldLocations = new HashMap<>();
|
||||
|
||||
public RTPTeleport getTeleport() {
|
||||
return teleport;
|
||||
@ -39,111 +40,19 @@ public class RTP {
|
||||
cancelOnDamage = config.getBoolean("Settings.Delay.CancelOnDamage");
|
||||
blockList = config.getStringList("BlacklistedBlocks");
|
||||
//Overrides
|
||||
loadOverrides();
|
||||
RTPLoader.loadOverrides(overriden);
|
||||
//WorldType
|
||||
loadWorldTypes();
|
||||
RTPLoader.loadWorldTypes(world_type);
|
||||
//CustomWorlds
|
||||
loadCustomWorlds();
|
||||
//Locations
|
||||
loadWorldLocations();
|
||||
RTPLoader.loadWorldLocations(worldLocations);
|
||||
teleport.load(); //Load teleporting stuff
|
||||
permConfig.load(); //Load permission configs
|
||||
}
|
||||
|
||||
private void loadOverrides() {
|
||||
overriden.clear();
|
||||
try {
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG;
|
||||
List<Map<?, ?>> override_map = config.getMapList("Overrides");
|
||||
for (Map<?, ?> m : override_map)
|
||||
for (Map.Entry<?, ?> entry : m.entrySet()) {
|
||||
overriden.put(entry.getKey().toString(), entry.getValue().toString());
|
||||
if (getPl().getSettings().debug)
|
||||
getPl().getLogger().info("- Override '" + entry.getKey() + "' -> '" + entry.getValue() + "' added");
|
||||
if (Bukkit.getWorld(entry.getValue().toString()) == null)
|
||||
getPl().getLogger().warning("The world `" + entry.getValue() + "` doesn't seem to exist! Please update `" + entry.getKey() + "'s` override! Maybe there are capital letters?");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//No Overrides
|
||||
}
|
||||
}
|
||||
|
||||
private void loadWorldTypes() {
|
||||
world_type.clear();
|
||||
try {
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG;
|
||||
for (World world : Bukkit.getWorlds())
|
||||
world_type.put(world.getName(), WORLD_TYPE.NORMAL);
|
||||
List<Map<?, ?>> world_map = config.getMapList("WorldType");
|
||||
for (Map<?, ?> m : world_map)
|
||||
for (Map.Entry<?, ?> entry : m.entrySet()) {
|
||||
if (world_type.containsKey(entry.getKey())) {
|
||||
try {
|
||||
WORLD_TYPE type = WORLD_TYPE.valueOf(entry.getValue().toString().toUpperCase());
|
||||
world_type.put(entry.getKey().toString(), type);
|
||||
} catch(IllegalArgumentException e) {
|
||||
StringBuilder valids = new StringBuilder();
|
||||
for (WORLD_TYPE type : WORLD_TYPE.values())
|
||||
valids.append(type.name()).append(", ");
|
||||
valids.replace(valids.length() - 2, valids.length(), "");
|
||||
getPl().getLogger().severe("World Type for '" + entry.getKey() + "' is INVALID '" + entry.getValue() +
|
||||
"'. Valid ID's are: " + valids.toString());
|
||||
//Wrong rtp world type
|
||||
}
|
||||
}/* else {
|
||||
if (getPl().getSettings().debug)
|
||||
getPl().getLogger().info("- World Type failed for '" + entry.getKey() + "' is it loaded?");
|
||||
}*/
|
||||
}
|
||||
if (getPl().getSettings().debug)
|
||||
for (String world : world_type.keySet())
|
||||
BetterRTP.debug("- World Type for '" + world + "' set to '" + world_type.get(world) + "'");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
//No World Types
|
||||
}
|
||||
}
|
||||
|
||||
public void loadCustomWorlds() {
|
||||
defaultWorld.setup();
|
||||
customWorlds.clear();
|
||||
try {
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG;
|
||||
List<Map<?, ?>> map = config.getMapList("CustomWorlds");
|
||||
for (Map<?, ?> m : map)
|
||||
for (Map.Entry<?, ?> entry : m.entrySet()) {
|
||||
customWorlds.put(entry.getKey().toString(), new WorldCustom(entry.getKey().toString()));
|
||||
if (getPl().getSettings().debug)
|
||||
BetterRTP.debug("- Custom World '" + entry.getKey() + "' registered");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//No Custom Worlds
|
||||
}
|
||||
}
|
||||
|
||||
public void loadWorldLocations() {
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.LOCATIONS;
|
||||
worldLocations.clear();
|
||||
if (!config.getBoolean("Enabled"))
|
||||
return;
|
||||
List<Map<?, ?>> map = config.getMapList("Locations");
|
||||
for (Map<?, ?> m : map)
|
||||
for (Map.Entry<?, ?> entry : m.entrySet()) {
|
||||
WorldLocations location = new WorldLocations(entry.getKey().toString());
|
||||
if (location.isValid()) {
|
||||
worldLocations.put(entry.getKey().toString(), location);
|
||||
if (getPl().getSettings().debug)
|
||||
BetterRTP.debug("- Location '" + entry.getKey() + "' registered");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> disabledWorlds() {
|
||||
return disabledWorlds;
|
||||
}
|
||||
|
||||
public List<String> getDisabledWorlds() {
|
||||
return disabledWorlds;
|
||||
public void loadCustomWorlds() { //Keeping this here because of the edit command
|
||||
RTPLoader.loadCustomWorlds(defaultWorld, customWorlds);
|
||||
}
|
||||
|
||||
public WorldPlayer getPlayerWorld(RTPSetupInformation setup_info) {
|
||||
@ -202,10 +111,6 @@ public class RTP {
|
||||
return pWorld;
|
||||
}
|
||||
|
||||
private BetterRTP getPl() {
|
||||
return BetterRTP.getInstance();
|
||||
}
|
||||
|
||||
public void start(RTPSetupInformation setup_info) {
|
||||
String world_name = setup_info.world;
|
||||
Player p = setup_info.player;
|
||||
@ -251,7 +156,6 @@ public class RTP {
|
||||
// Economy
|
||||
if (!getPl().getEco().hasBalance(sendi, pWorld))
|
||||
return;
|
||||
//BetterRTP.getInstance().getpInfo().setRTPType(p, rtpType);
|
||||
rtp(sendi, pWorld, setup_info.delay, setup_info.rtp_type);
|
||||
}
|
||||
|
||||
@ -270,4 +174,8 @@ public class RTP {
|
||||
rtpPlayer.randomlyTeleport(sendi);
|
||||
}
|
||||
}
|
||||
|
||||
private BetterRTP getPl() {
|
||||
return BetterRTP.getInstance();
|
||||
}
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
package me.SuperRonanCraft.BetterRTP.player.rtp;
|
||||
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import me.SuperRonanCraft.BetterRTP.references.file.FileBasics;
|
||||
import me.SuperRonanCraft.BetterRTP.references.worlds.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class RTPLoader {
|
||||
|
||||
static void loadCustomWorlds(@NotNull WorldDefault defaultWorld, @NotNull HashMap<String, RTPWorld> customWorlds) {
|
||||
defaultWorld.setup();
|
||||
customWorlds.clear();
|
||||
try {
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG;
|
||||
List<Map<?, ?>> map = config.getMapList("CustomWorlds");
|
||||
for (Map<?, ?> m : map)
|
||||
for (Map.Entry<?, ?> entry : m.entrySet()) {
|
||||
customWorlds.put(entry.getKey().toString(), new WorldCustom(entry.getKey().toString()));
|
||||
if (getPl().getSettings().debug)
|
||||
BetterRTP.debug("- Custom World '" + entry.getKey() + "' registered");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//No Custom Worlds
|
||||
}
|
||||
}
|
||||
|
||||
static void loadOverrides(@NotNull HashMap<String, String> overriden) {
|
||||
overriden.clear();
|
||||
try {
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG;
|
||||
List<Map<?, ?>> override_map = config.getMapList("Overrides");
|
||||
for (Map<?, ?> m : override_map)
|
||||
for (Map.Entry<?, ?> entry : m.entrySet()) {
|
||||
overriden.put(entry.getKey().toString(), entry.getValue().toString());
|
||||
if (getPl().getSettings().debug)
|
||||
getPl().getLogger().info("- Override '" + entry.getKey() + "' -> '" + entry.getValue() + "' added");
|
||||
if (Bukkit.getWorld(entry.getValue().toString()) == null)
|
||||
getPl().getLogger().warning("The world `" + entry.getValue() + "` doesn't seem to exist! Please update `" + entry.getKey() + "'s` override! Maybe there are capital letters?");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//No Overrides
|
||||
}
|
||||
}
|
||||
|
||||
static void loadWorldTypes(@NotNull HashMap<String, WORLD_TYPE> world_type) {
|
||||
world_type.clear();
|
||||
try {
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.CONFIG;
|
||||
for (World world : Bukkit.getWorlds())
|
||||
world_type.put(world.getName(), WORLD_TYPE.NORMAL);
|
||||
List<Map<?, ?>> world_map = config.getMapList("WorldType");
|
||||
for (Map<?, ?> m : world_map)
|
||||
for (Map.Entry<?, ?> entry : m.entrySet()) {
|
||||
if (world_type.containsKey(entry.getKey())) {
|
||||
try {
|
||||
WORLD_TYPE type = WORLD_TYPE.valueOf(entry.getValue().toString().toUpperCase());
|
||||
world_type.put(entry.getKey().toString(), type);
|
||||
} catch(IllegalArgumentException e) {
|
||||
StringBuilder valids = new StringBuilder();
|
||||
for (WORLD_TYPE type : WORLD_TYPE.values())
|
||||
valids.append(type.name()).append(", ");
|
||||
valids.replace(valids.length() - 2, valids.length(), "");
|
||||
getPl().getLogger().severe("World Type for '" + entry.getKey() + "' is INVALID '" + entry.getValue() +
|
||||
"'. Valid ID's are: " + valids.toString());
|
||||
//Wrong rtp world type
|
||||
}
|
||||
}/* else {
|
||||
if (getPl().getSettings().debug)
|
||||
getPl().getLogger().info("- World Type failed for '" + entry.getKey() + "' is it loaded?");
|
||||
}*/
|
||||
}
|
||||
if (getPl().getSettings().debug)
|
||||
for (String world : world_type.keySet())
|
||||
BetterRTP.debug("- World Type for '" + world + "' set to '" + world_type.get(world) + "'");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
//No World Types
|
||||
}
|
||||
}
|
||||
|
||||
static void loadWorldLocations(@NotNull HashMap<String, RTPWorld> worlds) {
|
||||
worlds.clear();
|
||||
FileBasics.FILETYPE config = FileBasics.FILETYPE.LOCATIONS;
|
||||
if (!config.getBoolean("Enabled"))
|
||||
return;
|
||||
List<Map<?, ?>> map = config.getMapList("Locations");
|
||||
for (Map<?, ?> m : map)
|
||||
for (Map.Entry<?, ?> entry : m.entrySet()) {
|
||||
WorldLocations location = new WorldLocations(entry.getKey().toString());
|
||||
if (location.isValid()) {
|
||||
worlds.put(entry.getKey().toString(), location);
|
||||
if (getPl().getSettings().debug)
|
||||
BetterRTP.debug("- Location '" + entry.getKey() + "' registered");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static BetterRTP getPl() {
|
||||
return BetterRTP.getInstance();
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@ import org.bukkit.block.Block;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@ -20,6 +21,8 @@ public class RTPPlayer {
|
||||
private final RTP settings;
|
||||
WorldPlayer pWorld;
|
||||
RTP_TYPE type;
|
||||
int attempts;
|
||||
List<Location> attemptedLocations = new ArrayList<>();
|
||||
|
||||
RTPPlayer(Player p, RTP settings, WorldPlayer pWorld, RTP_TYPE type) {
|
||||
this.p = p;
|
||||
@ -33,7 +36,7 @@ public class RTPPlayer {
|
||||
}
|
||||
|
||||
void randomlyTeleport(CommandSender sendi) {
|
||||
if (pWorld.getAttempts() >= settings.maxAttempts) //Cancel out, too many tries
|
||||
if (attempts >= settings.maxAttempts) //Cancel out, too many tries
|
||||
metMax(sendi, p);
|
||||
else { //Try again to find a safe location
|
||||
//Find a queue'd location
|
||||
@ -43,7 +46,8 @@ public class RTPPlayer {
|
||||
loc = event.getLocation();
|
||||
else
|
||||
loc = pWorld.generateLocation();
|
||||
//Load chunk and find out if safe location
|
||||
attempts++; //Add an attempt
|
||||
//Load chunk and find out if safe location (asynchronously)
|
||||
CompletableFuture<Chunk> chunk = PaperLib.getChunkAtAsync(loc);
|
||||
chunk.thenAccept(result -> {
|
||||
//BetterRTP.debug("Checking location for " + p.getName());
|
||||
@ -57,11 +61,11 @@ public class RTPPlayer {
|
||||
default:
|
||||
tpLoc = getLocAtNormal(loc.getBlockX(), loc.getBlockZ(), pWorld.getWorld(), yaw, pitch, pWorld);
|
||||
}
|
||||
attemptedLocations.add(loc);
|
||||
//Valid location?
|
||||
if (tpLoc != null && checkDepends(tpLoc)) {
|
||||
if (getPl().getEco().charge(p, pWorld)) {
|
||||
settings.teleport.sendPlayer(sendi, p, tpLoc, pWorld.getPrice(), pWorld.getAttempts(), type, pWorld.getWorldtype());
|
||||
}
|
||||
if (getPl().getEco().charge(p, pWorld))
|
||||
settings.teleport.sendPlayer(sendi, p, tpLoc, pWorld.getPrice(), attempts, type, pWorld.getWorldtype());
|
||||
} else
|
||||
randomlyTeleport(sendi);
|
||||
});
|
||||
|
@ -0,0 +1,225 @@
|
||||
package me.SuperRonanCraft.BetterRTP.references.database;
|
||||
|
||||
import me.ronancraft.AmethystGear.AmethystGear;
|
||||
import me.ronancraft.AmethystGear.resources.gear.ELEMENT_TYPE;
|
||||
import me.ronancraft.AmethystGear.resources.gear.GEAR_TIER;
|
||||
import me.ronancraft.AmethystGear.resources.gear.GEAR_TYPE;
|
||||
import me.ronancraft.AmethystGear.resources.gear.catalysts.Catalyst;
|
||||
import me.ronancraft.AmethystGear.resources.gear.gear.GearBaseInfo;
|
||||
import me.ronancraft.AmethystGear.resources.gear.gear.GearData;
|
||||
import me.ronancraft.AmethystGear.resources.helpers.HelperDate;
|
||||
import me.ronancraft.AmethystGear.resources.helpers.HelperJsonConverter;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class DatabaseCooldowns extends SQLite {
|
||||
|
||||
public DatabaseCooldowns() {
|
||||
super(DATABASE_TYPE.COOLDOWN);
|
||||
}
|
||||
|
||||
public enum COLUMNS {
|
||||
INDEX("id", "integer PRIMARY KEY AUTOINCREMENT"),
|
||||
UUID("owner", "varchar(32)"),
|
||||
//GEAR INFO
|
||||
GEAR_TIER("gear_tier", "text DEFAULT 'BRONZE'"),
|
||||
GEAR_LEVEL("level", "integer DEFAULT 1"),
|
||||
GEAR_XP("xp", "integer DEFAULT 0"),
|
||||
GEAR_CATALYST("catalyst", "text DEFAULT NULL"),
|
||||
GEAR_TRACKER("trackers", "text DEFAULT NULL"), //Trackers stored in json
|
||||
//BASIC GEAR INFO
|
||||
IDENTIFIER("identifier", "varchar(32)"),
|
||||
BASE_TIER("base_tier", "text DEFAULT 'BRONZE'"),
|
||||
BASE_ELEMENT("base_element", "text DEFAULT 'WATER'"),
|
||||
BASE_TYPE("base_type", "text DEFAULT 'SWORD'"),
|
||||
//MISC
|
||||
DATE_ACQUIRED("date_acquired", "text DEFAULT 'N/A'"),
|
||||
FAVORITE("favorite", "boolean DEFAULT false"),
|
||||
;
|
||||
|
||||
public String name;
|
||||
public String type;
|
||||
|
||||
COLUMNS(String name, String type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean removeGear(GearData gear) {
|
||||
String sql = "DELETE FROM " + table + " WHERE "
|
||||
+ COLUMNS.INDEX.name + " = ?";
|
||||
List<Object> params = new ArrayList<>() {{
|
||||
add(gear.getDatabase_id());
|
||||
}};
|
||||
return sqlUpdate(sql, params);
|
||||
}
|
||||
|
||||
public boolean removeGear(List<GearData> gear) {
|
||||
String sql = "DELETE FROM " + table + " WHERE "
|
||||
+ COLUMNS.INDEX.name + " IN (" + makePlaceholders(gear.size()) + ")";
|
||||
List<Object> params = new ArrayList<>();
|
||||
for (GearData gearData : gear)
|
||||
params.add(gearData.getDatabase_id());
|
||||
return sqlUpdate(sql, params);
|
||||
}
|
||||
|
||||
private static String makePlaceholders(int len) {
|
||||
StringBuilder sb = new StringBuilder(len * 2 - 1);
|
||||
sb.append("?");
|
||||
for (int i = 1; i < len; i++)
|
||||
sb.append(",?");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public List<GearData> getGear(Player p) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
List<GearData> gearList = new ArrayList<>();
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE " + COLUMNS.UUID.name + " = ?");
|
||||
ps.setString(1, p.getUniqueId().toString());
|
||||
|
||||
rs = ps.executeQuery();
|
||||
//Load all of players gear
|
||||
while (rs.next()) {
|
||||
GearBaseInfo baseInfo = new GearBaseInfo(
|
||||
rs.getString(COLUMNS.IDENTIFIER.name),
|
||||
ELEMENT_TYPE.valueOf(rs.getString( COLUMNS.BASE_ELEMENT.name)),
|
||||
GEAR_TIER.valueOf(rs.getString( COLUMNS.BASE_TIER.name)),
|
||||
GEAR_TYPE.valueOf(rs.getString( COLUMNS.BASE_TYPE.name))
|
||||
);
|
||||
GearData data = new GearData(
|
||||
baseInfo,
|
||||
rs.getInt( COLUMNS.INDEX.name),
|
||||
rs.getString( COLUMNS.DATE_ACQUIRED.name),
|
||||
GEAR_TIER.valueOf(rs.getString( COLUMNS.GEAR_TIER.name)),
|
||||
rs.getInt( COLUMNS.GEAR_LEVEL.name),
|
||||
rs.getInt( COLUMNS.GEAR_XP.name),
|
||||
HelperJsonConverter.getCatalystFromJson(rs.getString(COLUMNS.GEAR_CATALYST.name)),
|
||||
HelperJsonConverter.getTrackersFromJson(rs.getString(COLUMNS.GEAR_TRACKER.name))
|
||||
);
|
||||
data.setFavorite(rs.getBoolean(COLUMNS.FAVORITE.name));
|
||||
if (data.getGear().getIdentifier() != null)
|
||||
gearList.add(data);
|
||||
}
|
||||
return gearList;
|
||||
} catch (SQLException ex) {
|
||||
AmethystGear.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
close(ps, rs, conn);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void fixGear(Player player) {
|
||||
//This is here incase in the future someone smart breaks my database and I gotta fix it in code...
|
||||
}
|
||||
|
||||
//Create gear
|
||||
public GearData addNewGear(Player p, GearBaseInfo _gear) {
|
||||
String date = HelperDate.getDate(HelperDate.getDate());
|
||||
GearData gearData = new GearData(_gear, date);
|
||||
//Default Catalyst
|
||||
gearData.setCatalysts(List.of(new Catalyst(1, _gear.getElement())));
|
||||
|
||||
String pre = "INSERT INTO ";
|
||||
String sql = pre + table + " ("
|
||||
+ COLUMNS.UUID.name + ", "
|
||||
//GEAR INFO
|
||||
+ COLUMNS.GEAR_TIER.name + ", "
|
||||
+ COLUMNS.GEAR_LEVEL.name + ", "
|
||||
+ COLUMNS.GEAR_XP.name + ", "
|
||||
+ COLUMNS.GEAR_CATALYST.name + ", "
|
||||
+ COLUMNS.GEAR_TRACKER.name + ", "
|
||||
//BASE GEAR INFO
|
||||
+ COLUMNS.IDENTIFIER.name + ", "
|
||||
+ COLUMNS.BASE_ELEMENT.name + ", "
|
||||
+ COLUMNS.BASE_TYPE.name + ", "
|
||||
+ COLUMNS.BASE_TIER.name + ", "
|
||||
//MISC
|
||||
+ COLUMNS.DATE_ACQUIRED.name + ", "
|
||||
+ COLUMNS.FAVORITE.name + " "
|
||||
+ ") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
List<Object> params = new ArrayList<>() {{
|
||||
add(p.getUniqueId().toString());
|
||||
//GEAR INFO
|
||||
add(gearData.getTier().name());
|
||||
add(gearData.getLevel());
|
||||
add(gearData.getXp());
|
||||
add(HelperJsonConverter.getJsonFromCatalysts(gearData.getCatalysts()));
|
||||
add(HelperJsonConverter.getJsonFromTrackers(gearData.getTrackers()));
|
||||
//BASE GEAR INFO
|
||||
add(gearData.getGear().getIdentifier());
|
||||
add(gearData.getGear().getElement().name());
|
||||
add(gearData.getGear().getType().name());
|
||||
add(gearData.getGear().getTier().name());
|
||||
//MISC
|
||||
add(date);
|
||||
add(gearData.isFavorite());
|
||||
}};
|
||||
gearData.setDatabase_id(sqlGetIndex(sql, params));
|
||||
|
||||
return gearData;
|
||||
}
|
||||
|
||||
public boolean updateGear(GearData gear) {
|
||||
String sql = "UPDATE " + table + " SET "
|
||||
+ COLUMNS.GEAR_TIER.name + " = ?, "
|
||||
+ COLUMNS.GEAR_LEVEL.name + " = ?, "
|
||||
+ COLUMNS.GEAR_XP.name + " = ?, "
|
||||
+ COLUMNS.GEAR_CATALYST.name + " = ?, "
|
||||
+ COLUMNS.GEAR_TRACKER.name + " = ?, "
|
||||
+ COLUMNS.FAVORITE.name + " = ? "
|
||||
+ "WHERE "
|
||||
+ COLUMNS.INDEX.name + " = ?";
|
||||
List<Object> params = new ArrayList<>() {{
|
||||
add(gear.getTier().name());
|
||||
add(gear.getLevel());
|
||||
add(gear.getXp());
|
||||
add(HelperJsonConverter.getJsonFromCatalysts(gear.getCatalysts()));
|
||||
add(HelperJsonConverter.getJsonFromTrackers(gear.getTrackers()));
|
||||
add(gear.isFavorite());
|
||||
//Database Index
|
||||
add(gear.getDatabase_id());
|
||||
}};
|
||||
gear.setUpdated(false); //Gear has been uploaded, no need to update if nothing new has changed
|
||||
return sqlUpdate(sql, params);
|
||||
}
|
||||
|
||||
private int sqlGetIndex(String statement, List<Object> params) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
int index = -1;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement(statement, Statement.RETURN_GENERATED_KEYS);
|
||||
if (params != null) {
|
||||
Iterator<Object> it = params.iterator();
|
||||
int paramIndex = 1;
|
||||
while (it.hasNext()) {
|
||||
ps.setObject(paramIndex, it.next());
|
||||
paramIndex++;
|
||||
}
|
||||
}
|
||||
ps.executeUpdate();
|
||||
ResultSet rs = ps.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
//HelperItemGeneral.applyData(HelperData.getData(gear.item), GENERAL_DATA_INT.DATABASE_INDEX, rs.getInt(1));
|
||||
index = rs.getInt(1);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
AmethystGear.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
close(ps, null, conn);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package me.SuperRonanCraft.BetterRTP.references.database;
|
||||
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Error {
|
||||
public static void execute(BetterRTP plugin, Exception ex){
|
||||
plugin.getLogger().log(Level.SEVERE, "Couldn't execute MySQL statement: ", ex);
|
||||
}
|
||||
public static void close(BetterRTP plugin, Exception ex){
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to close MySQL connection: ", ex);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package me.SuperRonanCraft.BetterRTP.references.database;
|
||||
|
||||
public class Errors {
|
||||
public static String sqlConnectionExecute(){
|
||||
return "Couldn't execute MySQL statement: ";
|
||||
}
|
||||
public static String sqlConnectionClose(){
|
||||
return "Failed to close MySQL connection: ";
|
||||
}
|
||||
public static String noSQLConnection(){
|
||||
return "Unable to retreive MYSQL connection: ";
|
||||
}
|
||||
public static String noTableFound(){
|
||||
return "Database Error: No Table Found";
|
||||
}
|
||||
}
|
@ -0,0 +1,266 @@
|
||||
package me.SuperRonanCraft.BetterRTP.references.database;
|
||||
|
||||
import lombok.NonNull;
|
||||
import me.RonanCraft.Pueblos.resources.files.FileOther;
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.*;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class SQLite {
|
||||
|
||||
private static final String db_file_name = "database";
|
||||
//private final boolean sqlEnabled;
|
||||
String table;
|
||||
private String host, database, username, password;
|
||||
private int port;
|
||||
boolean sqlEnabled;
|
||||
//Connection connection;
|
||||
private boolean loaded;
|
||||
|
||||
public String addMissingColumns = "ALTER TABLE %table% ADD COLUMN %column% %type%";
|
||||
|
||||
private final DATABASE_TYPE type;
|
||||
|
||||
public SQLite(DATABASE_TYPE type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
// SQL creation stuff
|
||||
public Connection getSQLConnection() {
|
||||
if (sqlEnabled) {
|
||||
try {
|
||||
return getOnline();
|
||||
} catch (SQLException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
BetterRTP.getInstance().getLogger().info("MySQL setup is incorrect! Grabbing data from local database!");
|
||||
sqlEnabled = false;
|
||||
}
|
||||
}
|
||||
return getLocal();
|
||||
}
|
||||
|
||||
private Connection getOnline() throws SQLException, ClassNotFoundException {
|
||||
synchronized (this) {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
return DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database +
|
||||
"?autoReconnect=true&useSSL=false", this.username, this.password);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getLocal() {
|
||||
File dataFolder = new File(BetterRTP.getInstance().getDataFolder().getPath() + File.separator + "data", db_file_name + ".db");
|
||||
if (!dataFolder.exists()){
|
||||
try {
|
||||
dataFolder.getParentFile().mkdir();
|
||||
dataFolder.createNewFile();
|
||||
} catch (IOException e) {
|
||||
BetterRTP.getInstance().getLogger().log(Level.SEVERE, "File write error: " + dataFolder.getPath());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try {
|
||||
//if (connection != null && !connection.isClosed()) {
|
||||
// return connection;
|
||||
//}
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
return DriverManager.getConnection("jdbc:sqlite:" + dataFolder);
|
||||
//return connection;
|
||||
} catch (SQLException ex) {
|
||||
BetterRTP.getInstance().getLogger().log(Level.SEVERE, "SQLite exception on initialize", ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
BetterRTP.getInstance().getLogger().log(Level.SEVERE, "You need the SQLite JBDC library. Google it. Put it in /lib folder.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
loaded = false;
|
||||
FileOther.FILETYPE sql = FileOther.FILETYPE.MYSQL;
|
||||
String pre = "MySQL.";
|
||||
sqlEnabled = sql.getBoolean(pre + "enabled");
|
||||
host = sql.getString(pre + "host");
|
||||
port = sql.getInt(pre + "port");
|
||||
database = sql.getString(pre + "database");
|
||||
username = sql.getString(pre + "username");
|
||||
password = sql.getString(pre + "password");
|
||||
if (!sqlEnabled) { //Update table names back to default if online database fails
|
||||
switch (type) {
|
||||
case COOLDOWN: table = "BetterRTP_Cooldown"; break;
|
||||
}
|
||||
} else {
|
||||
switch (type) {
|
||||
case COOLDOWN: table = sql.getString(pre + "tablePrefix") + "Cooldown"; break;
|
||||
}
|
||||
}
|
||||
if (table == null) {
|
||||
BetterRTP.getInstance().getLogger().severe("The table for `" + type.name() + "` is invalid. Disabling the plugin!");
|
||||
Bukkit.getPluginManager().disablePlugin(BetterRTP.getInstance());
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTaskAsynchronously(BetterRTP.getInstance(), () -> {
|
||||
Connection connection = getSQLConnection();
|
||||
try {
|
||||
Statement s = connection.createStatement();
|
||||
s.executeUpdate(getCreateTable());
|
||||
//s.executeUpdate(createTable_bank);
|
||||
for (Enum<?> c : getColumns(type)) { //Add missing columns dynamically
|
||||
try {
|
||||
String _name = getColumnName(type, c);
|
||||
String _type = getColumnType(type, c);
|
||||
//System.out.println("Adding " + _name);
|
||||
s.executeUpdate(addMissingColumns.replace("%table%", table).replace("%column%", _name).replace("%type%", _type));
|
||||
} catch (SQLException e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
}
|
||||
s.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
initialize();
|
||||
loaded = true;
|
||||
//BetterRTP.getInstance().debug("Database " + type.name() + ":" + table + " configured and loaded!");
|
||||
});
|
||||
}
|
||||
|
||||
private String getCreateTable() {
|
||||
String str = "CREATE TABLE IF NOT EXISTS " + table + " (";
|
||||
Enum<?>[] columns = getColumns(type);
|
||||
for (Enum<?> c : columns) {
|
||||
String _name = getColumnName(type, c);
|
||||
String _type = getColumnType(type, c);
|
||||
str = str.concat("`" + _name + "` " + _type);
|
||||
if (c.equals(columns[columns.length - 1]))
|
||||
str = str.concat(")");
|
||||
else
|
||||
str = str.concat(", ");
|
||||
}
|
||||
//System.out.println("MySQL column string: `" + str + "`");
|
||||
return str;
|
||||
}
|
||||
|
||||
private Enum<?>[] getColumns(DATABASE_TYPE type) {
|
||||
switch (type) {
|
||||
default: return DatabaseCooldowns.COLUMNS.values();
|
||||
}
|
||||
}
|
||||
|
||||
private String getColumnName(DATABASE_TYPE type, Enum<?> column) {
|
||||
switch (type) {
|
||||
default: return ((DatabaseCooldowns.COLUMNS) column).name;
|
||||
}
|
||||
}
|
||||
|
||||
private String getColumnType(DATABASE_TYPE type, Enum<?> column) {
|
||||
switch (type) {
|
||||
default: return ((DatabaseCooldowns.COLUMNS) column).type;
|
||||
}
|
||||
}
|
||||
|
||||
//Processing
|
||||
boolean sqlUpdate(String statement, @NonNull List<Object> params) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
boolean success = true;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement(statement);
|
||||
Iterator<Object> it = params.iterator();
|
||||
int paramIndex = 1;
|
||||
while (it.hasNext()) {
|
||||
ps.setObject(paramIndex, it.next());
|
||||
paramIndex++;
|
||||
}
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
BetterRTP.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
success = false;
|
||||
} finally {
|
||||
close(ps, null, conn);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
boolean sqlUpdate(List<String> statement1, List<List<Object>> params1) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
boolean success = true;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
for (int i = 0; i < statement1.size(); i++) {
|
||||
String statement = statement1.get(i);
|
||||
List<Object> params = params1.get(i);
|
||||
if (ps == null)
|
||||
ps = conn.prepareStatement(statement);
|
||||
else
|
||||
ps.addBatch(statement);
|
||||
if (params != null) {
|
||||
Iterator<Object> it = params.iterator();
|
||||
int paramIndex = 1;
|
||||
while (it.hasNext()) {
|
||||
ps.setObject(paramIndex, it.next());
|
||||
paramIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert ps != null;
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
} catch (SQLException ex) {
|
||||
BetterRTP.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
success = false;
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
close(ps, null, conn);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
public void initialize() { //Let in console know if its all setup or not
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE " + getColumnName(type, getColumns(type)[0]) + " = 0");
|
||||
|
||||
rs = ps.executeQuery();
|
||||
} catch (SQLException ex) {
|
||||
BetterRTP.getInstance().getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex);
|
||||
} finally {
|
||||
close(ps, rs, conn);
|
||||
}
|
||||
}
|
||||
|
||||
void close(PreparedStatement ps, ResultSet rs, Connection conn) {
|
||||
try {
|
||||
if (ps != null) ps.close();
|
||||
if (conn != null) conn.close();
|
||||
if (rs != null) rs.close();
|
||||
} catch (SQLException ex) {
|
||||
Error.close(BetterRTP.getInstance(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLoaded() {
|
||||
return loaded;
|
||||
}
|
||||
|
||||
public enum DATABASE_TYPE {
|
||||
COOLDOWN
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ import java.util.Random;
|
||||
|
||||
public class WorldPlayer implements RTPWorld {
|
||||
private boolean useWorldborder;
|
||||
private int CenterX, CenterZ, maxBorderRad, minBorderRad, price, attempts;
|
||||
private int CenterX, CenterZ, maxBorderRad, minBorderRad, price;
|
||||
private List<String> Biomes;
|
||||
private final Player p;
|
||||
private final World world;
|
||||
@ -133,8 +133,6 @@ public class WorldPlayer implements RTPWorld {
|
||||
default:
|
||||
loc = generateSquare(getMaxRad(), getMinRad()); break;
|
||||
}
|
||||
|
||||
addAttempt(); //Add an attempt to keep track of the times we've attempted to generate a good location
|
||||
return loc;
|
||||
}
|
||||
|
||||
@ -217,8 +215,6 @@ public class WorldPlayer implements RTPWorld {
|
||||
return price;
|
||||
}
|
||||
|
||||
public int getAttempts() {return attempts; }
|
||||
|
||||
@Override
|
||||
public List<String> getBiomes() {
|
||||
return Biomes;
|
||||
@ -254,10 +250,6 @@ public class WorldPlayer implements RTPWorld {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public void addAttempt() {
|
||||
this.attempts++;
|
||||
}
|
||||
|
||||
//
|
||||
private void setBiomes(List<String> biomes) {
|
||||
this.Biomes = biomes;
|
||||
|
Loading…
x
Reference in New Issue
Block a user