Solved Object setup

Discussion in 'Spigot Plugin Development' started by zDoeProgram, Feb 8, 2019.

  1. Hey, im making a horse plugin, and I want to be able to spawn in horses with some abilities from either eggs or command.

    Now, I attempted on spawning in with eggs, but it got really messy, and I dont see how I can connect the horse with the given id. My IQ is overblown, im not that good in Java yet :p

    Anyways, was wondering if any good developers could just help me out to the next step.
    This is a list I've been following which I made
    TODO:
    - Create Horse object
    - Create Horse manager
    - setup custom yml file for each horse after given ID

    Code (Java):
    public class HorseObject {
       
        private UUID owner;
        private Player spawner;
        private Location loc;
        private final int horseID;
        private HashSet<UUID> members;
        private String name, jumpTrainig, speedTrainig;
        private double jumpHeight, speed;
        private int hunger, fitness;
        private final Gender gender;
        private final Personality personality;
        private Type type;
        private Injury injury;
        private Illness illness;
       
       
        public HorseObject(
                UUID owner,
                int horseID, int fitness, int hunger,
                String name,
                Gender gender, Personality personality, Type type, Injury injury, Illness illness,
                String jumpTrainig, String speedTrainig,
                double jumpHeight, double speed){
           
            this.owner = owner;
            this.horseID = horseID;
            this.fitness = fitness;
            this.hunger = hunger;
            this.name = name;
            this.gender = gender;
            this.type = type;
            this.injury = injury;
            this.illness = illness;
            this.personality = personality;
            this.jumpTrainig = jumpTrainig;
            this.speedTrainig = speedTrainig;
            this.jumpHeight = jumpHeight;
            this.speed = speed;
        }
       
        public HorseObject(
                int horseID, int fitness, int hunger,
                String name,
                Gender gender, Personality personality, Type type, Injury injury, Illness illness,
                String jumpTrainig, String speedTrainig,
                double jumpHeight, double speed){
           
            this.horseID = horseID;
            this.fitness = fitness;
            this.hunger = hunger;
            this.name = name;
            this.gender = gender;
            this.type = type;
            this.injury = injury;
            this.illness = illness;
            this.personality = personality;
            this.jumpTrainig = jumpTrainig;
            this.speedTrainig = speedTrainig;
            this.jumpHeight = jumpHeight;
            this.speed = speed;
        }

       
        public Player getSpawner() {
            return spawner;
        }
       
        public UUID getOwner() {
            return owner;
        }

        public void setOwner(UUID owner) {
            this.owner = owner;
        }
     
           // A bunch of getters and setters down here only...

    }

    Code (Java):
    public class HorseManager {
        public HashSet<HorseObject> horses = new HashSet<HorseObject>();

       
        public void createHorseByCommand(
                UUID owner,
                int horseID, int fitness, int hunger,
                String name, Gender gender, Personality personality, Type type, Injury injury, Illness illness,
                String jumpTrainig, String speedTrainig,
                double jumpHeight, double speed){
           
            this.horses.add(new HorseObject(owner, horseID, fitness, hunger, name, gender, personality, type, injury, illness, jumpTrainig, speedTrainig, jumpHeight, speed));
           
            HorseFile file = GCMCHorses.getPlugin().getHorseFile();
           
            file.getOrLoad(horseID).set("Horse" + ".owner", owner.toString());
            file.getOrLoad(horseID).set("Horse" + ".name", name);
            file.getOrLoad(horseID).set("Horse" + ".fitness", String.valueOf(fitness));
            file.getOrLoad(horseID).set("Horse" + ".hunger", String.valueOf(hunger));
            file.getOrLoad(horseID).set("Horse" + ".gender", String.valueOf(gender));
            file.getOrLoad(horseID).set("Horse" + ".personality", String.valueOf(personality));
            file.getOrLoad(horseID).set("Horse" + ".type", String.valueOf(type));
            file.getOrLoad(horseID).set("Horse" + ".jumpTrainig", jumpTrainig);
            file.getOrLoad(horseID).set("Horse" + ".speedTrainig", speedTrainig);
            file.getOrLoad(horseID).set("Horse" + ".injury", String.valueOf(injury));
            file.getOrLoad(horseID).set("Horse" + ".illness", String.valueOf(illness));
            file.getOrLoad(horseID).set("Horse" + ".jumpHeight", String.valueOf(jumpHeight));
            file.getOrLoad(horseID).set("Horse" + ".speed", String.valueOf(speed));
            file.getOrLoad(horseID).set("Horse" + ".raiders", "");
            file.save(horseID);
           
            //file.getOrLoad(horseID).set("Horse" + ".", );
           
        }
       
        public void createHorseBySpawnEgg(
                int horseID, int fitness, int hunger,
                String name, Gender gender, Personality personality, Type type, Injury injury, Illness illness,
                String jumpTrainig, String speedTrainig,
                double jumpHeight, double speed) {
           
            this.horses.add(new HorseObject(horseID, fitness, hunger, name, gender, personality, type, injury, illness, jumpTrainig, speedTrainig, jumpHeight, speed));
           
            HorseFile file = GCMCHorses.getPlugin().getHorseFile();
           
            file.getOrLoad(horseID).set("Horse" + ".owner", "");
            file.getOrLoad(horseID).set("Horse" + ".name", "");
            file.getOrLoad(horseID).set("Horse" + ".fitness", String.valueOf(fitness));
            file.getOrLoad(horseID).set("Horse" + ".hunger", String.valueOf(hunger));
            file.getOrLoad(horseID).set("Horse" + ".gender", String.valueOf(gender));
            file.getOrLoad(horseID).set("Horse" + ".personality", String.valueOf(personality));
            file.getOrLoad(horseID).set("Horse" + ".type", String.valueOf(type));
            file.getOrLoad(horseID).set("Horse" + ".jumpTrainig", jumpTrainig);
            file.getOrLoad(horseID).set("Horse" + ".speedTrainig", speedTrainig);
            file.getOrLoad(horseID).set("Horse" + ".injury", String.valueOf(injury));
            file.getOrLoad(horseID).set("Horse" + ".illness", String.valueOf(illness));
            file.getOrLoad(horseID).set("Horse" + ".jumpHeight", String.valueOf(jumpHeight));
            file.getOrLoad(horseID).set("Horse" + ".speed", String.valueOf(speed));
            file.getOrLoad(horseID).set("Horse" + ".raiders", "");
            file.save(horseID);
           
        }
       
        public HorseObject getHorse(int horseID) {
            for(HorseObject horseObject : this.horses) {
                if (horseObject.getHorseID() == horseID) {
                    return horseObject;
                }
            }
            return null;
        }
       
       
       
        public void deleteHorse(int horseID){
            HorseFile file = GCMCHorses.getPlugin().getHorseFile();
            try {
                file.getHorseFile(horseID).delete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        public HashSet<HorseObject> getHorses() {
            return horses;
        }
       
       

    }

    Code (Java):
    public class HorseFile {
       
        private static final String FOLDER = "horse-data";
        public static File horseDataFile;
        public static YamlConfiguration horseData;
       
        private final Map<Integer, FileConfiguration> configs = new HashMap<>();
       
        public static File getHorseDataFile() {
            return horseDataFile;
        }
       
        public static YamlConfiguration getHorseData() {
            return horseData;
        }
       
        public FileConfiguration getOrLoad(int id)
        {
            FileConfiguration config = null;
            try {
                config = configs.getOrDefault(id, YamlConfiguration.loadConfiguration(getHorseFile(id)));
            } catch (IOException e) {
                e.printStackTrace();
            }
           
            configs.put(id, config);
            return config;
        }
       
       
        public File getHorseFile(int id) throws IOException
        {
            File dataFolder = new File(GCMCHorses.getPlugin().getDataFolder(), FOLDER);
            if (!dataFolder.exists())
            {
               
                dataFolder.mkdirs();
            }

            File file = new File(GCMCHorses.getPlugin().getDataFolder(), FOLDER + File.separator + id + ".yml");

            if (!file.exists())
            {
               
                file.createNewFile();
            }

            return file;
        }
       
       
        public void save(int id)
        {
            File file = null;
            try {
                file = getHorseFile(id);
            } catch (IOException e) {
               
                e.printStackTrace();
            }
           
            FileConfiguration config = configs.remove(id);
            if (config == null)
            {
                return;
            }
           
            try {
                config.save(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    Now, I just want to give each horse 2 abilitties, which are speed and jump.

    I have created the code for that here:
    Code (Java):
    //RANDOM SPEED
                    Random randomSpeed = new Random();
                    double minSpeed = 5.0;
                    double maxSpeed = 12.0;
                    double randomSpeedNumber = minSpeed + (maxSpeed - minSpeed) * randomSpeed.nextDouble();
                    DecimalFormat dfSpeed = new DecimalFormat("#.##");
                    double formattedSpeed = Double.parseDouble(dfSpeed.format(randomSpeedNumber));
                   
                    //RANDOM Jump HEIGHT
                    Random randomJumpHeight = new Random();
                    double minJumpHeight = 1.0;
                    double maxJumpHeight = 3.0;
                    double randomJumpHeightNumber = minJumpHeight + (maxJumpHeight - minJumpHeight) * randomJumpHeight.nextDouble();
                    DecimalFormat dfJumpHeight = new DecimalFormat("#.##");
                    double formattedJumpHeight = Double.parseDouble(dfJumpHeight.format(randomJumpHeightNumber));
    The rest of the variables inside the contructor is not really related to the minecraft settings of the horse. I am just having difficulities imagining how to for example spawn in a horse using the horse egg, and then give that horse and ID and the rest of the variables.

    Code (Java):
    @EventHandler
        public void onSpawnHorse(PlayerInteractEvent event) {
            if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getMaterial() == Material.MONSTER_EGG) {
                Player p = event.getPlayer();
               
                ItemStack mainHand = p.getEquipment().getItemInMainHand();
                SpawnEggMeta eggMeta = (SpawnEggMeta) mainHand.getItemMeta();
               
                if(eggMeta.getSpawnedType() == EntityType.HORSE) {
                    event.setCancelled(true);
                   
                    BlockFace face = event.getBlockFace();
                    Location loc = event.getClickedBlock().getRelative(face).getLocation();
                   
                    Horse horse = (Horse) p.getWorld().spawnEntity(loc, EntityType.HORSE);

    //I cant think on how to add the id and everything to the horse.

                }
               
            }
        }

    How the ID of the horse is chosen:
    Code (Java):
    int id = GCMCHorses.getPlugin().getHorseManager().getHorses().size() + 1;
    IF anyone could give me an example to the next step or a list, that would be very helpfull. Thanks <3
     
  2. To connect the Horse with an ID you can do this:
    Code (Java):
    HashMap<Integer, HorseClass> horses = new HashMap<Integer, HorseClass>();
    in the HorseManager class
     
    #2 de_cifris, Feb 9, 2019
    Last edited: Feb 9, 2019
  3. By the horseclass, do you mean the actuall NMS horseclass or my horse object class?
     
  4. HorseObject class
     
  5. I am having difficulities trying to save and load that hashmap. It gives an error when I try to load them from my onEnable method if I understand it correctly.

    My hashmap:
    Code (Java):
    HashMap<UUID, HorseObject> horses = new HashMap<UUID, HorseObject>();
    My save and load method:

    Code (Java):
    public void save(){
            for (Entry<UUID, HorseObject> uuid : horses.entrySet()) {
                GCMCHorses.getPlugin().getConfig().set(String.valueOf(uuid.getKey()), uuid.getValue());
            }
            GCMCHorses.getPlugin().saveConfig();
        }
       
        public void load(){
           
            for(String str : GCMCHorses.getPlugin().getConfig().getKeys(false)) {
                UUID uuid = UUID.fromString(str);
                HorseObject horseObject = (HorseObject) GCMCHorses.getPlugin().getConfig().get(str);
                horses.put(uuid, horseObject);
            }
        }
    Not able to print out the error right now, but if anyone see what I did wrong in those methods. please help me. When I save the file it gets stored as this in my config file:

    Code (YAML):
    3e5369ee-c5be-409e-bd7b-b337ec9b0bf4: !!me.beastcraft3.gcmchorses.object.HorseObject {}
     
  6. Because you have to store in the config files the information you need. You stored the entire object.

    Correct way:
    Code (Java):
    public void save(){
    for (Entry<UUID, HorseObject> uuid : horses.entrySet()) {
    HorseObject horse = uuid.getValue();
    GCMCHorses.getPlugin().getConfig().set(String.valueOf(uuid.getKey()) + ".id", horse.getId());
    GCMCHorses.getPlugin().getConfig().set(String.valueOf(uuid.getKey()) + ".owner", horse.getOwner());
    //TODO other parameters that needs to be stored.
    }
    GCMCHorses.getPlugin().saveConfig();
    }
    For the load method, you just need to recreate the HorseObject class by loading stored information.
     
  7. I still get the error when I try to reload:

    Code (Text):
    [22:35:55 ERROR]: Error occurred while enabling GCMCHorses v1.0.0 (Is it up to date?)
    java.lang.ClassCastException: org.bukkit.configuration.MemorySection cannot be cast to me.beastcraft3.gcmchorses.object.HorseObject
            at me.beastcraft3.gcmchorses.manager.HorseManager.load(HorseManager.java:55) ~[?:?]
            at me.beastcraft3.gcmchorses.GCMCHorses.onEnable(GCMCHorses.java:21) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload(CraftServer.java:752) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.Bukkit.reload(Bukkit.java:525) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1399) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1234) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    [22:35:55 INFO]: Server permissions file permissions.yml is empty, ignoring it
    Code (Java):
    public void save(){
            for (Entry<UUID, HorseObject> uuid : horses.entrySet()) {
                HorseObject horse = uuid.getValue();
                GCMCHorses.getPlugin().getConfig().set(String.valueOf(uuid.getKey()) + ".name", horse.getName());
            }
            GCMCHorses.getPlugin().saveConfig();
        }
       
        public void load(){
           
            for(String str : GCMCHorses.getPlugin().getConfig().getKeys(false)) {
                UUID uuid = UUID.fromString(str);
                HorseObject horseObject = (HorseObject) GCMCHorses.getPlugin().getConfig().get(str);
                horses.put(uuid, horseObject);
            }
        }
    This is the line which gives me the error:
    Code (Java):
    HorseObject horseObject = (HorseObject) GCMCHorses.getPlugin().getConfig().get(str);
     

Share This Page