TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/bridge/EssentialsBridge.java

221 lines
6.0 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.bridge;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
public class EssentialsBridge extends FreedomService
{
2017-12-23 04:07:36 +00:00
private Essentials essentialsPlugin = null;
2017-12-23 04:07:36 +00:00
@Override
2020-07-01 01:51:06 +00:00
public void onStart()
2017-12-23 04:07:36 +00:00
{
}
2017-12-23 04:07:36 +00:00
@Override
2020-07-01 01:51:06 +00:00
public void onStop()
2017-12-23 04:07:36 +00:00
{
}
2017-12-23 04:07:36 +00:00
public Essentials getEssentialsPlugin()
{
if (essentialsPlugin == null)
2017-12-23 04:07:36 +00:00
{
try
{
final Plugin essentials = server.getPluginManager().getPlugin("Essentials");
assert essentials != null;
if (essentials instanceof Essentials)
2017-12-23 04:07:36 +00:00
{
essentialsPlugin = (Essentials)essentials;
}
}
2017-12-23 04:07:36 +00:00
catch (Exception ex)
{
FLog.severe(ex);
}
}
return essentialsPlugin;
}
2017-12-23 04:07:36 +00:00
public User getEssentialsUser(String username)
2017-12-23 04:07:36 +00:00
{
try
{
Essentials essentials = getEssentialsPlugin();
2017-12-23 04:07:36 +00:00
if (essentials != null)
{
return essentials.getUserMap().getUser(username);
}
}
2017-12-23 04:07:36 +00:00
catch (Exception ex)
{
FLog.severe(ex);
}
return null;
}
2017-12-23 04:07:36 +00:00
public void setNickname(String username, String nickname)
2017-12-23 04:07:36 +00:00
{
try
{
User user = getEssentialsUser(username);
2017-12-23 04:07:36 +00:00
if (user != null)
{
user.setNickname(nickname);
user.setDisplayNick();
}
}
2017-12-23 04:07:36 +00:00
catch (Exception ex)
{
FLog.severe(ex);
}
}
2017-12-23 04:07:36 +00:00
public String getNickname(String username)
2017-12-23 04:07:36 +00:00
{
try
{
User user = getEssentialsUser(username);
2017-12-23 04:07:36 +00:00
if (user != null)
{
return user.getNickname();
}
}
2017-12-23 04:07:36 +00:00
catch (Exception ex)
{
FLog.severe(ex);
}
return null;
}
2017-12-23 04:07:36 +00:00
public long getLastActivity(String username)
2017-12-23 04:07:36 +00:00
{
try
{
User user = getEssentialsUser(username);
2017-12-23 04:07:36 +00:00
if (user != null)
{
Long l = FUtil.getField(user, "lastActivity");
return (l != null) ? l : 0L;
}
}
2017-12-23 04:07:36 +00:00
catch (Exception ex)
{
FLog.severe(ex);
}
return 0L;
}
2017-12-23 04:07:36 +00:00
public void setVanished(String username, boolean vanished)
2017-12-23 04:07:36 +00:00
{
try
{
User user = getEssentialsUser(username);
2017-12-23 04:07:36 +00:00
if (user != null)
{
user.setVanished(vanished);
}
}
2017-12-23 04:07:36 +00:00
catch (Exception ex)
{
FLog.severe(ex);
}
}
2017-12-23 04:07:36 +00:00
@EventHandler(priority = EventPriority.MONITOR)
public void onInventoryClick(InventoryClickEvent event)
2017-12-23 04:07:36 +00:00
{
Player refreshPlayer = null;
Inventory inventory = event.getView().getTopInventory();
InventoryType inventoryType = inventory.getType();
Player player = (Player)event.getWhoClicked();
FPlayer fPlayer = plugin.pl.getPlayer(player);
if (inventoryType == InventoryType.PLAYER && fPlayer.isInvSee())
{
final InventoryHolder inventoryHolder = inventory.getHolder();
if (inventoryHolder instanceof HumanEntity)
2017-12-23 04:07:36 +00:00
{
Player invOwner = (Player)inventoryHolder;
Rank recieverRank = plugin.rm.getRank(player);
Rank playerRank = plugin.rm.getRank(invOwner);
2017-12-23 04:07:36 +00:00
if (playerRank.ordinal() >= recieverRank.ordinal() || !invOwner.isOnline())
{
event.setCancelled(true);
refreshPlayer = player;
}
}
}
2017-12-23 04:07:36 +00:00
if (refreshPlayer != null)
{
final Player p = refreshPlayer;
2017-12-23 04:07:36 +00:00
new BukkitRunnable()
{
@Override
public void run()
{
p.updateInventory();
}
}.runTaskLater(plugin, 20L);
}
}
2017-12-23 04:07:36 +00:00
@EventHandler(priority = EventPriority.MONITOR)
public void onInventoryClose(InventoryCloseEvent event)
2017-12-23 04:07:36 +00:00
{
Player refreshPlayer = null;
Inventory inventory = event.getView().getTopInventory();
InventoryType inventoryType = inventory.getType();
Player player = (Player)event.getPlayer();
FPlayer fPlayer = plugin.pl.getPlayer(player);
if (inventoryType == InventoryType.PLAYER && fPlayer.isInvSee())
2017-12-23 04:07:36 +00:00
{
fPlayer.setInvSee(false);
refreshPlayer = player;
}
2017-12-23 04:07:36 +00:00
if (refreshPlayer != null)
{
final Player p = refreshPlayer;
2017-12-23 04:07:36 +00:00
new BukkitRunnable()
{
@Override
public void run()
{
p.updateInventory();
}
}.runTaskLater(plugin, 20L);
}
}
2017-12-23 04:07:36 +00:00
// TODO: Actually use this for something or remove it.
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event)
2017-12-23 04:07:36 +00:00
{
Player player = event.getPlayer();
}
2017-12-23 04:07:36 +00:00
public boolean isEnabled()
2017-12-23 04:07:36 +00:00
{
final Essentials ess = getEssentialsPlugin();
return ess != null && ess.isEnabled();
}
}