Development Assistance seperating a line

Discussion in 'Plugin Help/Development/Requests' started by Scorpionvssub, Jul 22, 2016.

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

    Scorpionvssub

    I'm trying to seperate a line like

    - '20% cookie 10'

    Where cookie is the item so getmaterial but the 10 is the amount of said item..
    Im just a lil confused as to how to do this...as i doubt it will automaticly seperate the cookie and 10 and see it as item + amount.

    I know there are examples for : out there but was wondering if it can be done without the :
     
    Last edited: Jul 22, 2016
  2. @Scorpionvssub
    You can use
    Code:java
    1. String.split(" ")

    Which will split the string into an array of strings at every point in the string where " " is found.
     
  3. Offline

    Scorpionvssub

    @AlvinB But how then i mean okay i would do something like
    String[] words = line.split(" ");
    but how do i use/store the 10 into a different int


    Code:
            List<String> list = TTMain.rewards.getStringList("Rewards." + grade);
            for (String l : list) {
                rew.add(l);
            }
    
            Random r = new Random();
            for (int in = 0; in < rewards; in++) {
                String[] commands = new String[100];
                int x = 0;
    
                if (rew.size() == 0) return;
    
                for (String line : rew) {
                    String[] words = line.split(" ");
                    String percent = words[0];
                    int numPercent;
    
                    if (!percent.contains("%")) continue;
    
                    while (percent.endsWith("%"))
                        percent = percent.substring(0, percent.length() - 1);
    
                    try {
                        numPercent = Integer.valueOf(percent);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
    
                        continue;
                    }
    
                    StringBuilder command = new StringBuilder();
    
                    for (int i = 1; i < words.length; i++)
                        command.append(words[i]).append(" ");
    
                    for (int i = 0; i < numPercent && i < 100; i++)
                        commands[i + x] = command.toString();
                    x = numPercent + x;
                }
    it already splits the % from the whole thing and adds the specified line to a list 20 times but the 3rd entry in "20% cookie 10" the 10 has to be seperated aswell and i for some reason can't get my head around it
     
  4. Offline

    Scorpionvssub

    @AlvinB

    This all aint really helpfull but ill give it 1 more shot..

    Code:
                int amount = 0;
                for (String line : commands) {
                    String[] c = line.split(" ");
                    amount = Integer.parseInt(c[1]);
                }
    The amount returns a number correctly but there is more of an issue..

    Whenever i use this method for deciding how many of a certain item should be used...

    i get this:
    22.07 18:13:13 [Server] [Informatie] Caused by: java.lang.NullPointerException
    22.07 18:13:13 [Server] [Informatie] at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:68)

    While the only thing i did was add something and attempt to remove it

    i can guess the error but cant resolve the fix
     
  5. Offline

    Scorpionvssub

    fine and all but this doesn't helpt at all mainly cause changing the 1 to a 0 will for some reason grab the item instead and the 1 will cause the issue above.

    while completely removing the addition of an amount makes it all work just fine
     
    Last edited: Jul 22, 2016
  6. @Scorpionvssub
    Hmm, can you post your full class, so we can try to figure out why you get the error described?
     
  7. Offline

    Scorpionvssub

    Code:
        public void reward(Player p, int rewards, String grade) {
            ArrayList<String> rew = new ArrayList<>();
            ArrayList<String> rews = new ArrayList<>();
            List<String> list = TTMain.rewards.getStringList("Rewards." + grade);
            for (String l : list) {
                rew.add(l);
            }
    
            Random r = new Random();
            for (int in = 0; in < rewards; in++) {
                String[] commands = new String[100];
                int x = 0;
    
                if (rew.size() == 0) return;
    
                for (String line : rew) {
                    String[] words = line.split(" ");
                    String percent = words[0];
                    int numPercent;
    
                    if (!percent.contains("%")) continue;
    
                    while (percent.endsWith("%"))
                        percent = percent.substring(0, percent.length() - 1);
    
                    try {
                        numPercent = Integer.valueOf(percent);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
    
                        continue;
                    }
    
                    StringBuilder command = new StringBuilder();
    
                    for (int i = 1; i < words.length; i++)
                        command.append(words[i]).append(" ");
    
                    for (int i = 0; i < numPercent && i < 100; i++)
                        commands[i + x] = command.toString();
                    x = numPercent + x;
                }
    
                p.sendMessage("Commands " + Arrays.toString(commands));
    
                int amount = 0;
                for (String line : commands) {
                    String[] c = line.split(" ");
                    amount = Integer.parseInt(c[0]);
                }
    
                int changes = r.nextInt(100);
    
                String chosenCommand = commands[changes];
                p.sendMessage(ChatColor.DARK_GREEN + "amount " + amount);
    
                if (chosenCommand != null) {
                    chosenCommand = chosenCommand.substring(0, chosenCommand.length() - 1);
    
                    ItemStack is = new ItemStack(Material.getMaterial(chosenCommand), 1);
                    rews.add(String.valueOf(is.getType().name()));
                    p.getInventory().addItem(is);
                    p.updateInventory();
                }
            }
            plugin.sendConfigMessage(p, "rewards", String.valueOf(rews));
        }
    Array.toString(Commands) returns many...MANY <item> <amount>

    while amount returns the <amount> picked the parseint(c[0]); turned into 1 grabs that amount while 0 grabs <item> which command already does. i need to get the <amount> away from the command or <item> into a seperate section then at the item stack where the ,1 is replace that with the amount as some items are more than 1 of said item while a dia sword having 39 of those is not good.. X)
     
  8. @Scorpionvssub
    If you use c[1], it should work, can you post the full stacktrace of the error above?
     
  9. Offline

    Scorpionvssub

    Code:
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_91]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
        at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:68) ~[spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        at me.neo.tt.Blocks.reward(Blocks.java:202) ~[?:?]
        at me.neo.tt.Blocks.onBlockInteract(Blocks.java:64) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_91]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot1.8.new.jar:git-Spigot-db6de12-18fbb24]
        ... 17 more
    @AlvinB
    ItemStack is = new ItemStack(Material.getMaterial(chosenCommand), 1);

    so for some reason it removes something during its steps..
     
  10. @Scorpionvssub
    I found your issue. Because you are making an array called "commands" and initialize it to contain 100 objects, when you loop through it you will get all 100 objects, and a lot of them will be null. What I recommend is using a List, because it grows dynamically (it changes its size when you add/remove something from it).
     
  11. Offline

    Scorpionvssub

    unfortunatly this is the only way i know how to make a randomizer for items via a % like i said it works great without the "amount" stuff so no idea where to start with ur suggestion honestly :p

    I mean i can make lists sure but...
     
Thread Status:
Not open for further replies.

Share This Page