Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-6686

Changes in MaximumRepairCost for Anvil Rename cause inconsistency

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • None
    • None
    • None
    • CraftBukkit version 3207-Spigot-18c71bf-8537925 (MC: 1.17.1) (Implementing API version 1.17.1-R0.1-SNAPSHOT)
    • Yes

      If you change the value of MaximumRepairCost in PrepareAnvilEvent the client can show in anvil a different cost of the real.

       

      Code for test.

      @EventHandler
      public void onPrepareAnvilEvent(PrepareAnvilEvent event) {
          AnvilInventory inventory = event.getInventory();
          inventory.setMaximumRepairCost(50);
      
          final Player player = ((Player) event.getView().getPlayer());
      
          ItemStack leftItem = inventory.getItem(0);
          ItemStack rightItem = inventory.getItem(1);
      
          //check if both item slots are filled
          if (leftItem != null && rightItem != null) {
              player.sendMessage("TEST 1");
              handleLevelCost(player,event,inventory);
          } else if(leftItem != null) { // Start for detect rename
              if(inventory.getRenameText() != null && !inventory.getRenameText().isEmpty() && event.getResult() != null) { //Only Rename
                  handleLevelCost(player,event,inventory);
              }
          }
      }
      
      //Detect a player has click the item result in Anvil!
      @EventHandler
      public void onInventoryClick(InventoryClickEvent event) {
          // check whether the event has been cancelled by another plugin
          if (!event.isCancelled()) {
              HumanEntity humanEntity = event.getWhoClicked();
      
              if (humanEntity instanceof Player) {
                  Player player = (Player) humanEntity;
                  // Check if this event fired inside an anvil.
                  if (event.getInventory() instanceof AnvilInventory) {
                      final AnvilInventory anvilInventory = (AnvilInventory) event.getInventory();
                      InventoryView view = event.getView();
                      int rawSlot = event.getRawSlot();
      
                      // check if we are in the upper inventory of the anvil
                      if (rawSlot == view.convertSlot(rawSlot)) {
                          // check if we are talking about the result slot
                          if (rawSlot == 2) {
                              // get all 3 items in the anvil
                              ItemStack[] items = anvilInventory.getContents();
      
                              if (player.getLevel() < anvilInventory.getRepairCost() && !player.getGameMode().equals(GameMode.CREATIVE)) { // Handle level and vanilla feature for creative
                                  player.sendMessage("CANT PAY " + event.getResult()); // This return ALLOW
                              }
                          }
                      }
                  }
              }
          }
      }
      
      private void handleLevelCost(Player player, PrepareAnvilEvent event, AnvilInventory inventory) {
          int finalRepairCost = inventory.getRepairCost();
          player.sendMessage(ChatColor.RED + "Anvil Cost is: " + ChatColor.GREEN + finalRepairCost + ChatColor.RED + " levels.");
      }
      

      I test this using a sword and enchanting with every possible book for swords (in vanilla book per book for the RepairCost).. then when try to rename the item the anvil show cost 39 in GUI but the event show cost 64.


       

      If you have the levels for rename the item and try to click the item result the anvil take the levels but not rename the item cancel the process.

       

            Unassigned Unassigned
            Doc Doc
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: