Untitled

mail@pastecode.io avatar
unknown
java
3 years ago
3.4 kB
2
Indexable
Never
    public void execute() {
        try {

            HashMap<Integer, NBTCompound> changedItems = new HashMap<>();

            UUID playerUUID = playerDatFile.getUUID("UUID");
            NBTCompoundList playerInventory = playerDatFile.getCompoundList("Inventory");
            HashMap<UUID, FoundItemModel> itemsFound = new HashMap<UUID, FoundItemModel>();
            ArrayList<InsertItemAction> databaseAction = new ArrayList<InsertItemAction>();

            System.out.println("Before-> \t" + playerInventory.toString());

            for (int itemStackIndex : itemStackIndexes.keySet()) {
                NBTCompound item = playerInventory.get(itemStackIndex);
                if (ItemStackNBT.hasUUID(item)) {
                    UUID itemUUID = ItemStackNBT.getParsedUUID(item);
                    if (!itemsFound.containsKey(itemUUID)) {
                        FoundItemModel model = new FoundItemModel(itemUUID, itemStackIndexes.get(itemStackIndex).getId());
                        model.setQuantityFound(ItemStackNBT.getCount(item));
                        itemsFound.put(itemUUID, model);
                        continue;
                    }
                    itemsFound.get(itemUUID).setQuantityFound(itemsFound.get(itemUUID).getQuantityFound() + ItemStackNBT.getCount(item));
                    continue;
                }
                ItemStackNBT.generateUUID(item);
                databaseAction.add(new InsertItemAction(item, playerUUID.toString()));
                changedItems.put(itemStackIndex, item);
            }

            if (changedItems.size() > 0) {
                System.out.println("Player " + this.getPlayerName() + " had his inventory updated. Saving file.");
                if (Bukkit.getPlayer(this.getPlayerName()) != null) {
                    Player p = Bukkit.getPlayer(this.playerName);
                    // Update the player that is online directly via bukkit
                    for (Entry<Integer, NBTCompound> items : changedItems.entrySet()) {
                        p.getInventory().setItem(items.getKey(), NBTItem.convertNBTtoItem(items.getValue()));
                        System.out.println("Setting item to player " + p.getName());
                    }

                } else {
                    playerDatFile.save();
                }
            }

            // Updating database if the check continue

            for (InsertItemAction action : databaseAction) {
                AntiDupe.getInstance().getRareItemChecks().getDatabase().saveItemToDatabase(action);
            }

            System.out.println("After-> \t" + playerInventory.toString());

            for (Entry<UUID, FoundItemModel> foundItem : itemsFound.entrySet()) {
                ItemModel model = AntiDupe.getInstance().getRareItemChecks().getConfiguration().getItemCheckModel(foundItem.getValue().getModelID());

                for (ActionModel action : model.getActions()) {
                    if (action.checkAndExecuteAction(this.getPlayerName(), model, foundItem.getValue().getQuantityFound())) {
                        // Action executed
                        return;
                    }
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }