Merge pull request #282 from Fuzzlemann/master

PR for 4.0.0 (Fuzzlemann) (12)
This commit is contained in:
Rsl1122 2017-09-01 17:12:51 +03:00 committed by GitHub
commit b04554f5fd
15 changed files with 150 additions and 45 deletions

View File

@ -87,9 +87,7 @@ public enum Settings {
HIDE_TOWNS("Plugins.Towny.HideTowns"),
//
// Bungee
BUNGEE_IP("Server.IP")
;
BUNGEE_IP("Server.IP");
private final String configPath;
private Boolean value;

View File

@ -5,6 +5,7 @@ import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.command.SubCommand;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.utilities.FormattingUtils;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Permissions;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.locale.Locale;
@ -62,9 +63,12 @@ public class SearchCommand extends SubCommand {
public void run() {
try {
List<String> names = MiscUtils.getMatchingPlayerNames(args[0]);
sender.sendMessage(Locale.get(Msg.CMD_HEADER_SEARCH) + args[0] + " (" + names.size() + ")");
boolean empty = Verify.isEmpty(names);
sender.sendMessage(Locale.get(Msg.CMD_HEADER_SEARCH) + args[0] + " (" + (empty ? 0 : names.size()) + ")");
// Results
if (names.isEmpty()) {
if (empty) {
sender.sendMessage(Locale.get(Msg.CMD_INFO_NO_RESULTS).parse(Arrays.toString(args)));
} else {
sender.sendMessage(Locale.get(Msg.CMD_INFO_RESULTS).toString() + FormattingUtils.collectionToStringNoBrackets(names));

View File

@ -191,7 +191,6 @@ public class Session {
sessionEnd == session.sessionEnd &&
mobKills == session.mobKills &&
deaths == session.deaths &&
Objects.equals(sessionID, session.sessionID) &&
Objects.equals(worldTimes, session.worldTimes) &&
Objects.equals(playerKills, session.playerKills);
}

View File

@ -53,7 +53,7 @@ public class HookHandler {
configHandler.createSection(dataSource);
}
if (configHandler.isEnabled(dataSource)) {
Log.debug("Registered a new datasource: " + StringUtils.remove(dataSource.getPlaceholder(""), '%'));
Log.debug("Registered a new datasource: " + StringUtils.remove(dataSource.getPlaceholder(), '%'));
additionalDataSources.add(dataSource);
}
} catch (Exception e) {
@ -104,9 +104,9 @@ public class HookHandler {
continue;
}
try {
addReplace.put(source.getPlaceholder(""), source.getHtmlReplaceValue("", uuid));
addReplace.put(source.getPlaceholder(), source.getHtmlReplaceValue("", uuid));
} catch (Exception e) {
addReplace.put(source.getPlaceholder(""), "Error occurred: " + e);
addReplace.put(source.getPlaceholder(), "Error occurred: " + e);
Log.error("PluginDataSource caused an exception: " + source.getSourcePlugin());
Log.toLog("PluginDataSource " + source.getSourcePlugin(), e);
}

View File

@ -27,7 +27,7 @@ public class PluginConfigSectionHandler {
}
ConfigurationSection pluginSection = section.getConfigurationSection(pluginName);
return pluginSection.contains(dataSource.getPlaceholder(""));
return pluginSection.contains(dataSource.getPlaceholder());
}
private ConfigurationSection getPluginsSection() {

View File

@ -150,13 +150,23 @@ public abstract class PluginData {
*
* @param modifier Modifier determined by AnalysisType's
* placeholderModifier-variable.
* @return for example "%StepCounter_stepsTaken_total%"
* @return for example "${StepCounter_stepsTaken_total}"
* @see AnalysisType
*/
public final String getPlaceholder(String modifier) {
return "${" + sourcePlugin + "_" + placeholder + modifier + "}";
}
/**
* Used to get the placeholder without the modifier.
*
* @return for example "${StepCounter_stepsTaken}"
* @see #getPlaceholder(String)
*/
public final String getPlaceholder() {
return "${" + sourcePlugin + "_" + placeholder + "}";
}
/**
* Used to get the source plugin's name.
*

View File

@ -4,6 +4,7 @@ import main.java.com.djrapitops.plan.utilities.MiscUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* Class that tracks the time spent in each World based on GMTimes.
@ -124,6 +125,21 @@ public class WorldTimes {
worldTimes.put(world, gmTimes);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WorldTimes that = (WorldTimes) o;
return Objects.equals(worldTimes, that.worldTimes) &&
Objects.equals(currentWorld, that.currentWorld) &&
Objects.equals(currentGamemode, that.currentGamemode);
}
@Override
public int hashCode() {
return Objects.hash(worldTimes, currentWorld, currentGamemode);
}
@Override
public String toString() {
StringBuilder b = new StringBuilder("WorldTimes (Current: " + currentWorld + "){\n");

View File

@ -126,10 +126,10 @@ public class KillsTable extends UserIDTable {
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnVictimUserID +
" WHERE " + columnKillerUserID + "=" + usersTable.statementSelectID);
statement.setString(1, uuid.toString());
set = statement.executeQuery();
commit(statement.getConnection());
while (set.next()) {
int sessionID = set.getInt(columnSessionID);

View File

@ -48,15 +48,18 @@ public class UsersTable extends UserIDTable {
}
/**
* @return @throws SQLException
* @return a {@link Set} of the saved UUIDs.
* @throws SQLException when an error at retrieving the UUIDs happens
*/
public Set<UUID> getSavedUUIDs() throws SQLException {
Set<UUID> uuids = new HashSet<>();
PreparedStatement statement = null;
ResultSet set = null;
try {
Set<UUID> uuids = new HashSet<>();
statement = prepareStatement(Select.from(tableName, columnUUID).toString());
statement.setFetchSize(2000);
set = statement.executeQuery();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString(columnUUID));
@ -70,8 +73,8 @@ public class UsersTable extends UserIDTable {
}
/**
* @param uuid
* @return
* @param uuid the UUID of the user that should be removed.
* @return if the removal was successful.
*/
@Override
public boolean removeUser(UUID uuid) {
@ -91,12 +94,15 @@ public class UsersTable extends UserIDTable {
}
/**
* @return
* @return the name of the column that inherits the ID.
*/
public String getColumnID() {
return columnID;
}
/**
* @return the name of the column that inherits the UUID.
*/
public String getColumnUUID() {
return columnUUID;
}
@ -257,6 +263,37 @@ public class UsersTable extends UserIDTable {
}
}
/**
* Gets the names of the players which names or nicknames match {@code name}.
*
* @param name the name / nickname.
* @return a list of distinct names.
* @throws SQLException when an error at fetching the names happens.
*/
public List<String> getMatchingNames(String name) throws SQLException {
String searchString = "%" + name + "%";
List<String> matchingNames = new ArrayList<>();
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(
"SELECT name FROM plan_users WHERE name LIKE LOWER(?) UNION SELECT name FROM plan_users WHERE id = (SELECT user_id FROM plan_nicknames WHERE nickname LIKE LOWER(?))"
);
statement.setString(1, searchString);
statement.setString(2, searchString);
set = statement.executeQuery();
while (set.next()) {
matchingNames.add(set.getString("name"));
}
return matchingNames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
public String getColumnName() {
return columnName;
}

View File

@ -4,6 +4,7 @@ import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.command.CommandUtils;
import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.utilities.Compatibility;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Permissions;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.IPlan;
@ -14,7 +15,11 @@ import main.java.com.djrapitops.plan.locale.Msg;
import java.io.Closeable;
import java.io.IOException;
import java.util.*;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;
/**
@ -88,14 +93,14 @@ public class MiscUtils {
* @return Alphabetically sorted list of matching player names.
*/
public static List<String> getMatchingPlayerNames(String search) {
final String searchFor = search.toLowerCase();
Database db = Plan.getInstance().getDB();
List<String> matches = new ArrayList<>();
// try {
// TODO GetMatchingPlayerNamesFromDB
// } catch (SQLException e) {
// Log.toLog("MiscUtils.getMatchingPlayerNames", e);
// }
List<String> matches;
try {
matches = db.getUsersTable().getMatchingNames(search);
} catch (SQLException e) {
Log.toLog("MiscUtils.getMatchingPlayerNames", e);
return null;
}
Collections.sort(matches);
return matches;
}

View File

@ -192,7 +192,7 @@ public class Analysis {
Log.debug("Analysis", "Additional Sources: " + sources.size());
sources.parallelStream().filter(Verify::notNull).forEach(source -> {
try {
Benchmark.start("Source " + StringUtils.remove(source.getPlaceholder(""), '%'));
Benchmark.start("Source " + StringUtils.remove(source.getPlaceholder(), '%'));
final List<AnalysisType> analysisTypes = source.getAnalysisTypes();
if (analysisTypes.isEmpty()) {
return;
@ -218,11 +218,11 @@ public class Analysis {
replaceMap.put(source.getPlaceholder(boolTot.getPlaceholderModifier()), AnalysisUtils.getBooleanTotal(boolTot, source, uuids));
}
} catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError e) {
Log.error("A PluginData-source caused an exception: " + StringUtils.remove(source.getPlaceholder(""), '%'));
Log.error("A PluginData-source caused an exception: " + StringUtils.remove(source.getPlaceholder(), '%'));
Log.toLog(this.getClass().getName(), e);
} finally {
Benchmark.stop("Analysis", "Source " + StringUtils.remove(source.getPlaceholder(""), '%'));
Benchmark.stop("Analysis", "Source " + StringUtils.remove(source.getPlaceholder(), '%'));
}
});
Benchmark.stop("Analysis", "3rd party");
@ -303,7 +303,7 @@ public class Analysis {
return value instanceof Boolean
&& (boolean) value;
} catch (Exception | NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) {
Log.toLog(pluginData.getSourcePlugin() + pluginData.getPlaceholder("") + " (Cause) ", e);
Log.toLog(pluginData.getSourcePlugin() + pluginData.getPlaceholder() + " (Cause) ", e);
return false;
}
});

View File

@ -195,7 +195,7 @@ public class AnalysisUtils {
}
private static String logPluginDataCausedError(PluginData source, Throwable e) {
String placeholder = StringUtils.remove(source.getPlaceholder(""), '%');
String placeholder = StringUtils.remove(source.getPlaceholder(), '%');
Log.error("A PluginData-source caused an exception: " + placeholder);
Log.toLog("PluginData-source caused an exception: " + placeholder, e);

View File

@ -336,7 +336,7 @@ public class HtmlStructure {
String pluginName = source.getSourcePlugin();
List<String> pluginPlaceholderList = placeholders.getOrDefault(pluginName, new ArrayList<>());
pluginPlaceholderList.add(source.getPlaceholder(""));
pluginPlaceholderList.add(source.getPlaceholder());
placeholders.put(pluginName, pluginPlaceholderList);
}

View File

@ -1,5 +1,6 @@
package main.java.com.djrapitops.plan.utilities;
import com.djrapitops.plugin.api.TimeAmount;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
@ -32,10 +33,8 @@ public class FormatUtilsTest {
@Test
public void testFormatTimeAmount() {
long ms = 1000L;
String expResult = "1s";
String result = FormatUtils.formatTimeAmount(ms);
String result = FormatUtils.formatTimeAmount(TimeAmount.SECOND.ms());
assertEquals(expResult, result);
}

View File

@ -7,19 +7,27 @@ package main.java.com.djrapitops.plan.utilities;
import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.command.bukkit.BukkitCMDSender;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.database.tables.UsersTable;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfo;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.RandomData;
import test.java.utils.TestInit;
import java.util.List;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author Rsl1122
@ -28,6 +36,9 @@ import static org.junit.Assert.assertEquals;
@PrepareForTest({JavaPlugin.class, Bukkit.class})
public class MiscUtilsTest {
private Plan plan;
private SQLDB db;
@Test
public void testGetPlayerDisplayNameArgsPerm() {
String[] args = new String[]{"Rsl1122", "Test"};
@ -86,7 +97,7 @@ public class MiscUtilsTest {
}
@Test
public void testGetPlayerDisplaynameConsole() {
public void testGetPlayerDisplayNameConsole() {
String[] args = new String[]{"TestConsoleSender"};
ISender sender = new BukkitCMDSender(MockUtils.mockConsoleSender());
@ -97,31 +108,57 @@ public class MiscUtilsTest {
}
@Test
@Ignore("DB mock")
public void testGetMatchingDisplayNames() throws Exception {
TestInit.init();
String search = "testname";
public void testGetMatchingNames() throws Exception {
setupDatabase();
String exp1 = "TestName";
String exp2 = "TestName2";
UsersTable usersTable = db.getUsersTable();
usersTable.registerUser(UUID.randomUUID(), 0L, exp1);
usersTable.registerUser(UUID.randomUUID(), 0L, exp2);
String search = "testname";
List<String> result = MiscUtils.getMatchingPlayerNames(search);
assertNotNull(result);
assertEquals(2, result.size());
assertEquals(exp1, result.get(0));
assertEquals(exp2, result.get(1));
}
@Test
@Ignore("DB mock")
public void testGetMatchingDisplayNames2() throws Exception {
TestInit.init();
public void testGetMatchingNickNames() throws Exception {
setupDatabase();
UUID uuid = UUID.randomUUID();
String userName = RandomData.randomString(10);
db.getUsersTable().registerUser(uuid, 0L, userName);
String nickname = "2" + RandomData.randomString(10);
db.getNicknamesTable().saveUserName(uuid, nickname);
String search = "2";
String exp2 = "TestName2";
List<String> result = MiscUtils.getMatchingPlayerNames(search);
assertNotNull(result);
assertEquals(1, result.size());
assertEquals(exp2, result.get(0));
assertEquals(userName, result.get(0));
}
private void setupDatabase() throws Exception {
TestInit.init();
TestInit t = TestInit.init();
plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
db.init();
db.getServerTable().saveCurrentServerInfo(new ServerInfo(-1, TestInit.getServerUUID(), "ServerName", ""));
when(plan.getDB()).thenReturn(db);
}
}