Solved Making food item edible even if hunger is full

Discussion in 'Spigot Plugin Development' started by Poofyjerry, Mar 27, 2020.

  1. On my server the players have full hunger all the time.
    However, I would like them to be able to eat cake when they have it even if their hunger is full, sort of like a golden apple.

    I tried this lil snippet of code here but its sortve dodgy, looking for easier way cause my server keeps crashing and i get this error java.lang.AssertionError: TRAP when i try changing the item type to null.
    Code (Text):

      public void onInteract(PlayerInteractEvent e) {
        if (e.getAction().equals(Action.RIGHT_CLICK_AIR)
          || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
          ItemStack item = e.getItem();
          if (item == null) {
            return;
          }
          Player player = e.getPlayer();
     
          //ECONOMY
          if(item.getType().equals(Material.CAKE)) {
            player.incrementStatistic(Statistic.CAKE_SLICES_EATEN, 1);
            ItemStack[] findSunflower = player.getInventory().getContents();
            for(int i = 0; i < findSunflower.length; i++) {
              player.sendMessage("Hi");
              if(findSunflower[i] == null) {
                continue;
              }
              if(findSunflower[i].getType() == null) {
                continue;
              }
              if(findSunflower[i].getType().equals(Material.CAKE)){
                findSunflower[i].setType(Material.COAL);
                break;
              }
            }
            player.getInventory().setContents(findSunflower);
            return;
          }
          }
    }
     
    The reasoning for this is I am going to use the CAKE_SLICES_EATEN as the economy on my server :p
    THANK YOU
     
    #1 Poofyjerry, Mar 27, 2020
    Last edited: Mar 27, 2020
  2. Stupid idea, but if you are keeping your players hunger bar at full all the time anyways, why not just keep it at full minus half a hunger bar and when they eat something, instantly set it back to that?
     
  3. Use ==‘s for enums, and you should swap your logic to return instead of indent.

    Instead of

    if action is what you want:
    # code here

    you should do

    if action is NOT what you want:
    return

    # code here



    try setting the item to air instead of null
     
    • Like Like x 1
  4. Thanks boss, im new to plugin developement