Skip to content
This repository was archived by the owner on Dec 13, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/main/java/org/maxgamer/maxbans/MaxBansPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
import org.maxgamer.maxbans.context.PluginContext;
import org.maxgamer.maxbans.context.component.CommandExecutorComponent;
import org.maxgamer.maxbans.exception.ConfigException;
import org.maxgamer.maxbans.exception.SchemaBrokenException;
import org.maxgamer.maxbans.exception.RejectedException;
import org.maxgamer.maxbans.exception.SchemaBrokenException;
import org.maxgamer.maxbans.locale.Locale;
import org.maxgamer.maxbans.transaction.TransactionLayer;
import org.maxgamer.maxbans.util.FlywayUtil;
import org.maxgamer.maxbans.util.SentryLogger;

import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.channels.OverlappingFileLockException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -31,7 +34,7 @@
* @author Dirk Jamieson
*/
public class MaxBansPlus extends JavaPlugin {
private Locale locale = new Locale();
private Locale locale;
private PluginContext context;
private File messagesFile;
private Logger sentryLogger;
Expand Down Expand Up @@ -63,8 +66,12 @@ public void reloadConfig() {
getPluginLoader().disablePlugin(this);
return;
}

context.components().locale().load(getMessageConfiguration());
}
}

private YamlConfiguration getMessageConfiguration() {
YamlConfiguration localeConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(getResource("messages.yml")));
try {
localeConfig.load(new FileReader(messagesFile));
Expand All @@ -74,7 +81,7 @@ public void reloadConfig() {
throw new IllegalStateException("Bad YML configuration file: " + messagesFile, e);
}

locale.load(localeConfig);
return localeConfig;
}

