Jump to content

Removing final on classes (Access Transformers)


HashtagShell

Recommended Posts

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
Link to comment
Share on other sites

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);
  }
}

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

"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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • As the title say, game suddenly crushed during the loading. I got this error message The game crashed whilst rendering overlay Error: java.lang.RuntimeException: null   And Here's the link of crash log bit.ly/3IcVUQG
    • Do these mods have incompatibles: Dynamic View[Forge] (by someaddon) Server Performance - Smooth Chunk Save[Forge/Fabric] (by someaddon) EdivadLib (by 3divad99) Krypton Reforged (by Anthxny) AttributeFix (by DarkhaxDev) Create Stuff & Additions (by Furti_Two) Cristel Lib (by Cristelknight) Resourceful Config (by ThatGravyBoat) Applied Cooking (by ItsSebastrn) Thermal Cultivation (by TeamCoFH) SuperMartijn642's Config Lib (by SuperMartijn642) Create: Bells & Whistles (by sudolev) Thermal Integration (by TeamCoFH) SebastrnLib (by ItsSebastrn) Farmer's Delight (by vectorwing) Right Click, Get Crops (by TeamCoFH) Observable (by tasgon) FPS optimizer (by The_TCT) Dungeons and Taverns (by Nova_Wostra) Explorify – Dungeons & Structures (by bebebea_loste) Fast Leaf Decay (by olafskiii) Create: Vintage Improvements (by Negodya1) Sophisticated Backpacks (by P3pp3rF1y) SuperMartijn642's Core Lib (by SuperMartijn642) Create Crafts & Additions (by MRHminer) Structory (by botanydev) Oculus (by Asek3) Tectonic (by Apollo) Embeddium (Rubidium) Extra (by dimadencep) Athena (by CodexAdrian) Jade 🔍 (by Snownee) Mekanism Tools (by bradyaidanc) Carry On (by Tschipp) Create: Sweets & Treats (by AstralButOnCurseForge) Extreme Reactors Create Compat (by ZeroNoRyouki) AI Improvements (by QueenOfMissiles) AE2 QoL Recipes (by Christofmeg) Create: ComputerCraft (CC: Tweaked) (by EndRage) Create Enchantment Industry (by DragonsPlus) Macaw's Fences and Walls (by sketch_macaw) Better Villages - Forge (by jtl_elisa) Puzzles Lib [Forge & Fabric] (by Fuzs) Mekanism (by bradyaidanc) Create Recycle Everything (by nocubeyt) Searchables (by Jaredlll08) Immersive Paintings [Fabric/Forge] (by Conczin) Create Deco (by talrey) Thermal Foundation (by TeamCoFH) Controlling (by Jaredlll08) Kotlin for Forge (by thedarkcolour) Lithostitched (by Apollo) Let Me Despawn (by frikinjay) MCEF (Minecraft Chromium Embedded Framework) (by ds58) FPS Boost (by The_TCT) Ad Astra (by AlexNijjar) TerraBlender (Forge) (by TheAdubbz) Falling Leaves (Forge) (by Cheaterpaul) CC:C Bridge (by Sammy_echt) Xaero's Minimap (by xaero96) CoFH Core (by TeamCoFH) Create: Applied Kinetics (by Forsteri123) AE Additions - ExtraCells2 Fork (by MasterYodAT9G) Just Enough Items (JEI) (by mezz) Biomes O' Plenty (by Forstride) Curios API (Forge/NeoForge) (by TheIllusiveC4) AE2 Network Analyser (by GlodBlock) Resourceful Lib (by ThatGravyBoat) ChoiceTheorem's Overhauled Village (by ChoiceTheorem) Clockwork: Create x Valkyrien Skies (by triode_) Architectury API (Fabric/Forge/NeoForge) (by shedaniel) Canary (by AbdElAziz333) Structure Essentials[Forge/Fabric] (by someaddon) Library Ferret - Forge (by jtl_elisa) Create Confectionery (by Furti_Two) Leaky - Farm leak detection[Forge/Fabric] (by someaddon) Game Menu Mod Option [Forge] (by morimori0317) Macaw's Bridges (by sketch_macaw) FramedBlocks (by XFactHD) Cherished Worlds (Fabric/Forge/NeoForge/Quilt) (by TheIllusiveC4) Entangled (by SuperMartijn642) Refined Storage: Requestify (by Buuz135) ME Requester (by Relentless) Inventory Sorter (by cpw) Xaero's World Map (by xaero96) William Wythers' Expanded Ecosphere (by Cristelknight) Cloth Config API (Fabric/Forge/NeoForge) (by shedaniel) FTB Library (Forge) (by FTB) Cupboard (by someaddon) Thermal Dynamics (by TeamCoFH) Patchouli (by Vazkii) Entity Culling Fabric/Forge (by tr7zw) Chipped (by terrariumearth) FTB XMod Compat (by FTB) FerriteCore ((Neo)Forge) (by malte0811) Extra Disks (by MelanX) Nature's Compass (by Chaosyr) Thermal Locomotion (by TeamCoFH) TexTrue's Embeddium Options (by TexTrue) Traveler's Backpack (by Tiviacz1337) Geophilic – Vanilla Biome Overhauls (by bebebea_loste) Create: Power Loader (by Lysine) Polymorph (Fabric/Forge/Quilt) (by TheIllusiveC4) Enlightend (by lixir) Create Central Kitchen (by DragonsPlus) Energy Meter (by Relentless) Botarium (by CodexAdrian) FTB Teams (Forge) (by FTB) Storage Drawers (by Texelsaur) Create: Extended Cogwheels (by Rabbitminers) Embeddium/Rubidium Dynamic Lights (by Anthxny) Biome Makeover (Forge) (by Lemonszz) Create Guardian Beam Defense (by ibarnstormer_) Create: The Factory Must Grow (by Dr_Mango_Tea) Create Chunkloading (by embeddedt) Balm (Forge Edition) (by BlayTheNinth) Glodium (by GlodBlock) Thermal Expansion (by TeamCoFH) Chunk-Pregenerator (by Speiger) Advanced Peripherals (by srrendi) Create: Garnished (by DakotaPrideModding) Tidal Towns (by joshieman) Easy Magic [Forge & Fabric] (by Fuzs) Extreme Reactors (by ZeroNoRyouki) Applied Energistics 2 Wireless Terminals (by Mari_023) Redstone Arsenal (by TeamCoFH) ExtraStorage (by 3divad99) Thermal Innovation (by TeamCoFH) Collective (by Serilum) Wizard's Reborn (by MaxBogomol) GraveStone Mod (by henkelmax) Mekanism Additions (by bradyaidanc) Create: New Age (by nilBlade) Refined Storage (by raoulvdberge) Embeddium/Rubidium Extras (by Anthxny) ZeroCore 2 (by ZeroNoRyouki) Mekanism Generators (by bradyaidanc) WebDisplays (by ds58) Clumps (by Jaredlll08) Vein Mining (Fabric/Forge/Quilt) (by TheIllusiveC4) Villager Names (by Serilum) Create Slice & Dice (by possible_triangle) Embeddium (by embeddedt) Create: Steam 'n' Rails (by mattentosh) Get It Together, Drops! (by bl4ckscor3) Sophisticated Core (by P3pp3rF1y) Supplementaries (by MehVahdJukaar) Jade Addons (Forge) (by Snownee) Create: Design n' Decor (by MilkyFur) Construction Wand (by ThetaDev) Create: Diesel Generators (by george8188262) Create : Misc & Things (by to0pa_fr) Valkyrien Skies (Forge/Fabric) (by triode_) Refined Storage Addons (by raoulvdberge) FTB Chunks (Forge) (by FTB) FTB Ranks (Forge) (by FTB) Moonlight Lib (by MehVahdJukaar) CC: Tweaked (by SquidDev) Waystones (by BlayTheNinth) Applied Energistics 2 (by thetechnici4n) Trowels fork (by Luciano_cz) Cooking for Blockheads (by BlayTheNinth) Create (by simibubi) Towns and Towers (by Biban_Auriu)
    • Hi, you guys are probably all tired of having to deal with the same topic everytimes. I want to play mods, but forge doesn't want me to. And after days of videos and forum, i have no choices to ask before giving up.. The classic Error code 1 ( here's my debug.log : https://paste.ee/p/0WMSP). I tryed everything. The Forge version fit the mod version. I have java jdk 17 and the path to java is set. My mods folder is empty. My Minecraft luncher path has been changed. Java, Minecraft and Forged has been reinstalled 3 times. I tryed with the curseforge luncher. The only thing i didn't do, and I think it might be the problem, is updating my graphic card drivers. For the only reason that my computer is an Ace Magician ( Tiny pc) that don't have a graphic card. But then I went with Fabric. And it works just fine.  Anyway I can't find anything so thank you for your help. Sorry for my english if there's any mistake. Razziel.  
    • I am trying to make my own modpack and at the moment it crashes before I can get to the main menu. I have been trying to google and figure it out myself but would really appreciate some help. I've been using CurseForge to install mod and think I've gotten all the dependencies but I'm not 100% sure. I hope I got the paste.ee thing right Thanks in advance! https://paste.ee/p/NXdSO
  • Topics

×
×
  • Create New...

Important Information

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