Jump to content

How can I make calls to methods that are common between Item and Block less sloppy?


Recommended Posts

Posted

When creating 'BasicItem', 'BasicBlock', 'Basic[ToolName]' etc Classes, I have found that they all have some identical constructor sections. I have created a generic util method but I feel absolutely filthy for doing it this sloppy way. However, Generics don't allow you to call methods on the 'actual' type because that would break encapsulation. That was the only other way I saw this working. Is there anything I'm forgetting or missing out on? I just really feel like a heathen for doing it this way

public static <T extends IForgeRegistryEntry.Impl<T>> void initItem(T item, String unlocalisedName, String registryName) {
    if (item instanceof Block) {
        ((Block) item).setUnlocalizedName(NameUtils.formatUnlocalisedName(unlocalisedName));
        ((Block) item).setRegistryName(registryName);
        ((Block) item).setCreativeTab(ExampleMod.EXAMPLE_TAB);
    } else {
        ((Item) item).setUnlocalizedName(NameUtils.formatUnlocalisedName(unlocalisedName));
        ((Item) item).setRegistryName(registryName);
        ((Item) item).setCreativeTab(ExampleMod.EXAMPLE_TAB);
    }
}

 

I love solving puzzles and also coding. What a great combo! I also want to have a career in programming when I have gone through university. 9GAG and Quora stand in the way of me ascending from mediocre programmer to ...meh programmer and beyond!

 

I am always looking for fun projects to look at and maybe be a part of. If you have a project and want to co-operate, email me via [email protected]

 

Keep coding :)

Posted
13 minutes ago, hoodwink_dude said:

When creating 'BasicItem', 'BasicBlock', 'Basic[ToolName]' etc Classes, I have found that they all have some identical constructor sections.

Let me stop you right there. Don't create BasicX. There is already a BasicX, it's called X.(BasicBlock -> Block, etc.) This is an antipattern and should be avoided.

 

14 minutes ago, hoodwink_dude said:

Generics don't allow you to call methods on the 'actual' type because that would break encapsulation. That was the only other way I saw this working. Is there anything I'm forgetting or missing out on?

Use an interface to define your contracts?

Posted
8 minutes ago, diesieben07 said:

This. However, you need to be careful, since even though Block#setUnlocalizedName and Item#setUnlocalizedName have the same signature at dev time, they will have separate SRG names in the actual game, so you will not be able to have them implement a common interface. You'll need to write your own methods, still.

This is what I was afraid of.

 

12 minutes ago, V0idWa1k3r said:

Let me stop you right there. Don't create BasicX. There is already a BasicX, it's called X.(BasicBlock -> Block, etc.) This is an antipattern and should be avoided.

I guess I got too stuck into this 1.12.2 tutorial series that I didn't stop to think if it was really needed. It's a good thing this has been caught relatively early. Thanks

I love solving puzzles and also coding. What a great combo! I also want to have a career in programming when I have gone through university. 9GAG and Quora stand in the way of me ascending from mediocre programmer to ...meh programmer and beyond!

 

I am always looking for fun projects to look at and maybe be a part of. If you have a project and want to co-operate, email me via [email protected]

 

Keep coding :)

Posted
25 minutes ago, V0idWa1k3r said:

Let me stop you right there. Don't create BasicX. There is already a BasicX, it's called X.(BasicBlock -> Block, etc.) This is an antipattern and should be avoided.

In the context of the linked tutorial, “basic” can be replaced with “example”

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)

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.