Custom Chest/Sign Shop - How do I store the player's shop?

Discussion in 'Spigot Plugin Development' started by TheGamerDoug, Jul 1, 2018.

  1. Hello!

    I want to figure out a way to hold a sign shops owner, even after a reload/restart, and when the sign is removed, it gets removed from storage as well. I have got as far as trying to save the data, but, my config doesn't want to work for me.
    Code (Text):
    @EventHandler
        public void onSignPlace(SignChangeEvent event) {
            Player player = event.getPlayer();
            if (event.getBlock().getType() == Material.WALL_SIGN) {
                Sign sign = (Sign) event.getBlock().getState();
                org.bukkit.material.Sign signMat = (org.bukkit.material.Sign) event.getBlock().getState().getData();
                Block attached = event.getBlock().getRelative((signMat.getAttachedFace()));
                if (attached.getType() == Material.CHEST) {
                    if (sign.getLine(0).equalsIgnoreCase("[Buy]")) {
                        sign.setLine(0, ChatColor.GREEN + sign.getLine(0));
                        sign.update();
                        main.getShopsFile().set(player.getUniqueId() + ".shops." + event.getBlock().getWorld() + ".x", sign.getLocation().getBlockX());
                        main.getShopsFile().set(player.getUniqueId() + ".shops." + event.getBlock().getWorld() + ".y", sign.getLocation().getBlockY());
                        main.getShopsFile().set(player.getUniqueId() + ".shops." + event.getBlock().getWorld() + ".z", sign.getLocation().getBlockZ());
                        main.getShopsFile();
                        try {
                            main.getShopsFile().save(main.getShops());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

    This is what I have so far. I have a system in place that detects the sign on a chest and exchanges with the console, but I want players to be able to create their own shops, and sell to eachother, without the need of a console.

    Any help would be appreciated!

    Thank you!

    EDIT: I don't fully understand the best way to store my data (player who places the sign shop) without storing their name on the sign.
     
    #1 TheGamerDoug, Jul 1, 2018
    Last edited: Jul 2, 2018
  2. You have to write their name on the sign, so you can check who's the owner or you create a .yml file to save it.

    You can use this:
    Code (Text):


        private static File file;
        public static FileConfiguration data;

        File_Data() {
            file = new File("plugins/PLUGIN", "data.yml");
            data = YamlConfiguration.loadConfiguration(file);
            data.options().copyDefaults(true);
            data.addDefault("data.version", 1);
            saveData();
        }

        public static void saveData() {
            try {
                data.save(file);
                file = new File("plugins/PLUGIN", "data.yml");
                data = YamlConfiguration.loadConfiguration(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void loadData() {
            file = new File("plugins/PLUGIN", "data.yml");
            data = YamlConfiguration.loadConfiguration(file);
        }

    }
    To use:
    write in the onEnable function "File_Data data = new File_Data();"
    now you can access data with File_Data.data.getString()
     
    #2 Christian34, Jul 9, 2018
    Last edited: Jul 9, 2018
  3. When you discover that when you post code here everyone can use it and modify it. No copyright zorry
     
  4. The copyright signature was automatically written and I forgot to remove it. I'll remove it.
     
  5. Don't worry xD
     
  6. IMO, You should store all the values for a shop in a config, in a section for the coordinates of the sign. By this when the sign is clicked, you can pass the coords to the section getter and get all data for that sign including the item, sign owner etc. Just make sure to save the config everytime you add more data.

    For Example:

    Code (YAML):
    #Generated when sign is placed

    140-69-270
    : #Coordinates from X Y Z
        shopOwner
    : UUID-HERE
     
    Also if this is what your using to detect the sign creation, use a BlockPlaceEvent
     
  7. BlockPlaceEvent is not usable here. He wants to check what in the first line is written (sign.getLine(0).equalsIgnoreCase("[Buy]") and this is only possible with SignChangeEvent.