Jump to content

Hooking into event for *all* blocks without modifying base classes


Recommended Posts

Posted

Hi all

 

Is there an easy way to allow my mod to modify the behaviour of blocks in general without modifying the base class? I want to be able to adjust speed depending on block type an entity is on (like soul sand). I saw the event onEntityCollidedWithBlock is used for soul sand, but can I hook that in the general case for all blocks?

 

Also, if I can do this can I stop the soul sand's version of the event from triggering? I don't want to be having to modify soul sand as a special case if possible.

 

Thanks for the help.

Posted

Any advice is always welcome! I'm trying to learn how to mod and I thought I'd do something that I wanted a while back.

 

Basically I want to be able to make the "base speed" of different blocks variable - eg. stone is faster to move across than sand and so on. Would it be better to just replace the base classes since in theory I'd like it to be able to work with any block (from mods etc)?

Posted

Would that work if I want it configurable for every type of block though? Say for instance someone has a mod that adds hundreds of different blocks. I'd like to be able to have in a config file a list of block ids (or some way to reference them) and a speed multiplier that gets assigned to each block. Obviously the default would be one for all blocks but I imagine it would get quite messy if I was overriding many blocks (most of which I may not know about since they are from other mods).

 

Am I missing some "easy" way that this would work with replacing them with the custom block? My understanding is that I am needing to override the onEntityCollidedWithBlock for all blocks. Is there another way to do this?

Posted

actually, all you would need to do is give it a normal collision box. it's slightly lower than a normal block, hence why you sink into it.

 

Just FYI:

There's two different functions going on here that result in Soul Sand's behavior.

1) Soul Sand's collision box is smaller than a full cube.  This is required (minimum 1% smaller) for soul sand's effect to work

2) Soul Sand's on-collision function, which actually slows the player (cactus does damage, etc*).

 

*Web actually works differently

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.

Posted

You could use something close to event handler - i mean, do the tick and when block below you == something, then set player speed to something. I am sure I can find those methods, because I've been making a road block (sadly mine was full ModBlock so i used just call for EntityWalking or something like this, yours are vanilia ones, thats why you would need to use ticks). And other way is to of course make Reflection and stuff proposed before.

1.7.10 is no longer supported by forge, you are on your own.

Posted

If I did it by tick would it be overridden by, say, the default soul sand behaviour?

 

If I was to use reflection is there a way that I could keep it flexible enough that someone could put in a config something like

 

MARBLE 1.2

 

where MARBLE is some block from a mod (that my mod has no knowledge of) and 1.2 is a speed? Obviously there may need to be another way to represent it but that's the basic idea I'm after.

 

Thanks for all the advice guys, I really appreciate it.

Posted

