#BlameMojang

lucariomaster2

New Member
Jul 29, 2019
317
0
1
Wouldn't a much better solution be to define an abstract static method getTrackingID() in Entity, that each class then implements? Then that entire mess of if/else statements could be condensed to just differentiating between EntityPlayer/EntityLiving/everything else, and returning the SPacketSpawnObject with a call to this.trackedEntity.getTrackingID() for the second argument.

But I suppose this is Mojang.
 

Lethosos

New Member
Jul 29, 2019
898
-7
0
But I suppose this is Mojang.
everythings_made_up.jpg
 

Strikingwolf

New Member
Jul 29, 2019
3,709
-26
1
Wouldn't a much better solution be to define an abstract static method getTrackingID() in Entity, that each class then implements? Then that entire mess of if/else statements could be condensed to just differentiating between EntityPlayer/EntityLiving/everything else, and returning the SPacketSpawnObject with a call to this.trackedEntity.getTrackingID() for the second argument.

But I suppose this is Mojang.
There are many, many better ways to do this, they just didn't decide on any of them, because it's Mojang
 
  • Like
Reactions: SatanicSanta

RavynousHunter

New Member
Jul 29, 2019
2,784
-3
1
Wouldn't a much better solution be to define an abstract static method getTrackingID() in Entity, that each class then implements? Then that entire mess of if/else statements could be condensed to just differentiating between EntityPlayer/EntityLiving/everything else, and returning the SPacketSpawnObject with a call to this.trackedEntity.getTrackingID() for the second argument.

But I suppose this is Mojang.

That's what I would've done. Then again, I at least try to act like I know what I'm doing.
 

Reika

RotaryCraft Dev
FTB Mod Dev
Sep 3, 2013
5,079
5,331
550
Toronto, Canada
sites.google.com
At risk of necroposting, I found this gem in 1.9:

