My Money voucher plugin is actually working, but not like as I want

Discussion in 'Plugin Development' started by stimoze, Nov 2, 2016.

Thread Status:
Not open for further replies.
  1. I had an idea to make a plugin what gives other players vouchers with a command, Everything went good, but i found a little bug.

    The bug is when I give a voucher to someone (Lets say that someone is p1), it's lore is OK. But if i give another one to anybody, (Lets say he/she's p2) It's like:
    Code:
    From:
    Me
    For:
    p1
    Money:
    500
    From:
    Me
    For:
    p2
    Money:
    500
    If you still don't get what I mean check the pictures I uploaded.,

    My class code:
    Code:
    package soleplugin;
    
    import java.util.ArrayList;
    
    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.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Moneyvoucher implements CommandExecutor, Listener {
       
        public static Org plg;
        public Moneyvoucher(Org org) {
            plg = org;
        }
    
        static ItemStack voucher = new ItemStack(Material.PAPER);
        static ItemMeta meta = voucher.getItemMeta();
        static ArrayList<String> lore = new ArrayList<String>();
       
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender send, Command cmd, String label, String[] args) {
            Player p = (Player) send;
            StringBuilder sb = new StringBuilder();
            for(int i = 3; i < args.length; i++){
                sb.append(args[i]).append(" ");
            }
            String msgArgs = sb.toString().trim();
                if (cmd.getName().equalsIgnoreCase("uvc")){
                    if (args.length == 0){
                        send.sendMessage("§b§l[ §9§lV o u c h e r s §b§l]");
                        send.sendMessage("/uvc send §e- §fGift someone with a money voucher");
                        send.sendMessage("§b§l================");
                    }
                    else if (args[0].equalsIgnoreCase("send")){
                        try{
                                if (msgArgs.isEmpty()){
                                    if (Org.economy.getBalance(p.getName()) >= Integer.valueOf(args[2])){
                                        //VoucherItem
                                        meta.setDisplayName("Money voucher");
                                        lore.add(0,"§f§lFrom:");
                                        lore.add(1,p.getName());
                                        lore.add(2,"§f§lFor:");
                                        lore.add(3,args[1]);
                                        lore.add(4,"§a§lMoney:");
                                        lore.add(5,args[2]);
                                        lore.add(6,"§eRight click to activate it!");
                                        meta.setLore(lore);
                                        voucher.setItemMeta(meta);
                                        //end
                                        Bukkit.getPlayerExact(args[1]).getInventory().addItem(new ItemStack[] { voucher });
                                        Org.economy.withdrawPlayer(args[1], Integer.valueOf(args[2]));
                                        send.sendMessage("[Output] Successfully sent a voucher for " + args[1] + ".");
                                        Bukkit.getPlayerExact(args[1]).sendMessage("§b[§9Vouchers§b] §fYou have received a voucher from §e" + p.getName() + "§f.");
                                        Bukkit.getPlayerExact(args[1]).sendMessage("§b[§9Vouchers§b] §fA [§a" + args[2] + "$§f] voucher was added to your inventory.");
                                    }else{
                                        send.sendMessage("[Output] You don't have enough money to send a voucher.");
                                    }
                                }
                                else{
                                    if (Org.economy.getBalance(p.getName()) >= Integer.valueOf(args[2])){
                                        //VoucherItem
                                        meta.setDisplayName("Money voucher");
                                        lore.add(0,"§f§lFrom:");
                                        lore.add(1,p.getName());
                                        lore.add(2,"§f§lFor:");
                                        lore.add(3,args[1]);
                                        lore.add(4,"§a§lMoney:");
                                        lore.add(5,args[2]);
                                        lore.add(6,"§cMessage:");
                                        lore.add(7,"§b"+msgArgs);
                                        lore.add(8,"§eRight click to activate it!");
                                        meta.setLore(lore);
                                        voucher.setItemMeta(meta);
                                        //end
                                        Bukkit.getPlayerExact(args[1]).getInventory().addItem(new ItemStack[] { voucher });
                                        Org.economy.withdrawPlayer(args[1], Integer.valueOf(args[2]));
                                        send.sendMessage("[Output] Successfully sent a voucher for " + args[1] + " with text: " + msgArgs);
                                        Bukkit.getPlayerExact(args[1]).sendMessage("§b[§9Vouchers§b] §fYou have received a voucher from §e" + p.getName() + "§f.");
                                        Bukkit.getPlayerExact(args[1]).sendMessage("§b[§9Vouchers§b] §fA [§a" + args[2] + "$§f] voucher was added to your inventory.");
                                        Bukkit.getPlayerExact(args[1]).sendMessage("§cMessage: §f" + msgArgs);
                                    }else{
                                        send.sendMessage("[Output] You don't have enough money to send a voucher.");
                                    }
                                }
                        }catch(Exception ex){
                            send.sendMessage("[Output] Wrong syntax. /uvc send <username> <amount> [text]");
                        }
                       
                    }
                }
            return true;
        }
       
        @EventHandler
        public void action (PlayerInteractEvent e){
            if (e.getPlayer().getItemInHand().isSimilar(voucher)){
                if (lore.get(3).equals(e.getPlayer().getName())){
                    if (e.getAction().equals(Action.RIGHT_CLICK_AIR) | e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                        Org.economy.depositPlayer(e.getPlayer(), Integer.valueOf(lore.get(5)));
                        e.getPlayer().getInventory().remove(voucher);
                        e.getPlayer().sendMessage("§b[§9Vouchers§b] §fA [§a" + lore.get(5) + "$§f] voucher was used. " + "§a" + lore.get(5)+ "$ §fwas added to your account.");
                    }
                }
            }
        }       
    }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @stimoze Your voucher, meta, and lore variables should be in the onCommand, not as static variable.
    Now when somebody runs it it fills lore, second time it just adds to lore, repeat till server crash.
     
  3. Offline

    Whoneedspacee

    You aren't resetting the variable it looks like, should have defined a local one in the command section so you didn't have to bother.
     
  4. But then how do i call the voucher itemstack if it is in the command section?
     
  5. Offline

    timtower Administrator Administrator Moderator

    @stimoze Then you check the material instead of the isSimilar
     
  6. Code:
        @EventHandler
        public void action (PlayerInteractEvent e){
            ItemMeta imeta = e.getPlayer().getItemInHand().getItemMeta();
            ArrayList<String> lore = new ArrayList<String>();
            imeta.setLore(lore);
            if (e.getPlayer().getItemInHand().equals(Material.PAPER)){
                if (lore.get(3).equals(e.getPlayer().getName())){
                    if (e.getAction().equals(Action.RIGHT_CLICK_AIR) | e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                        Org.economy.depositPlayer(e.getPlayer(), Integer.valueOf(lore.get(5)));
                        e.getPlayer().getInventory().remove(e.getPlayer().getItemInHand());
                        e.getPlayer().sendMessage("§b[§9Vouchers§b] §fA [§a" + lore.get(5) + "$§f] voucher was used. " + "§a" + lore.get(5)+ "$ §fwas added to your account.");
                    }
                }
            }
        }    
    I have this code. It's actually now doing nothing.
     
  7. Offline

    Tecno_Wizard

    @stimoze

    Use binary logical OR, not unary binary. It's faster if the expression short circuits. And make sure with a sysout that the func is even firing.

    For the duplicating lore, I've run into this before. I'm guessing that you're using a seed stack and editing it, correct? Unfortunately, you would need to clone that seed first or alias behavior kicks in and you get what you are seeing. ItemMeta might not be cloned in this process so you many need to explicitly clone that as well. See how it works out.
     
    Last edited: Nov 2, 2016
  8. Offline

    timtower Administrator Administrator Moderator

    @stimoze getIteminhand.getType==paper
    And don't set the lore in the event, why would you do that?
    Set the lore in the onCommand, using a local variable.
    In the event you get the lore on the item, then you check that.
    Your original code would work on any piece of paper
     
  9. Offline

    mythbusterma

    @Tecno_Wizard

    There is no unary "or," such a thing wouldn't make sense (it would look like " | expression"). You mean binary or, and that actually is inconsequential in this case, the only advantage logical or provides on boolean expressions in Java is short-circuit evaluation.
     
  10. @timtower Your method worked! Thanks to you I can publish the first version. Thank you :)

    @Tecno_Wizard I don't understand what is your problem with "|" instead of "||" Both are the same

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 2, 2016
  11. Offline

    timtower Administrator Administrator Moderator

  12. Offline

    Tecno_Wizard

    I have a bone to pick with my Java prof tomorrow. You're right.
     
  13. Offline

    Zombie_Striker

    @stimoze
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page