Add ItemShortcut

This commit is contained in:
GeorgH93 2020-01-05 03:16:46 +01:00
parent 87b2c93a1b
commit 01776e3489
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
5 changed files with 204 additions and 9 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>Minepacks</artifactId>
<version>2.0.13</version>
<version>2.1-RC1</version>
<scm>
<connection>scm:git:git@github.com:GeorgH93/Minepacks.git</connection>

View File

@ -146,6 +146,18 @@ WorldSettings:
# It will not disable the Shulkerboxes filter!
BlacklistMode: "Message"
# Gives the players an item that they can interact with to open their backpack. Only works on MC 1.8 or newer
ItemShortcut:
# If enabled the players will be given an item they can interact with to open the backpack.
# The item may not be removed from their inventory as long as this option is enabled.
Enabled: true
# The name of the item in the inventory
ItemName: "&eBackpack"
# The texture value for the head.
# Heads can be found here: https://minecraft-heads.com/custom-heads/
# The correct value is the one listed on the head page under Other/Value
HeadTextureValue: "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvOGRjYzZlYjQwZjNiYWRhNDFlNDMzOTg4OGQ2ZDIwNzQzNzU5OGJkYmQxNzVjMmU3MzExOTFkNWE5YTQyZDNjOCJ9fX0="
Misc:
# Enables/Disables the auto-update function of the plugin.
AutoUpdate: true
@ -153,4 +165,4 @@ Misc:
UseBungeeCord: false
# Config file version. Don't touch it!
Version: 21
Version: 22

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 GeorgH93
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -36,7 +36,7 @@
public class Config extends Configuration implements DatabaseConnectionConfiguration
{
private static final int CONFIG_VERSION = 21, UPGRADE_THRESHOLD = 21;
private static final int CONFIG_VERSION = 22, UPGRADE_THRESHOLD = 22, PRE_V2_VERSION = 20;
public Config(JavaPlugin plugin)
{
@ -54,7 +54,7 @@ protected void doUpdate()
@Override
protected void doUpgrade(@NotNull YamlFileManager oldConfig)
{
if(oldConfig.getVersion() < 20) // Pre V2.0 config file
if(oldConfig.getVersion() < PRE_V2_VERSION) // Pre V2.0 config file
{
OldFileUpdater.updateConfig(oldConfig.getYamlE(), getConfigE());
}
@ -312,5 +312,22 @@ public WorldBlacklistMode getWorldBlacklistMode()
return blacklistMode;
}
//endregion
//region ItemShortcut settings
public boolean isItemShortcutEnabled()
{
return MCVersion.isNewerOrEqualThan(MCVersion.MC_1_11) && getConfigE().getBoolean("ItemShortcut.Enabled", true);
}
public String getItemShortcutItemName()
{
return getConfigE().getString("ItemShortcut.ItemName", "&eBackpack");
}
public String getItemShortcutHeadValue()
{
return getConfigE().getString("ItemShortcut.HeadTextureValue", "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvOGRjYzZlYjQwZjNiYWRhNDFlNDMzOTg4OGQ2ZDIwNzQzNzU5OGJkYmQxNzVjMmU3MzExOTFkNWE5YTQyZDNjOCJ9fX0=");
}
//endregion
//endregion
}

View File

@ -0,0 +1,168 @@
/*
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.Listener;
import at.pcgamingfreaks.Bukkit.MCVersion;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.Iterator;
import java.util.UUID;
public class ItemShortcut implements Listener
{
private static final Material HEAD_MATERIAL = MCVersion.isNewerOrEqualThan(MCVersion.MC_1_13) ? Material.PLAYER_HEAD : Material.valueOf("SKULL");
private final String itemName;
private final ItemStack item;
public ItemShortcut(Minepacks plugin)
{
item = (MCVersion.isNewerOrEqualThan(MCVersion.MC_1_13)) ? new ItemStack(HEAD_MATERIAL) : new ItemStack(HEAD_MATERIAL, 1, (byte) 3);
itemName = ChatColor.translateAlternateColorCodes('&', plugin.getConfiguration().getItemShortcutItemName());
String name = (MCVersion.isNewerOrEqualThan(MCVersion.MC_1_13)) ? "{\\\"text\\\":\\\"" + itemName + "\\\"}" : itemName;
String str = "{display:{Name:\\\"\" + name + \"\\\"},SkullOwner:{Id:\"" + UUID.nameUUIDFromBytes("Minepacks".getBytes()) + "\",Properties:{textures:[{Value:\"" + plugin.getConfiguration().getItemShortcutHeadValue() + "\"}]}}}";
plugin.getLogger().info(str);
Bukkit.getUnsafe().modifyItemStack(item, str);
}
private boolean isItemShortcut(@Nullable ItemStack stack)
{
//noinspection ConstantConditions
return stack != null && stack.getType() == HEAD_MATERIAL && stack.hasItemMeta() && stack.getItemMeta().getDisplayName().equals(itemName);
}
private void addItem(Player player)
{
boolean empty = false, item = false;
for(ItemStack itemStack : player.getInventory())
{
if(itemStack == null || itemStack.getType() == Material.AIR) empty = true;
else if(isItemShortcut(itemStack))
{
item = true;
break;
}
}
if(!item && empty) player.getInventory().addItem(this.item);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onJoin(PlayerJoinEvent event)
{
addItem(event.getPlayer());
}
@EventHandler(priority = EventPriority.LOWEST)
public void onItemInteract(PlayerInteractEvent event)
{
if ((event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)) return;
if(isItemShortcut(event.getItem()))
{
event.getPlayer().performCommand("backpack open");
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onItemClick(InventoryClickEvent event)
{
if(event.getWhoClicked() instanceof Player)
{
if(isItemShortcut(event.getCurrentItem()))
{
if(event.getClick() == ClickType.RIGHT || event.getClick() == ClickType.SHIFT_RIGHT)
{
((Player) event.getWhoClicked()).performCommand("backpack open");
event.setCancelled(true);
}
else if(event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY)
{
event.setCancelled(true);
}
}
else if(isItemShortcut(event.getCursor()) && !event.getWhoClicked().getInventory().equals(event.getClickedInventory()))
{
event.setCancelled(true);
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onItemDrag(InventoryDragEvent event)
{ //TODO improve
if(!event.getInventory().equals(event.getWhoClicked().getInventory()))
{
if(isItemShortcut(event.getCursor()))
{
event.setCancelled(true);
}
else if(isItemShortcut(event.getOldCursor()))
{
event.setCancelled(true);
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onDeath(PlayerDeathEvent event)
{
Iterator<ItemStack> itemStackIterator = event.getDrops().iterator();
while(itemStackIterator.hasNext())
{
if(isItemShortcut(itemStackIterator.next()))
{
itemStackIterator.remove();
break;
}
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onSpawn(PlayerDropItemEvent event)
{
if(isItemShortcut(event.getItemDrop().getItemStack()))
{
event.setCancelled(true);
}
}
@EventHandler
public void onSpawn(PlayerRespawnEvent event)
{
addItem(event.getPlayer());
}
}

View File

@ -31,10 +31,7 @@
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Database;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Language;
import at.pcgamingfreaks.Minepacks.Bukkit.Listener.BackpackEventListener;
import at.pcgamingfreaks.Minepacks.Bukkit.Listener.DisableShulkerboxes;
import at.pcgamingfreaks.Minepacks.Bukkit.Listener.DropOnDeath;
import at.pcgamingfreaks.Minepacks.Bukkit.Listener.ItemFilter;
import at.pcgamingfreaks.Minepacks.Bukkit.Listener.*;
import at.pcgamingfreaks.StringUtils;
import at.pcgamingfreaks.Updater.UpdateProviders.BukkitUpdateProvider;
import at.pcgamingfreaks.Updater.UpdateProviders.JenkinsUpdateProvider;
@ -195,6 +192,7 @@ private void load()
pluginManager.registerEvents(itemFilter, this);
}
if(config.isShulkerboxesDisable()) pluginManager.registerEvents(new DisableShulkerboxes(this), this);
if(config.isItemShortcutEnabled()) pluginManager.registerEvents(new ItemShortcut(this), this);
//endregion
if(config.getFullInvCollect()) collector = new ItemsCollector(this);
worldBlacklist = config.getWorldBlacklist();