1

I'm making a minecraft-forge mod and I'm having problems putting an String into Block#getBlockFromName(String name). the String:

String oreName = "minecraft:iron_ore -replace:minecraft:stone.minecraft:netherrack.minecraft:end_stone;";

And like you see after -replace: I have an block name (minecraft:stone) and a dot and than again block name and a dot. I want to split each block name into a separate String and read it one by one so I can insert it into Block#getBlockFromName and get 1 block out of each String. I tried using String#split(".") to split it into an array, but when I print out the array when I switched it back to String by using Arrays#toString it was empty. I want to split it by dots because I have an config file so those blocks can be changed to anything, I can't just pull out minecraft:stone out of the String because the ones who are using my mod will be able to configure that minecraft:stone to something else and add another block name by using a dot after the first block name. This is what I've done so far:

String oreName = "minecraft:iron_ore -replace:minecraft:stone.minecraft:netherrack.minecraft:end_stone;";

    String block1 = oreName.substring(oreName.indexOf("-replace:"));
    String block2 = block1.replace("-replace:", "");
    String block3 = block2.contains(" -") ? block2.substring(0, block2.indexOf(" ")) : block2.replace(";", "");
    System.out.println("The Block Names Are: " + block3); // It prints it just without "minecraft:iron_ore -replace:" and ";" at the end
    String[] block4 = block3.split(".");
    System.out.println("The array: " + Arrays.toString(block4)); // only prints out "The array: []"
Terrails
  • 67
  • 1
  • 9
  • 1
    Possible duplicate of [Split String on dot . as delimiter](http://stackoverflow.com/questions/3387622/split-string-on-dot-as-delimiter) – esin88 Apr 21 '17 at 20:28
  • Problem almost entirely due to the duplicate older question. Also, dot is probably not a good delimiter anyway. As soon as a mod block comes along with a name like, say, `mymod:ores.copper` your system falls apart. `;`, `!`, or `|` would be better. – Draco18s no longer trusts SE Apr 27 '17 at 16:21

1 Answers1

0

Problem is that String.split() takes regular expression as argument. For your case you need to escape . symbol with:

String[] block4 = block3.split("\\.");

Which prints

The array: [minecraft:stone, minecraft:netherrack, minecraft:end_stone]
esin88
  • 3,091
  • 30
  • 35