Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize SearchMap in EntityPlayerMP #467

Open
1 task done
Fabboz opened this issue Jan 6, 2025 · 2 comments
Open
1 task done

Optimize SearchMap in EntityPlayerMP #467

Fabboz opened this issue Jan 6, 2025 · 2 comments

Comments

@Fabboz
Copy link

Fabboz commented Jan 6, 2025

Your GTNH Discord Username

No response

Feature Request

in EntityPlayerMp there is this code:

    public void onUpdateEntity()
    {
        try
        {
            super.onUpdate();

            for (int var1 = 0; var1 < this.inventory.getSizeInventory(); ++var1)
            {
                ItemStack var6 = this.inventory.getStackInSlot(var1);

                if (var6 != null && var6.getItem().isMap())
                {
                    Packet var8 = ((ItemMapBase)var6.getItem()).func_150911_c(var6, this.worldObj, this);

                    if (var8 != null)
                    {
                        this.playerNetServerHandler.sendPacket(var8);
                    }
                }
            }

i think is better to check onlythe held item:

    public void onUpdateEntity()
    {
        try
        {
            super.onUpdate();
                ItemStack var6 = this.getHeldItem();
                if (var6 != null && var6.getItem().isMap())
                {
                    Packet var8 = ((ItemMapBase)var6.getItem()).func_150911_c(var6, this.worldObj, this);

                    if (var8 != null)
                    {
                        this.playerNetServerHandler.sendPacket(var8);
                    }
                }

I did a mixin but I don't think it's the best way to get the result:

import com.mojang.authlib.GameProfile;
import net.minecraft.crash.CrashReport;
import net.minecraft.crash.CrashReportCategory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.ICrafting;
import net.minecraft.item.ItemMapBase;
import net.minecraft.item.ItemStack;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S06PacketUpdateHealth;
import net.minecraft.network.play.server.S1FPacketSetExperience;
import net.minecraft.scoreboard.IScoreObjectiveCriteria;
import net.minecraft.scoreboard.ScoreObjective;
import net.minecraft.stats.AchievementList;
import net.minecraft.stats.StatisticsFile;
import net.minecraft.util.ReportedException;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;


@Mixin(EntityPlayerMP.class)
public abstract class MixinEntityPlayerMP extends EntityPlayer implements ICrafting {

    public MixinEntityPlayerMP(World p_i45324_1_, GameProfile p_i45324_2_) {
        super(p_i45324_1_, p_i45324_2_);
    }
    @Shadow
    public NetHandlerPlayServer playerNetServerHandler;
    @Shadow
    private float field_130068_bO = Float.MIN_VALUE;

    @Shadow
    private float lastHealth = -1.0E8F;

    @Shadow
    private int lastFoodLevel = -99999999;

    @Shadow
    private boolean wasHungry = true;
    @Shadow
    private int lastExperience = -99999999;

    /**
     * @author
     * @reason
     */
    @Overwrite
    public void onUpdateEntity()
    {
        try
        {
            super.onUpdate();

//            for (int var1 = 0; var1 < this.inventory.getSizeInventory(); ++var1)
//            {
                ItemStack var6 = this.getHeldItem();
                if (var6 != null && var6.getItem().isMap())
                {
                    Packet var8 = ((ItemMapBase)var6.getItem()).func_150911_c(var6, this.worldObj, this);

                    if (var8 != null)
                    {
                        this.playerNetServerHandler.sendPacket(var8);
                    }
                }
//            }

            if (this.getHealth() != this.lastHealth || this.lastFoodLevel != this.foodStats.getFoodLevel() || this.foodStats.getSaturationLevel() == 0.0F != this.wasHungry)
            {
                this.playerNetServerHandler.sendPacket(new S06PacketUpdateHealth(this.getHealth(), this.foodStats.getFoodLevel(), this.foodStats.getSaturationLevel()));
                this.lastHealth = this.getHealth();
                this.lastFoodLevel = this.foodStats.getFoodLevel();
                this.wasHungry = this.foodStats.getSaturationLevel() == 0.0F;
            }

            if (this.getHealth() + this.getAbsorptionAmount() != this.field_130068_bO)
            {
                this.field_130068_bO = this.getHealth() + this.getAbsorptionAmount();
                Collection var5 = this.getWorldScoreboard().func_96520_a(IScoreObjectiveCriteria.health);
                Iterator var7 = var5.iterator();

                while (var7.hasNext())
                {
                    ScoreObjective var9 = (ScoreObjective)var7.next();
                    this.getWorldScoreboard().func_96529_a(this.getCommandSenderName(), var9).func_96651_a(Arrays.asList(new EntityPlayer[] {this}));
                }
            }

            if (this.experienceTotal != this.lastExperience)
            {
                this.lastExperience = this.experienceTotal;
                this.playerNetServerHandler.sendPacket(new S1FPacketSetExperience(this.experience, this.experienceTotal, this.experienceLevel));
            }

            if (this.ticksExisted % 20 * 5 == 0 && !this.func_147099_x().hasAchievementUnlocked(AchievementList.field_150961_L))
            {
                this.func_147098_j();
            }
        }
        catch (Throwable var4)
        {
            CrashReport var2 = CrashReport.makeCrashReport(var4, "Ticking player");
            CrashReportCategory var3 = var2.makeCategory("Player being ticked");
            this.addEntityCrashInfo(var3);
            throw new ReportedException(var2);
        }
    }

    @Shadow
    public StatisticsFile func_147099_x()
    {
        return null;
    }
    @Shadow
    protected void func_147098_j(){
    }
}

Final Checklist

  • I have searched the issues and haven't found a similar issue.
@kurrycat2004
Copy link
Contributor

you dont need an overwrite here and also shouldnt use one, theres probably a lot of other mixins for this method which would then break.
simply letting getSizeInventory return 1 and getStackInSlot return getHeldItem would do the same thing, but idk if you should do this in the first place or if it would break something.
idk what that code does, im only on my phone rn, but is that packet for updating the map data? if so, this would stop maps in the player inv from updating which is probably not wanted

@Fabboz
Copy link
Author

Fabboz commented Jan 6, 2025

the map update them "pixel" only when are rendered in hand.
i was trying to force the update only on a single slot to try the update even when are not in hand, the result was all other map when in hand don't update or stay blank if a new created and in the proper slot update normally.
It might be a feature that mojang didn't fully develop .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants