Skip to content

Commit

Permalink
Merge pull request #3 from tomjschwanke/dev/1.1-R
Browse files Browse the repository at this point in the history
Release 1.1-R
  • Loading branch information
tomjschwanke authored Mar 11, 2021
2 parents 4607e96 + acb12d5 commit a1ade09
Show file tree
Hide file tree
Showing 10 changed files with 516 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ To opt out, edit the `/plugins/bstats/config.yml` in your server folder.

## To-Do
- [x] Initial release
- [ ] Toggleable per player
- [x] Toggleable per player

## Screenshots
![image1](pre-send.png)
![image2](post-send.png)
![image2](post-send.png)
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.tomjschwanke.mc</groupId>
<artifactId>chuwu</artifactId>
<version>1.0-R</version>
<version>1.1-R</version>
<packaging>jar</packaging>

<name>chuwu</name>
Expand Down Expand Up @@ -83,5 +83,10 @@
<version>2.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
</dependencies>
</project>
27 changes: 26 additions & 1 deletion src/main/java/de/tomjschwanke/mc/chuwu/Chuwu.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,43 @@
import org.bstats.bukkit.Metrics;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Objects;

public final class Chuwu extends JavaPlugin {

private static Chuwu instance;

@Override
public void onEnable() {
// Instance for other classes
instance = this;

// bStats
Metrics metrics = new Metrics(this, 10396);

// Init config
ChuwuConfig config = new ChuwuConfig();
config.initConfig();

// Init database
ChuwuPlayerData playerData = new ChuwuPlayerData();
playerData.initDatabase();

// Register commands
ChuwuCommands commands = new ChuwuCommands();
Objects.requireNonNull(getCommand("chuwu")).setExecutor(commands);
Objects.requireNonNull(getCommand("chuwu")).setTabCompleter(commands);

// Register chat event
getServer().getPluginManager().registerEvents(new ChuwuEvents(), this);
getServer().getPluginManager().registerEvents(new ChuwuEventListener(), this);
}

@Override
public void onDisable() {
instance = null;
}

public static Chuwu instance() {
return instance;
}
}
294 changes: 294 additions & 0 deletions src/main/java/de/tomjschwanke/mc/chuwu/ChuwuCommands.java

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions src/main/java/de/tomjschwanke/mc/chuwu/ChuwuConfig.java
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 src/main/java/de/tomjschwanke/mc/chuwu/ChuwuEventListener.java
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"));
}
}
}
17 changes: 0 additions & 17 deletions src/main/java/de/tomjschwanke/mc/chuwu/ChuwuEvents.java

This file was deleted.

94 changes: 94 additions & 0 deletions src/main/java/de/tomjschwanke/mc/chuwu/ChuwuPlayerData.java
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;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalstate: true
playerdefault: false
31 changes: 29 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@ name: Chuwu
version: ${project.version}
main: de.tomjschwanke.mc.chuwu.Chuwu
api-version: 1.15
prefix: [ chuwu ]
prefix: chuwu
authors: [ tomjschwanke ]
description: Replaces 'r' and 'l' with '2' in chat
description: Replaces 'r' and 'l' with 'w' in chat
commands:
chuwu:
description: Manage Chuwu status
permission: chuwu.toggle
permissions:
chuwu.*:
description: All Chuwu permissions
children:
chuwu.toggle: true
chuwu.toggle.global: true
chuwu.toggle.others: true
chuwu.config: true
chuwu.toggle:
description: Toggle Chuwu for yourself
default: true
children:
chuwu.toggle.global: true
chuwu.toggle.others: true
chuwu.toggle.global:
description: Toggle Chuwu for the entire server
default: op
chuwu.toggle.others:
description: Toggle Chuwu for others
default: false
chuwu.config:
description: Configure Chuwu and reload config from disk
default: op

0 comments on commit a1ade09

Please sign in to comment.