Jump to content

[SOLVED] Help with soft dependencies in 1.18 referencing other mod classes


Rawket Sushi

Recommended Posts

So I'm attempting to make a mod that's pretty much just an aesthetics mod. I'm trying to make a variety of items place-able. When it comes to vanilla I have that figured out. The problem is when it comes to doing the same for some other mods. Of course you need minecraft to run a minecraft mod so I don't have to worry about what happens if minecraft isn't present. But the same cannot be said for other mods, and in my situation, I want to make an item class that has the potential to function like a normal BlockItem when crouching, and place a new custom block, but can still act like it's original item when not crouching.

Right now I have set up a custom class, effectively a BlockItem, that extends a custom item class from the mod I want to reference. It all works fine if the mod is installed, but I don't know how to make it independent so that it works, and adds all the stuff specific to that mod, if the other mod is present but it doesn't need the other mod to be present to run and just have the vanilla based stuff.

I know I can't keep that custom BlockItem class as it is, but I don't know how to add the place-able functionality to it and also have it keep it's normal functionality without extending it's class.

Edited by Rawket Sushi
Link to comment
Share on other sites

Er well now I'm just having trouble figuring out how to register it.
I keep running into the same issues. I think I must have made the "create" method incorrectly. And I'm not sure I'm registering the item correctly for this either. Basically I keep running into the create method not being allowed to be referenced from a static context, which I don't really understand cuz the method in which I call it is not static, nor is the create method itself. Unless the "itemImpl" or "coinItem" in my case can't be static, but if that isn't static then I can't use it to register the way it's set up because in order to register the IDE tells me to make it static.

When I say I think I might have made the create method wrong, I mean the as it stands, it's really just

public Item create() {
	return this;
}

cuz I didn't know what else to have it do. I have so far not seen that it causes any issues(other than the back and forth between one side of the issue wanting it to be static and the other not static) because ultimately it does come back to an item, albeit through several "extends" but it has had no issue taking it as a return. Unless this will also cause the same crash you were talking about which I can also see being a possibility but I haven't been able to get that far to find out for myself yet. I can't make the method static because when I do it gives me an error that "this" cannot be referenced from a static context. but then often I also end up seeing "create" cannot be referenced from a static context which again not sure what it means when the method containing the "create" call isn't static, nor is the "create" method itself.

With the "if" statement, I have it setting the item to be either the mod item or just a normal item because if the other mod isn't loaded I don't need this item to do anything because it will be inaccessible. 

if (ModList.get().isLoaded("thermal")) {
	coinItem = PlonkCoinItem.create();
} else {
	coinItem = new Item(new Item.Properties());
}

"coinItem" in this case is initialized in the class and then set here.

and then finally there's the registry object

public static final RegistryObject<Item> LEAD_COIN = THERMAL_ITEMS.register("lead_coin",
            () -> new coinItem(PlonkBlocks.LEAD_COIN.get(), new Item.Properties()));

I can't put any of the properties in the if statement because I need to make several of these, but also the "new Item" has to have that part about Item.Properties or else I get an error and so I'm not even sure at this point if I'm registering it correctly either.

Link to comment
Share on other sites

4 hours ago, Rawket Sushi said:

Unless the "itemImpl" or "coinItem" in my case can't be static, but if that isn't static then I can't use it to register the way it's set up because in order to register the IDE tells me to make it static.

your IDE has the solution for your, you also need to return a new instance inside the create method

4 hours ago, Rawket Sushi said:

With the "if" statement, I have it setting the item to be either the mod item or just a normal item because if the other mod isn't loaded I don't need this item to do anything because it will be inaccessible.

where did you use the if statement, since it should be inside the Supplier of the RegistryObject

4 hours ago, Rawket Sushi said:

I can't put any of the properties in the if statement because I need to make several of these, but also the "new Item" has to have that part about Item.Properties or else I get an error and so I'm not even sure at this point if I'm registering it correctly either.

i'm not 100% sure what's the problem, but every Item needs a Item.Properties, so why did you not create a new instance each time?

Link to comment
Share on other sites

Oh wait the if statement goes inside the registry object? So every registry object should have the if statement? If yes then that should solve everything I haven't been trying to give specific properties and stuff cuz I had the if statement not in the registry object and I was trying to make it useful to each but if I have to do it for each individually then I guess that aught to solve the whole issue.

Also I wasn't creating a new instance of the custom item because the custom item extends a class from thermal foundation and does even stated that doing "new custom item" would crash cuz it's still going reference that unloaded class hence the .create however I think from what you're saying, I should return "new coinitem" and like just pass in the specific properties?

 

 

 

 

Edited by Rawket Sushi
Link to comment
Share on other sites

2 hours ago, Rawket Sushi said:

Oh wait the if statement goes inside the registry object? So every registry object should have the if statement?

yes

