Skip to content

Commit

Permalink
Merge pull request #16 from FlrQue/auto_smelting
Browse files Browse the repository at this point in the history
Release Candidate 4
Change log
- Checking if namespace for Crafting Recipe was not already reserved
- Fixed Database connection (issue #14)
- Added Drop Multiplier recovery after restart
- Displaying more info and loading database content in async thread
- Updated log messages (issue #15)
- Added boss bar displayed when player is mining stone and Drop Multiplier is active (issue #13)
- More minor tweaks...
  • Loading branch information
Florke64 authored Sep 5, 2020
2 parents 3b55e40 + 765f429 commit 318949f
Show file tree
Hide file tree
Showing 12 changed files with 485 additions and 285 deletions.
8 changes: 5 additions & 3 deletions StoneAge.iml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.15-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.25" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:19.0.0" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.3" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:3.8.1" level="project" />
<orderEntry type="library" name="Maven: org.mariadb.jdbc:mariadb-java-client:2.1.2" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-simple:1.7.22" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.22" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.3" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.21" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.11.4" level="project" />
</component>
</module>
23 changes: 14 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>win.flrque.g2p.stoneage</groupId>
<artifactId>StoneAge</artifactId>
<version>1.1-SNAPSHOT</version>
<version>1.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>StoneAge</name>
Expand Down Expand Up @@ -87,21 +87,26 @@
<version>19.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.1.2</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
</dependencies>
</project>
105 changes: 95 additions & 10 deletions src/main/java/win/flrque/g2p/stoneage/StoneAge.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
package win.flrque.g2p.stoneage;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
Expand All @@ -30,7 +35,9 @@
import win.flrque.g2p.stoneage.listener.*;
import win.flrque.g2p.stoneage.machine.ApplicableTools;
import win.flrque.g2p.stoneage.machine.StoneMachine;
import win.flrque.g2p.stoneage.util.Message;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
Expand All @@ -48,6 +55,8 @@ public final class StoneAge extends JavaPlugin {

private SQLManager sqlManager;
private BukkitRunnable autosaveRunnable;
private BukkitRunnable multiplierBossBarRunnable;
private BossBar multiplierBossBar;

@Override
public void onEnable() {
Expand Down Expand Up @@ -78,6 +87,8 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new StatisticsIncreaseListener(), this);
getServer().getPluginManager().registerEvents(new MinerLevelUpListener(), this);

getServer().getPluginManager().registerEvents(new DropMultiplierCallListener(), this);

// getServer().getPluginManager().registerEvents(new DebugGameJoin(), this);

//Registering Plugin Commands
Expand All @@ -92,6 +103,10 @@ public void onEnable() {
initAsyncAutosave(period);
if(autosaveRunnable != null)
autosaveRunnable.runTaskTimerAsynchronously(this, period * minute, period * minute);

initMultiplierBossBar();
if(multiplierBossBarRunnable != null)
multiplierBossBarRunnable.runTaskTimerAsynchronously(this, 10*20, minute/4);
}

private void initStoneMachines() {
Expand Down Expand Up @@ -182,8 +197,21 @@ public void reloadConfig() {
databaseConfig.readDatabaseConnectionDetails();
sqlManager = new SQLManager(databaseConfig);

playerSetup.loadPersonalStoneStatsFromDatabase();
playerSetup.loadPersonalDropConfigFromDatabase();
new BukkitRunnable() {
@Override
public void run() {
final long dbLoadStartTime = System.currentTimeMillis();

//Loading data of all players from database
final int statsCount = playerSetup.loadPersonalStoneStatsFromDatabase();
final int configsCount = playerSetup.loadPersonalDropConfigFromDatabase();

final long dbLoadFinishTime = System.currentTimeMillis();

getLogger().log(Level.INFO, "Loaded drop stats and config from database (in number of "+ statsCount +" and "+ configsCount + ")");
getLogger().log(Level.INFO, "Loading from database took " + (dbLoadFinishTime - dbLoadStartTime) + "ms");
}
}.runTaskAsynchronously(this);

getDropCalculator().getDropMultiplier().readPreviousMultiplierFromDatabase();
}
Expand All @@ -207,15 +235,66 @@ public void run() {
final PlayerConfig dropConfig = getPlayerSetup().getPersonalDropConfig(playerUUID);
final PlayerStats dropStats = getPlayerSetup().getPlayerStoneMachineStats(playerUUID);

getPlayerSetup().savePersonalDropConfigInDatabase(dropConfig);
getPlayerSetup().savePersonalStoneStatsInDatabase(dropStats);
try {
getPlayerSetup().savePersonalDropConfigInDatabase(dropConfig);
getPlayerSetup().savePersonalStoneStatsInDatabase(dropStats);
} catch (SQLException ex) {
ex.printStackTrace();
}

a++;
}

System.out.println("Saved " + a + " players data into the database. Next Save in " + period + " minutes");
StoneAge.this.getLogger().log(Level.INFO, "Saved " + a + " players data into the database. Next Auto-Save in " + period + " minutes");
}
};
}

private void initMultiplierBossBar() {

getLogger().log(Level.INFO, "Initialized Multiplier visualization via Boss Bar.");

final DropMultiplier multiplier = StoneAge.this.getDropCalculator().getDropMultiplier();

final NamespacedKey bossBarKey = new NamespacedKey(this, "multiplier_bossbar");
multiplierBossBar = Bukkit.createBossBar(bossBarKey, ChatColor.RED + "Go2Play", BarColor.BLUE, BarStyle.SEGMENTED_10);
multiplierBossBar.setVisible(false);

multiplierBossBarRunnable = new BukkitRunnable() {

private boolean textSwitch = false;

@Override
public void run() {
multiplierBossBar.removeAll();
if(!multiplier.isActive()) {
multiplierBossBar.setVisible(false);
return;
}

final long fullTime = ((multiplier.getMultiplierTimeout() - multiplier.getMultiplierStartTime()) / 1000) / 60;
final int leftTime = multiplier.getMinutesLeft();
final float value = multiplier.getCurrentDropMultiplier();

final double percentage = ((double) leftTime / (double) fullTime);

final Message bossBarTitle = new Message();
bossBarTitle.addLines("&6Mnoznik dropu: &7x&c$_1 &6(aktywny przez &c$_2&7min&6)");
bossBarTitle.addLines("&5Mnoznik dropu z kamienia aktywny, nie przegap okazji!");
bossBarTitle.setVariable(1, Float.toString(value));
bossBarTitle.setVariable(2, Integer.toString(leftTime));
multiplierBossBar.setTitle(bossBarTitle.getPreparedMessage().get(textSwitch? 0 : 1));

multiplierBossBar.setProgress(percentage);
multiplierBossBar.setColor(percentage < 0.2d? BarColor.RED : BarColor.BLUE);

for(final Player player : playerSetup.getMinersFromLast(60*1000)) {
multiplierBossBar.addPlayer(player);
}

multiplierBossBar.setVisible(true);
this.textSwitch = !this.textSwitch;
}
};
}

Expand Down Expand Up @@ -260,14 +339,20 @@ public FileConfiguration getConfig() {
@Override
public void onDisable() {
// Plugin shutdown logic
this.getLogger().log(Level.INFO, "onDisable()");
this.getLogger().log(Level.INFO, "Called plugin's onDisable() method. Bye cruel world!");

this.getLogger().log(Level.INFO, "closing windows");
getWindowManager().closeAllWindows(); //TODO: NullPointer Exception <- onDisable?
this.getLogger().log(Level.INFO, "Closing all Window Manager's GUIs... ");
getWindowManager().closeAllWindows();

this.getLogger().log(Level.INFO, "saving...");
this.getLogger().log(Level.INFO, "Syncing all unsaved data with the databasse...");
playerSetup.onDisable();
this.getLogger().log(Level.INFO, "closing db");

this.getLogger().log(Level.INFO, "Disconnecting database, closing connection pool...");
sqlManager.onDisable();
}

public BossBar getMultiplierBossBar() {
return multiplierBossBar;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import win.flrque.g2p.stoneage.config.DatabaseConfigReader;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionPoolManager {

Expand Down Expand Up @@ -47,11 +47,15 @@ private void init() {

private void setupPool() {
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("org.mariadb.jdbc.MariaDbDataSource");
// config.setDataSourceClassName("org.mariadb.jdbc.MariaDbDataSource");

config.setJdbcUrl("jdbc:mysql://" +hostname+ ":" +port+ "/" +database);
// config.setJdbcUrl("jdbc:mysql://" +hostname+ ":" +port+ "/" +database);
config.setJdbcUrl("jdbc:mysql://" + hostname + "/" + database + "?user=" + username + "&password=" + password + "&useUnicode=true&characterEncoding=UTF-8&verifyServerCertificate=false&useSSL=false&requireSSL=false");
config.setUsername(username);
config.setPassword(password);
config.setPoolName("StoneAgeDatabasePool");
config.setConnectionTestQuery("SELECT 1;");
config.addDataSourceProperty("autoReconnect", true);

try {
dataSource = new HikariDataSource(config);
Expand All @@ -62,13 +66,13 @@ private void setupPool() {
}

public Connection getConnection() throws SQLException {
return dataSource != null? dataSource.getConnection() : null;
return (dataSource != null) ? dataSource.getConnection() : null;
}

public void close(Connection conn, PreparedStatement ps, ResultSet res) {
if (conn != null) try { conn.close(); } catch (SQLException ignored) {}
if (ps != null) try { ps.close(); } catch (SQLException ignored) {}
if (res != null) try { res.close(); } catch (SQLException ignored) {}
public void close(Connection connection, Statement statement, ResultSet resultSet) {
if (connection != null) try { connection.close(); } catch (SQLException ignored) {}
if (statement != null) try { statement.close(); } catch (SQLException ignored) {}
if (resultSet != null) try { resultSet.close(); } catch (SQLException ignored) {}
}

public void closePool() {
Expand Down
Loading

0 comments on commit 318949f

Please sign in to comment.