That would be perfect but could that work (with other mods) using reflection? I can see how to do it easily (in theory anyway) if I can modify the block class (and the soul sand class to remove it's special case) but as that is a no-no I'd like to know how to do it properly but still keep the flexibility of someone being able to put in a block id of any block from any mod and the speed adjustment work.

Posted

I think I'm confusing myself now. Using reflection, can I do this without actually changing the block for a custom block class? If I changed the block to a custom one I wouldn't be able to know what special functions/events etc the block I'm replacing would need (due to being possibly from another mod) so I don't think that would work. That's not a definitive statement by the way, I'm happy to be proven wrong.

Posted

I think I'm confusing myself now. Using reflection, can I do this without actually changing the block for a custom block class? If I changed the block to a custom one I wouldn't be able to know what special functions/events etc the block I'm replacing would need (due to being possibly from another mod) so I don't think that would work. That's not a definitive statement by the way, I'm happy to be proven wrong.

>_>

 

<_<

 

Yes, but you shouldn't.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Posted

Is there any "correct" way to do what I am wanting then? If the other methods are "dodgy" anyway then I think I'd prefer to go dodgy all the way and just modify the base classes but I'd like to do the right thing if possible.

Posted

The funny thing is, it seems like "block placed" and "block destroyed" are two of the most common and most wanted events, but I can't seem to find any sort of associated event. Which makes me think that it's some other, stupidly obvious method, which is why I'm afraid to post it on the hook suggestions, either for fear of "There's already a method stop bugging me lockban!" or "I'm going to implement one stop bugging me lockban!"

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Posted

I thought "lockban" was someone's name for a second there haha

 

What are the actual problems with modifying a base class (other than it being incompatible if I were to change things too much)? In essence I am just adding one event method to all blocks so I assume that isn't the problem itself. Assuming no change I make has any issue with other mods that don't also modify the class itself (obviously if two mods both change the same file there would be issues) are there any other issues to watch for?

Posted

I'm still not understanding how the event could override the soul-sand case without modifying base classes though.

 

If there was a way to "unregister" the soul-sand "onEntityCollidedWithBlock" and register a hook for all blocks for "onEntityWalking" (and possibly "onEntityCollidedWithBlock" if necessary) then this would be ideal. Is this what you are suggesting I request the ability to do? Without the ability to remove the soul-sand's event I don't think it would work.

Posted

in pull requests, you are able to modify base classes. that's how forge adds all of the abilities it adds. it modifies the original code. hence why you need to put minecraft forge in the jar file.

Posted

Ah, I understand. What would the pull request involve though, in my case? I assume what I am trying to do isn't general enough to wind up in the forge core.

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

    • IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); It says that '.get()' is deprecated since version 1.21.1 and has been marked for removal. How should I replace this line or section of code so it works? Or is there a way to fix it? Full Block of Code: IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();    // This is the line that's messed up... I don't know why modEventBus.addListener(this::commonSetup); MinecraftForge.EVENT_BUS.register(this);
    • Sorry to have bothered everyone.  I think I've figured it out: NbtUtils.snbtToStructure() will allow me to get the compound tag from a string I can load, and ItemStack.setTag() will allow me to write it to an item. Again, sorry to bother everyone -- it took a long time to work it out, it seems finding answers on the internet by googling isn't a viable way to get info so it took a lot of guessing, experimenting, and trial and error until I hit upon the path to a solution.
    • Hello, My modded forge server was working with no issues up until last night. The current issue is that the server crashes as soon as another player joins.   Steps I have already tried: Deleted these files: banned-ips.json, banned-players.json, ops.json, usercache.json, whitelist.json. Deleted all player files Deleted the chunk that player was in   Mods: Pixelmon 1.20.2, 9.2.10 Crash log: https://pastebin.com/qauuZLRE Latest Log: https://pastebin.com/TxCGS4sc Other issue I get when starting up the server: [LanServerPinger #1/WARN] [net.minecraft.client.server.LanServerPinger/]: LanServerPinger: Network is unreachable
    • I tried to reinstall the server multiple times but nothing worked and I keep getting the same error... I do have the correct java 21 installed or whatever it was. I couldn't find whats causing this problem as I tried a lot to reinstall the files again and again.
    • OK, what I need is to be able to take a JSON string, from a config file or data pack, and convert into NBT that can be applied to any arbitrary item (technically, item stack). This could easily be done in 1.12.2 using JsonToNBT.getTagFromJson(String data) like so: NBTFromJsonWrapper(String label, String data) { super(label, label); try { wrapped = JsonToNBT.getTagFromJson(data); } catch (NBTException e) { System.err.println("Exception reading json-nbt string: " + e.getMessage()); wrapped = new NBTTagCompound(); } } However, I can't seem to find a way to do this in newer version, specifically 1.19.4 (though once I'm done updating to that version I plan to be updating the latest 1.20 and 1.21 version). Alternately, of these NBT strings that resemble JSON but are not quite would be just as good, perhaps better: {ench:[{lvl:3s,id:35s},{lvl:5s,id:32s}],Unbreakable:1b,display:{Lore:["Believe it or not, this comes from ","an ancient Vanilla World. "],Name:"Fortuna Major"}} This was previously used for several purposes, one was simply to create purely vanilla potions.  The other was to create special items, primarily as trophies albeit practically useful trophies, often in the form of Easter Eggs. (I also had a homebrew system along side it, but I don't think I'll be updating it.) Any help would be appreciated.  Surely there is still a way to do this, and codecs do not seem to be the answer.  
  • Topics

×
×
  • Create New...

Important Information

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