Code:
    private Packet<?> createSpawnPacket()
    {
        if (this.trackedEntity.isDead)
        {
            LOGGER.warn("Fetching addPacket for removed entity");
        }

        Packet pkt = net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.getEntitySpawningPacket(this.trackedEntity);
        if (pkt != null) return pkt;

        if (this.trackedEntity instanceof EntityItem)
        {
            return new SPacketSpawnObject(this.trackedEntity, 2, 1);
        }
        else if (this.trackedEntity instanceof EntityPlayerMP)
        {
            return new SPacketSpawnPlayer((EntityPlayer)this.trackedEntity);
        }
        else if (this.trackedEntity instanceof EntityMinecart)
        {
            EntityMinecart entityminecart = (EntityMinecart)this.trackedEntity;
            return new SPacketSpawnObject(this.trackedEntity, 10, entityminecart.getType().getId());
        }
        else if (this.trackedEntity instanceof EntityBoat)
        {
            return new SPacketSpawnObject(this.trackedEntity, 1);
        }
        else if (this.trackedEntity instanceof IAnimals)
        {
            this.lastHeadMotion = MathHelper.floor_float(this.trackedEntity.getRotationYawHead() * 256.0F / 360.0F);
            return new SPacketSpawnMob((EntityLivingBase)this.trackedEntity);
        }
        else if (this.trackedEntity instanceof EntityFishHook)
        {
            Entity entity2 = ((EntityFishHook)this.trackedEntity).angler;
            return new SPacketSpawnObject(this.trackedEntity, 90, entity2 != null ? entity2.getEntityId() : this.trackedEntity.getEntityId());
        }
        else if (this.trackedEntity instanceof EntitySpectralArrow)
        {
            Entity entity1 = ((EntitySpectralArrow)this.trackedEntity).shootingEntity;
            return new SPacketSpawnObject(this.trackedEntity, 91, 1 + (entity1 != null ? entity1.getEntityId() : this.trackedEntity.getEntityId()));
        }
        else if (this.trackedEntity instanceof EntityTippedArrow)
        {
            Entity entity = ((EntityArrow)this.trackedEntity).shootingEntity;
            return new SPacketSpawnObject(this.trackedEntity, 60, 1 + (entity != null ? entity.getEntityId() : this.trackedEntity.getEntityId()));
        }
        else if (this.trackedEntity instanceof EntitySnowball)
        {
            return new SPacketSpawnObject(this.trackedEntity, 61);
        }
        else if (this.trackedEntity instanceof EntityPotion)
        {
            return new SPacketSpawnObject(this.trackedEntity, 73);
        }
        else if (this.trackedEntity instanceof EntityExpBottle)
        {
            return new SPacketSpawnObject(this.trackedEntity, 75);
        }
        else if (this.trackedEntity instanceof EntityEnderPearl)
        {
            return new SPacketSpawnObject(this.trackedEntity, 65);
        }
        else if (this.trackedEntity instanceof EntityEnderEye)
        {
            return new SPacketSpawnObject(this.trackedEntity, 72);
        }
        else if (this.trackedEntity instanceof EntityFireworkRocket)
        {
            return new SPacketSpawnObject(this.trackedEntity, 76);
        }
        else if (this.trackedEntity instanceof EntityFireball)
        {
            EntityFireball entityfireball = (EntityFireball)this.trackedEntity;
            SPacketSpawnObject spacketspawnobject = null;
            int i = 63;

            if (this.trackedEntity instanceof EntitySmallFireball)
            {
                i = 64;
            }
            else if (this.trackedEntity instanceof EntityDragonFireball)
            {
                i = 93;
            }
            else if (this.trackedEntity instanceof EntityWitherSkull)
            {
                i = 66;
            }

            if (entityfireball.shootingEntity != null)
            {
                spacketspawnobject = new SPacketSpawnObject(this.trackedEntity, i, ((EntityFireball)this.trackedEntity).shootingEntity.getEntityId());
            }
            else
            {
                spacketspawnobject = new SPacketSpawnObject(this.trackedEntity, i, 0);
            }

            spacketspawnobject.setSpeedX((int)(entityfireball.accelerationX * 8000.0D));
            spacketspawnobject.setSpeedY((int)(entityfireball.accelerationY * 8000.0D));
            spacketspawnobject.setSpeedZ((int)(entityfireball.accelerationZ * 8000.0D));
            return spacketspawnobject;
        }
        else if (this.trackedEntity instanceof EntityShulkerBullet)
        {
            SPacketSpawnObject spacketspawnobject1 = new SPacketSpawnObject(this.trackedEntity, 67, 0);
            spacketspawnobject1.setSpeedX((int)(this.trackedEntity.motionX * 8000.0D));
            spacketspawnobject1.setSpeedY((int)(this.trackedEntity.motionY * 8000.0D));
            spacketspawnobject1.setSpeedZ((int)(this.trackedEntity.motionZ * 8000.0D));
            return spacketspawnobject1;
        }
        else if (this.trackedEntity instanceof EntityEgg)
        {
            return new SPacketSpawnObject(this.trackedEntity, 62);
        }
        else if (this.trackedEntity instanceof EntityTNTPrimed)
        {
            return new SPacketSpawnObject(this.trackedEntity, 50);
        }
        else if (this.trackedEntity instanceof EntityEnderCrystal)
        {
            return new SPacketSpawnObject(this.trackedEntity, 51);
        }
        else if (this.trackedEntity instanceof EntityFallingBlock)
        {
            EntityFallingBlock entityfallingblock = (EntityFallingBlock)this.trackedEntity;
            return new SPacketSpawnObject(this.trackedEntity, 70, Block.getStateId(entityfallingblock.getBlock()));
        }
        else if (this.trackedEntity instanceof EntityArmorStand)
        {
            return new SPacketSpawnObject(this.trackedEntity, 78);
        }
        else if (this.trackedEntity instanceof EntityPainting)
        {
            return new SPacketSpawnPainting((EntityPainting)this.trackedEntity);
        }
        else if (this.trackedEntity instanceof EntityItemFrame)
        {
            EntityItemFrame entityitemframe = (EntityItemFrame)this.trackedEntity;
            return new SPacketSpawnObject(this.trackedEntity, 71, entityitemframe.facingDirection.getHorizontalIndex(), entityitemframe.getHangingPosition());
        }
        else if (this.trackedEntity instanceof EntityLeashKnot)
        {
            EntityLeashKnot entityleashknot = (EntityLeashKnot)this.trackedEntity;
            return new SPacketSpawnObject(this.trackedEntity, 77, 0, entityleashknot.getHangingPosition());
        }
        else if (this.trackedEntity instanceof EntityXPOrb)
        {
            return new SPacketSpawnExperienceOrb((EntityXPOrb)this.trackedEntity);
        }
        else if (this.trackedEntity instanceof EntityAreaEffectCloud)
        {
            return new SPacketSpawnObject(this.trackedEntity, 3);
        }
        else
        {
            throw new IllegalArgumentException("Don\'t know how to add " + this.trackedEntity.getClass() + "!");
        }
    }
This is very common in Mojang code (my excerpts are from 1.7.10):
http://i.imgur.com/4GSes67.png
http://i.imgur.com/H1NlE3g.png
http://i.imgur.com/EQTkTzh.png
http://i.imgur.com/cFgLH42.png
http://i.imgur.com/hh1CE33.png
http://i.imgur.com/bTZNbO8.png
http://i.imgur.com/1rioLj8.png
http://i.imgur.com/DfsBtVL.png

