Jump to content

[Solved] [1.12.2] How to handle exceptions in constructors


IceMetalPunk

Recommended Posts

I have a class that extends Block. It also has composed in it a different, generic, class: OreDictManager. So in other words, the structure is basically this:
 

public class BasicBlock extends Block {
    private final OreDictManager<Block> oreDictManager;
}

I'm actually instantiating the new OreDictManager<Block> in the BasicBlock's constructor; the OreDictManager's constructor takes "this" as a parameter to bind the two together.

But here's the problem I'm having: the OreDictManager can manage either Block or Item classes. I don't want it to be able to be called with any other type. I couldn't find a way to have a Java generic that could take only two different classes which don't share a common ancestor, so instead I'm explicitly checking if the passed in argument's class is assignable from Block or Item, and throwing an exception in the OreDictManager's constructor if not.

My mod will be adding several BasicBlock instances to a list, but if any of them throw an exception upon instantiation, they shouldn't be added to the list. So what I figured was I'd just allow the exception thrown in OreDictManager's constructor to bubble up through BasicBlock's constructor, and in the registry code that adds the blocks to a list, just put a try/catch around the instantiation and list-adding.

But that's the problem: I don't know if this is the right approach. If I do this, there would be no code in the catch block, as I simply want the BasicBlock that failed instantiation to just not be added to the list. On the other hand, a try without a catch feels a bit like a bad habit.

What's the best way to handle this?

Edited by IceMetalPunk
Added [Solved] to title

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

Both block and item extend IForgeRegistryEntry if that helps

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

1 minute ago, Cadiboo said:

Both block and item extend IForgeRegistryEntry if that helps

Unfortunately, so do a ton of other things besides blocks and items, which shouldn't be supported. So I'd still need to do the explicit check as I'm doing now if I used that.

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

Only do items and handle blocks if they are ItemBlock?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

11 minutes ago, Draco18s said:

Only do items and handle blocks if they are ItemBlock?

Hm... Looking back at my code, I may be able to make it work with just ItemStacks instead... and only Items and Blocks can be passed to an ItemStack's constructor, right? So that'll at least move the burden of exception handling onto the code instantiating the OreDictManager rather than anywhere else... I'll try that and see if it looks cleaner...

*EDIT* Wait, no, shoot. One reason I went with templating was so that the OreDictManager's methods could return the original object, for method chaining. I can't do that if it's not actually templated...

Edited by IceMetalPunk

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

34 minutes ago, IceMetalPunk said:

Unfortunately, so do a ton of other things besides blocks and items, which shouldn't be supported. So I'd still need to do the explicit check as I'm doing now if I used that.

I dont see why this is a problem actually, are you the only one using this code? If so then you should know that the only two types it can accept are Blocks and Items.

Ultimately you could make two classes that extend your OreDictManager that explicitly uses Item and Block respectively and possibly make your OreDictManager abstract unless there won't be a difference between the two functionally.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

9 minutes ago, Animefan8888 said:

I dont see why this is a problem actually, are you the only one using this code? If so then you should know that the only two types it can accept are Blocks and Items.

Ultimately you could make two classes that extend your OreDictManager that explicitly uses Item and Block respectively and possibly make your OreDictManager abstract unless there won't be a difference between the two functionally.

For now, yes, but I may allow IMC-based registration for other mods in the future. And the two have identical functionality, but because they (a) need to return the bound object from some methods and (b) call ore dictionary methods which exist only for Items and Blocks... well, you see the problem.

I suppose I could just make two different classes that are identical except for the data types that those particular methods return/use... that seems like a ton of repetition, though, and I was always taught that the DRY approach is the best, for various reasons.

 

*EDIT* A little more background about what this OreDictManager does: I'm using it so that blocks and items can supply ore prefixes and suffixes, and every combination will be registered automatically for them. So for instance, if I wanted an item to be registered as nuggetIron, nuggetFerrous, coinIron, coinFerrous, shardIron, and shardFerrous; rather than registering all those manually, I could just register the prefixes "nugget", "coin", and "shard" with the item's OreDictManager, and the suffixes "Iron" and "Ferrous", and all combinations will be registered automatically when appropriate. So I could use something like this:
 

this.oreDictManager.registerSuffixes("Iron", "Ferrous");
this.oreDictManager.registerPrefixes("nugget", "coin", "shard");

Wouldn't even have to make any calls to the OreDictionary itself, as the manager handles that.

Edited by IceMetalPunk

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

4 minutes ago, IceMetalPunk said:

For now, yes, but I may allow IMC-based registration for other mods in the future. And the two have identical functionality, but because they (a) need to return the bound object from some methods and (b) call ore dictionary methods which exist only for Items and Blocks... well, you see the problem.

Force them to use a Block or Ite instance then you can limit that by making methods that add them, but only take a Block or an Item.

 

5 minutes ago, IceMetalPunk said:

I suppose I could just make two different classes that are identical except for the data types that those particular methods return/use... that seems like a ton of repetition, though, and I was always taught that the DRY approach is the best, for various reasons.

Hence why I said

10 minutes ago, Animefan8888 said:

you could make two classes that extend your OreDictManager

