Version 1.0.0 - Added to server for testing
This commit is contained in:
parent
a5e6e3d818
commit
f9204b2abe
30
pom.xml
30
pom.xml
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>com.alttd</groupId>
|
||||
<artifactId>AltitudeTag</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>1.0.0</version>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
|
@ -58,6 +58,12 @@
|
|||
</repositories>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
|
|
@ -68,6 +74,28 @@
|
|||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<artifactSet>
|
||||
<includes>
|
||||
<include>org.mariadb.jdbc</include>
|
||||
</includes>
|
||||
</artifactSet>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.alttd.altitudetag;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
|
@ -12,6 +13,7 @@ import com.alttd.altitudetag.listeners.ConnectionListener;
|
|||
import com.alttd.altitudetag.listeners.InteractListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.boss.BossBar;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class AltitudeTag extends JavaPlugin
|
||||
|
|
@ -37,7 +39,7 @@ public class AltitudeTag extends JavaPlugin
|
|||
return;
|
||||
}
|
||||
|
||||
Lang.update();
|
||||
reloadConfiguration();
|
||||
|
||||
// update the CommandLang values
|
||||
CommandLang.NO_PERMISSION.setValue(Lang.NO_PERMS.getRawMessage()[0]);
|
||||
|
|
@ -46,10 +48,21 @@ public class AltitudeTag extends JavaPlugin
|
|||
CommandLang.ONLY_PLAYERS.setValue(Lang.ONLY_PLAYERS.getRawMessage()[0]);
|
||||
CommandLang.USAGE_FORMAT.setValue(Lang.USAGE.getRawMessage()[0]);
|
||||
|
||||
Config.update();
|
||||
TagConnection.initialize();
|
||||
try
|
||||
{
|
||||
TagConnection.initialize();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
getLogger().severe("*** Could not connect to the database. ***");
|
||||
getLogger().severe("*** This plugin will be disabled. ***");
|
||||
this.setEnabled(false);
|
||||
return;
|
||||
}
|
||||
Leaderboard.initialize();
|
||||
|
||||
NotificationHandler.loadBossBar();
|
||||
|
||||
CommandHandler.initialize();
|
||||
CommandHandler.getInstance().registerCommand(new TagCommand(), this);
|
||||
|
||||
|
|
@ -57,6 +70,25 @@ public class AltitudeTag extends JavaPlugin
|
|||
Bukkit.getPluginManager().registerEvents(new InteractListener(), this);
|
||||
}
|
||||
|
||||
private static void reloadConfiguration()
|
||||
{
|
||||
// check lang
|
||||
File langFile = new File(instance.getDataFolder(), "lang.yml");
|
||||
if (!langFile.exists())
|
||||
{
|
||||
instance.saveResource("lang.yml", false);
|
||||
}
|
||||
Lang.update();
|
||||
|
||||
// check config
|
||||
File configFile = new File(instance.getDataFolder(), "config.yml");
|
||||
if (!configFile.exists())
|
||||
{
|
||||
instance.saveDefaultConfig();
|
||||
}
|
||||
Config.update();
|
||||
}
|
||||
|
||||
public static BossBar getBossBar()
|
||||
{
|
||||
return instance.bossBar;
|
||||
|
|
@ -67,24 +99,6 @@ public class AltitudeTag extends JavaPlugin
|
|||
instance.bossBar = bossBar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current tagger.
|
||||
*
|
||||
* @param tagger the new tagger.
|
||||
*
|
||||
* @return the previous tagger.
|
||||
*/
|
||||
public static UUID setTagger(UUID tagger)
|
||||
{
|
||||
UUID prev = instance.tagger;
|
||||
instance.tagger = tagger;
|
||||
|
||||
// announce that a new person is it
|
||||
Bukkit.getOnlinePlayers().stream().filter(player -> !player.getUniqueId().equals(tagger)).forEach(player -> player.sendMessage());
|
||||
|
||||
return prev;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current tagger.
|
||||
*
|
||||
|
|
@ -95,6 +109,25 @@ public class AltitudeTag extends JavaPlugin
|
|||
return instance.tagger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current tagger.
|
||||
*
|
||||
* @param tagger the new tagger.
|
||||
*/
|
||||
public static void setTagger(UUID tagger)
|
||||
{
|
||||
// announce that a new person is it
|
||||
Player previousPlayer = Bukkit.getPlayer(instance.tagger);
|
||||
|
||||
if (tagger != null)
|
||||
{
|
||||
NotificationHandler.sendGlobalNotifications(previousPlayer != null
|
||||
? previousPlayer.getName()
|
||||
: null, Bukkit.getPlayer(tagger).getName());
|
||||
}
|
||||
instance.tagger = tagger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a tag for the given player.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ public class Leaderboard
|
|||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
initializeLeaderboard();
|
||||
Bukkit.getScheduler().runTaskLater(AltitudeTag.getInstance(), Leaderboard::initializeLeaderboard, 20);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -99,9 +99,9 @@ public class Leaderboard
|
|||
{
|
||||
ps.setString(1, uuid.toString());
|
||||
|
||||
ResultSet rs = ps.getResultSet();
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
if (rs.next())
|
||||
if (rs != null && rs.next())
|
||||
{
|
||||
// call the consumer when the query returns back
|
||||
consumer.accept(rs.getInt(1));
|
||||
|
|
@ -134,8 +134,8 @@ public class Leaderboard
|
|||
config.set("leaderboard.location.z", Config.LEADERBOARD_LOCATION_Z.getValue());
|
||||
AltitudeTag.getInstance().saveConfig();
|
||||
|
||||
hologram.delete();
|
||||
initializeLeaderboard();
|
||||
hologram.teleport(location);
|
||||
refreshLeaderboard();
|
||||
}
|
||||
|
||||
private static void initializeLeaderboard()
|
||||
|
|
@ -160,8 +160,11 @@ public class Leaderboard
|
|||
|
||||
private static void refreshLeaderboard()
|
||||
{
|
||||
if (Config.LEADERBOARD_ENABLED.getValue())
|
||||
if (!Config.LEADERBOARD_ENABLED.getValue())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Objects.requireNonNull(hologram);
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(AltitudeTag.getInstance(), () ->
|
||||
|
|
@ -170,22 +173,26 @@ public class Leaderboard
|
|||
try (PreparedStatement ps = TagConnection.getConnection().prepareStatement(sql))
|
||||
{
|
||||
ps.setInt(1, Config.LEADERBOARD_TOP.getValue());
|
||||
ResultSet rs = ps.getResultSet();
|
||||
ResultSet rs = ps.executeQuery();
|
||||
for (int i = 0; i < Config.LEADERBOARD_TOP.getValue(); i++)
|
||||
{
|
||||
final int finalInt = i;
|
||||
String text;
|
||||
if (rs.next())
|
||||
if (rs != null && rs.next())
|
||||
{
|
||||
text = Lang.renderString(Config.LEADERBOARD_FORMAT.getValue(),
|
||||
"{rank}", i,
|
||||
"{player}", Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("player_uuid"))),
|
||||
"{player}", Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("player_uuid"))).getName(),
|
||||
"{tags}", rs.getInt("player_tags"));
|
||||
}
|
||||
else
|
||||
{
|
||||
text = "";
|
||||
}
|
||||
((TextLine) hologram.getLine(i)).setText(text);
|
||||
if (!((TextLine) hologram.getLine(finalInt + 1)).getText().equals(text))
|
||||
{
|
||||
Bukkit.getScheduler().runTask(AltitudeTag.getInstance(), () -> ((TextLine) hologram.getLine(finalInt + 1)).setText(text));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException ex)
|
||||
|
|
@ -193,6 +200,6 @@ public class Leaderboard
|
|||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,18 +68,26 @@ public class NotificationHandler
|
|||
{
|
||||
if (attacker == null && Config.NOTIFICATION_GLOBAL_CHAT_OTHER_REASON_ENABLED.getValue())
|
||||
{
|
||||
// TODO make this actually do what it's supposed to
|
||||
Bukkit.getOnlinePlayers().forEach(p -> p.sendMessage(Lang.renderString(Config.NOTIFICATION_GLOBAL_CHAT_OTHER_REASON_MESSAGE.getValue(),
|
||||
"{victim}", victim)));
|
||||
"{target}", victim)));
|
||||
}
|
||||
if (attacker != null && Config.NOTIFICATION_GLOBAL_CHAT_AFTER_TAG_ENABLED.getValue())
|
||||
{
|
||||
Bukkit.getOnlinePlayers().forEach(p -> p.sendMessage(Lang.renderString(Config.NOTIFICATION_GLOBAL_CHAT_AFTER_TAG_MESSAGE.getValue(),
|
||||
"{victim}", victim,
|
||||
"{target}", victim,
|
||||
"{attacker}", attacker)));
|
||||
}
|
||||
|
||||
if (Config.NOTIFICATION_GLOBAL_BOSS_BAR_ENABLED.getValue())
|
||||
{
|
||||
AltitudeTag.getBossBar().setTitle(Lang.renderString(Config.NOTIFICATION_GLOBAL_BOSS_BAR_MESSAGE.getValue(), "{player}", victim,
|
||||
BarUtils.parseBarColor(Config.NOTIFICATION_GLOBAL_BOSS_BAR_COLOR.getValue()),
|
||||
BarUtils.parseBarStyle(Config.NOTIFICATION_GLOBAL_BOSS_BAR_SEGMENTS.getValue())));
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendBossBar()
|
||||
public static void loadBossBar()
|
||||
{
|
||||
if (Config.NOTIFICATION_GLOBAL_BOSS_BAR_ENABLED.getValue())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@ package com.alttd.altitudetag;
|
|||
public enum Permission
|
||||
{
|
||||
COMMAND_ADMIN("tag.commands.admin"),
|
||||
COMMAND_ADMIN_LOCATION("tag.commands.admin.location"),
|
||||
QUEUE_COMMAND("altiqueue.queue-command");
|
||||
COMMAND_ADMIN_LOCATION("tag.commands.admin.location");
|
||||
|
||||
private String permission;
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public class TagConnection
|
|||
private int port;
|
||||
private String description;
|
||||
|
||||
private TagConnection()
|
||||
private TagConnection() throws SQLException, ClassNotFoundException
|
||||
{
|
||||
this.host = Config.DATABASE_HOSTNAME.getValue();
|
||||
this.database = Config.DATABASE_DATABASE.getValue();
|
||||
|
|
@ -28,14 +28,9 @@ public class TagConnection
|
|||
this.port = Config.DATABASE_PORT.getValue();
|
||||
this.description = Config.DATABASE_DESCRIPTION.getValue();
|
||||
|
||||
try
|
||||
{
|
||||
instance.openConnection();
|
||||
}
|
||||
catch (SQLException | ClassNotFoundException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
instance = this;
|
||||
|
||||
instance.openConnection();
|
||||
}
|
||||
|
||||
private void openConnection() throws SQLException, ClassNotFoundException
|
||||
|
|
@ -70,7 +65,7 @@ public class TagConnection
|
|||
return instance.connection;
|
||||
}
|
||||
|
||||
public static void initialize()
|
||||
public static void initialize() throws SQLException, ClassNotFoundException
|
||||
{
|
||||
instance = new TagConnection();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ public class TagCommand extends ValidBaseCommand
|
|||
{
|
||||
public TagCommand()
|
||||
{
|
||||
super("tag", "Tag, you're it!", new String[]{ "tags", "tagging", "tagger" });
|
||||
super("tag", "Tag, you're it!", "tag.play", new String[]{ "tags", "tagging", "tagger" });
|
||||
|
||||
addSubCommand(new TagAdminCommand());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
package com.alttd.altitudetag.commands.parsers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alttd.altitudeapi.commands.CommandHandler;
|
||||
import com.alttd.altitudeapi.commands.Parser;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class StringParser implements Parser<String>
|
||||
{
|
||||
@Override
|
||||
public String parseArgument(CommandSender sender, String[] label, String rawArgument)
|
||||
{
|
||||
return rawArgument;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getRecommendations(CommandSender sender, String lastWord)
|
||||
{
|
||||
return CommandHandler.defaultTabComplete(sender, lastWord);
|
||||
}
|
||||
}
|
||||
|
|
@ -20,8 +20,10 @@ public class ConnectionListener implements Listener
|
|||
if (AltitudeTag.getTagger() == null)
|
||||
{
|
||||
AltitudeTag.setTagger(event.getPlayer().getUniqueId());
|
||||
|
||||
NotificationHandler.sendVictimTitle(event.getPlayer(), true);
|
||||
}
|
||||
if (AltitudeTag.getBossBar() != null)
|
||||
{
|
||||
AltitudeTag.getBossBar().addPlayer(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,24 +15,26 @@ public class InteractListener implements Listener
|
|||
if (event.getDamager() instanceof Player && event.getEntity() instanceof Player)
|
||||
{
|
||||
final Player tagger = (Player) event.getDamager();
|
||||
if (!AltitudeTag.getTagger().equals(tagger.getUniqueId()))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
final Player tagged = (Player) event.getEntity();
|
||||
|
||||
// add the new tag
|
||||
AltitudeTag.addTag(tagger.getUniqueId(), () ->
|
||||
{
|
||||
// if they left, we can stop
|
||||
if (!tagger.isOnline())
|
||||
if (tagger.isOnline())
|
||||
{
|
||||
return;
|
||||
NotificationHandler.sendTaggerNotifications(tagger, tagged.getName());
|
||||
}
|
||||
NotificationHandler.sendTaggerNotifications(tagger, tagged.getName());
|
||||
});
|
||||
|
||||
AltitudeTag.setTagger(tagged.getUniqueId());
|
||||
|
||||
NotificationHandler.sendVictimTitle(tagged, false);
|
||||
|
||||
NotificationHandler.sendGlobalNotifications(tagger.getName(), tagged.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,56 +0,0 @@
|
|||
package com.alttd.altiqueue.configuration;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConfigTest
|
||||
{
|
||||
private List<String> updateStrings = new ArrayList<>();
|
||||
|
||||
private List<String> declarationStrings = new ArrayList<>();
|
||||
|
||||
@Test
|
||||
public void test_defaults()
|
||||
{
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(new File("src/main/resources/config.yml"));
|
||||
|
||||
printChildren(config);
|
||||
|
||||
//declarationStrings.forEach(System.out::println);
|
||||
//updateStrings.forEach(System.out::println);
|
||||
}
|
||||
|
||||
private void printChildren(ConfigurationSection config)
|
||||
{
|
||||
for (String key : config.getKeys(false))
|
||||
{
|
||||
if (config.isConfigurationSection(key))
|
||||
{
|
||||
printChildren(Objects.requireNonNull(config.getConfigurationSection(key)));
|
||||
}
|
||||
else
|
||||
{
|
||||
String path = config.getCurrentPath() + "." + key;
|
||||
String value = config.isString(key) ? "\"" + config.get(key) + "\"" : Objects.requireNonNull(config.get(key)).toString();
|
||||
String type = config.isBoolean(key) ? "Boolean"
|
||||
: config.isDouble(key)
|
||||
? "Double"
|
||||
: config.isInt(key)
|
||||
? "Integer"
|
||||
: "String";
|
||||
String enumTitle = path.replace(".", "_").replace("-", "_").toUpperCase();
|
||||
|
||||
updateStrings.add(String.format("updateValue(config, save, \"%s\", %s);", path, enumTitle));
|
||||
declarationStrings.add(String.format("public static final MutableValue<%s> %s = new MutableValue<>(%s);", type, enumTitle, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
package com.alttd.altiqueue.configuration;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import com.alttd.altitudetag.configuration.Lang;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LangTest
|
||||
{
|
||||
private FileConfiguration config;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
File file = new File("src/main/resources/lang.yml");
|
||||
|
||||
config = YamlConfiguration.loadConfiguration(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_file_contains_options()
|
||||
{
|
||||
for (Lang lang : Lang.values())
|
||||
{
|
||||
//System.out.println(lang.getPath() + ": \"" + lang.getRawMessageCompiled() + "\"");
|
||||
}
|
||||
for (Lang lang : Lang.values())
|
||||
{
|
||||
if (!config.contains(lang.getPath()))
|
||||
{
|
||||
Assert.fail("Value missing from lang.yml: " + lang.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_defaults_match()
|
||||
{
|
||||
for (Lang lang : Lang.values())
|
||||
{
|
||||
if (!config.getString(lang.getPath()).equals(lang.getRawMessageCompiled()))
|
||||
{
|
||||
Assert.fail("Lang values don't match: " + lang.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,158 +0,0 @@
|
|||
package com.alttd.altiqueue.listeners;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.alttd.altitudetag.AltitudeTag;
|
||||
import com.alttd.altitudetag.NotificationHandler;
|
||||
import com.alttd.altitudetag.listeners.ConnectionListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.powermock.api.mockito.PowerMockito.doReturn;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ AltitudeTag.class, Bukkit.class, NotificationHandler.class })
|
||||
public class ConnectionListenerTest
|
||||
{
|
||||
private ConnectionListener listener;
|
||||
|
||||
private PlayerJoinEvent joinEvent;
|
||||
|
||||
private PlayerQuitEvent quitEvent;
|
||||
|
||||
private Player player;
|
||||
private Player differentPlayer;
|
||||
|
||||
private UUID playerUuid;
|
||||
private UUID differentUuid;
|
||||
|
||||
private List<Player> players;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
// create the uuids used
|
||||
differentUuid = UUID.fromString("00000000-0000-0000-0000-000000000001");
|
||||
playerUuid = UUID.fromString("00000000-0000-0000-0000-000000000000");
|
||||
|
||||
listener = new ConnectionListener();
|
||||
|
||||
// set up the base player
|
||||
player = mock(Player.class);
|
||||
when(player.getUniqueId()).thenReturn(playerUuid);
|
||||
|
||||
// set up the other player
|
||||
differentPlayer = mock(Player.class);
|
||||
when(differentPlayer.getUniqueId()).thenReturn(differentUuid);
|
||||
|
||||
// create the list of players
|
||||
players = new ArrayList<>();
|
||||
players.add(player);
|
||||
players.add(differentPlayer);
|
||||
|
||||
// Bukkit is mine now!
|
||||
mockStatic(Bukkit.class);
|
||||
|
||||
// AltitudeTag is... still mine!
|
||||
mockStatic(AltitudeTag.class);
|
||||
when(AltitudeTag.setTagger(any())).thenCallRealMethod();
|
||||
|
||||
// NotificationHandler is... ah you get the point
|
||||
mockStatic(NotificationHandler.class);
|
||||
|
||||
// do return players when Bukkit.getOnlinePlayers() is called
|
||||
doReturn(players).when(Bukkit.class);
|
||||
Bukkit.getOnlinePlayers();
|
||||
|
||||
// set the instance of AltitudeTag
|
||||
Whitebox.setInternalState(AltitudeTag.class, "instance", mock(AltitudeTag.class));
|
||||
|
||||
joinEvent = new PlayerJoinEvent(player, "");
|
||||
quitEvent = new PlayerQuitEvent(player, "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_join_no_tagger()
|
||||
{
|
||||
// test when there is no tagger
|
||||
when(AltitudeTag.getTagger()).thenReturn(null).thenCallRealMethod();
|
||||
|
||||
listener.onJoin(joinEvent);
|
||||
|
||||
assertSame(playerUuid, AltitudeTag.getTagger());
|
||||
verifyStatic(NotificationHandler.class, times(1));
|
||||
NotificationHandler.sendVictimTitle(player, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_join_active_tagger()
|
||||
{
|
||||
// test when there is a tagger
|
||||
when(AltitudeTag.getTagger()).thenReturn(differentUuid);
|
||||
|
||||
listener.onJoin(joinEvent);
|
||||
|
||||
verify(player, never()).sendMessage(any(String.class));
|
||||
verify(player, never()).sendMessage(any(String[].class));
|
||||
assertNotSame(playerUuid, AltitudeTag.getTagger());
|
||||
verifyStatic(Bukkit.class, never());
|
||||
Bukkit.getOnlinePlayers();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_quit_not_tagger()
|
||||
{
|
||||
// test when they leave and aren't the tagger
|
||||
when(AltitudeTag.getTagger()).thenReturn(differentUuid);
|
||||
|
||||
listener.onLeave(quitEvent);
|
||||
|
||||
verifyStatic(Bukkit.class, never());
|
||||
Bukkit.getOnlinePlayers();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_quit_active_tagger_no_others()
|
||||
{
|
||||
// set the tagger to be the event player
|
||||
when(AltitudeTag.getTagger()).thenReturn(playerUuid);
|
||||
// make the event player be the only one online
|
||||
players.remove(differentPlayer);
|
||||
|
||||
listener.onLeave(quitEvent);
|
||||
|
||||
verifyStatic(AltitudeTag.class, times(1));
|
||||
AltitudeTag.setTagger(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_quit_active_tagger_are_others()
|
||||
{
|
||||
when(AltitudeTag.getTagger()).thenReturn(playerUuid);
|
||||
|
||||
listener.onLeave(quitEvent);
|
||||
|
||||
verifyStatic(AltitudeTag.class, times(1));
|
||||
AltitudeTag.setTagger(differentUuid);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,216 +0,0 @@
|
|||
package com.alttd.altiqueue.listeners;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.alttd.altitudetag.AltitudeTag;
|
||||
import com.alttd.altitudetag.listeners.InteractListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Sheep;
|
||||
import org.bukkit.entity.Wolf;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.powermock.api.mockito.PowerMockito.doNothing;
|
||||
import static org.powermock.api.mockito.PowerMockito.mock;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
import static org.powermock.api.mockito.PowerMockito.spy;
|
||||
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
|
||||
import static org.powermock.api.mockito.PowerMockito.when;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ AltitudeTag.class, Bukkit.class })
|
||||
public class InteractListenerTest
|
||||
{
|
||||
private InteractListener listener;
|
||||
|
||||
private EntityDamageByEntityEvent event;
|
||||
|
||||
private Player attacker;
|
||||
private UUID attackerUuid = UUID.fromString("00000000-0000-0000-0000-000000000001");
|
||||
|
||||
private Player victim;
|
||||
private UUID victimUuid = UUID.fromString("00000000-0000-0000-0000-000000000000");
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
listener = new InteractListener();
|
||||
|
||||
mockStatic(Bukkit.class);
|
||||
when(Bukkit.getOnlinePlayers()).thenReturn(Collections.emptyList());
|
||||
}
|
||||
|
||||
private void setup_both_players()
|
||||
{
|
||||
attacker = mock(Player.class);
|
||||
when(attacker.getUniqueId()).thenReturn(attackerUuid);
|
||||
|
||||
victim = mock(Player.class);
|
||||
when(victim.getUniqueId()).thenReturn(victimUuid);
|
||||
|
||||
event = spy(new EntityDamageByEntityEvent(attacker, victim, DamageCause.ENTITY_ATTACK, 1d));
|
||||
|
||||
mockStatic(AltitudeTag.class);
|
||||
when(AltitudeTag.getTagger()).thenCallRealMethod();
|
||||
when(AltitudeTag.setTagger(any())).thenCallRealMethod();
|
||||
|
||||
Whitebox.setInternalState(AltitudeTag.class, "instance", mock(AltitudeTag.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_two_players_normal()
|
||||
{
|
||||
setup_both_players();
|
||||
|
||||
// override addTag
|
||||
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
|
||||
|
||||
doNothing().when(AltitudeTag.class);
|
||||
AltitudeTag.addTag(eq(attackerUuid), runnableCaptor.capture());
|
||||
|
||||
// override getTag
|
||||
ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class);
|
||||
|
||||
doNothing().when(AltitudeTag.class);
|
||||
AltitudeTag.getTags(eq(attackerUuid), consumerCaptor.capture());
|
||||
|
||||
// they're always online
|
||||
when(attacker.isOnline()).thenReturn(true);
|
||||
|
||||
// call the event
|
||||
listener.onHit(event);
|
||||
|
||||
// capture the runnable and run it
|
||||
Runnable addTagRunnable = runnableCaptor.getValue();
|
||||
addTagRunnable.run();
|
||||
|
||||
// capture the consumer and run it
|
||||
Consumer<Integer> getTagsConsumer = consumerCaptor.getValue();
|
||||
getTagsConsumer.accept(10);
|
||||
|
||||
assertEquals(victimUuid, AltitudeTag.getTagger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_two_players_tagger_leave_before_message()
|
||||
{
|
||||
setup_both_players();
|
||||
|
||||
// override addTag
|
||||
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
|
||||
doNothing().when(AltitudeTag.class);
|
||||
AltitudeTag.addTag(eq(attackerUuid), runnableCaptor.capture());
|
||||
|
||||
// override getTag
|
||||
ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class);
|
||||
doNothing().when(AltitudeTag.class);
|
||||
AltitudeTag.getTags(eq(attackerUuid), consumerCaptor.capture());
|
||||
|
||||
// they're always online
|
||||
when(attacker.isOnline()).thenReturn(true).thenReturn(false);
|
||||
|
||||
// call the event
|
||||
listener.onHit(event);
|
||||
|
||||
// capture the runnable and run it
|
||||
Runnable addTagRunnable = runnableCaptor.getValue();
|
||||
addTagRunnable.run();
|
||||
|
||||
// capture the consumer and run it
|
||||
Consumer<Integer> getTagsConsumer = consumerCaptor.getValue();
|
||||
getTagsConsumer.accept(10);
|
||||
|
||||
// assertions
|
||||
verify(attacker, times(2)).isOnline();
|
||||
|
||||
verify(attacker, never()).sendMessage((String) any());
|
||||
verify(attacker, never()).sendMessage((String[]) any());
|
||||
|
||||
assertEquals(victimUuid, AltitudeTag.getTagger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_two_players_tagger_leave_before_get_tags()
|
||||
{
|
||||
setup_both_players();
|
||||
|
||||
// override addTag
|
||||
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
|
||||
|
||||
doNothing().when(AltitudeTag.class);
|
||||
AltitudeTag.addTag(eq(attackerUuid), runnableCaptor.capture());
|
||||
|
||||
// they're always offline
|
||||
when(attacker.isOnline()).thenReturn(false);
|
||||
|
||||
// call the event
|
||||
listener.onHit(event);
|
||||
|
||||
// capture the runnable and run it
|
||||
Runnable addTagRunnable = runnableCaptor.getValue();
|
||||
addTagRunnable.run();
|
||||
|
||||
verify(attacker, never()).sendMessage((String) any());
|
||||
verify(attacker, never()).sendMessage((String[]) any());
|
||||
|
||||
verifyStatic(AltitudeTag.class, never());
|
||||
AltitudeTag.getTags(any(), any());
|
||||
|
||||
assertEquals(victimUuid, AltitudeTag.getTagger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_no_players()
|
||||
{
|
||||
Wolf attacker = mock(Wolf.class);
|
||||
Sheep victim = mock(Sheep.class);
|
||||
|
||||
event = spy(new EntityDamageByEntityEvent(attacker, victim, DamageCause.ENTITY_ATTACK, 1d));
|
||||
|
||||
listener.onHit(event);
|
||||
|
||||
verify(event, never()).getEntity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_player_attacker_non_player_victim()
|
||||
{
|
||||
Player attacker = mock(Player.class);
|
||||
Sheep victim = mock(Sheep.class);
|
||||
|
||||
event = spy(new EntityDamageByEntityEvent(attacker, victim, DamageCause.ENTITY_ATTACK, 1d));
|
||||
|
||||
listener.onHit(event);
|
||||
|
||||
verify(event, times(1)).getEntity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_non_player_attacker_player_victim()
|
||||
{
|
||||
Wolf attacker = mock(Wolf.class);
|
||||
Player victim = mock(Player.class);
|
||||
|
||||
event = spy(new EntityDamageByEntityEvent(attacker, victim, DamageCause.ENTITY_ATTACK, 1d));
|
||||
|
||||
listener.onHit(event);
|
||||
|
||||
verify(event, never()).getEntity();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user