Jump to content

How to get All Woods and their Planks?


JoeBox9

Recommended Posts

How to get All Woods and their Planks? I want to shorten this piece of code and easily add compatibility for other mods. I want to get logs as an array and their planks as an array, so I can go through a loop and shorten this:

   if (offHandItem.is(Items.OAK_LOG)) {
            offHandItem.shrink(1);
            ItemStack woodStack = Items.OAK_PLANKS.getDefaultInstance();
            woodStack.grow(5);
            player.addItem(woodStack);
        }
        if (offHandItem.is(Items.SPRUCE_LOG)) {
            offHandItem.shrink(1);
            ItemStack woodStack = Items.SPRUCE_PLANKS.getDefaultInstance();
            woodStack.grow(5);
            player.addItem(woodStack);
        }
        if (offHandItem.is(Items.BIRCH_LOG)) {
            offHandItem.shrink(1);
            ItemStack woodStack = Items.BIRCH_PLANKS.getDefaultInstance();
            woodStack.grow(5);
            player.addItem(woodStack);
        }
        if (offHandItem.is(Items.JUNGLE_LOG)) {
            offHandItem.shrink(1);
            ItemStack woodStack = Items.JUNGLE_PLANKS.getDefaultInstance();
            woodStack.grow(5);
            player.addItem(woodStack);
        }
        if (offHandItem.is(Items. ACACIA_LOG)) {
            offHandItem.shrink(1);
            ItemStack woodStack = Items.ACACIA_PLANKS.getDefaultInstance();
            woodStack.grow(5);
            player.addItem(woodStack);
        }
        if (offHandItem.is(Items.DARK_OAK_LOG)) {
            offHandItem.shrink(1);
            ItemStack woodStack = Items.DARK_OAK_PLANKS.getDefaultInstance();
            woodStack.grow(5);
            player.addItem(woodStack);
        }
        if (offHandItem.is(Items.CRIMSON_HYPHAE)) {
            offHandItem.shrink(1);
            ItemStack woodStack = Items.CRIMSON_PLANKS.getDefaultInstance();
            woodStack.grow(5);
            player.addItem(woodStack);
        }
        if (offHandItem.is(Items.WARPED_HYPHAE)) {
            offHandItem.shrink(1);
            ItemStack woodStack = Items.WARPED_PLANKS.getDefaultInstance();
            woodStack.grow(5);
            player.addItem(woodStack);
        }
        if (offHandItem.is(Items.MANGROVE_LOG)) {
            offHandItem.shrink(1);
            ItemStack woodStack = Items.MANGROVE_PLANKS.getDefaultInstance();
            woodStack.grow(5);
            player.addItem(woodStack);
        }

I don't want to use a crafting recipe as it is way cooler to have it in your offhand and make wood.

Link to comment
Share on other sites

i do something similar and is complicated 

	 
	        public static Map<String, BlockState> blocklist = new HashMap<String, BlockState>();// list whit all blocks
	        Block actualBlock = null;
        String type = "";
        String nnn = "";
        int count = 0;
	 
	//load hashmap whit all blocks and fix their names 
	        for (Entry<ResourceKey<Block>, Block> ResourceKey : ForgeRegistries.BLOCKS.getEntries()) {
            actualBlock = ResourceKey.getValue();
	            nnn = fix_blkfullname(actualBlock.getName().getString());
            blocklist.put(nnn, actualBlock.defaultBlockState());
	            if (nnn.contains("slab")) {
                slablist.put(nnn, "");
            }
            count++;
        }
	 
	    // ########## ########## ########## ########## ########## ##########
    //
    public static String fix_blkfullname(String blkfullname) {
        if (blkfullname.contains(".")) {
            String[] split1 = blkfullname.split("\\."); // "\\."
	            if (split1.length > 1) {
                blkfullname = split1[split1.length - 1].replaceAll("_", " ");
            }
        }
	        return blkfullname.toLowerCase();
    }
	

is up to you to use the if (nnn.contains("slab")) { slablist.put(nnn, "");  }    to find all tha planks and logs in the list and sort them as it suits you 

 

Link to comment
Share on other sites

18 minutes ago, perromercenary00 said:

i do something similar and is complicated 
 

	 
	        public static Map<String, BlockState> blocklist = new HashMap<String, BlockState>();// list whit all blocks
	        Block actualBlock = null;
        String type = "";
        String nnn = "";
        int count = 0;
	 
	//load hashmap whit all blocks and fix their names 
	        for (Entry<ResourceKey<Block>, Block> ResourceKey : ForgeRegistries.BLOCKS.getEntries()) {
            actualBlock = ResourceKey.getValue();
	            nnn = fix_blkfullname(actualBlock.getName().getString());
            blocklist.put(nnn, actualBlock.defaultBlockState());
	            if (nnn.contains("slab")) {
                slablist.put(nnn, "");
            }
            count++;
        }
	 
	    // ########## ########## ########## ########## ########## ##########
    //
    public static String fix_blkfullname(String blkfullname) {
        if (blkfullname.contains(".")) {
            String[] split1 = blkfullname.split("\\."); // "\\."
	            if (split1.length > 1) {
                blkfullname = split1[split1.length - 1].replaceAll("_", " ");
            }
        }
	        return blkfullname.toLowerCase();
    }
	

 

