Add customizable items

This commit is contained in:
GeorgH93 2020-03-19 02:59:31 +01:00
parent c32cf7419c
commit e2ffc63499
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
10 changed files with 345 additions and 23 deletions

View File

@ -0,0 +1,34 @@
# The items define the possible backpack items that can be used as shortcuts
Items:
# Example item that uses a player head
BackpackHeadBlue:
# Use player_head if it should be a head with a custom texture. Do not use the id or the head item name of your minecraft version!
Material: player_head
# 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
Model: "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvOGRjYzZlYjQwZjNiYWRhNDFlNDMzOTg4OGQ2ZDIwNzQzNzU5OGJkYmQxNzVjMmU3MzExOTFkNWE5YTQyZDNjOCJ9fX0="
DisplayName: "&eBackpack"
Lore:
- "Right click to open your backpack."
- "Drag items on it to place them in your backpack."
Minepacks:
Material: shulker_shell
# Will set the damage value and unbreakable
on MC versions older than 1.14 or the custom_model_data for MC 1.14 and newer
Model: 10
DisplayName: "&eBackpack"
Lore:
- "Right click to open your backpack."
- "Drag items on it to place them in your backpack."
MinepacksLegacy:
Material: CARROT_STICK
Model: 1
DisplayName: "&eBackpack"
Lore:
- "Right click to open your backpack."
- "Drag items on it to place them in your backpack."
# Config file version. Don't touch it!
Version: 1

View File

@ -166,12 +166,8 @@ ItemShortcut:
# If enabled the players will be given an item they can interact with to open the backpack. # 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. # The item may not be removed from their inventory as long as this option is enabled.
Enabled: true Enabled: true
# The name of the item in the inventory # The item that should be used as a shortcut, you can create custom ones in the backpacks.yml file
ItemName: "&eBackpack" Item: BackpackHeadBlue
# 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="
# Increases compatibility with some death chest plugins. Enable this if the backpack item is added to your death chest! # Increases compatibility with some death chest plugins. Enable this if the backpack item is added to your death chest!
ImproveDeathChestCompatibility: false ImproveDeathChestCompatibility: false
# Prevents the backpack from being used as a hat # Prevents the backpack from being used as a hat
@ -206,4 +202,4 @@ Misc:
UseBungeeCord: false UseBungeeCord: false
# Config file version. Don't touch it! # Config file version. Don't touch it!
Version: 28 Version: 29

View File

