Refactoring and do not add the backpack item in worlds where the plugin is disabled

This commit is contained in:
GeorgH93 2020-02-06 13:57:29 +01:00
parent fc9151b99f
commit 1b73ba2b2f
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8

View File

@ -20,6 +20,7 @@
import at.pcgamingfreaks.Bukkit.HeadUtils;
import at.pcgamingfreaks.Bukkit.MCVersion;
import at.pcgamingfreaks.Bukkit.Message.Message;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import org.bukkit.ChatColor;
@ -45,11 +46,13 @@
public class ItemShortcut implements Listener
{
private static final UUID MINEPACKS_UUID = UUID.nameUUIDFromBytes("Minepacks".getBytes());
private final Minepacks plugin;
private final String itemName, value;
private final Message messageDoNotRemoveItem;
public ItemShortcut(Minepacks plugin)
{
this.plugin = plugin;
itemName = ChatColor.translateAlternateColorCodes('&', plugin.getConfiguration().getItemShortcutItemName());
value = plugin.getConfiguration().getItemShortcutHeadValue();
messageDoNotRemoveItem = plugin.getLanguage().getMessage("Ingame.DontRemoveShortcut");
@ -79,12 +82,30 @@ else if(isItemShortcut(itemStack))
}
}
//region Add backpack item
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onJoin(PlayerJoinEvent event)
{
if(plugin.isDisabled(event.getPlayer()) != WorldBlacklistMode.None) return;
addItem(event.getPlayer());
}
@EventHandler
public void onSpawn(PlayerRespawnEvent event)
{
if(plugin.isDisabled(event.getPlayer()) != WorldBlacklistMode.None) return;
addItem(event.getPlayer());
}
@EventHandler
public void onWorldChange(PlayerChangedWorldEvent event)
{
if(plugin.isDisabled(event.getPlayer()) != WorldBlacklistMode.None) return;
addItem(event.getPlayer());
}
//endregion
//region Prevent placing of backpack item
@EventHandler(priority = EventPriority.LOWEST)
public void onItemInteract(PlayerInteractEvent event)
{
@ -127,7 +148,9 @@ public void onItemFrameInteract(PlayerInteractEntityEvent event)
event.setCancelled(true);
}
}
//endregion
//region Handle inventory acctions
@EventHandler(priority = EventPriority.LOWEST)
public void onItemClick(InventoryClickEvent event)
{
@ -181,6 +204,20 @@ else if(isItemShortcut(event.getOldCursor()))
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onDropItem(PlayerDropItemEvent event)
{
if(isItemShortcut(event.getItemDrop().getItemStack()))
{
event.setCancelled(true);
messageDoNotRemoveItem.send(event.getPlayer());
}
}
//endregion
/**
* Removes the backpack item form the drops on death
*/
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onDeath(PlayerDeathEvent event)
{
@ -194,20 +231,4 @@ public void onDeath(PlayerDeathEvent event)
}
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onSpawn(PlayerDropItemEvent event)
{
if(isItemShortcut(event.getItemDrop().getItemStack()))
{
event.setCancelled(true);
messageDoNotRemoveItem.send(event.getPlayer());
}
}
@EventHandler
public void onSpawn(PlayerRespawnEvent event)
{
addItem(event.getPlayer());
}
}