#Changing a plugin to use CustomModelData?

1 messages · Page 1 of 1 (latest)

dire sapphire
#

I made a plugin a while ago that basically turns a specified block into a 'scrap block'. A scrap block can be right clicked and the player receives one of the x specified rewards. There's then a cooldown till the player can rightclick it again. There's also a particle right above the block to indicate that it's a scrap block.
My issue is, right now I can specify all the rewards the player can get, but I want this to include custommodeldata as well, seeing as I'll be working with a bunch of custom items.
Here's the part that lets an admin turn a block into a scrap block:

        switch (args[0]) {
            case "add" -> {
                if (args.length == 4) {
                    // convert args[1] to particle
                    Particle particle = validParticles.contains(args[1].toUpperCase()) ? Particle.valueOf(args[1].toUpperCase()) : Particle.ASH;
                    // convert args[2] to int
                    int cooldown = Integer.parseInt(args[2]);

                    // convert args[3] to List<ItemStack>
                    List<ItemStack> items = new ArrayList<>();
                    String[] itemStrings = args[3].split(",");
                    for (String itemString : itemStrings) {
                        Material material = Material.matchMaterial(itemString.trim());
                        if (material != null) {
                            ItemStack item = new ItemStack(material);
                            items.add(item);
                        }
                    }
                    ChestObject chest = new ChestObject(false, blockLoc, particle, cooldown, items);
                    pHandler.AddLoc(chest, sender);
                    return true;
                }```
How canI change it so that you can specify custommodeldata per given item? When it's not given it should just be custommodeldata 0.
#

An example of the command:
/sc add SCRAPE 5 white_dye,sand,gravel,diamond_pickaxe

#

SCRAPE is the particle type

#

5 is 5 seconds cooldown

#

So what I want to change it to is something like this:
/sc add SCRAPE 5 white_dye(1),sand,gravel,diamond_pickaxe(20)
where the numbers represent the custom model data

rotund summit
#

Well

#

You basically just need to parse your itemstring

#

Regex could help, or you can write something manual

dire sapphire
#

Yeah I was thinking about using regex, but Im just not sure how I'd store the custommodeldata specific to the item

rotund summit
dire sapphire
#

oh wait, I have an idea. What if I change this:

                  String[] itemStrings = args[3].split(",");
                  for (String itemString : itemStrings) {
                        Material material = Material.matchMaterial(itemString.trim());
                        if (material != null) {
                            ItemStack item = new ItemStack(material);
                            items.add(item);
                        }```
to something like this:
```java
                  String[] itemStrings = args[3].split(",");
                  for (String itemString : itemStrings) {
                        //seperate the (x) from the string, and the string from the x
                        //int custommodeldata = x. x = 0 if no brackets
                        Material material = Material.matchMaterial(itemString.trim());
                        if (material != null) {
                            ItemStack item = new ItemStack(material);
                            items.add(item);
                            // ????????
                        }

At the '????????', what do I do there? I mean, I need to store the custommodeldata, but how

rotund summit
#

Yeah that's the idea

#

I'd split it into a separate method to start with

dire sapphire
#

Split what into a seperate method?

rotund summit
#
private ItemStack parseItem(String input) {

}
dire sapphire
#

ah?

rotund summit
#

Let's start with that

#

We can then do our regex magic

#
private static final Pattern PATTERN = Pattern.compile("(\w+)\\(*(\d+)*\\)*");

private ItemStack parseItem(String input) {
  Matcher matcher = PATTERN.matcher(input);

  if(!matcher.matches()) {
    return null;
  }

  int groupCount = matcher.groupCount();

  if(groupCount == 0) {
    return null; // wtf
  }

  Material material = Material.matchMaterial(matcher.group(1));
  int model = (groupCount == 1 ? 0 : Integer.parseInt(matcher.group(2));

  ...
}
#

something like that

dire sapphire
rotund summit
#

regex stuff

dire sapphire
#

right...

rotund summit
#

there

#

It basically grabs the pattern and matches it to our input

dire sapphire
#

and what's the groupcount?

rotund summit
#

allowing us to get our groups

dire sapphire
#

ahhh

rotund summit
#

p sure the groupcount check will fail because I might be an idiot

#

so you might want to check for null

#

I don't do regex often

dire sapphire
#

yeah no me neither

dire sapphire
rotund summit
#

modeldata is part of the itemmeta

#

ItemStack stack = ...
ItemMeta meta = stack.getItemMeta

meta.setCustomModelData(model)
stack.setItemMeta(meta)

#

basic itemstack api

dire sapphire
#

See, if I use a command like this:
/sc add SCRAPE 5 white_dye(1),sand,gravel,diamond_pickaxe(20)
The middle 2 won't have a custom custommodeldata, so if it's stored it would look like this right?
items: white_dye, sand, gravel, diamond_pickaxe
models: 1, 20

dire sapphire
#

So, in this case

ItemStack item = new ItemStack(material);
                            items.add(item);```
item would store both the material and custommodeldata?
rotund summit
#

think about it for a second

rotund summit
#

white_dye(1),sand,gravel,diamond_pickaxe(20) -> ["white_dye(1)", "sand", "gravel", "diamond_pickaxe(20)"]

#

Convert every entry to an itemstack

#

white_dye(1)
group 1 : white_dye
group 2 : 1

sand
group 1 : sand
group 2 : undefined

gravel
group 1 : gravel
group 2 : undefined

diamond_pickaxe(20)
group 1 : diamond_pickaxe
group 2 : 20

#

bim bim bam bam

#
Collection<ItemStack> items = new ArrayList<>();
for(String input : myArg.split(",")) {
  items.add(parseItem(input);
}
dire sapphire
#

👍 I get it, I think

#

Let me try to implement it

dire sapphire
#

Also, I don't necessarily need to do this right?
if(groupCount == 0) {
return null; // wtf
}
If I leave that out, 0 will just be stored as custommodeldata

rotund summit
#

Add another \

#

Uhh no

dire sapphire
#

wdym

#

oh nevermind

#

group is the amount of arguments in arg[3]

#

or items listed

dire sapphire
#

@rotund summit the line you wrote for me when helping doesn't really work and I don't see what's wrong with it

  private ItemStack parseItem(String input) {
        Matcher matcher = PATTERN.matcher(input);

        if (!matcher.matches()) return null;
        int groupCount = matcher.groupCount();
        if (groupCount == 0) return null;

        Material material = Material.matchMaterial(matcher.group(1));
        int model = (groupCount == 1 ? 0 : Integer.parseInt(matcher.group(2)));     // This causes the error
        if (material != null) {
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            if (meta == null) return item;
            meta.setCustomModelData(model);
            item.setItemMeta(meta);
            return item;
            }
        return null;
    }```
[19:22:49 INFO]: JustinS_2006 issued server command: /sc add ASH 5 sand,grass(5),dirt(200)
[19:22:49 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'sc' in plugin ScrapCollection v1.0
    ....
    at java.lang.Thread.run(Thread.java:833) [?:?]
Caused by: java.lang.NumberFormatException: Cannot parse null string
    ...
    at me.JustinS_2006.particles.AddParticleCommand.parseItem(AddParticleCommand.java:117) ~[?:?]
    at me.JustinS_2006.particles.AddParticleCommand.onCommand(AddParticleCommand.java:69) ~[?:?]
    ...
#

The error only happens when one of the items don't have brackets

#

if I change it to /sc add ASH 5 sand(3),grass(5),dirt(200) it does work PERFECTLY which I thank you for so incredibly much