Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few suggestions #11

Open
Gershon123 opened this issue Nov 10, 2021 · 0 comments
Open

A few suggestions #11

Gershon123 opened this issue Nov 10, 2021 · 0 comments

Comments

@Gershon123
Copy link

Gershon123 commented Nov 10, 2021

Hi there, I forked your plugin and it's working really well. I have a couple of suggestions that I implemented that would improve this plugin.

Here is a gif:
https://i.imgur.com/U5FRm9j.mp4

  • Replace "Buy & exit" & "Buy and back" with just "Back" & "Buy", if a user want to buy and exit they can just press Esc after buying something
  • Show the quantity of the item in the menu. This will restrict to buy 64 at a time, but I'm okay with that.
  • Replace quantity options to "Set to 1", "Remove 1", "Remove 10", "Add 1", "Add 10", "Set to 64". This improved the quantity experience, my users were confused with the original
  • Remove the "Clear" option since we auto start at 1 time, so that option is not needed
  • Hide "Sell" when you can't sell an item
  • Add a /sellhand command, this allows users to not need to open the shop (I will provided a snippet)
  • Replace the shoplist in the config with just loading shops by their file name, this resolved some of the saving issues I had and others reported (I will provide a snippet)

I would create a pull request but I'm running off your API 7 fork, and I'm not sure if you're okay with some of my changes.

Snippets:
Sellhand

package sawfowl.guishopmanager.utils.commands;

import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.data.type.HandTypes;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.query.QueryOperationTypes;
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.api.text.serializer.TextSerializers;
import sawfowl.guishopmanager.GuiShopManager;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

public class SellHand implements CommandExecutor {

    GuiShopManager plugin;

    public SellHand(GuiShopManager instance) {
        plugin = instance;
    }

    @Override
    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
        if (!(src instanceof Player)) {
            src.sendMessage(plugin.getLocales().getLocalizedText(src.getLocale(), "Messages", "OnlyPlayer"));
        } else {
            Player player = (Player) src;
            Optional<ItemStack> itemStackOptional = player.getItemInHand(HandTypes.MAIN_HAND);
            AtomicBoolean foundItem = new AtomicBoolean(false);
            if (itemStackOptional.isPresent()) {
                ItemStack itemStack = itemStackOptional.get();
                plugin.getAllShops().forEach(shop -> {
                    shop.getMenus().values().forEach(shopMenuData -> {
                        shopMenuData.getItems().values().forEach(shopItem -> {
                            if (shopItem.isSell() && itemStackEqualsIgnoreSize(shopItem.getItemStack(), itemStack)) {
                                foundItem.set(true);
                                Optional<ItemStack> playerHand = player.getItemInHand(HandTypes.MAIN_HAND);
                                if (playerHand.isPresent() && playerHand.get().equalTo(itemStack)) {
                                    player.setItemInHand(HandTypes.MAIN_HAND, null);
                                    shopItem.getPrices().forEach(serializedShopPrice -> {
                                        Task.builder().async().execute(() -> {
                                            plugin.getEconomy().addToPlayerBalance(player, serializedShopPrice.getCurrency(), serializedShopPrice.getSellPrice(), itemStack);
                                        }).submit(plugin);
                                    });
                                } else {
                                    player.sendMessage(TextSerializers.FORMATTING_CODE.deserialize("&cSale cancelled."));
                                }
                            }
                        });
                    });
                });
            }
            if (!foundItem.get()) {
                player.sendMessage(TextSerializers.FORMATTING_CODE.deserialize("&cThis item is not sold."));
            }
        }
        return CommandResult.success();
    }

    public static boolean itemStackEqualsIgnoreSize(ItemStack o1, ItemStack o2) {

        ItemStack copy1 = o1.copy();
        ItemStack copy2 = o2.copy();

        copy1.setQuantity(1);
        copy2.setQuantity(1);

        return copy1.equalTo(copy2);
    }

}

Loading shops as files

    @Override
    public void loadShops() {
        Task.builder().async().execute(() -> {
            try {
                File[] files = plugin.getConfigDir().resolve(plugin.getRootNode().getNode("StorageFolder").getString()).toFile().listFiles();
                for (File file : files) {
                    try {
                        ConfigurationLoader<CommentedConfigurationNode> shopConfigLoader = HoconConfigurationLoader.builder().setPath(file.toPath()).build();
                        CommentedConfigurationNode shopNode = shopConfigLoader.load();
                        String fname = file.getName();
                        int pos = fname.lastIndexOf(".");
                        if (pos > 0) {
                            fname = fname.substring(0, pos);
                        }
                        plugin.addShop(fname, shopNode.getNode("ShopData").getValue(TypeToken.of(SerializedShop.class)).deserialize());
                    } catch (Exception e) {
                        plugin.getLogger().error("Failed to load " + file.getName());
                        plugin.getLogger().error(e.getLocalizedMessage());
                    }
                }
            } catch (Exception e) {
                plugin.getLogger().error(e.getLocalizedMessage());
            }
        }).submit(plugin);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant