Skip to content

Commit

Permalink
Added updating player count when players enter/exit vanished mode.
Browse files Browse the repository at this point in the history
 Works with EssentialsX, SuperVanish, and VanishNoPacket.
  • Loading branch information
TheMrEngMan committed Apr 13, 2021
1 parent 43a6954 commit c28424f
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 12 deletions.
36 changes: 34 additions & 2 deletions src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import com.earth2me.essentials.Essentials;
import org.kitteh.vanish.VanishPlugin;
import org.bukkit.plugin.PluginManager;
import uk.co.angrybee.joe.commands.minecraft.CommandAbout;
import uk.co.angrybee.joe.commands.minecraft.CommandReload;
import uk.co.angrybee.joe.commands.minecraft.CommandStatus;
Expand All @@ -14,6 +17,9 @@
import uk.co.angrybee.joe.stores.RemovedList;
import uk.co.angrybee.joe.stores.UserList;
import uk.co.angrybee.joe.stores.WhitelistedPlayers;
import uk.co.angrybee.joe.events.EssentialsVanishEvents;
import uk.co.angrybee.joe.events.SuperVanishEvents;
import uk.co.angrybee.joe.events.VanishNoPacketEvents;

import java.util.List;
import java.util.logging.Logger;
Expand Down Expand Up @@ -45,6 +51,11 @@ public class DiscordWhitelister extends JavaPlugin
private static Server thisServer;
private static Logger pluginLogger;

// Plugins
public static Essentials essentialsPlugin;
public static VanishPlugin vanishNoPacketPlugin;
public static boolean hasSuperVanishOrPremiumVanish;

// For not counting vanished players when other players join/leave
private static int vanishedPlayersCount;

Expand All @@ -65,6 +76,12 @@ public void onEnable()
thisServer = thisPlugin.getServer();
pluginLogger = thisPlugin.getLogger();

// Get/check for plugin
PluginManager pluginManager = getServer().getPluginManager();
essentialsPlugin = (Essentials) pluginManager.getPlugin("Essentials");
vanishNoPacketPlugin = (VanishPlugin) pluginManager.getPlugin("VanishNoPacket");
hasSuperVanishOrPremiumVanish = pluginManager.getPlugin("SuperVanish") != null || pluginManager.getPlugin("PremiumVanish") != null;

int initSuccess = InitBot(true);