3 hours ago, Rawket Sushi said:

Also I wasn't creating a new instance of the custom item because the custom item extends a class from thermal foundation and does even stated that doing "new custom item" would crash cuz it's still going reference that unloaded class hence the .create however I think from what you're saying, I should return "new coinitem" and like just pass in the specific properties?

as D7 already told you the create method schuld return a new instance of the Item from the Dependency, but the return type of the method should be a vanilla super class (in your case Item),

inside the create method you can do every thing you want, and yeah you can (need) to pass in the specific properties

Link to comment
Share on other sites

I know he said it needs to return an item, I didn't see that it needed to return a new vanilla class. I just saw that it needed to return an item. But that does make sense, I kinda thought that was a possibility, is why I mentioned that that at one point. Like I didn't just miss that possibility, I knew I might have to try it that way but I was just gonna cross that bridge when I got to it cuz that hadn't caused me issues yet so I wanted to just deal with what was giving me issues.

But just to make sure, if I have it return a vanilla blockitem is it still going to have the other functionality that the original mod item has?

 

Edited by Rawket Sushi
Link to comment
Share on other sites

14 minutes ago, Rawket Sushi said:

But just to make sure, if I have it return a vanilla blockitem is it still going to have the other functionality that the original mod item has?

what did you mean by functionality? place a Block on right click?

Link to comment
Share on other sites

Well this item for example is a coin. In thermal foundation, when you right click it, it puts heads or tails in chat. That's pretty minor of a function but I want to know how to retain that while also making it so you can place it as a block that I'm creating to add to it, that way I know how to do it moving forward for more complex items as well. Right now my custom item containing three create method extends that coin item from thermal, but if the create method returns just a vanilla item idk if it'll retain the functionally it has with the thermal mod.

I mean I'll prolly try it anyway just to see what happens but that's what I'm trying to do

Edited by Rawket Sushi
Link to comment
Share on other sites

if the Thermal Mod is installed, the retunred Item by the create method is your PlonkCoinItem, then you be able to cast it back
so yeah the Item keeps it functionality and btw this is basic OOP programming
if the Thermal Mod is not installed, it's your fallback Item so it does not have the functionality of Thermal Mod Item your are extending

Link to comment
Share on other sites

2 hours ago, Luis_ST said:

the retunred Item by the create method is your PlonkCoinItem

Alright so that's what I was thinking then, sorry I just misread something, ADHD makes reading retention hard. When you said the return type needs to be vanilla I thought you said that the thing that is returned needs to be vanilla. 
 

2 hours ago, Luis_ST said:

and btw this is basic OOP programming

And I'm a beginner. I'm just trying to learn. We all gotta suck at something before we can be good at it. I did put effort in to learning the basics, now I'm just trying to get the experience so that I like actually know it know it, like so I can retain it long term so that I don't have to keep asking on forums and stuff, I only do so as a last resort cuz I don't wanna like rely on it. And I'm asking all these follow ups in an attempt to better understand this stuff. 

Link to comment
Share on other sites

Ok so I can't compile without the mods loaded because the PlonkCoinItem extends an item from that mod. It just tells me package does not exist.

create method within the custom Item class looks like this

public static Item create(Block pBlock, Item.Properties pProperties) {
        return new PlonkCoinItem(pBlock, pProperties);
    }

if statement looks like this

public static Item getCoinItem(Block pBlock, Item.Properties pProperties) {
        Item coinItem;
        if (ModList.get().isLoaded("thermal")) {
			coinItem = PlonkCoinItem.create(pBlock, pProperties);
        } else {
            coinItem = new Item(new Item.Properties());
        }
        return coinItem;
    }

registryobject looks like this

public static final RegistryObject<Item> LEAD_COIN = THERMAL_ITEMS.register("lead_coin",
            () -> InitModItems.getCoinItem(PlonkBlocks.LEAD_COIN.get(), new Item.Properties()));

I made "InitModItems" to hold the if statement just to test if that would help, I didn't expect it to but I still wanted to just give it a shot . But clearly I've done something wrong and idk what because I have no errors other than the mod package not existing.

Link to comment
Share on other sites

I did and it works fine when they're there. But I was trying to run it without that to see if it still worked cuz the whole point here is to make it so that the other mod doesn't need to be loaded for mine to still work, just in that case without the stuff relating to that other mod. Is there another way to test or do I just have to like fully compile/export the project to test that.

Link to comment
Share on other sites

I think I figured it out. Am I supposed to like fully remove it? Cuz I instead changed it to compileOnly and it works as intended. But does that properly simulate that mod just not being present? Like if I were to export this as is and load it into a proper game it would work the same?

Link to comment
Share on other sites

  • Rawket Sushi changed the title to [SOLVED] Help with soft dependencies in 1.18 referencing other mod classes

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.



×
×
  • Create New...

Important Information

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