Added talisman inventories

This commit is contained in:
Auxilor
2021-01-02 18:38:17 +00:00
parent 2f4dc1aca9
commit 6cb3ee22ef
9 changed files with 148 additions and 9 deletions

View File

@@ -7,7 +7,7 @@ plugins {
dependencies {
implementation project(":eco-core").getSubprojects()
implementation 'com.willfp:eco:1.0.2'
implementation 'com.willfp:eco:1.0.3'
}
allprojects {

View File

@@ -3,6 +3,6 @@ version rootProject.version
subprojects {
dependencies {
compileOnly 'com.willfp:eco:1.0.2'
compileOnly 'com.willfp:eco:1.0.3'
}
}

View File

@@ -6,8 +6,11 @@ import com.willfp.eco.util.interfaces.EcoRunnable;
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
import com.willfp.eco.util.protocollib.AbstractPacketAdapter;
import com.willfp.talismans.commands.CommandTaldebug;
import com.willfp.talismans.commands.CommandTalismans;
import com.willfp.talismans.commands.CommandTalreload;
import com.willfp.talismans.config.TalismansConfigs;
import com.willfp.talismans.data.PlayerData;
import com.willfp.talismans.data.inventory.TalismanInventoryListeners;
import com.willfp.talismans.display.packets.PacketChat;
import com.willfp.talismans.display.packets.PacketOpenWindowMerchant;
import com.willfp.talismans.display.packets.PacketSetCreativeSlot;
@@ -62,6 +65,7 @@ public class TalismansPlugin extends AbstractEcoPlugin {
@Override
public void disable() {
this.getExtensionLoader().unloadExtensions();
PlayerData.save();
}
/**
@@ -90,6 +94,8 @@ public class TalismansPlugin extends AbstractEcoPlugin {
}
}, 1);
});
PlayerData.save();
}
/**
@@ -121,7 +127,8 @@ public class TalismansPlugin extends AbstractEcoPlugin {
public List<AbstractCommand> getCommands() {
return Arrays.asList(
new CommandTaldebug(this),
new CommandTalreload(this)
new CommandTalreload(this),
new CommandTalismans(this)
);
}
@@ -151,7 +158,8 @@ public class TalismansPlugin extends AbstractEcoPlugin {
return Arrays.asList(
new WatcherTriggers(this),
new BlockPlaceListener(),
new TalismanCraftListener()
new TalismanCraftListener(),
new TalismanInventoryListeners()
);
}

View File

@@ -0,0 +1,28 @@
package com.willfp.talismans.commands;
import com.willfp.eco.util.command.AbstractCommand;
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
import com.willfp.talismans.data.inventory.TalismanInventoryUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class CommandTalismans extends AbstractCommand {
/**
* Instantiate a new /talismans command handler.
*
* @param plugin The plugin for the commands to listen for.
*/
public CommandTalismans(@NotNull final AbstractEcoPlugin plugin) {
super(plugin, "talismans", "talismans.inventory", true);
}
@Override
public void onExecute(@NotNull final CommandSender sender,
@NotNull final List<String> args) {
Player player = (Player) sender;
TalismanInventoryUtils.showInventory(player);
}
}

View File

@@ -9,10 +9,15 @@ public class Data extends BaseConfig {
* data.yml.
*/
public Data() {
super("data.yml", false);
super("data", false);
}
/**
* Get the config file.
*
* @return The file.
*/
public File getFile() {
return super.configFile;
return this.getConfigFile();
}
}

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.willfp.talismans.config.TalismansConfigs;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -29,7 +30,8 @@ public final class PlayerData {
* The player's talisman inventory.
*/
@Getter
private final Map<Integer, ItemStack> inventory = new HashMap<>();
@Setter
private Map<Integer, ItemStack> inventory = new HashMap<>();
private PlayerData(@NotNull final UUID uuid) {
this.uuid = uuid;
@@ -76,6 +78,7 @@ public final class PlayerData {
return data;
}
return new PlayerData(player.getUniqueId());
PLAYER_DATA.put(player.getUniqueId(), new PlayerData(player.getUniqueId()));
return get(player);
}
}

View File

@@ -0,0 +1,63 @@
package com.willfp.talismans.data.inventory;
import com.willfp.talismans.data.PlayerData;
import com.willfp.talismans.talismans.util.TalismanChecks;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class TalismanInventoryListeners implements Listener {
/**
* On inventory close.
*
* @param event The event to listen for.
*/
@EventHandler
public void onTalismanInventoryClose(@NotNull final InventoryCloseEvent event) {
if (!event.getView().getTitle().toLowerCase().contains("talisman")) {
return;
}
Map<Integer, ItemStack> contents = new HashMap<>();
for (int i = 0; i < event.getInventory().getSize(); i++) {
contents.put(i, event.getInventory().getItem(i));
}
PlayerData.get((Player) event.getPlayer()).setInventory(contents);
}
/**
* On add item to talisman inventory.
*
* @param event The event to listen for.
*/
@EventHandler
public void onPutItemInTalismanInventory(@NotNull final InventoryClickEvent event) {
if (!event.getView().getTitle().toLowerCase().contains("talisman")) {
return;
}
if (event.getClick() == ClickType.SHIFT_LEFT || event.getClick() == ClickType.SHIFT_RIGHT) {
ItemStack item = event.getCurrentItem();
if (TalismanChecks.getTalismanOnItem(item) == null) {
event.setCancelled(true);
}
}
if (Objects.equals(event.getClickedInventory(), event.getView().getTopInventory())) {
ItemStack item = event.getCursor();
if (TalismanChecks.getTalismanOnItem(item) == null) {
event.setCancelled(true);
}
}
}
}

View File

@@ -0,0 +1,25 @@
package com.willfp.talismans.data.inventory;
import com.willfp.talismans.data.PlayerData;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
@UtilityClass
public class TalismanInventoryUtils {
/**
* Show talisman inventory to player.
*
* @param player The player to show the inventory to.
* @return The created inventory.
*/
public Inventory showInventory(@NotNull final Player player) {
Inventory inventory = Bukkit.createInventory(null, 54, "Talisman Inventory");
PlayerData.get(player).getInventory().forEach(inventory::setItem);
player.openInventory(inventory);
return inventory;
}
}

View File

@@ -2,7 +2,7 @@ name: Talismans
version: ${projectVersion}
main: com.willfp.talismans.TalismansPlugin
api-version: 1.15
authors: [Auxilor]
authors: [ Auxilor ]
website: willfp.com
load: STARTUP
depend:
@@ -28,6 +28,9 @@ commands:
taldebug:
description: Debug information
permission: talismans.taldebug
talismans:
description: Shows talisman inventory
permission: talismans.inventory
permissions:
talismans.*:
@@ -36,6 +39,7 @@ permissions:
children:
talismans.reload: true
talismans.taldebug: true
talismans.talismans: true
talismans.fromtable.*: true
talismans.reload:
@@ -44,6 +48,9 @@ permissions:
talismans.taldebug:
description: Allows the use of /taldebug to print verbose debug information to console
default: op
talismans.inventory:
description: Allows the use of /talismans to show the player's talisman inventory.
default: true
talismans.fromtable.*:
description: Allows crafting all talismans
default: true