Solved Create percentage with equation

Discussion in 'Plugin Development' started by ScopeTowel, Feb 13, 2020.

  1. Offline

    ScopeTowel

    Well, what I would like to do is a kind of plugin that calculates the number of diamonds that you have mined and puts it as a percentage according to the total number of diamonds mined by the players connected. Here is the problem, when I do the calculation but only 1 player is concerned, no problem he mined 100% of the blocks, but if another player mines one, it puts the percentages of all players at 0, you have a idea? Here's my code!

    Main Class :
    Code:
    package qc.scopetowel.anticheat;
    
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import qc.scopetowel.anticheat.commands.CmdLuck;
    import qc.scopetowel.anticheat.events.EventBreakDiamond;
    
    import java.util.HashMap;
    
    public class main extends JavaPlugin {
    
    
        public static main instance;
        public HashMap<Player, Integer> diamondbacks = new HashMap<>();
    
        public static main getInstance(){
            return instance;
        }
    
        @Override
        public void onEnable(){
            instance = this;
    
            System.out.println("Le plugin : ModerationToolsCheat s'est bien allumer");
            getCommand("topluck").setExecutor(new CmdLuck(this));
            getServer().getPluginManager().registerEvents(new EventBreakDiamond(this), this);
    
    
            super.onEnable();
        }
    
        @Override
        public void onDisable() {
            System.out.println("Le plugin : ModerationToolsCheat s'est bien eteint");
    
            super.onDisable();
        }
    }
    
    Event Class :

    Code:
    package qc.scopetowel.anticheat.events;
    
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import qc.scopetowel.anticheat.main;
    
    import java.util.HashMap;
    
    public class EventBreakDiamond implements Listener {
    
    
        main pl;
    
        public EventBreakDiamond(main main) {
            this.pl = main;
        }
    
        @EventHandler
        public void DiamondBreak(BlockBreakEvent e){
            Player p = e.getPlayer();
            Block block = e.getBlock();
    
            if(block.getType() == Material.DIAMOND_ORE){
                if(!pl.diamondbacks.containsKey(p)){ pl.diamondbacks.put(p, 0); }
                Integer GetDiamondHave = pl.diamondbacks.get(p);
                Integer diamondAfter = GetDiamondHave +1;
                pl.diamondbacks.put(p, diamondAfter);
    
            }
    
    }
    
    
    }
    


    Commands :
    Code:
    package qc.scopetowel.anticheat.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.SkullMeta;
    import qc.scopetowel.anticheat.events.EventBreakDiamond;
    import qc.scopetowel.anticheat.main;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    
    
    public class CmdLuck implements CommandExecutor {
        main pl;
    
        public CmdLuck(main main) {
            this.pl = main;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] args) {
            Player p = (Player) sender;
            /*
            1 - Récupéré tous les joueurs en ligne
            2 - Ouvrir un inventaire avec la tête de chaque joueur
            3 - Faire un listener des blocks de diamants cassé
            4 - Faire un système de calcul de block cassé
            5 - Afficher les blocks cassé sur sa tête
            6 - Interaction de quand on clique sur sa tête
             */
            if(cmd.getName().equalsIgnoreCase("topluck")) {
    
                int nbPlayer = Bukkit.getOnlinePlayers().size();
                Inventory invcheat = Bukkit.createInventory(null, 54, "Players : §6" + nbPlayer);
    
    
                int totalblock = 0;
    
                for(Player onlineplayer : Bukkit.getOnlinePlayers()){
                    if(pl.diamondbacks.get(onlineplayer) == null) { pl.diamondbacks.put(onlineplayer, 0);}
                        totalblock = totalblock + pl.diamondbacks.get(onlineplayer);
                }
    
                for (Player ponline : Bukkit.getOnlinePlayers()) {
                    p.sendMessage("Le nombre total de block miné est de : " + totalblock);
                    p.sendMessage(ponline.getName() + pl.diamondbacks.get(ponline));
                    ItemStack crane = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
                    SkullMeta craneM = (SkullMeta) crane.getItemMeta();
                    craneM.setDisplayName("Statistique de : §6" + ponline.getName());
                    float division = (pl.diamondbacks.get(ponline) / totalblock) * 100;
                    p.sendMessage(String.valueOf(division));
    
    
                    craneM.setLore(Arrays.asList("§7Minerais de diamants miné : §c" + pl.diamondbacks.get(ponline), "§7Pourcentage de minerais de diamants miné : §c" + division + "%"));
                    craneM.setOwningPlayer(ponline);
                    crane.setItemMeta(craneM);
                    invcheat.addItem(crane);
    
                }
    
                p.openInventory(invcheat);
    
    
                /*todo
                   1- Faire un système de place
                   2 - Interaction
                 */
    
    
    
            }
            return false;
        }
    }
    
     
  2. Online

    DerDonut

    Dividing a smaller number with a greater number will always result in a value between 0 and 1. That's why you multiplied it by 100 which is correct. The problem is that you divide two integers resulting in an integer quotient (either 0 or 1 there is no integer in between). Make ponline or totalblock a float and your problem should be fixed :) (or just cast it when dividing)
     
  3. Offline

    ScopeTowel

    Thanks !
     

Share This Page