Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[Version 1.18.2, SOLVED] Increased hitbox size only works for entity collision, but not for block collision


LeeCrafts
 Share

Recommended Posts

Posted (edited)

Hello,

I was playing around with changing the size of the AABB and discovered something that stumped me. As seen in my previous posts, I have been able to increase the size of the player with a custom status effect. I have been trying to do the same for all living entities, not just the player. Therefore, I--ideally--would not have to change the AABB by hardcoded values, like below:

// overriding MobEffect::applyEffectTick in my custom effect class

pLivingEntity.setBoundingBox(new AABB(pLivingEntity.getX()+2.5, pLivingEntity.getY()+3.6, pLivingEntity.getZ()+2.5, pLivingEntity.getX()-2.5, pLivingEntity.getY(), pLivingEntity.getZ()-2.5));

Simply using something like "pLivingEntity.setBoundingBox(pLivingEntity.getBoundingBox().inflate(2.5, 1.8, 2.5))" would be dangerous because the entity's AABB would expand to astronomical proportions. So I tried using a capability that stores the "original" dimensions of the entity's AABB when that entity joins the world (e.g. EntityJoinWorldEvent). This is what it looked like:

// custom capability that stores the original AABB dimensions of the entity

pLivingEntity.getCapability(ModCapabilities.HITBOX_CAPABILITY).ifPresent(iHitbox -> {
    Hitbox hitbox = (Hitbox) iHitbox;
    pLivingEntity.setBoundingBox(new AABB(
            pLivingEntity.getX() + hitbox.originalXSize * 4.2,
            pLivingEntity.getY() + hitbox.originalYSize * 2,
            pLivingEntity.getZ() + hitbox.originalZSize * 4.2,
            pLivingEntity.getX() - hitbox.originalXSize * 4.2,
            pLivingEntity.getY(),
            pLivingEntity.getZ() - hitbox.originalZSize * 4.2)
    );
});

When I tested this, the changed hitbox collides with entities but not with blocks. I am quite confused because changing the AABB by hardcoded values worked for both entity and block collision. How?

 

Previous post for reference (where I set the bounding box): https://forums.minecraftforge.net/topic/111159-version-1182-solved-custom-status-effect-that-changes-the-players-size-part-ii/?do=findComment&comment=496252

 

Edited by LeeCrafts
Link to comment
Share on other sites

  • LeeCrafts changed the title to [Version 1.18.2] Increased hitbox size only works for entity collision, but not for block collision
Posted (edited)

Update: I realized I don't need capabilities. In fact, I don't need to override applyEffectTick at all. But it seems like I need reflections. The increased hitbox size now works for both entity and block collision--with a little drawback. This is what it looks like so far:

// this event handler checks every tick if the living entity has the "big" effect
@SubscribeEvent
public static void entitySizeChange(LivingEvent.LivingUpdateEvent event) {
    LivingEntity livingEntity = event.getEntityLiving();
    if (livingEntity.getActiveEffectsMap() != null && livingEntity.hasEffect(ModEffects.BIG.get())) {
        EntityDimensions newDims = livingEntity.getDimensions(livingEntity.getPose()).scale(8.0F, 2.0F);
        try {
            // using reflection
            Field field = Entity.class.getDeclaredField("dimensions");
            field.setAccessible(true);
            field.set(livingEntity, newDims); // setting the living entity's EntityDimensions
            EntityDimensions newEntityDimensions = (EntityDimensions) field.get(livingEntity);
            livingEntity.setBoundingBox(newEntityDimensions.makeBoundingBox( // setting the living entity's AABB
                    livingEntity.getX(),
                    livingEntity.getY(),
                    livingEntity.getZ()
            ));
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

 

The aforementioned drawback is that, when the effect wears off, the living entity still keeps its bigger hitbox until it changes pose (i.e. my player crouching). Given that the EntityEvent.Size event fires whenever an entity changes pose, should I manually fire it with EVENT_BUS::post when that entity's effect wears off? If so, where? I tried manually firing EntityEvent.Size on my overridden removeAttributeModifiers(), but to no avail. 

Edited by LeeCrafts
Link to comment
Share on other sites

14 hours ago, LeeCrafts said:
Field field = Entity.class.getDeclaredField("dimensions");
            field.setAccessible(true);

You should save this somewhere static, instead of computing it every time.

15 hours ago, LeeCrafts said:

I tried manually firing EntityEvent.Size on my overridden removeAttributeModifiers(), but to no avail.

That's not how forge events work. The event doesn't fire to set the size, it fires when the size is changed to allow mods to override it. It is called from the (mojmap) method `Entity#refreshDimensions`, so if you call that method you should restore the entity size. Alternatively, if you don't want to allow other mods to react to the change in size, just recompute the AABB as vanilla does (either setPos, or setBoundingBox(makeBoundingBox())), but I would strongly suggest using the first method.

  • Thanks 1
Link to comment
Share on other sites

38 minutes ago, Alpvax said:

You should save this somewhere static, instead of computing it every time.

in addition you need to use ObfuscationReflectionHelper and
you need the SRG name of "dimensions" otherwiese it wont work outside of your IDE

  • Thanks 1
Link to comment
Share on other sites

Posted (edited)

Solved and it works consistently. Calling Entity::refreshDimensions every tick would be problematic, so I called it only when I needed to. Thank you both for the help!

 

// edit: lol forgot to use SRG name
private static final Field dimensionsField = ObfuscationReflectionHelper.findField(Entity.class, "f_19815_");

public ModEvents() {
    dimensionsField.setAccessible(true);
}

// this event handler increases the living entity's hitbox size if it has the "big" effect
@SubscribeEvent
public static void entityHitboxSizeChange(LivingEvent.LivingUpdateEvent event) throws IllegalAccessException {
    LivingEntity livingEntity = event.getEntityLiving();
    if (livingEntity.getActiveEffectsMap() != null) {

        EntityDimensions entityDimensions = livingEntity.getDimensions(livingEntity.getPose());

        boolean isBig = livingEntity.hasEffect(ModEffects.BIG.get());

        // Refresh dimensions if the living entity does not have the "big" effect AND its AABB has not changed back to normal.
        double aabbWidth = roundDigits(livingEntity.getBoundingBox().getXsize(), 4);
        double bigWidth = roundDigits(8 * entityDimensions.width, 4);
        if (!isBig && aabbWidth == bigWidth) livingEntity.refreshDimensions();

        // change dimensions of living entity using reflections
        if (isBig) {
            dimensionsField.set(livingEntity, entityDimensions.scale(8.0F, 2.0F));
            EntityDimensions newEntityDimensions = (EntityDimensions) dimensionsField.get(livingEntity);
            livingEntity.setBoundingBox(newEntityDimensions.makeBoundingBox(
                    livingEntity.getX(),
                    livingEntity.getY(),
                    livingEntity.getZ()
            ));
        }

    }
}

// little helper function that rounds a double to n digits
private static double roundDigits(double num, int digits) {
    double tenPower = Math.pow(10, digits);
    return Math.round(num * tenPower) / tenPower;
}
Edited by LeeCrafts
Link to comment
Share on other sites

  • LeeCrafts changed the title to [Version 1.18.2, SOLVED] Increased hitbox size only works for entity collision, but not for block collision

EntityEvent.Size exists specifically for this purpose. You don't need to set the dimensions field using reflection. Your use of reflection will also crash outside of the development environment (you must use SRG names). Also not that you should put the Field instance in a static final field, this allows the JVM to more aggressively optimize it.

Link to comment
Share on other sites

Posted (edited)

(updated the code in my previous comment)

 

The reason why I have been reluctant to use EntityEvent.Size is because whenever I increase the hitbox size of the living entity via setNewSize(), the enlarged hitbox starts jittering whenever it touches a solid block. Would there be anything else I have to do besides call setNewSize()?

Edited by LeeCrafts
Link to comment
Share on other sites

When I tried using EntityEvent.Size, this is the code:

@SubscribeEvent
public static void entityHitboxSizeChange(EntityEvent.Size event) {
    if (event.getEntity() instanceof LivingEntity livingEntity && livingEntity.getActiveEffectsMap() != null) {
        EntityDimensions entityDimensions = livingEntity.getDimensions(livingEntity.getPose());
        if (livingEntity.hasEffect(ModEffects.BIG.get())) {
            event.setNewSize(entityDimensions.scale(8, 2));
        }
        else {
            event.setNewSize(entityDimensions);
        }
    }
}

 

// When I refreshDimensions in removeAttributeModifiers(), the size still does not revert immediately unless the pose changes (i.e. player crouching).
// This is under the assumption that refreshDimensions() always triggers an EntityEvent.Size event
@Override
public void addAttributeModifiers(@NotNull LivingEntity pLivingEntity, @NotNull AttributeMap pAttributeMap, int pAmplifier) {
    if (!pLivingEntity.level.isClientSide()) {
      
        // other stuff ...
      
        pLivingEntity.refreshDimensions();
    }
    super.addAttributeModifiers(pLivingEntity, pAttributeMap, pAmplifier);
}

@Override
public void removeAttributeModifiers(@NotNull LivingEntity pLivingEntity, @NotNull AttributeMap pAttributeMap, int pAmplifier) {
    if (!pLivingEntity.level.isClientSide()) {
      
        // other stuff ...
      
        pLivingEntity.refreshDimensions();
    }
    super.removeAttributeModifiers(pLivingEntity, pAttributeMap, pAmplifier);
}

 

 

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

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Although that too has issues. The forge defined configuration files are loaded after registration
    • Unless I have misunderstood your requirements, I don't think what you are trying to do is going to work.   The closest things in minecraft are banner patterns or paintings. But these are registered in code (not what you want) on both the client and the server. Then when it loads resource packs on the client it looks up the texture for each one.   It does it this way because the game defines what should exist, then lets the user/modpack developer give them textures (with you providing a default texture).   You seem to want to populate a registry from what textures exist in a resource pack? So kind of the reverse. This isn't going to work for at least 2 reasons: * By the time resource packs are loaded, the registries are frozen - It is too late. * Servers don't even load resource packs, so there is no way it can use this mechanism    You need some other mechanism to populate your registry. e.g. a configuration file holding the names of what should exist, but it doesn't sound like what you want?
    • For the most part, this worked, somehow more mods were outdated and i need to fix them
    • You have a version of oculus from the beta (1.2.5) The latest version is mc1.19-1.2.5a, if that doesn't work contact the mod author https://www.curseforge.com/minecraft/mc-mods/oculus/files  
    • ---- Minecraft Crash Report ---- // Hi. I'm Minecraft, and I'm a crashaholic. Time: 8/10/22 7:20 PM Description: Mod loading error has occurred java.lang.Exception: Mod Loading has failed     at net.minecraftforge.fml.CrashReportExtender.dumpModLoadingCrashReport(CrashReportExtender.java:85) [?:?] {re:classloading}     at net.minecraftforge.fml.client.ClientModLoader.completeModLoading(ClientModLoader.java:188) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.lambda$null$1(Minecraft.java:508) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft$$Lambda$3569/1174919051.run(Unknown Source) [?:?] {}     at net.minecraft.util.Util.func_215077_a(Util.java:320) [?:?] {re:classloading}     at net.minecraft.client.Minecraft.lambda$new$2(Minecraft.java:504) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft$$Lambda$3352/424832797.accept(Unknown Source) [?:?] {}     at net.minecraft.client.gui.ResourceLoadProgressGui.func_230430_a_(ResourceLoadProgressGui.java:113) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.GameRenderer.func_195458_a(GameRenderer.java:481) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_195542_b(Minecraft.java:977) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:607) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:184) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51] {}     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_51] {}     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_51] {}     at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_51] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider.lambda$launchService$0(FMLClientLaunchProvider.java:51) [forge-1.16.5-36.2.34.jar:36.2] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider$$Lambda$420/566698125.call(Unknown Source) [forge-1.16.5-36.2.34.jar:36.2] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.1.3.jar:?] {} A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Render thread Stacktrace:     at net.minecraftforge.fml.CrashReportExtender.lambda$dumpModLoadingCrashReport$7(CrashReportExtender.java:88) ~[?:?] {re:classloading} -- MOD jei -- Details:     Mod File: jei-1.16.5-7.7.1.152.jar     Failure message: fml.modloading.dupedmod     Mod Version: 7.7.1.152     Mod Issue URL: https://github.com/mezz/JustEnoughItems/issues?q=is%3Aissue     Exception message: MISSING EXCEPTION MESSAGE Stacktrace:     at net.minecraftforge.fml.CrashReportExtender.lambda$dumpModLoadingCrashReport$7(CrashReportExtender.java:88) ~[?:?] {re:classloading}     at net.minecraftforge.fml.CrashReportExtender$$Lambda$3599/485371408.accept(Unknown Source) ~[?:?] {}     at java.util.ArrayList.forEach(ArrayList.java:1249) ~[?:1.8.0_51] {}     at net.minecraftforge.fml.CrashReportExtender.dumpModLoadingCrashReport(CrashReportExtender.java:86) [?:?] {re:classloading}     at net.minecraftforge.fml.client.ClientModLoader.completeModLoading(ClientModLoader.java:188) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.lambda$null$1(Minecraft.java:508) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft$$Lambda$3569/1174919051.run(Unknown Source) [?:?] {}     at net.minecraft.util.Util.func_215077_a(Util.java:320) [?:?] {re:classloading}     at net.minecraft.client.Minecraft.lambda$new$2(Minecraft.java:504) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft$$Lambda$3352/424832797.accept(Unknown Source) [?:?] {}     at net.minecraft.client.gui.ResourceLoadProgressGui.func_230430_a_(ResourceLoadProgressGui.java:113) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.GameRenderer.func_195458_a(GameRenderer.java:481) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_195542_b(Minecraft.java:977) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:607) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:184) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51] {}     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_51] {}     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_51] {}     at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_51] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider.lambda$launchService$0(FMLClientLaunchProvider.java:51) [forge-1.16.5-36.2.34.jar:36.2] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider$$Lambda$420/566698125.call(Unknown Source) [forge-1.16.5-36.2.34.jar:36.2] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.1.3.jar:?] {} -- System Details -- Details:     Minecraft Version: 1.16.5     Minecraft Version ID: 1.16.5     Operating System: Windows 10 (amd64) version 10.0     Java Version: 1.8.0_51, Oracle Corporation     Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation     Memory: 507993864 bytes (484 MB) / 1791492096 bytes (1708 MB) up to 3817865216 bytes (3641 MB)     CPUs: 4     JVM Flags: 4 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump -Xss1M -Xmx4096m -Xms256m     ModLauncher: 8.1.3+8.1.3+main-8.1.x.c94d18ec     ModLauncher launch target: fmlclient     ModLauncher naming: srg     ModLauncher services:          /mixin-0.8.4.jar mixin PLUGINSERVICE          /eventbus-4.0.0.jar eventbus PLUGINSERVICE          /forge-1.16.5-36.2.34.jar object_holder_definalize PLUGINSERVICE          /forge-1.16.5-36.2.34.jar runtime_enum_extender PLUGINSERVICE          /accesstransformers-3.0.1.jar accesstransformer PLUGINSERVICE          /forge-1.16.5-36.2.34.jar capability_inject_definalize PLUGINSERVICE          /forge-1.16.5-36.2.34.jar runtimedistcleaner PLUGINSERVICE          /mixin-0.8.4.jar mixin TRANSFORMATIONSERVICE          /forge-1.16.5-36.2.34.jar fml TRANSFORMATIONSERVICE      FML: 36.2     Forge: net.minecraftforge:36.2.34     FML Language Providers:          javafml@36.2         minecraft@1         kotori_scala@2.13.8-build-3     Mod List:          forge-1.16.5-36.2.34-client.jar                   |Minecraft                     |minecraft                     |1.16.5              |NONE      |Manifest: NOSIGNATURE         forge-1.16.5-36.2.34-universal.jar                |Forge                         |forge                         |36.2.34             |NONE      |Manifest: 22:af:21:d8:19:82:7f:93:94:fe:2b:ac:b7:e4:41:57:68:39:87:b1:a7:5c:c6:44:f9:25:74:21:14:f5:0d:90
  • Topics

×
×
  • Create New...

Important Information

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