And make the constructor of the OreDictManager be protected so only classes extending it or in the same package can access instances of it. 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Have a look at my solution to this problem at https://github.com/Cadiboo/WIPTechAlpha/blob/master/src/main/java/cadiboo/wiptech/util/ModEnums.java (ModMaterials)

all my metals are registered here with properties (hasOre, hasNugget, hasTools etc.) and registration is done automatically based on this. It’s even possible for other mods to add, remove & modify the materials

Edited by Cadiboo

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

17 minutes ago, Animefan8888 said:

Force them to use a Block or Ite instance then you can limit that by making methods that add them, but only take a Block or an Item.

 

Hence why I said

And make the constructor of the OreDictManager be protected so only classes extending it or in the same package can access instances of it. 

...I think I misread your original post >_< . I also have no idea why I didn't think of using a protected constructor with inheritance... that would probably be the best way to do this! Thank you! I'll be implementing it tomorrow-ish, as it's late here, and then I'll post with the results.

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

My bad sent this as I received your message.

Edited by Animefan8888
Removed for uselessness.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When i try to launch Minecraft Java 1.20.1 forge with mods (82 mods),on start it loaded all mods,but at end Java just crashed.  p.s i allocated 8GB ram for minecraft and i didn't runned another instance.Log file pp.s I use ATlauncher   Environment: Organising filesystem [24/04/2024 23:40:08 PM] ATLauncher Version: 3.4.36.3 [11ae0b2334c236e93ee8128de980952b2a1b8900] [24/04/2024 23:40:08 PM] App Arguments: ["--install-method=aur","--no-launcher-update"] [24/04/2024 23:40:08 PM] JVM Arguments: ["-Dawt.useSystemAAFontSettings=on","-Dswing.aatext=true"] [24/04/2024 23:40:08 PM] Java Version: Java 22 (22) [24/04/2024 23:40:08 PM] Java Path: /usr/lib/jvm/java-22-openjdk [24/04/2024 23:40:08 PM] 64 Bit Java: true [24/04/2024 23:40:08 PM] RAM Available: 14931MB [24/04/2024 23:40:08 PM] Launcher Directory: **USERSDIR** [24/04/2024 23:40:08 PM] GPU: IvyBridge GT2 [HD Graphics 4000] (Intel Corporation (0x8086)) unknown 256MB VRAM [24/04/2024 23:40:08 PM] CPU: Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz 4 cores/8 threads [24/04/2024 23:40:08 PM] Operating System: EndeavourOS (unknown (unknown) build 6.8.7-arch1-1) [24/04/2024 23:40:08 PM] Bitness: 64 [24/04/2024 23:40:08 PM] Uptime: 15889 [24/04/2024 23:40:08 PM] Manufacturer: GNU/Linux
    • If you have nvidia graphics, don't touch your amd drivers, otherwise it might fix it but keep running on integrated graphics, which will result in terrible performance. For nvidia graphics, you need to tell windows and nvidia control panel that anything Minecraft related (the launcher, java, etc...) should prefer high performance graphics so that it actually uses your nvidia gpu
    • In the ever-evolving landscape of technology, the rise of cryptocurrencies and digital assets has introduced both unparalleled opportunities and unprecedented challenges. As these digital currencies become increasingly prevalent, so too does the risk of theft and loss. Yet, amidst the complexity and uncertainty, there exists a beacon of hope: ADRIAN LAMO HACKER. Technology has indeed become more sophisticated and enhanced, presenting new challenges in the realm of asset recovery. However, just as any other currency can be stolen or lost, crypto and digital assets are not beyond redemption. With the right expertise and guidance, recovery is possible and achievable. Contact ADRIAN LAMO HACKER via the website: https://adrianlamohackpro.online/ , a trusted, honest, and certified agency specializing in the retrieval of stolen or lost digital assets. In my own experience, I found myself in dire straits after falling victim to cybercriminals who absconded with a significant portion of my crypto holdings. It was a daunting situation, but I refused to succumb to despair. Upon engaging ADRIAN LAMO HACKER, their professionalism, and integrity immediately struck me, as an unwavering commitment to their clients. They deeply understand blockchain technology and utilize advanced methodologies to trace and recover lost or stolen funds. Their approach is meticulous, their expertise unparalleled, and their results speak for themselves. In a matter of days, ADRIAN LAMO HACKER successfully traced and recovered over 90% of my stolen funds, a feat I once believed to be unattainable. Their fees were fair and transparent, and communication throughout the process was nothing short of excellent. They kept me informed every step of the way, providing reassurance and guidance when I needed it most. For anyone who has fallen victim to crypto theft or loss, I wholeheartedly recommend ADRIAN LAMO HACKER. They are not just experts in their field; they are guardians of justice in the digital realm. With their assistance, you can reclaim what's rightfully yours and emerge stronger than ever before. So, if you find yourself grappling with the devastation of lost or stolen digital assets, don't despair. Reach out to ADRIAN LAMO HACKER via website: https://adrianlamohackpro.online/  / Telegram: @ADRIANLAMOHACKERTECH and let them guide you toward a brighter tomorrow.
  • Topics

×
×
  • Create New...

Important Information

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