Skip to content

Commit

Permalink
Make /ignore,/leave persistent.
Browse files Browse the repository at this point in the history
  • Loading branch information
creatorfromhell committed Jan 9, 2019
1 parent 411590d commit ee1abaf
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 43 deletions.
58 changes: 58 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.javalite</groupId>
<artifactId>activejdbc-instrumentation</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down Expand Up @@ -91,8 +104,34 @@
<id>placeholderapi</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>

<repository>
<id>javalite-snapshots</id>
<name>JavaLite Snapshots</name>
<url>http://repo.javalite.io/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>javalite-plugin-snapshots</id>
<name>JavaLite Plugin Snapshots</name>
<url>http://repo.javalite.io/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
Expand All @@ -118,6 +157,25 @@
<version>2.9.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.javalite</groupId>
<artifactId>activejdbc</artifactId>
<version>2.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>compile</scope>
</dependency>
<!-- SLF4J is a real dickhead. -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
27 changes: 4 additions & 23 deletions src/net/tnemc/tnc/core/ChatManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.tnemc.tnc.core.common.chat.ChatEntry;
import net.tnemc.tnc.core.common.chat.ChatHandler;
import net.tnemc.tnc.core.common.chat.ChatVariable;
import net.tnemc.tnc.core.common.chat.db.IgnoredChannel;
import net.tnemc.tnc.core.common.chat.handlers.CoreHandler;
import net.tnemc.tnc.core.common.chat.variables.core.DisplayVariable;
import net.tnemc.tnc.core.common.chat.variables.core.LevelVariable;
Expand All @@ -27,7 +28,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -58,7 +58,6 @@ public class ChatManager implements Listener {
private Map<String, String> replacements = new HashMap<>();

private Map<String, String> channels = new HashMap<>();
private Map<String, HashSet<String>> ignored = new HashMap<>();

/**
* The core variables used
Expand Down Expand Up @@ -255,9 +254,10 @@ private Collection<Player> getRecipients(final Collection<Player> recipients, fi
final String permission, final String channel) {
Collection<Player> newRecipients = new HashSet<>();

TheNewChat.saveManager().open();
for(Player p : recipients) {
if(!p.hasPermission(permission)) continue;
if(ignored.containsKey(channel) && ignored.get(channel).contains(p.getUniqueId().toString())) continue;
if(IgnoredChannel.exists(p.getUniqueId(), channel)) continue;
if(radial) {
if(p.getLocation().distance(player.getLocation()) <= radius) continue;
}
Expand All @@ -267,6 +267,7 @@ private Collection<Player> getRecipients(final Collection<Player> recipients, fi
}
newRecipients.add(p);
}
TheNewChat.saveManager().close();
return newRecipients;
}

Expand Down Expand Up @@ -340,26 +341,6 @@ public void addHandler(ChatHandler handler) {
handlersMap.put(handler.getTypes().keySet(), handler.getName());
}

public void ignore(final UUID id, String channel) {
if(ignored.containsKey(channel)) {
ignored.get(channel).add(id.toString());
}
HashSet<String> ignoring = new HashSet<>();
ignoring.add(id.toString());
ignored.put(channel, ignoring);
}

public Map<String, HashSet<String>> getIgnored() {
return ignored;
}

public boolean ignoring(final UUID id, String channel) {
if(ignored.containsKey(channel)) {
return ignored.get(channel).contains(id.toString());
}
return false;
}

public LinkedHashMap<String, ChatHandler> getHandlers() {
return handlers;
}
Expand Down
30 changes: 30 additions & 0 deletions src/net/tnemc/tnc/core/TheNewChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.tnemc.tnc.core.command.IgnoreCommand;
import net.tnemc.tnc.core.command.ReloadCommand;
import net.tnemc.tnc.core.command.TNECommand;
import net.tnemc.tnc.core.common.chat.db.SaveManager;
import net.tnemc.tnc.core.common.chat.db.Version;
import net.tnemc.tnc.core.common.configuration.ConfigurationEntry;
import net.tnemc.tnc.core.common.configuration.CoreConfigNodes;
import net.tnemc.tnc.core.utils.FileMgmt;
Expand Down Expand Up @@ -45,6 +47,9 @@ public class TheNewChat extends JavaPlugin {

private File chatsFile;
private FileConfiguration chatsConfiguration;
private String version = "0.0.1.0";

private SaveManager saveManager;

public void onLoad() {
instance = this;
Expand All @@ -62,6 +67,23 @@ public void onEnable() {
initializeConfigurations();
loadConfigurations();

try {
new File(getDataFolder(), "TheNewChat.db").createNewFile();
} catch(IOException ignore) {
}

saveManager = new SaveManager();

saveManager.createTables();
saveManager.open();
if(Version.informationExists()) {
if(Version.outdated()) {
saveManager.updateTables(version);
Version.add(version);
}
}
saveManager.close();

this.manager = new ChatManager(CoreConfigNodes.CORE_GENERAL_CHAT_HANDLER.getString());
commandManager = new CommandManager();

Expand Down Expand Up @@ -162,4 +184,12 @@ public static TheNewChat instance() {
FileConfiguration getChatsConfiguration() {
return chatsConfiguration;
}

public String getVersion() {
return version;
}

public static SaveManager saveManager() {
return instance().saveManager;
}
}
44 changes: 25 additions & 19 deletions src/net/tnemc/tnc/core/command/IgnoreCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.tnemc.tnc.core.command;

import net.tnemc.tnc.core.TheNewChat;
import net.tnemc.tnc.core.common.chat.db.IgnoredChannel;
import net.tnemc.tnc.core.common.configuration.CoreConfigNodes;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

Expand Down Expand Up @@ -29,7 +31,7 @@ public String getName() {
@Override
public String[] getAliases() {
return new String[] {
"ignorec", "igc"
"ignorec", "igc", "leave"
};
}

Expand All @@ -53,27 +55,31 @@ public String[] getHelpLines() {
@Override
public boolean execute(CommandSender sender, String command, String[] arguments) {
if(arguments.length > 0) {
final UUID id = getPlayer(sender).getUniqueId();
if(plugin.getManager().getCommands().values().contains(arguments[0]) || arguments[0].equalsIgnoreCase("general")) {
final String handler = plugin.getManager().getHandler(arguments[0]);
boolean ignorable = (arguments[0].equalsIgnoreCase("general"))? CoreConfigNodes.CORE_GENERAL_CHAT_IGNORABLE.getBoolean() :
plugin.getManager().getChats().get(handler).get(arguments[0]).isIgnorable();
if(ignorable) {
if(!plugin.getManager().ignoring(id, arguments[0])) {
plugin.getManager().ignore(id, arguments[0]);
sender.sendMessage(ChatColor.GOLD + "Now ignoring channel: " + arguments[0] + ".");
return true;
} else {
plugin.getManager().getIgnored().get(arguments[0]).remove(id.toString());
Bukkit.getScheduler().runTaskAsynchronously(TheNewChat.instance(), ()->{
final UUID id = getPlayer(sender).getUniqueId();
if(plugin.getManager().getCommands().values().contains(arguments[0]) || arguments[0].equalsIgnoreCase("general")) {
final String handler = plugin.getManager().getHandler(arguments[0]);
boolean ignorable = (arguments[0].equalsIgnoreCase("general"))? CoreConfigNodes.CORE_GENERAL_CHAT_IGNORABLE.getBoolean() :
plugin.getManager().getChats().get(handler).get(arguments[0]).isIgnorable();
if(ignorable) {
TheNewChat.saveManager().open();
if(!IgnoredChannel.exists(id, arguments[0])) {
IgnoredChannel.add(id, arguments[0]);
sender.sendMessage(ChatColor.GOLD + "Now ignoring channel: " + arguments[0] + ".");
TheNewChat.saveManager().close();
return;
}
IgnoredChannel.delete(id, arguments[0]);
sender.sendMessage(ChatColor.GOLD + "No longer ignoring channel: " + arguments[0] + ".");
return true;
TheNewChat.saveManager().close();
return;
}
sender.sendMessage(ChatColor.RED + "Cannot ignore that channel.");
return;
}
sender.sendMessage(ChatColor.RED + "Cannot ignore that channel.");
return false;
}
sender.sendMessage(ChatColor.RED + "Invalid channel.");
return false;
sender.sendMessage(ChatColor.RED + "Invalid channel.");
return;
});
}
help(sender);
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/net/tnemc/tnc/core/command/TNECommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.tnemc.tnc.core.command;

import net.tnemc.tnc.core.TheNewChat;
import net.tnemc.tnk.core.utils.Message;
import net.tnemc.tnc.core.utils.Message;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down
22 changes: 22 additions & 0 deletions src/net/tnemc/tnc/core/common/chat/db/DataProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.tnemc.tnc.core.common.chat.db;

/**
* Created by creatorfromhell on 3/18/2018.
* <p>
* The New Chat Minecraft Server Plugin
* <p>
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to
* Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
*/
public interface DataProvider {

String getDriver();
Boolean dataSource();
String dataSourceURL();
String getURL(String file, String host, int port, String database);

default void preConnect(String file, String host, int port, String database) {

}
}
43 changes: 43 additions & 0 deletions src/net/tnemc/tnc/core/common/chat/db/IgnoredChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.tnemc.tnc.core.common.chat.db;

import org.javalite.activejdbc.Model;
import org.javalite.activejdbc.annotations.CompositePK;
import org.javalite.activejdbc.annotations.DbName;
import org.javalite.activejdbc.annotations.Table;

import java.util.UUID;

/**
* Created by creatorfromhell.
*
* The New VPN Blocker Minecraft Server Plugin
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
* International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/
* or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
*/
@DbName("TNC")
@Table("tnc_ignored")
@CompositePK({ "uuid", "channel" })
public class IgnoredChannel extends Model {

public static boolean add(UUID id, String channel) {
IgnoredChannel chan = (exists(id, channel))? getChannel(id, channel) : new IgnoredChannel();
chan.set("uuid", id.toString());
chan.set("channel", channel);

return chan.saveIt();
}

public static boolean exists(UUID id, String channel) {
return getChannel(id, channel) != null;
}

public static IgnoredChannel getChannel(UUID id, String channel) {
return IgnoredChannel.findFirst("uuid = ? AND channel = ?", id.toString(), channel);
}

public static void delete(UUID id, String channel) {
IgnoredChannel.delete("uuid = ? AND channel = ?", id.toString(), channel);
}
}
Loading

0 comments on commit ee1abaf

Please sign in to comment.