/**
Expand Down Expand Up @@ -105,7 +112,8 @@ public void onEnable() {
sentryLogger = new SentryLogger(this, Event.Level.WARNING, client);
}

context = new PluginContext(this, config, locale, getServer(), getDataFolder(), getErrorLogger(), getServer().getPluginManager());
context = new PluginContext(this, config, getServer(), getDataFolder(), getMessageConfiguration(), getErrorLogger(), getServer().getPluginManager());
locale = context.components().locale();

try {
// Update our database if necessary
Expand Down Expand Up @@ -173,10 +181,6 @@ public PluginContext getContext() {
return context;
}

public Locale getLocale() {
return locale;
}

public Logger getErrorLogger() {
if(sentryLogger == null) {
// Just use the regular plugin logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public void restrict(CommandSender source, User user, Duration duration, String
userService.ban(banner, user, reason, duration);

MessageBuilder message = locale.get()
.with("name", user.getName())
.withUserOrConsole("name", user)
.with("reason", reason)
.with("source", banner == null ? "Console" : banner.getName())
.withUserOrConsole("source", banner)
.with("duration", TemporalDuration.of(duration));

Player player = locatorService.player(user);
if(player != null) player.kickPlayer(message.get("ban.kick"));
if(player != null) player.kickPlayer(message.get("ban.kick").toString());

broadcastService.broadcast(message.get("ban.broadcast"), silent, source, player);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.entity.Player;
import org.maxgamer.maxbans.exception.MessageException;
import org.maxgamer.maxbans.exception.RejectedException;
import org.maxgamer.maxbans.locale.Message;
import org.maxgamer.maxbans.orm.User;
import org.maxgamer.maxbans.service.HistoryService;
import org.maxgamer.maxbans.service.UserService;
Expand Down Expand Up @@ -35,7 +36,7 @@ public HistoryCommandExecutor() {
public void perform(CommandSender sender, Command command, String cmd, String[] userArgs) throws MessageException {
LinkedList<String> args = new LinkedList<>(Arrays.asList(userArgs));

List<String> messages;
List<Message> messages;

int page = 0;
if (!args.isEmpty()) {
Expand Down Expand Up @@ -73,8 +74,8 @@ public void perform(CommandSender sender, Command command, String cmd, String[]
messages = historyService.getHistory(page);
}

for (String message : messages) {
sender.sendMessage(message);
for (Message message : messages) {
message.send(sender);
}

sender.sendMessage("--- Page " + (page + 1) + " ---");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public void restrict(CommandSender source, Address address, User user, Duration
addressService.ban(banner, address, reason, duration);

MessageBuilder message = locale.get()
.with("name", user == null ? null : user.getName())
.with("name", user)
.with("address", address.getHost())
.with("reason", reason)
.with("source", banner == null ? "Console" : banner.getName())
.withUserOrConsole("source", banner)
.with("duration", TemporalDuration.of(duration));

broadcastService.broadcast(message.get("ipban.broadcast"), silent, source);

for(Player player : locatorService.players(address)) {
player.kickPlayer(message.get("ipban.kick"));
player.kickPlayer(message.get("ipban.kick").toString());
}

// Shouldn't be necessary, if everything else is working, to kick the player by retrieving them by the user object here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public void restrict(CommandSender source, Address address, User user, Duration
addressService.mute(muter, address, reason, duration);

MessageBuilder message = locale.get()
.with("name", user == null ? null : user.getName())
.with("name", user)
.with("address", address.getHost())
.with("reason", reason)
.with("source", muter == null ? "Console" : muter.getName())
.withUserOrConsole("source", muter)
.with("duration", TemporalDuration.of(duration));

broadcastService.broadcast(message.get("mute.broadcast"), silent, source, locatorService.player(user));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void perform(CommandSender sender, Command command, String s, String[] us
.with("name", player.getName())
.with("reason", reason);

player.kickPlayer(properties.get("kick.message"));
player.kickPlayer(properties.get("kick.message").toString());

broadcastService.broadcast(properties.get("kick.broadcast"), silent, sender);
metricService.increment(MetricService.KICKS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.command.CommandSender;
import org.maxgamer.maxbans.exception.PermissionException;
import org.maxgamer.maxbans.exception.RejectedException;
import org.maxgamer.maxbans.locale.Message;
import org.maxgamer.maxbans.orm.Address;
import org.maxgamer.maxbans.orm.User;
import org.maxgamer.maxbans.service.UserService;
Expand All @@ -25,21 +26,21 @@ public LookupCommandExecutor() {
@Override
public void restrict(CommandSender source, Address address, User user, Duration duration, String reason, boolean silent) throws RejectedException, PermissionException {
if(user != null) {
String message = userService
Message message = userService
.report(user, locale)
.get("iplookup.user");

source.sendMessage(message);
message.send(source);
}

// Newline to separate the reports
source.sendMessage("-----------------------------------");

// Address can't be null
String message = addressService
Message message = addressService
.report(address, locale)
.get("iplookup.ip");

source.sendMessage(message);
message.send(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public void restrict(CommandSender source, User user, Duration duration, String
userService.mute(banner, user, reason, duration);

MessageBuilder message = locale.get()
.with("name", user.getName())
.with("name", user)
.with("reason", reason)
.with("source", banner == null ? "Console" : banner.getName())
.withUserOrConsole("source", banner)
.with("duration", TemporalDuration.of(duration));

broadcastService.broadcast(message.get("mute.broadcast"), silent, source, locatorService.player(user));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public void restrict(CommandSender sender, Address address, User user, Duration
User source = (sender instanceof Player ? userService.getOrCreate((Player) sender) : null);

MessageBuilder message = locale.get()
.with("source", source == null ? "Console" : source.getName());
.withUserOrConsole("source", source);

boolean any = false;
if(user != null && userService.getBan(user) != null) {
userService.unban(source, user);
message.with("name", user.getName());
message.with("name", user);
broadcastService.broadcast(message.get("ban.unban"), silent, sender);
any = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public void restrict(CommandSender sender, Address address, User user, Duration
User source = (sender instanceof Player ? userService.getOrCreate((Player) sender) : null);

MessageBuilder message = locale.get()
.with("source", source == null ? "Console" : source.getName());
.withUserOrConsole("source", source);

boolean any = false;
if(user != null && userService.getMute(user) != null) {
userService.unmute(source, user);
message.with("name", user.getName());
message.with("name", user);
any = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public final void perform(CommandSender sender, Command command, String s, Strin
}

Duration duration = RestrictionUtil.getDuration(args);
String reason = String.join(" ", args);

String reason = null;
if (args.size() > 0) {
reason = String.join(" ", args);
}

restrict(sender, user, duration, reason, silent);
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/maxgamer/maxbans/config/PluginConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.maxgamer.maxbans.config;

import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.configuration.Configuration;
import org.maxgamer.maxbans.exception.ConfigException;
Expand All @@ -18,6 +19,7 @@ public class PluginConfig {
private Set<String> chatCommands;
private boolean errorTracking;
private boolean metrics;
private boolean tooltips;

public PluginConfig() {
setJdbcConfig(new JdbcConfig());
Expand All @@ -36,6 +38,15 @@ public void load(Configuration configuration, Server server) throws ConfigExcept
this.setChatCommands(configuration.getStringList("chat-commands"));
this.setErrorTracking(configuration.getBoolean("error-tracking", false));
this.setMetrics(configuration.getBoolean("metrics", true));

boolean wantsTooltips = configuration.getBoolean("tooltips", true);
if (wantsTooltips && !server.getVersion().contains("Spigot")) {
server.getLogger().warning("MaxBans has tooltips: true in it's configuration, but that requires Spigot for Minecraft 1.8.3 or higher");

wantsTooltips = false;
}

this.setTooltips(wantsTooltips);
}

public JdbcConfig getJdbcConfig() {
Expand All @@ -60,6 +71,15 @@ public boolean isOffline() {
return isOffline;
}

public boolean isTooltips() {
return tooltips;
}

public PluginConfig setTooltips(boolean tooltips) {
this.tooltips = tooltips;
return this;
}

public PluginConfig setOffline(boolean offline) {
isOffline = offline;
return this;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/maxgamer/maxbans/context/PluginContext.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.maxgamer.maxbans.context;

import org.bukkit.Server;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginManager;
Expand All @@ -13,7 +14,6 @@
import org.maxgamer.maxbans.context.component.DaggerPluginComponent;
import org.maxgamer.maxbans.context.component.PluginComponent;
import org.maxgamer.maxbans.context.module.PluginModule;
import org.maxgamer.maxbans.locale.Locale;

import java.io.File;
import java.util.logging.Logger;
Expand All @@ -27,15 +27,14 @@ public class PluginContext {
private File dataFolder;
private PluginComponent modules;
private PluginModule pluginModule;
public PluginContext(MaxBansPlus plugin, PluginConfig config, Locale locale, Server server, File dataFolder, Logger logger, PluginManager pluginManager) {

public PluginContext(MaxBansPlus plugin, PluginConfig config, Server server, File dataFolder, ConfigurationSection messageConfiguration, Logger logger, PluginManager pluginManager) {
this.config = config;
this.server = server;
this.dataFolder = dataFolder;

FileConfiguration lockdownCfg = YamlConfiguration.loadConfiguration(new File(dataFolder, "lockdown.yml"));

pluginModule = new PluginModule(plugin, server, config, lockdownCfg, locale, logger, pluginManager);
pluginModule = new PluginModule(plugin, server, config, lockdownCfg, messageConfiguration, logger, pluginManager);

modules = DaggerPluginComponent
.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.maxgamer.maxbans.context.module.JdbcModule;
import org.maxgamer.maxbans.context.module.PluginModule;
import org.maxgamer.maxbans.context.module.ServiceModule;
import org.maxgamer.maxbans.locale.Locale;
import org.maxgamer.maxbans.transaction.Transactor;

import javax.inject.Singleton;
Expand All @@ -31,4 +32,5 @@ public interface PluginComponent {
Logger logger();
SessionFactory sessionFactory();
PluginManager pluginManager();
Locale locale();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import dagger.Module;
import dagger.Provides;
import org.bukkit.Server;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginManager;
import org.maxgamer.maxbans.MaxBansPlus;
import org.maxgamer.maxbans.config.PluginConfig;
import org.maxgamer.maxbans.locale.Locale;
import org.maxgamer.maxbans.service.GeoIPService;

import javax.inject.Singleton;
import java.util.logging.Logger;
Expand All @@ -22,16 +24,16 @@ public class PluginModule {
private PluginConfig config;
private Server server;
private FileConfiguration configuration;
private Locale locale;
private PluginManager pluginManager;
private ConfigurationSection messageConfiguration;
private boolean sessionInitialised = false;

public PluginModule(MaxBansPlus plugin, Server server, PluginConfig config, FileConfiguration configuration, Locale locale, Logger logger, PluginManager pluginManager) {
public PluginModule(MaxBansPlus plugin, Server server, PluginConfig config, FileConfiguration configuration, ConfigurationSection messageConfiguration, Logger logger, PluginManager pluginManager) {
this.plugin = plugin;
this.config = config;
this.server = server;
this.configuration = configuration;
this.locale = locale;
this.messageConfiguration = messageConfiguration;
this.logger = logger;
this.pluginManager = pluginManager;
}
Expand Down Expand Up @@ -74,8 +76,8 @@ public FileConfiguration getConfiguration() {

@Provides
@Singleton
public Locale getLocale() {
return locale;
public Locale getLocale(GeoIPService geoIPService, PluginConfig config) {
return new Locale(geoIPService, config, messageConfiguration);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public String getMessage(Locale locale) {
// The message has a translation
MessageBuilder builder = toBuilder(locale);

return builder.get(getMessage());
return builder.get(getMessage()).toString();
} else {
// This message has no translation. Perhaps it's not defined or perhaps it's a lazy message
return super.getMessage();
Expand Down
Loading