@ -38,7 +38,7 @@
public class Config extends Configuration implements DatabaseConnectionConfiguration public class Config extends Configuration implements DatabaseConnectionConfiguration
{ {
private static final int CONFIG_VERSION = 28, UPGRADE_THRESHOLD = CONFIG_VERSION, PRE_V2_VERSION = 20; private static final int CONFIG_VERSION = 29, UPGRADE_THRESHOLD = CONFIG_VERSION, PRE_V2_VERSION = 20;
public Config(JavaPlugin plugin) public Config(JavaPlugin plugin)
{ {
@ -377,13 +377,9 @@ public boolean isItemShortcutEnabled()
public String getItemShortcutItemName() public String getItemShortcutItemName()
{ {
return getConfigE().getString("ItemShortcut.ItemName", "&eBackpack"); return getConfigE().getString("ItemShortcut.Item", "BackpackHeadBlue");
} }
public String getItemShortcutHeadValue()
{
return getConfigE().getString("ItemShortcut.HeadTextureValue", "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvOGRjYzZlYjQwZjNiYWRhNDFlNDMzOTg4OGQ2ZDIwNzQzNzU5OGJkYmQxNzVjMmU3MzExOTFkNWE5YTQyZDNjOCJ9fX0=");
}
public boolean isItemShortcutImproveDeathChestCompatibilityEnabled() public boolean isItemShortcutImproveDeathChestCompatibilityEnabled()
{ {

View File

@ -0,0 +1,87 @@
/*
* 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.Item;
import at.pcgamingfreaks.Bukkit.Configuration;
import at.pcgamingfreaks.Bukkit.MCVersion;
import at.pcgamingfreaks.YamlFileManager;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class BackpacksConfig extends Configuration
{
private static final int CONFIG_VERSION = 1;
private Map<String, ItemConfig> itemConfigs = new HashMap<>();
public BackpacksConfig(final @NotNull JavaPlugin plugin)
{
super(plugin, CONFIG_VERSION, CONFIG_VERSION, "backpacks.yml");
loadItemConfigs();
}
@Override
protected void doUpgrade(@NotNull YamlFileManager oldConfig)
{
doUpgrade(oldConfig, new HashMap<>(), getYamlE().getKeysFiltered("Items\\..*"));
}
private void loadItemConfigs()
{
getYamlE().getKeysFiltered("Items\\.[^.]*\\.Material").forEach(materialKey -> {
final String key = materialKey.substring(0, materialKey.length() - ".Material".length());
final List<String> lore = getConfigE().getStringList(key + ".Lore", new ArrayList<>(0));
final List<String> loreFinal;
if(lore.size() == 0) loreFinal = null;
else
{
loreFinal = new ArrayList<>(lore.size());
lore.forEach(loreEntry -> loreFinal.add(ChatColor.translateAlternateColorCodes('&', loreEntry)));
}
final String displayName = ChatColor.translateAlternateColorCodes('&', getConfigE().getString(key + ".DisplayName", "&eBackpack"));
final String material = getYamlE().getString(key + ".Material", "player_head");
if(material.equalsIgnoreCase("player_head"))
{
itemConfigs.put(key, new ItemConfigHead(displayName, getConfigE().getString(key + ".Model", ""), loreFinal));
}
else
{
itemConfigs.put(key, new ItemConfigItem(getMaterialFromString(material), displayName, getYamlE().getInt(key + ".Model", 1), loreFinal));
}
});
}
private Material getMaterialFromString(String name)
{
name = name.toUpperCase(Locale.ENGLISH);
Material mat = Material.getMaterial(name);
if(mat == null && MCVersion.isNewerOrEqualThan(MCVersion.MC_1_13)) mat = Material.getMaterial(name, true);
//TODO from id
return mat;
}
public @Nullable ItemConfig getItemConfig(final @NotNull String name)
{
return itemConfigs.get(name);
}
}

View File

@ -0,0 +1,40 @@
/*
* 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.Item;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.List;
@Getter
@AllArgsConstructor
public abstract class ItemConfig
{
protected final @NotNull String displayName;
protected final @Nullable List<String> lore;
public abstract ItemStack make(final int amount);
public abstract Material getMaterial();
}

View File

@ -0,0 +1,62 @@
/*
* 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.Item;
import at.pcgamingfreaks.Bukkit.HeadUtils;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import lombok.Getter;
import java.util.List;
import java.util.UUID;
public class ItemConfigHead extends ItemConfig
{
private static final UUID MINEPACKS_UUID = UUID.nameUUIDFromBytes("Minepacks".getBytes());
@Getter protected final String value;
public ItemConfigHead(final @NotNull String displayName, final @NotNull String value, final @Nullable List<String> lore)
{
super(displayName, lore);
this.value = value;
}
@Override
public ItemStack make(final int amount)
{
ItemStack stack = HeadUtils.fromBase64(value, displayName, MINEPACKS_UUID);
if(lore != null)
{
ItemMeta meta = stack.getItemMeta();
meta.setLore(lore);
stack.setItemMeta(meta);
}
return stack;
}
@Override
public Material getMaterial()
{
return HeadUtils.HEAD_MATERIAL;
}
}

View File

@ -0,0 +1,91 @@
/*
* 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.Item;
import at.pcgamingfreaks.Bukkit.MCVersion;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import lombok.Getter;
import java.util.List;
@Getter
public class ItemConfigItem extends ItemConfig
{
private static final Producer PRODUCER = MCVersion.isOlderThan(MCVersion.MC_1_14) ? new ProducerLegacy() : new ProducerModelId();
protected final @NotNull Material material;
protected final int value;
public ItemConfigItem(final @NotNull Material material, final @NotNull String displayName, final int value, final @Nullable List<String> lore)
{
super(displayName, lore);
this.material = material;
this.value = value;
}
@Override
public ItemStack make(int amount)
{
return PRODUCER.make(this, amount);
}
//region producers
private interface Producer
{
ItemStack make(final @NotNull ItemConfigItem itemConfig, final int amount);
}
private static class ProducerLegacy implements Producer
{
@Override
public ItemStack make(final @NotNull ItemConfigItem itemConfig, final int amount)
{
ItemStack stack = new ItemStack(itemConfig.material, amount, (short) itemConfig.value);
ItemMeta meta = stack.getItemMeta();
assert meta != null;
meta.setDisplayName(itemConfig.displayName);
meta.setUnbreakable(true);
if(itemConfig.lore != null) meta.setLore(itemConfig.lore);
stack.setItemMeta(meta);
return stack;
}
}
private static class ProducerModelId implements Producer
{
@Override
public ItemStack make(@NotNull ItemConfigItem itemConfig, int amount)
{
ItemStack stack = new ItemStack(itemConfig.material, amount);
ItemMeta meta = stack.getItemMeta();
assert meta != null;
meta.setDisplayName(itemConfig.displayName);
meta.setCustomModelData(itemConfig.value);
if(itemConfig.lore != null) meta.setLore(itemConfig.lore);
stack.setItemMeta(meta);
return stack;
}
}
//endregion
}

View File

@ -17,16 +17,16 @@
package at.pcgamingfreaks.Minepacks.Bukkit.Listener; package at.pcgamingfreaks.Minepacks.Bukkit.Listener;
import at.pcgamingfreaks.Bukkit.HeadUtils;
import at.pcgamingfreaks.Bukkit.MCVersion; import at.pcgamingfreaks.Bukkit.MCVersion;
import at.pcgamingfreaks.Bukkit.Message.Message; import at.pcgamingfreaks.Bukkit.Message.Message;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack; import at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Events.InventoryClearedEvent; import at.pcgamingfreaks.Minepacks.Bukkit.API.Events.InventoryClearedEvent;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode; import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode;
import at.pcgamingfreaks.Minepacks.Bukkit.Item.BackpacksConfig;
import at.pcgamingfreaks.Minepacks.Bukkit.Item.ItemConfig;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Minepacks.Bukkit.Permissions; import at.pcgamingfreaks.Minepacks.Bukkit.Permissions;
import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -50,23 +50,28 @@ public class ItemShortcut implements Listener
{ {
private static final UUID MINEPACKS_UUID = UUID.nameUUIDFromBytes("Minepacks".getBytes()); private static final UUID MINEPACKS_UUID = UUID.nameUUIDFromBytes("Minepacks".getBytes());
private final Minepacks plugin; private final Minepacks plugin;
private final String itemName, value;
private final Message messageDoNotRemoveItem; private final Message messageDoNotRemoveItem;
private final boolean improveDeathChestCompatibility, blockAsHat, allowRightClickOnContainers; private final boolean improveDeathChestCompatibility, blockAsHat, allowRightClickOnContainers;
private final int preferredSlotId; private final int preferredSlotId;
private final Set<Material> containerMaterials = new HashSet<>(); private final Set<Material> containerMaterials = new HashSet<>();
private final ItemConfig itemConfig;
public ItemShortcut(Minepacks plugin) public ItemShortcut(Minepacks plugin)
{ {
this.plugin = plugin; this.plugin = plugin;
itemName = ChatColor.translateAlternateColorCodes('&', plugin.getConfiguration().getItemShortcutItemName());
value = plugin.getConfiguration().getItemShortcutHeadValue();
improveDeathChestCompatibility = plugin.getConfiguration().isItemShortcutImproveDeathChestCompatibilityEnabled(); improveDeathChestCompatibility = plugin.getConfiguration().isItemShortcutImproveDeathChestCompatibilityEnabled();
blockAsHat = plugin.getConfiguration().isItemShortcutBlockAsHatEnabled(); blockAsHat = plugin.getConfiguration().isItemShortcutBlockAsHatEnabled();
allowRightClickOnContainers = plugin.getConfiguration().isItemShortcutRightClickOnContainerAllowed(); allowRightClickOnContainers = plugin.getConfiguration().isItemShortcutRightClickOnContainerAllowed();
preferredSlotId = plugin.getConfiguration().getItemShortcutPreferredSlotId(); preferredSlotId = plugin.getConfiguration().getItemShortcutPreferredSlotId();
messageDoNotRemoveItem = plugin.getLanguage().getMessage("Ingame.DontRemoveShortcut"); messageDoNotRemoveItem = plugin.getLanguage().getMessage("Ingame.DontRemoveShortcut");
itemConfig = new BackpacksConfig(plugin).getItemConfig("Items." + plugin.getConfiguration().getItemShortcutItemName());
if(itemConfig == null)
{
plugin.getLogger().severe("Item '" + plugin.getConfiguration().getItemShortcutItemName() + "' is not defined in the backpacks.yml file! Item shortcut will be disabled!");
throw new IllegalArgumentException("The item is not defined.");
}
if(allowRightClickOnContainers) if(allowRightClickOnContainers)
{ {
containerMaterials.add(Material.CHEST); containerMaterials.add(Material.CHEST);
@ -85,7 +90,7 @@ public ItemShortcut(Minepacks plugin)
private boolean isItemShortcut(@Nullable ItemStack stack) private boolean isItemShortcut(@Nullable ItemStack stack)
{ {
//noinspection ConstantConditions //noinspection ConstantConditions
return stack != null && stack.getType() == HeadUtils.HEAD_MATERIAL && stack.hasItemMeta() && itemName.equals(stack.getItemMeta().getDisplayName()); return stack != null && stack.getType() == itemConfig.getMaterial() && stack.hasItemMeta() && itemConfig.getDisplayName().equals(stack.getItemMeta().getDisplayName());
} }
private void addItem(Player player) private void addItem(Player player)
@ -109,11 +114,11 @@ else if(isItemShortcut(itemStack))
ItemStack stack = player.getInventory().getItem(preferredSlotId); ItemStack stack = player.getInventory().getItem(preferredSlotId);
if(stack == null || stack.getType() == Material.AIR) if(stack == null || stack.getType() == Material.AIR)
{ {
player.getInventory().setItem(preferredSlotId, HeadUtils.fromBase64(value, itemName, MINEPACKS_UUID)); player.getInventory().setItem(preferredSlotId, itemConfig.make(1));
return; return;
} }
} }
player.getInventory().addItem(HeadUtils.fromBase64(value, itemName, MINEPACKS_UUID)); player.getInventory().addItem(itemConfig.make(1));
} }
} }
} }

View File

@ -199,7 +199,18 @@ private void load()
pluginManager.registerEvents(itemFilter, this); pluginManager.registerEvents(itemFilter, this);
} }
if(config.isShulkerboxesDisable()) pluginManager.registerEvents(new DisableShulkerboxes(this), this); if(config.isShulkerboxesDisable()) pluginManager.registerEvents(new DisableShulkerboxes(this), this);
if(config.isItemShortcutEnabled()) pluginManager.registerEvents(new ItemShortcut(this), this); if(config.isItemShortcutEnabled())
{
try
{
pluginManager.registerEvents(new ItemShortcut(this), this);
}
catch(IllegalArgumentException ignored) {}
catch(Exception e)
{
e.printStackTrace();
}
}
//endregion //endregion
if(config.getFullInvCollect()) collector = new ItemsCollector(this); if(config.getFullInvCollect()) collector = new ItemsCollector(this);
worldBlacklist = config.getWorldBlacklist(); worldBlacklist = config.getWorldBlacklist();

View File

@ -7,7 +7,7 @@
<packaging>pom</packaging> <packaging>pom</packaging>
<properties> <properties>
<revision>2.3.5-RC1</revision> <revision>2.4-ALPHA-SNAPSHOT</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties> </properties>