-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from tomjschwanke/dev/1.1-R
Release 1.1-R
- Loading branch information
Showing
10 changed files
with
516 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
294 changes: 294 additions & 0 deletions
294
src/main/java/de/tomjschwanke/mc/chuwu/ChuwuCommands.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.tomjschwanke.mc.chuwu; | ||
|
||
public class ChuwuConfig { | ||
|
||
void initConfig() { | ||
Chuwu.instance().getConfig().addDefault("globalstate", true); | ||
Chuwu.instance().getConfig().addDefault("playerdefault", false); | ||
Chuwu.instance().saveDefaultConfig(); | ||
} | ||
|
||
void reloadConfig() { | ||
Chuwu.instance().reloadConfig(); | ||
} | ||
|
||
void toggleGlobalState() { | ||
setGlobalState(!getGlobalState()); | ||
} | ||
|
||
void togglePlayerDefault() { | ||
setPlayerDefault(!getPlayerDefault()); | ||
} | ||
|
||
void setGlobalState(boolean state) { | ||
Chuwu.instance().getConfig().set("globalstate", state); | ||
Chuwu.instance().saveConfig(); | ||
} | ||
|
||
void setPlayerDefault(boolean state) { | ||
Chuwu.instance().getConfig().set("playerdefault", state); | ||
Chuwu.instance().saveConfig(); | ||
} | ||
|
||
boolean getGlobalState() { | ||
return Chuwu.instance().getConfig().getBoolean("globalstate"); | ||
} | ||
|
||
boolean getPlayerDefault() { | ||
return Chuwu.instance().getConfig().getBoolean("playerdefault"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/de/tomjschwanke/mc/chuwu/ChuwuEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package de.tomjschwanke.mc.chuwu; | ||
|
||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.AsyncPlayerChatEvent; | ||
|
||
/** | ||
* ChuwuEventListener implements the EventListener to listen for player-sent chat messages, | ||
* intercept them (so the original ones will never appear in chat), | ||
* then replace the characters to uwu-ify it and | ||
* replace the original message, making the modified on appear in chat. | ||
*/ | ||
public class ChuwuEventListener implements Listener { | ||
ChuwuConfig chuwuConfig = new ChuwuConfig(); | ||
ChuwuPlayerData playerData = new ChuwuPlayerData(); | ||
// Event listener to intercept player-sent chat messages | ||
@EventHandler | ||
public void onPlayerChat(AsyncPlayerChatEvent event) { | ||
if(chuwuConfig.getGlobalState() && playerData.getPlayerState(event.getPlayer().getUniqueId().toString())) { | ||
event.setMessage(event.getMessage().replace("R", "W").replace("L", "W").replace("r", "w").replace("l", "w")); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
src/main/java/de/tomjschwanke/mc/chuwu/ChuwuPlayerData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package de.tomjschwanke.mc.chuwu; | ||
|
||
import org.h2.api.ErrorCode; | ||
|
||
import java.sql.*; | ||
import java.util.logging.Level; | ||
|
||
|
||
public class ChuwuPlayerData { | ||
|
||
ChuwuConfig chuwuConfig = new ChuwuConfig(); | ||
|
||
private static final String DB_DRIVER = "org.h2.Driver"; | ||
private static final String DB_CONNECTION = "jdbc:h2:file:" + Chuwu.instance().getDataFolder().getAbsolutePath() + "/playerstates;TRACE_LEVEL_FILE=0;TRACE_LEVEL_SYSTEM_OUT=0"; | ||
private static final String DB_USER = ""; | ||
private static final String DB_PASSWORD = ""; | ||
|
||
private static Connection getDatabaseConnection() { | ||
Connection databaseConnection = null; | ||
try { | ||
Class.forName(DB_DRIVER); | ||
} catch (ClassNotFoundException exception) { | ||
Chuwu.instance().getLogger().log(Level.SEVERE, "DB Driver not found"); | ||
Chuwu.instance().getLogger().log(Level.SEVERE, exception.getMessage()); | ||
} | ||
try { | ||
databaseConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); | ||
} catch (SQLException exception) { | ||
Chuwu.instance().getLogger().log(Level.SEVERE, "DB Connection failed"); | ||
Chuwu.instance().getLogger().log(Level.SEVERE, exception.getMessage()); | ||
} | ||
return databaseConnection; | ||
} | ||
|
||
void initDatabase() { | ||
String createTableQuery = "CREATE TABLE IF NOT EXISTS `playerstates` ( `uuid` VARCHAR(36) NOT NULL , `state` BOOLEAN NOT NULL , PRIMARY KEY (`uuid`))"; | ||
try (Connection connection = getDatabaseConnection()) { | ||
Statement createTableStatement; | ||
createTableStatement = connection.createStatement(); | ||
createTableStatement.executeUpdate(createTableQuery); | ||
} catch (SQLException exception) { | ||
if (exception.getErrorCode() == ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1) { | ||
Chuwu.instance().getLogger().log(Level.INFO, "Playerdata table already exists"); | ||
} else { | ||
Chuwu.instance().getLogger().log(Level.SEVERE, "Playerdata table creation failed"); | ||
Chuwu.instance().getLogger().log(Level.SEVERE, exception.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
void savePlayerState(String uuid, boolean state) { | ||
String insertQuery = "MERGE INTO `playerstates` (`uuid`, `state`) VALUES (?,?)"; | ||
try (Connection connection = getDatabaseConnection()) { | ||
PreparedStatement insertStatement = connection.prepareStatement(insertQuery); | ||
insertStatement.setString(1, uuid); | ||
insertStatement.setBoolean(2, state); | ||
insertStatement.executeUpdate(); | ||
} catch (SQLException exception) { | ||
Chuwu.instance().getLogger().log(Level.SEVERE, "DB insert/update failed"); | ||
Chuwu.instance().getLogger().log(Level.SEVERE, exception.getMessage()); | ||
} | ||
} | ||
|
||
void resetPlayerState(String uuid) { | ||
String deleteQuery = "DELETE FROM `playerstates` WHERE `uuid` LIKE ?"; | ||
try(Connection connection = getDatabaseConnection()) { | ||
PreparedStatement deleteStatement = connection.prepareStatement(deleteQuery); | ||
deleteStatement.setString(1, uuid); | ||
deleteStatement.executeUpdate(); | ||
}catch (SQLException exception) { | ||
Chuwu.instance().getLogger().log(Level.SEVERE, "DB delete failed"); | ||
Chuwu.instance().getLogger().log(Level.SEVERE, exception.getMessage()); | ||
} | ||
} | ||
|
||
boolean getPlayerState(String uuid) { | ||
// Set state to playerdefault as fallback if player is not in DB | ||
boolean state = chuwuConfig.getPlayerDefault(); | ||
String selectQuery = "SELECT state FROM `playerstates` WHERE `uuid` LIKE ?"; | ||
try (Connection connection = getDatabaseConnection()) { | ||
PreparedStatement selectStatement; | ||
selectStatement = connection.prepareStatement(selectQuery); | ||
selectStatement.setString(1, uuid); | ||
ResultSet resultSet = selectStatement.executeQuery(); | ||
if (resultSet.next()) { | ||
state = resultSet.getBoolean("state"); | ||
} | ||
} catch (SQLException exception) { | ||
Chuwu.instance().getLogger().log(Level.SEVERE, "DB query failed"); | ||
Chuwu.instance().getLogger().log(Level.SEVERE, exception.getMessage()); | ||
} | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
globalstate: true | ||
playerdefault: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters