Save health of entity in Durability

Discussion in 'Plugin Development' started by CraterHater, Jun 28, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    CraterHater

    I am making a plugin that summons and despawns mobs upon right clicking a saddle. I want to make it so that when the player despawn the entity he stores the health of the entity in the custom durability of the saddle and so make it that if the entity dies and the death cause is not by the player despawning the saddle breaks. How do I do that?

    Here is my code for spawning and de-spawning, this all works:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
                                                                                                          public void onItemStackRightClick1(PlayerInteractEvent e)
                                                                                                          {
                                                                                                            Player player = e.getPlayer();
                                                                                                            if(player.getItemInHand().getType() == Material.SADDLE)
                                                                                                              if(player.getItemInHand().getItemMeta().hasDisplayName())
                                                                                                                 
                                                                                                                 
                                                                                                                 
                                                                                                              {
                                                                                                                      Player p = e.getPlayer();
                                                                                                                  if (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + p.getName() + "'s §5Mount tier §2one"))
                                                                                                                      
                                                                                                                  {
                                                                                                                      int value4 = getConfig().getInt(p.getName() + ".Spawn");
                                                                                                                      if(value4 == 0) {
                                                                                                                      player.sendMessage(ChatColor.LIGHT_PURPLE + "[Dragons]:§5You spawned your §2 green dragon");
                                                                                                                      Location loc = p.getLocation();
                                                                                                                      Entity irongolem = loc.getWorld().spawnEntity(loc, EntityType.BAT);
                                                                                                                      irongolem.setCustomName(ChatColor.GREEN + p.getName() + "'s §5Mount tier §2one");
                                                                                                                      Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "entitydata " + irongolem.getUniqueId() + " {NoAI:1}");
                                                                                                                      giveSpawn(p, 1);
                                                                                                                     
                                                                                                                  }else{   
                                                                                                                      if (Cooldowns.tryCooldown(p, "x19", 5000)) {
                                                                                                                      int value5 = getConfig().getInt(p.getName() + ".Spawn");
                                                                                                                      if(value5 == 1) {
                                                                                                                      p.sendMessage(ChatColor.LIGHT_PURPLE + "[Dragons]:§5You despawned your §2 green dragon");
                                                                                                                      for(Entity bat : p.getWorld().getEntities()) {
                                                                                                                          if(bat instanceof Bat) {
                                                                                                                              if(bat.getCustomName() != null)
                                                                                                                                  if(bat.getCustomName().equals(ChatColor.GREEN + p.getName() + "'s §5Mount tier §2one"))
                                                                                                                                      ((Bat)bat).setHealth(0.0D);
                                                                                                                                         
                                                                                                                                          setSpawn(p, 0);               
                                                                                                        
     
  2. I only see this in your code
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
     
    Last edited: Jun 28, 2015
    KingFaris11 and mine-care like this.
  3. Offline

    CraterHater

    @FisheyLP scroll to the right... My eclipse is a bit weird
     
  4. Off-topic: I died of laughter IRL when reading this
     
    Zombie_Striker and CraterHater like this.
  5. Offline

    CraterHater

    Why?
     
  6. No sorry, I was replying to his comment (saying about what he sees in your code)

    On-topic though, I can't help you as I don't understand what you're trying to do exactly. E.g.
    What's this "custom durability" thing..
     
  7. Offline

    CraterHater

    @KingFaris11 That custom durability is like this: When you right click on a specific named saddle it summons a mob, this is done, but the thing is that any damage the mob has taken can be restored by simple despawning and respawning it. I want the damage that the mob takes to be transferred into durability like a sword on the saddle so that I can take the durability of the saddle and transform that back into the health of the mob. And when the durability hits 0 it destroy the saddle. NOTE: the despawning system sets the mobs health to 0 so that shouldn't get transformed into durability.
     
  8. Hmmm, why can't you set the saddle's durability to: (short) entity.getHealth() (yes it'd lose precision as health is a double, not a short) and then when using the saddle to spawn a mob, if the durability is greater than or equal to 1, set the entity's health to the item's durability.

    For handling entity death, I don't really deal with entities much so I don't know much about this and this solution isn't fully complete:
    When a player spawns an entity using the saddle, add a custom metavalue of 'Owner' with the value of the player's UUID.

    Then, in the EntityDeathEvent handler method, check if the entity has an 'Owner' tag, if so, get the player (by the UUID). Loop through every item in the player's inventory, check if the item corresponds to that entity (somehow, not sure here. Maybe set the item's lore to the entity UUID), and if so, remove it from the player's inventory.

    Issues with this:
    • If a player puts their saddle in an ender chest or a chest, this won't remove the item.
    • The UUID has to be in the item lore, which may look ugly.
     
    Last edited: Jun 28, 2015
  9. Offline

    CraterHater

    @KingFaris11 note that a saddle does not have an durability on its own like a sword so how do I make the custom durability
     
  10. All items have a data value but only tools, armour, fishing rods, flint and steel and a few more items have a durability bar. Setting a saddle's durability should still work (in replacement for using data values), but it just won't show a damage bar on the item. I'm not sure if it's possible to make a damage bar for a saddle.
     
  11. Offline

    bobacadodl

    Its not possible to make the damage bar appear
     
  12. Offline

    CraterHater

    @bobacadodl why not? Minecraft is coded in java and I am making a plugin In Java so its possible...
     
  13. Offline

    bobacadodl

    The durability bar is client side, not server side. You would need to make a mod
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page