Jump to content

Recommended Posts

Posted (edited)

Some classes in Minecraft are final, probably the most commonly used being ItemStack. I need to subclass this class. Reflection cannot be used for this. So I added a line in my AT file

public-f net.minecraft.item.ItemStack

and rebuilt the workspace. ItemStack was not final. The AT file is being loaded, I successfully used it to make a field public. I looked through FG's and SS's sources, and it would seem to me that this should work, seeing that the final flag in the JVM is the same for classes and members. Making a class nonfinal should be possible with bytecode modification, so unless there is an explicit reason to disallow such ATs or there is a mistake on my part somewhere, I would see this as a bug.

I am essentially trying to get a callback on any edits that happen to a specific instance of an ItemStack (that I create), that is changes in the contained item, and, more importantly, its size. Usually, the way to do this is by creating a subclass. If there is any other way I will be happy to use that instead.

Edited by HashtagShell
Posted
6 minutes ago, HashtagShell said:

I am essentially trying to get a callback on any edits that happen to a specific instance of an ItemStack (that I create), that is changes in the contained item, and, more importantly, its size. 

 

 

Posted

I am afraid not. I am giving out an ItemStack to some unknown automation function, and if it makes any changes to it, I need to know about it to act upon those changes. The function also doesn't return execution to me (rather it calls my function through an interface call, and I return execution along with the ItemStack to it), so I cannot check for any changes, it has to be a callback on the setCount or whatever methods. Note that even if the function stores the instance around for a few years, and then changes it, I still want to act upon it.

 

I would see my subclass looking something like this:

public class ReflexiveStack extends ItemStack
{
  //Constructor
  
  @Override
  public void setCount(int count)
  {
    //Handle change
    super.setCount(count);
  }
}

 

Posted

I actually want my subclass to vanish as soon as the item is serialised, cloned, etc.

Picture the following:

  1. Function gets an ItemStack instance from me
  2. It expects a reference to the same instance to exist in my code (ie. the stack is passed by reference)
  3. So it can edit the stack without the need to give it back to me
  4. Usually, this is fine, but I need to do extra things when it edits this particular instance, to simulate the "passed by reference" contract
  5. Once it clones, serialises, etc. the stack, the function can no longer expect the "passed by reference" contract to be kept, so I don't want to take any actions.
  6. If it needs this contract, it will re-query the instance from me, at which point #2 applies.
Posted

"I am afraid not" and the rest of the comment was in response to loordgek's question, if it was an inventory that I had created.

 

This "automation function" is any function that can make changes to the ItemStack in general - usually this is a method in some TileEntity.

Picture me having a TileEntity, which a hopper is pumping into. The hopper will query for a stack in my slot, and directly make changes to it, without returning it - it expects that I passed the stack by reference. I need to complement this by reference passing with some extra functionality.

Posted

No. If I meant after the chunk was reloaded, I would say I needed it be persistent or something like that. "A few years later" means that even if this instance (instance requires no serialisation, cloning, etc.) of the ItemStack was kept around for years, I still would want the callback. This was a very unreal and impractical example of a chunk that would be kept loaded for years.

Posted

So, specifics:

I am making an IInventory that doesn't store its contents as a whole but shares contents with another tile in the world.

You are of course familiar with the getStackInSlot function, which (by reference) returns the ItemStack in a slot. I need to return the unity of two stacks to this function, so I create a new one with its count being sum of the counts of the two input stacks. Say, that any tile (picture hopper, etc.) can query this stack, and because it expects the by reference thing, it will directly modify it. But, because I created a new stack in the getStackInSlot function, the changes will not be automatically reflected in my inventory. Plus, I need to split the change evenly between the two original ItemStacks, which is what I hoped to do in the overridden ItemStack class.

Essentially, I am trying to have an inventory act as a unity of two, but machines expecting ItemStacks to be passed by reference are making this painful.

Posted

This looks nice, however: Won't many modded tiles try to cast my tile to IInventory? What if I want to use ISided? Why can the IItemHandler#getStackInSlot function return null, all (at least Mojang's) code uses NonNullLists and ItemStack.EMPTY everywhere?

Posted

Vanilla tiles still do implement IInventory, don't they? Wouldn't that cause compatibility issues? Say that I wanted to check if a block is an inventory, how would I go about that when there are still IInventories around?

Posted