Honestly, the truth is that it looks like Notch had no idea what a switch() statement was, and even Mojang fails to grasp inheritance and polymorphism.
 

RavynousHunter

New Member
Jul 29, 2019
2,784
-3
1
This is very common in Mojang code (my excerpts are from 1.7.10):
http://i.imgur.com/4GSes67.png
http://i.imgur.com/H1NlE3g.png
http://i.imgur.com/EQTkTzh.png
http://i.imgur.com/cFgLH42.png
http://i.imgur.com/hh1CE33.png
http://i.imgur.com/bTZNbO8.png
http://i.imgur.com/1rioLj8.png
http://i.imgur.com/DfsBtVL.png

Honestly, the truth is that it looks like Notch had no idea what a switch() statement was, and even Mojang fails to grasp inheritance and polymorphism.

...
...
...
AHHHHHHHHHHHHHHHHHHHHHHHHHHH!

Windows_XP_BSOD.png
 

ratchet freak

Well-Known Member
Nov 11, 2012
1,198
243
79
This is very common in Mojang code (my excerpts are from 1.7.10):
http://i.imgur.com/4GSes67.png
http://i.imgur.com/H1NlE3g.png
http://i.imgur.com/EQTkTzh.png
http://i.imgur.com/cFgLH42.png
http://i.imgur.com/hh1CE33.png
http://i.imgur.com/bTZNbO8.png
http://i.imgur.com/1rioLj8.png
http://i.imgur.com/DfsBtVL.png

Honestly, the truth is that it looks like Notch had no idea what a switch() statement was, and even Mojang fails to grasp inheritance and polymorphism.
Or the strategy pattern,

You know the one where you use a Map<ID, IDoingSomething> and reduce all those ifs to "strat.get(input.getID()).doIt(params);". With a null check of course.
 

RavynousHunter

New Member
Jul 29, 2019
2,784
-3
1
Or the strategy pattern,

You know the one where you use a Map<ID, IDoingSomething> and reduce all those ifs to "strat.get(input.getID()).doIt(params);". With a null check of course.

Never thought of that...I think. Well, would something like this count? (Using C# because its what I know best.)

Code:
Dictionary<string, BaseItem> Items = LoadItems();  // Loads item scripts from some local repository.
{...}
string CurrentItemDescription = Items[CurrentItemID].Description();

Where BaseItem is an abstract base class and Description() is an abstract method within that must be overridden in any script class that derives from it?
 

Lethosos

New Member
Jul 29, 2019
898
-7
0
Could work. May have to bug the Win10 devs to be sure of the implementation, but hey, anything they do would be a step better since MS Games programmers have a tendancy to compulsively optimize their code.

Sent from my Puzzle Box of Yogg-Saron using Tapatalk 2
 

RavynousHunter

New Member
Jul 29, 2019
2,784
-3
1
Could work. May have to bug the Win10 devs to be sure of the implementation, but hey, anything they do would be a step better since MS Games programmers have a tendancy to compulsively optimize their code.

Sent from my Puzzle Box of Yogg-Saron using Tapatalk 2

Hey, me too! I should work for Microsoft. ...I'd actually like that.
 
  • Like
Reactions: SatanicSanta

ratchet freak

Well-Known Member
Nov 11, 2012
1,198
243
79
Never thought of that...I think. Well, would something like this count? (Using C# because its what I know best.)

Code:
Dictionary<string, BaseItem> Items = LoadItems();  // Loads item scripts from some local repository.
{...}
string CurrentItemDescription = Items[CurrentItemID].Description();

Where BaseItem is an abstract base class and Description() is an abstract method within that must be overridden in any script class that derives from it?
looks right, though with the C# Dictionary you can do getOrDefault (or whatever it is called) where the default is a dummy BaseItem that returns a decent default description. So you don't have to explicitly null check.
 

RavynousHunter

New Member
Jul 29, 2019
2,784
-3
1
looks right, though with the C# Dictionary you can do getOrDefault (or whatever it is called) where the default is a dummy BaseItem that returns a decent default description. So you don't have to explicitly null check.
Not sure if it has that functionality, but I think it does. Either way, you would ideally do that kind of checking as you load the scripts in, ensuring that they're not null, as well as a type that's instantiable and derives from BaseItem. At least, that's how I do it. In fact, here's how I do it. Likely needlessly overoptimized, but (in theory) it should be able to handle large libraries and take full advantage of multi-core CPUs by its very nature.