is up to you to use the if (nnn.contains("slab")) { slablist.put(nnn, "");  }    to find all tha planks and logs in the list and sort them as it suits you 

 

It's giving me only this error:

/home/yusuf/IdeaProjects/Orangecraft/src/main/java/com/mys/orangecraft/Woodcutter.java:57: error: method put in class ConversionTable cannot be applied to given types;
                slablist.put(nnn, "");
                        ^
  required: Class<T>,Function<? super T,Object>
  found:    String,String
  reason: cannot infer type-variable(s) T
    (argument mismatch; String cannot be converted to Class<T>)
  where T is a type-variable:
    T extends Object declared in method <T>put(Class<T>,Function<? super T,Object>)

 

Edited by JoeBox9
Added word only to clarify
Link to comment
Share on other sites

  The way to get the list for all logs (including modded logs) is:

 ForgeRegistries.ITEMS.tags().getTag(ItemTags.LOGS).forEach(item -> blah);

and similar for ItemTags.PLANKS

But there is no association between the 2 lists except for that provided by the recipes.

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

8 minutes ago, warjort said:

  The way to get the list for all logs (including modded logs) is:

 ForgeRegistries.ITEMS.tags().getTag(ItemTags.LOGS).forEach(item -> blah);

and similar for ItemTags.PLANKS

But there is no association between the 2 lists except for that provided by the recipes.

How do I declare the blah var?

Link to comment
Share on other sites

That's for you to decide. "blah" is a placeholder for your code. 🙂 

You could also replace the .forEach() with .stream().toList() or .iterator() if you are not familiar with consumer iteration.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

6 minutes ago, warjort said:

That's for you to decide. "blah" is a placeholder for your code. 🙂 

You could also replace the .forEach() with .stream().toList() or .iterator() if you are not familiar with consumer iteration.

I used a list and appended to it like this:

        List<ITag> logs = null;
        for (Item item : ForgeRegistries.ITEMS.tags().getTag(ItemTags.LOGS)) {
            logs.add((ITag) item);
        }

It was an auto suggestion from Intellij, I was able to do planks like:

        for (i = 0; i > logs.size();i++) {
            if (offHandItem.is((Item) logs.get(i))) {

but I can't do:

 ItemStack woodStack = planks.get(i).getDeafultInstance();

It can't find it

Link to comment
Share on other sites

(Re)read what I wrote above about recipes.

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

You need to use the RecipeManager

serverLevel.getServer().getRecipeManager()

to find the Crafting(Shapeless)Recipe for the logs you have (assuming there is one).

That is the only thing that associates logs with planks.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

5 minutes ago, warjort said:

You need to use the RecipeManager

serverLevel.getServer().getRecipeManager()

to find the Crafting(Shapeless)Recipe for the logs you have (assuming there is one).

That is the only thing that associates logs with planks.

I am very confused. What is the exact code I need to associate logs with planks?

Link to comment
Share on other sites

Enough. You need to spend more than 5 minutes researching this. Instead of expecting us to write your mod for you.

e.g. look at how the 2x2 crafting grid works in InventoryMenu

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

There is no "Exact code".

You need to loop through the list of logs (that you got from the tag), and then compare those blocks with recipes which you can find from the recipemanager.

Minecraft doesn't make any links between logs and planks other than the recipes. There is no good way to do what you want, because there is no way to define "Block Y is a plank version of log X". Just "If you craft log X by itself, you get block Y". If someone adds a recipe for a single log to a diamond block, does that make a diamond block a plank?

Link to comment
Share on other sites

1 hour ago, warjort said:

You need to use the RecipeManager

serverLevel.getServer().getRecipeManager()

to find the Crafting(Shapeless)Recipe for the logs you have (assuming there is one).

That is the only thing that associates logs with planks.

What is the method that returns the crafting recipe for each log, I can't find any doc on this from a search and the autocomplete is not enough

Link to comment
Share on other sites

There isn't one, you have to loop through all the (shapeless) recipes and check them yourself (i.e. check that there is a single ingredient and it is in the logs tag (and probably also check the output is something sensible, as I mentioned before)).

Alternatively, you might be able to look up the recipes manually by looping through the items in the logs tag, converting them to a single ingredient, then looking up the recipe for that single ingredient. I haven't ever tried, so I don't know how that would work, but it is probably more efficient.

Edited by Alpvax
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.