I made a Player Vault Plugin but it isn't tracking the items right

Discussion in 'Spigot Plugin Help' started by HackedPlayz, Jun 19, 2021.

  1. I made a Player Vault Plugin using a YouTube tutorial but It only keeps track of the items if its also in your inventory I have been looking around for how to fix but i cant seem to

    This is the main Class
    package hack.playz.portableec.main.Portableec;


    import hack.playz.portableec.main.Portableec.commands.opencommand;
    import hack.playz.portableec.main.Portableec.listeners.listeners;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.Objects;

    public final class VaultPlugin extends JavaPlugin {
    private static VaultPlugin plugin;

    public static VaultPlugin Plugin;




    @Override
    public void onEnable() {
    // Plugin startup logic

    plugin = this;

    Objects.requireNonNull(getCommand("vault")).setExecutor(new opencommand());

    getServer().getPluginManager().registerEvents(new listeners(), this);

    }

    @Override
    public void onDisable() {
    // Plugin shutdown logic
    }
    }

    This is to open the command

    package hack.playz.portableec.main.Portableec.commands;

    import hack.playz.portableec.main.Portableec.utils.VaultUtils;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import java.util.ArrayList;

    public class opencommand implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {


    if (commandSender instanceof Player){

    Player p = (Player) commandSender;
    if (args.length > 0){
    if (args[0].equalsIgnoreCase("open")){


    ArrayList<ItemStack> vaultItems = VaultUtils.getItems(p);

    Inventory vault = Bukkit.createInventory(p,54,"Personal Vault");

    vaultItems.stream()
    .forEach(itemStack -> vault.addItem(itemStack));

    p.openInventory(vault);

    }
    }

    }



    return true;
    }
    }

    This is the listener

    package hack.playz.portableec.main.Portableec.listeners;

    import hack.playz.portableec.main.Portableec.VaultPlugin;
    import hack.playz.portableec.main.Portableec.utils.VaultUtils;
    import org.bukkit.NamespacedKey;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.persistence.PersistentDataContainer;
    import org.bukkit.persistence.PersistentDataType;

    import java.util.ArrayList;
    import java.util.Arrays;

    public class listeners implements Listener {

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e){

    Player p = e.getPlayer();

    PersistentDataContainer data = p.getPersistentDataContainer();

    if (!data.has(new NamespacedKey(VaultPlugin.getPlugin(VaultPlugin.class),"vault"), PersistentDataType.STRING)){
    data.set(new NamespacedKey(VaultPlugin.getPlugin(VaultPlugin.class),"vault"), PersistentDataType.STRING,"");
    }
    }

    @EventHandler
    public void onInventoryClose(InventoryCloseEvent e){

    Player p = (Player) e.getPlayer();

    if (e.getView().getTitle().equalsIgnoreCase("Personal Vault")){

    ArrayList<ItemStack> prunedItems = new ArrayList<>();

    Arrays.stream(p.getInventory().getContents())
    .filter(itemStack -> {
    if (itemStack == null){
    return false;
    }
    return true;
    })
    .forEach(itemStack -> prunedItems.add(itemStack));

    VaultUtils.storeItems(prunedItems, p);

    }}}

    This helps with the vault

    import org.bukkit.NamespacedKey;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.persistence.PersistentDataContainer;
    import org.bukkit.persistence.PersistentDataType;
    import org.bukkit.util.io.BukkitObjectInputStream;
    import org.bukkit.util.io.BukkitObjectOutputStream;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Base64;
    import java.util.List;

    public class VaultUtils {

    public static void storeItems(List<ItemStack> items, Player p){

    PersistentDataContainer data = p.getPersistentDataContainer();

    if (items.size() == 0){
    data.set(new NamespacedKey(VaultPlugin.getPlugin(VaultPlugin.class),"vault"), PersistentDataType.STRING,"");
    }else{

    try{
    ByteArrayOutputStream io = new ByteArrayOutputStream();
    BukkitObjectOutputStream os = new BukkitObjectOutputStream(io);


    os.writeInt(items.size());

    for (int i = 0; i < items.size(); i++){
    os.writeObject(items.get(i));
    }

    os.flush();

    byte[] rawData = io.toByteArray();

    String encodedData = Base64.getEncoder().encodeToString(rawData);

    data.set(new NamespacedKey(VaultPlugin.getPlugin(VaultPlugin.class),"vault"), PersistentDataType.STRING,encodedData);

    os.close();

    }catch (IOException ex){
    System.out.println();
    }
    }
    }

    public static ArrayList<ItemStack> getItems(Player p){

    PersistentDataContainer data = p.getPersistentDataContainer();

    ArrayList<ItemStack> items = new ArrayList<>();

    String encodedItems = data.get(new NamespacedKey(VaultPlugin.getPlugin(VaultPlugin.class),"vault"), PersistentDataType.STRING);

    if (!encodedItems.isEmpty()){

    byte[] rawData = Base64.getDecoder().decode(encodedItems);

    try{

    ByteArrayInputStream io = new ByteArrayInputStream(rawData);
    BukkitObjectInputStream in = new BukkitObjectInputStream(io);

    int itemsCount = in.readInt();

    for (int i = 0; i < itemsCount; i++){
    items.add((ItemStack) in.readObject());
    }

    in.close();

    }catch (IOException | ClassNotFoundException ex){
    System.out.println(ex);
    }

    }

    return items;
    }
    }

    I can send the plugin.yml if i need to
     
  2. Okay couple things, firstly please insert it as code cause else it's a pain to read. And secondly, do you understand this code at all?