So I don't implement anything, but create IItemHandler instances for every side that I want to handle, store them in instance fields, and if <T> in getCapability is of type IItemHandler (for which I check by reference comparing the queried capability with net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) I return the IItemHandler instance for the appropriate side, but I need to do an unchecked cast to <T>, because the compiler doesn't know I verified <T> though the capability?

 

This capability system looks cool, also appreciate the unified energy storage capability

Posted
2 minutes ago, HashtagShell said:

but I need to do an unchecked cast to <T>, because the compiler doesn't know I verified <T> though the capability?

no do it like so

@Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
            return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(fuelSlot);

        return super.getCapability(capability, facing);
    }

lets post this for fun

Posted

Hmm... that method is just doing an unchecked cast and suppressing the warning - which I can also do. A suppressed unchecked cast seems more readable to me. Besides, Forge (in the Furnace for example) just casts it directly. Not that there is a big difference, except maybe a method call overhead, which is like nonexistent these days.

Posted

Thanks, this pretty much solved problem.

Coming back to the original question, are we not supposed to de-finalize classes, am I doing something wrong, or is this a bug and should be reported?

Posted (edited)

It didn't work, ItemStack still was final, even though I added the AT. If it had worked the first time, I wouldn't have reason to post anything in the first place.

 

(Just to be clear I won't be subclassing it but using the Cap system instead, tis just theoretical now)

Edited by HashtagShell

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

    • Please see https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/ for information on how to post your log correctly.
    • Hello!  The detailed description of how you got to where you are is certainly valuable.  But, at the end of the day (well, any time of the day actually), it is going to be the actual logs that going to provide the necessary details to hopefully solve your startup issue. Part of me wonders if you have installed a client-only mod on a dedicated server.  But I may very well be wrong, and it will be the logs that will tell that story.
    • Hello there! I didn't quite know where to go regarding this, but ended up deciding to post it here. I have been running a forge server with around 200 mods for me and some friends to play on casually, but have recently started to get an issue when booting the server. This all started after I decided to add some new mods to the server. Like usual, I add a mod, test run the server for any issues, and if all is well, I'll add a next one and so on until I have added all that I wanted to. After doing so, in all test runs, it all seemed to work just fine. However, the next day, after trying to boot the server, I kept getting an error regarding java.lang.NullPointerException, towards one of the mods I had recently added. So far so good, I removed the mod that was causing the issue, started up the server again, and here in when things took a turn for the worse. I received another java.lang.NullPointerException null error that wouldn't allow me to boot the server, but this time with a mod that wasn't part of the new ones I had recently added. I found this weird, but nonetheless, I removed it thinking it might be causing some conflicts with some of the new ones. Afterwards, booting the server again proved to be impossible, as it gave me another java.lang.NullPointerException null error with the 3rd mod I had ever installed on the server! This mod was there since the start, it added some biomes and had been just fine so far. This turn of events made me remove all the newer mods I had recently added in hopes to fix this whole ordeal, but alas, to no avail. Same error, with that same biome mod that had been there since day one. Reluctantly, I removed the biome mod, booted the server, and voila! The server was running, although without a major mod that had always been there to begin with. As I do not wish to part ways with this mod, specially since it had been working so far without any issues, I tried to bring everything back to how it was before I added those new mods, but kept on getting the same java.lang.NullPointerException null error for the biome mod. Even adding the newer mods won't cause me this error, with exception of the one that started it all, which I find quite odd since the mods I had been using without any issues are now giving me the same error the newer one that started it all gave me. Now, I have checked that everything is up to date regarding the mods, forge (forge-1.20.1-47.3.12) and java. The modpack runs perfectly fine when I start Minecraft itself, and play singleplayer, or even when I open a LAN world, everything works. Everything aside from the server. From what I could gather, this java.lang.NullPointerException null error would point to a missing value of sorts, for an item perhaps, within the mod that is causing the error, but aside from removing the whole mod, I lack the knowledge on how to fix this. With this in mind, if anyone would be so kind as to shine some light into this situation, with a way to fix all this blunder, I would be most grateful!
    • If you want a mana GUI, call Minecraft.getInstance().setScreen(new ManaScreen()); somewhere display your GUI. I would recommend creating a keybind and listening to key events, and if your key bind is pressed down, running that code.
    • You are creating an entire new screen, or GUI. What you probably want is an Overlay.
  • Topics

×
×
  • Create New...

Important Information

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