if(initSuccess == 0)
Expand Down Expand Up @@ -245,8 +262,23 @@ else if(configCreated)
// Only attempt to set player count if the bot successfully initialized
if(mainConfig.getFileConfiguration().getBoolean("show-player-count"))
{
// Register events if enabled
thisServer.getPluginManager().registerEvents(new JoinLeaveEvents(), thisPlugin);
if(firstInit) {
// Register events if enabled
thisServer.getPluginManager().registerEvents(new JoinLeaveEvents(), thisPlugin);
//pluginLogger.info("Registered join/leave events!");
if (hasSuperVanishOrPremiumVanish) {
thisServer.getPluginManager().registerEvents(new SuperVanishEvents(), thisPlugin);
//pluginLogger.info("Registered SuperVanish events!");
}
if(vanishNoPacketPlugin != null) {
thisServer.getPluginManager().registerEvents(new VanishNoPacketEvents(), thisPlugin);
//pluginLogger.info("Registered VanishNoPacket events!");
}
if (essentialsPlugin != null) {
thisServer.getPluginManager().registerEvents(new EssentialsVanishEvents(), thisPlugin);
//pluginLogger.info("Registered Essentials vanish events!");
}
}

// Set initial player count
DiscordClient.SetPlayerCountStatus(getOnlineUsers());
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/uk/co/angrybee/joe/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package uk.co.angrybee.joe;

import de.myzelyam.api.vanish.VanishAPI;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;

import static uk.co.angrybee.joe.DiscordWhitelister.*;

public class Utils {

// Check if player is vanished
public static boolean isVanished(Player player) {

// For Essentials
if(essentialsPlugin != null) {
//getPluginLogger().info("Checking via EssX if " + player.getDisplayName() + " is vanished: " + essentialsPlugin.getUser(player).isVanished());
//getPluginLogger().info("Checking via EssX for list of vanished players: " + essentialsPlugin.getVanishedPlayers());
if(essentialsPlugin.getUser(player).isVanished()) return true;
}

// For SuperVanish / PremiumVanish
if(VanishAPI.getPlugin() != null) {
//getPluginLogger().info("Checking via SV if " + player.getDisplayName() + " is vanished: " + VanishAPI.isInvisible(player));
//getPluginLogger().info("Checking via SV for list of vanished players: " + VanishAPI.getAllInvisiblePlayers());
if(VanishAPI.isInvisible(player)) return true;
}

// For VanishNoPacket
if(vanishNoPacketPlugin != null) {
//getPluginLogger().info("Checking via VNP if " + player.getDisplayName() + " is vanished: " + vanishNoPacketPlugin.getManager().isVanished(player));
//getPluginLogger().info("Checking via VNP for list of vanished players: " + vanishNoPacketPlugin.getManager().getVanishedPlayers());
if(vanishNoPacketPlugin.getManager().isVanished(player)) return true;
}

// For others (maybe)
for (MetadataValue meta : player.getMetadata("vanished")) {
if (meta.asBoolean()) return true;
}

// Otherwise, player is not vanished
return false;

}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.co.angrybee.joe.events;

import net.ess3.api.events.VanishStatusChangeEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class EssentialsVanishEvents implements Listener {

@EventHandler
public void onVanishStatusChangeEvent(VanishStatusChangeEvent event){
// If value is true (player just vanished)
if(event.getValue()) {
VanishEvents.onPlayerHide(event.getAffected().getDisplayName());
}
// If value is false (player just un-vanished)
else {
VanishEvents.onPlayerShow(event.getAffected().getDisplayName());
}
}

}
21 changes: 11 additions & 10 deletions src/main/java/uk/co/angrybee/joe/events/JoinLeaveEvents.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
package uk.co.angrybee.joe.events;

import org.bukkit.entity.Player;
import org.bukkit.event.EventPriority;
import uk.co.angrybee.joe.DiscordWhitelister;
import uk.co.angrybee.joe.DiscordClient;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import uk.co.angrybee.joe.Utils;

// Used for showing player count in the discord bots status
public class JoinLeaveEvents implements Listener
{
@EventHandler
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();
if(!DiscordWhitelister.showVanishedPlayersInCount)
{
if(event.getPlayer().hasPermission("discordsrv.silentjoin")
|| event.getPlayer().hasPermission("discordsrv.silentquit")
|| event.getPlayer().hasPermission("sv.joinvanished"))
if(Utils.isVanished(player))
{
DiscordWhitelister.getPlugin().getLogger().info("Player " + event.getPlayer().getDisplayName() + " joined with silent joining/quitting permission, not incrementing player count");
DiscordWhitelister.getPlugin().getLogger().info("Player " + player.getDisplayName() + " joined while vanished, not incrementing player count");
DiscordWhitelister.addVanishedPlayer();
return;
}
}
DiscordClient.SetPlayerCountStatus(DiscordWhitelister.getOnlineUsers());
}

@EventHandler
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerLeave(PlayerQuitEvent event)
{
Player player = event.getPlayer();
if(!DiscordWhitelister.showVanishedPlayersInCount)
{
if(event.getPlayer().hasPermission("discordsrv.silentjoin")
|| event.getPlayer().hasPermission("discordsrv.silentquit")
|| event.getPlayer().hasPermission("sv.joinvanished"))
if(Utils.isVanished(player))
{
DiscordWhitelister.getPlugin().getLogger().info("Player " + event.getPlayer().getDisplayName() + " quit with silent joining/quitting permission, not decrementing player count");
DiscordWhitelister.getPlugin().getLogger().info("Player " + player.getDisplayName() + " quit while vanished, not decrementing player count");
DiscordWhitelister.removeVanishedPlayer();
return;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/uk/co/angrybee/joe/events/SuperVanishEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.co.angrybee.joe.events;

import de.myzelyam.api.vanish.PlayerHideEvent;
import de.myzelyam.api.vanish.PlayerShowEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import uk.co.angrybee.joe.DiscordWhitelister;

public class SuperVanishEvents implements Listener {

@EventHandler
public void onPlayerShowEvent(PlayerShowEvent event){
VanishEvents.onPlayerShow(event.getPlayer().getDisplayName());
}

@EventHandler
public void onPlayerHideEvent(PlayerHideEvent event){
VanishEvents.onPlayerHide(event.getPlayer().getDisplayName());
}

}
25 changes: 25 additions & 0 deletions src/main/java/uk/co/angrybee/joe/events/VanishEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package uk.co.angrybee.joe.events;

import uk.co.angrybee.joe.DiscordClient;
import uk.co.angrybee.joe.DiscordWhitelister;

public class VanishEvents {

// Called when a player exits vanished mode
public static void onPlayerShow(String playerName) {
if(!DiscordWhitelister.showVanishedPlayersInCount) {
DiscordWhitelister.getPlugin().getLogger().info("Player " + playerName + " un-vanished, incrementing player count");
DiscordWhitelister.removeVanishedPlayer();
DiscordClient.SetPlayerCountStatus(DiscordWhitelister.getOnlineUsers());
}
}

// Called when a player enters vanished mode
public static void onPlayerHide(String playerName) {
if(!DiscordWhitelister.showVanishedPlayersInCount) {
DiscordWhitelister.getPlugin().getLogger().info("Player " + playerName + " vanished, decrementing player count");
DiscordWhitelister.addVanishedPlayer();
DiscordClient.SetPlayerCountStatus(DiscordWhitelister.getOnlineUsers());
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/uk/co/angrybee/joe/events/VanishNoPacketEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.co.angrybee.joe.events;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.kitteh.vanish.event.VanishStatusChangeEvent;

public class VanishNoPacketEvents implements Listener {

@EventHandler
public void onVanishStatusChangeEvent(VanishStatusChangeEvent event){
// If value is true (player just vanished)
if(event.isVanishing()) {
VanishEvents.onPlayerHide(event.getPlayer().getDisplayName());
}
// If value is false (player just un-vanished)
else {
VanishEvents.onPlayerShow(event.getPlayer().getDisplayName());
}
}

}
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ version: 1.4.8
author: Joe Shimell
main: uk.co.angrybee.joe.DiscordWhitelister
description: Discord Whitelister.
softdepend: [Essentials, SuperVanish, VanishNoPacket]
commands:
discordwhitelister:
description: See info about this plugin
Expand Down

0 comments on commit c28424f

Please sign in to comment.