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

    • OLXTOTO: Platform Maxwin dan Gacor Terbesar Sepanjang Masa OLXTOTO telah menetapkan standar baru dalam dunia perjudian dengan menjadi platform terbesar untuk pengalaman gaming yang penuh kemenangan dan kegacoran, sepanjang masa. Dengan fokus yang kuat pada menyediakan permainan yang menghadirkan kesenangan tanpa batas dan peluang kemenangan besar, OLXTOTO telah menjadi pilihan utama bagi para pencinta judi berani di Indonesia. Maxwin: Mengejar Kemenangan Terbesar Maxwin bukan sekadar kata-kata kosong di OLXTOTO. Ini adalah konsep yang ditanamkan dalam setiap aspek permainan yang mereka tawarkan. Dari permainan slot yang menghadirkan jackpot besar hingga berbagai opsi permainan togel dengan hadiah fantastis, para pemain dapat memperoleh peluang nyata untuk mencapai kemenangan terbesar dalam setiap taruhan yang mereka lakukan. OLXTOTO tidak hanya menawarkan kesempatan untuk menang, tetapi juga menjadi wadah bagi para pemain untuk meraih impian mereka dalam perjudian yang berani. Gacor: Keberuntungan yang Tak Tertandingi Keberuntungan seringkali menjadi faktor penting dalam perjudian, dan OLXTOTO memahami betul akan hal ini. Dengan berbagai strategi dan analisis yang disediakan, pemain dapat menemukan peluang gacor yang tidak tertandingi dalam setiap taruhan. Dari hasil togel yang tepat hingga putaran slot yang menguntungkan, OLXTOTO memastikan bahwa setiap taruhan memiliki potensi untuk menjadi momen yang mengubah hidup. Inovasi dan Kualitas Tanpa Batas Tidak puas dengan prestasi masa lalu, OLXTOTO terus berinovasi untuk memberikan pengalaman gaming terbaik kepada para pengguna. Dengan menggabungkan teknologi terbaru dengan desain yang ramah pengguna, platform ini menyajikan antarmuka yang mudah digunakan tanpa mengorbankan kualitas. Setiap pembaruan dan peningkatan dilakukan dengan tujuan tunggal: memberikan pengalaman gaming yang tanpa kompromi kepada setiap pengguna. Komitmen Terhadap Kepuasan Pelanggan Di balik kesuksesan OLXTOTO adalah komitmen mereka terhadap kepuasan pelanggan. Tim dukungan pelanggan yang profesional siap membantu para pemain dalam setiap langkah perjalanan gaming mereka. Dari pertanyaan teknis hingga bantuan dengan transaksi keuangan, OLXTOTO selalu siap memberikan pelayanan terbaik kepada para pengguna mereka. Penutup: Mengukir Sejarah dalam Dunia Perjudian Daring OLXTOTO bukan sekadar platform perjudian berani biasa. Ini adalah ikon dalam dunia perjudian daring Indonesia, sebuah destinasi yang menyatukan kemenangan dan keberuntungan dalam satu tempat yang mengasyikkan. Dengan komitmen mereka terhadap kualitas, inovasi, dan kepuasan pelanggan, OLXTOTO terus mengukir sejarah dalam perjudian dunia berani, menjadi nama yang tak terpisahkan dari pengalaman gaming terbaik. Bersiaplah untuk mengalami sensasi kemenangan terbesar dan keberuntungan tak terduga di OLXTOTO - platform maxwin dan gacor terbesar sepanjang masa.
    • OLXTOTO - Bandar Togel Online Dan Slot Terbesar Di Indonesia OLXTOTO telah lama dikenal sebagai salah satu bandar online terkemuka di Indonesia, terutama dalam pasar togel dan slot. Dengan reputasi yang solid dan pengalaman bertahun-tahun, OLXTOTO menawarkan platform yang aman dan andal bagi para penggemar perjudian daring. DAFTAR OLXTOTO DISINI DAFTAR OLXTOTO DISINI DAFTAR OLXTOTO DISINI Beragam Permainan Togel Sebagai bandar online terbesar di Indonesia, OLXTOTO menawarkan berbagai macam permainan togel. Mulai dari togel Singapura, togel Hongkong, hingga togel Sidney, pemain memiliki banyak pilihan untuk mencoba keberuntungan mereka. Dengan sistem yang transparan dan hasil yang adil, OLXTOTO memastikan bahwa setiap taruhan diproses dengan cepat dan tanpa keadaan. Slot Online Berkualitas Selain togel, OLXTOTO juga menawarkan berbagai permainan slot online yang menarik. Dari slot klasik hingga slot video modern, pemain dapat menemukan berbagai opsi permainan yang sesuai dengan preferensi mereka. Dengan grafis yang memukau dan fitur bonus yang menggiurkan, pengalaman bermain slot di OLXTOTO tidak akan pernah membosankan. Keamanan dan Kepuasan Pelanggan Terjamin Keamanan dan kepuasan pelanggan merupakan prioritas utama di OLXTOTO. Mereka menggunakan teknologi enkripsi terbaru untuk melindungi data pribadi dan keuangan para pemain. Tim dukungan pelanggan yang ramah dan responsif siap membantu pemain dengan setiap pertanyaan atau masalah yang mereka hadapi. Promosi dan Bonus Menarik OLXTOTO sering menawarkan promosi dan bonus menarik kepada para pemainnya. Mulai dari bonus selamat datang hingga bonus deposit, pemain memiliki kesempatan untuk meningkatkan kemenangan mereka dengan memanfaatkan berbagai penawaran yang tersedia. Penutup Dengan reputasi yang solid, beragam permainan berkualitas, dan komitmen terhadap keamanan dan kepuasan pelanggan, OLXTOTO tetap menjadi salah satu pilihan utama bagi para pecinta judi online di Indonesia. Jika Anda mencari pengalaman berjudi yang menyenangkan dan terpercaya, OLXTOTO layak dipertimbangkan.
    • I have been having a problem with minecraft forge. Any version. Everytime I try to launch it it always comes back with error code 1. I have tried launching from curseforge, from the minecraft launcher. I have also tried resetting my computer to see if that would help. It works on my other computer but that one is too old to run it properly. I have tried with and without mods aswell. Fabric works, optifine works, and MultiMC works aswell but i want to use forge. If you can help with this issue please DM on discord my # is Haole_Dawg#6676
    • Add the latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
    • I have no idea how a UI mod crashed a whole world but HUGE props to you man, just saved me +2 months of progress!  
  • Topics

×
×
  • Create New...

Important Information

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