Jump to content

Problem with ItemCraftedEvent in PlayerEvent


braian hernandez

Recommended Posts

Hi to everyone, i need help to render custom name on Crafted Items, for my mod in minecraft 1.19.2 forge, i try many forms, but cant add name and edit values like attack damage, durability, attack speed, attack reach, armor, armor thoughness.
I have this:
@SubscribeEvent
public static void onCraftedItem(PlayerEvent.ItemCraftedEvent event) {
Player entity = event.getEntity();
int level = entity.getCapability(CustomLevelProvider.ENTITY_CUSTOMLEVEL).map(Customlevel::get).orElse(1);
double luck = entity.getCapability(LuckProvider.ENTITY_LUCK).map(Luck::get).orElse(1.0);
temStack output = event.getCrafting();
if(luck > 10000){ luck = 10000; }
int opt = selectTier(luck-1);
if(!event.getEntity().getLevel().isClientSide()){ String name = output.getDisplayName().getString();
switch (opt) {
case 1 -> {
Component iQualityComponent = Component.literal("[Crafted - Common] - {"+level+"} "+name).withStyle(ChatFormatting.GRAY);
 output.setHoverName(iQualityComponent);
extra = 0.1*(double)((level/10000)+1);
}
*others cases*
}
Item item = output.getItem(); double lvlExtra = (level*0.00025)+1;
if (item instanceof BowItem) { entity.getCapability(DexterityProvider.ENTITY_DEXTERITY).ifPresent(extraDamage -> { var customDamage = lvlExtra*(1 + (2+(extraDamage.get()*0.0015))*(4.15*0.35)*1.1)+(1.1*extra); output.setDamageValue((int)customDamage); event.setCanceled(true); event.getInventory().setItem(1,output); }); }

else if(item instanceof CrossbowItem){ entity.getCapability(DexterityProvider.ENTITY_DEXTERITY).ifPresent(extraDamage -> { var customDamage = lvlExtra*(1 + (2+(extraDamage.get()*0.0015))*(4.15*0.35)*1.1)+(1.1*extra); output.setDamageValue((int)customDamage); event.setCanceled(true); event.getInventory().setItem(1,output); }); }

else if(item instanceof TridentItem){ entity.getCapability(DexterityProvider.ENTITY_DEXTERITY).ifPresent(extraDamage -> { var customDamage = lvlExtra*((1+(extraDamage.get()*0.0025))*(2.15*0.25)*1.9)+(1.9*extra); output.setDamageValue((int)customDamage);
event.setCanceled(true);
event.getInventory().setItem(1,output); }); }

else if(item instanceof PickaxeItem){ entity.getCapability(IntelligenceProvider.ENTITY_INTELLIGENCE).ifPresent(stat ->{ var customDamage = lvlExtra*((1+(stat.get()*0.0025))*(2.15*0.25)*1.9)+(1.9*extra); output.setDamageValue((int)customDamage); event.setCanceled(true);
event.getInventory().setItem(1,output); }); }

else if(item instanceof AxeItem){ entity.getCapability(StrengthProvider.ENTITY_STRENGTH).ifPresent(stat ->{ var customDamage = lvlExtra*((1+(stat.get()*0.0025))*(2.15*0.25)*1.9)+(1.9*extra); output.setDamageValue((int)customDamage); event.setCanceled(true);
event.getInventory().setItem(1,output); }); }

else if(item instanceof SwordItem){ entity.getCapability(StrengthProvider.ENTITY_STRENGTH).ifPresent(stat ->{ var customDamage = lvlExtra*((1+(stat.get()*0.0025))*(2.15*0.25)*1.9)+(1.9*extra); output.setDamageValue((int)customDamage); event.setCanceled(true);
event.getInventory().setItem(1,output); }); }

else if(item instanceof HoeItem){ entity.getCapability(AgilityProvider.ENTITY_AGILITY).ifPresent(stat ->{ var customDamage = lvlExtra*((1+(stat.get()*0.0025))*(2.15*0.25)*1.9)+(1.9*extra); output.setDamageValue((int)customDamage); event.setCanceled(true);
event.getInventory().setItem(1,output); }); } } }


I want to know how i can do to receive the item i do.

When i use println, i can see that the hoverName is correctly apply, and if i try with a tag, it is added too, but not displayed.

Link to comment
Share on other sites

Hello, please upload the project to github, and give a link

I'm going to do the same in my mod, and I'll try to help you

Also try to use the trigger when the item is crafted in the item procedures

public class TestItem extends Item {
	public TestItem() {
		super(new Item.Properties().tab(CreativeModeTab.TAB_MISC).stacksTo(64).rarity(Rarity.COMMON));
	}

	@Override
	public void onCraftedBy(ItemStack itemstack, Level world, Player entity) {
		super.onCraftedBy(itemstack, world, entity);
		customcode.execute(world, entity.getX(), entity.getY(), entity.getZ());
	}
}

I also suspect that the event when an item is crafted, from player events, is being listened to in the crafting grid of the player's inventory window, but I could be wrong

Also try to display the values you need through tooltips

Edited by andreybadrey
  • Like 1
Link to comment
Share on other sites

it is also possible that all the values that you change change on the server, but remain the same on the client, especially if you use capability, this is relevant for you...
If this case is possible, then you have two options...
option 1: duplicate your server code for the client side...
option 2: write a sync system that will send batches of changes each time it changes...

Now I suspect that you simply do not synchronize data with the server

  • Like 1
Link to comment
Share on other sites

you need to slow down. you can't just write two screens worth of mess and ask why it doesn't work.

make a short event that does minimal modification of the item. then - only then when a minimal event works fine, write a real thing.

you wrote up there "event.setCanceled(true);"    - are you sure you want that? are you sure you know what it does?

you wrote " event.getInventory().setItem(1,output); " why would you do that? test that in a minimal event without all that mess, you'll see that it doesn't do what you expected.

  • Like 1
Link to comment
Share on other sites

2 hours ago, MFMods said:

you need to slow down. you can't just write two screens worth of mess and ask why it doesn't work.

make a short event that does minimal modification of the item. then - only then when a minimal event works fine, write a real thing.

you wrote up there "event.setCanceled(true);"    - are you sure you want that? are you sure you know what it does?

you wrote " event.getInventory().setItem(1,output); " why would you do that? test that in a minimal event without all that mess, you'll see that it doesn't do what you expected.

I use event,setCanceled(true) in various events, because i need to know if vanilla change something, actually i cancel:
Regeneration Event
Damage event
Hurt event
Attack event
Some of the Gui event

I dont know it that affect somehow, other events, i fix when i found error.


 

 

4 hours ago, andreybadrey said:

it is also possible that all the values that you change change on the server, but remain the same on the client, especially if you use capability, this is relevant for you...
If this case is possible, then you have two options...
option 1: duplicate your server code for the client side...
option 2: write a sync system that will send batches of changes each time it changes...

Now I suspect that you simply do not synchronize data with the server

I have some packets, to sync all player capability and try to do same for all living entity, but it is hard, for that way i have another problem, when player leave and join again, something go wrong and max life dont render correctly, now i will work to fix that, and i dont know how to do for living entity dont leave and join again with the level, so they dont get all capability again, that change their rank capability, life, level and armor.


 

 

4 hours ago, andreybadrey said:

Hello, please upload the project to github, and give a link

I'm going to do the same in my mod, and I'll try to help you

Also try to use the trigger when the item is crafted in the item procedures

public class TestItem extends Item {
	public TestItem() {
		super(new Item.Properties().tab(CreativeModeTab.TAB_MISC).stacksTo(64).rarity(Rarity.COMMON));
	}

	@Override
	public void onCraftedBy(ItemStack itemstack, Level world, Player entity) {
		super.onCraftedBy(itemstack, world, entity);
		customcode.execute(world, entity.getX(), entity.getY(), entity.getZ());
	}
}

I also suspect that the event when an item is crafted, from player events, is being listened to in the crafting grid of the player's inventory window, but I could be wrong

Also try to display the values you need through tooltips

Sorry, first time that i request help, so i dont know that, sure, i have always upload github project.

Code start in line 837

https://github.com/JuanHernandezMorani/Perfect-Minecraft-rpg-1.19.2/blob/main/src/main/java/net/cheto97/rpgcraftmod/event/ModEvents.java
 

Link to comment
Share on other sites

Sorry, you have a very large mod, I can't take the time to study it right now

But I heard a little about the synchronization of opportunities

a little later I will study your mod, maybe I will see a solution

at the moment I want to talk about synchronization

in my mod, in order to synchronize the capabilities of an entity that is not a player, I use a similar package


import com.bodryak.gmod.variables.server.ProviderMDS;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.network.NetworkEvent;

import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class MDSyncS2CPacket {
    private final long hp;
    private final UUID entity;
    private BlockPos pos;


    public MDSyncS2CPacket(long hp, UUID entity, BlockPos pos) {
        this.hp = hp;
        this.entity = entity;
        this.pos = pos;
    }

    public MDSyncS2CPacket(FriendlyByteBuf buf){
        this.hp = buf.readLong();
        this.entity = buf.readUUID();
        this.pos = buf.readBlockPos();
    }


    public void toBytes(FriendlyByteBuf buf){
        buf.writeLong(this.hp);
        buf.writeUUID(this.entity);
        buf.writeBlockPos(this.pos);
    }

    public boolean handle(Supplier<NetworkEvent.Context> supplier){
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(() -> {
            assert Minecraft.getInstance().player != null;
            Level world = Minecraft.getInstance().player.level;
            final Vec3 _center = new Vec3(this.pos.getX(), this.pos.getY(), this.pos.getZ());
            List<Entity> _entfound = world.getEntitiesOfClass(Entity.class, new AABB(_center, _center).inflate(4 / 2d), e -> true).stream().sorted(Comparator.comparingDouble(_entcnd -> _entcnd.distanceToSqr(_center))).collect(Collectors.toList());
            for (Entity entityiterator : _entfound) {
                if (entityiterator.getUUID().toString().equals(this.entity.toString())){

                    entityiterator.getCapability(ProviderMDS.MOB_DATA).ifPresent(m -> {
                        m.setHp(this.hp);
                    });
                }
            }
        });
        return true;
    }
}

this package is sent to all clients, and synchronizes the capabilities of downloaded creatures on all clients

but still need to synchronize entities when loading a chunk

I haven't worked on it yet, it's just to come in my mod

try to use event when chunk is loaded

and send the package via

public static <MSG> void sendToClients(MSG message) {
        INSTANCE.send(PacketDistributor.ALL.noArg(), message);
    }

perhaps this post will not be relevant, but later, when I study your mod, I will try to find a clearer solution

Link to comment
Share on other sites

1 issue: 

some versions need to use event.getOriginal().revive();  in PlayerEvent.Clone event

otherwise event.getOriginal().getCapability always null... maybe on your assembly it will work as it should, I did not check

 

2 issue:

onEntityDamage(LivingDamageEvent event) need to be removed

event.setCanceled(true);   need to be removed from onEntityHurt(LivingHurtEvent event)

use instead setAmount(yuo value) in final 

every event.setCanceled(true);  can affect the correctness of calculations, and the entire logic of the custom damage system

 

3 issue:

LivingEvent.LivingTickEvent event should stop using it, your code in this event gives a huge load on both the client and the server

in my example, I use getting mob statistics in the overlay code


import com.bodryak.gmod.util.RayTrace;
import com.bodryak.gmod.variables.client.PDC;
import com.bodryak.gmod.variables.server.ProviderMDS;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiComponent;

import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.client.event.RenderGuiEvent;
import net.minecraftforge.api.distmarker.Dist;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.Minecraft;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.platform.GlStateManager;


@Mod.EventBusSubscriber({Dist.CLIENT})
public class HudOverlay {

    static final ResourceLocation TARGET_FRAME = new ResourceLocation("gmod:textures/gui/target_frame.png");
    static final ResourceLocation TARGET_BAR = new ResourceLocation("gmod:textures/gui/target_bar.png");
    static final ResourceLocation HUD = new ResourceLocation("gmod:textures/gui/hud.png");
    static final ResourceLocation EXP_BAR = new ResourceLocation("gmod:textures/gui/exp_bar.png");
    static final ResourceLocation EXP_F = new ResourceLocation("gmod:textures/gui/exp_bar_f.png");
    static final ResourceLocation HP_BAR = new ResourceLocation("gmod:textures/gui/hp_bar.png");
    static final ResourceLocation MP_BAR = new ResourceLocation("gmod:textures/gui/mp_bar.png");

    static final RayTrace TRACE = new RayTrace();

    static int target_bar_x = -70;
    static int target_hp;
    @SubscribeEvent(priority = EventPriority.NORMAL)
    public static void eventHandler(RenderGuiEvent.Pre event) {




        Font font = Minecraft.getInstance().font;
        int w = event.getWindow().getGuiScaledWidth();
        int h = event.getWindow().getGuiScaledHeight();
        int x = 195;
        int max_w_1 = w-14-x;
        int all_w = max_w_1 +8;

        double point =  all_w * 0.01;
        double exp_p = PDC.getExp() / (PDC.getNlv() * 0.01);

        double exp_w = exp_p * point;
        double exp_f = 0;
        if (exp_w > max_w_1){
            exp_f = exp_w - max_w_1;
            exp_w = max_w_1;
        }

        double hp_w = (197*0.01) * (PDC.getHp() / (PDC.getMax_hp()*0.01));
        double mp_w = (159*0.01) * (PDC.getMana()/ (PDC.getMax_mana()*0.01));

        int target_bar_start = -70;
        int target_bar_x_final = 138;


        RenderSystem.disableDepthTest();
        RenderSystem.depthMask(false);
        RenderSystem.enableBlend();
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
        RenderSystem.setShaderColor(1, 1, 1, 1);
        if (true) {
            RenderSystem.setShaderTexture(0, TARGET_FRAME);
            GuiComponent.blit(event.getPoseStack(), target_bar_x, 9, 0, 0, 247, 35, 247, 35);

            RenderSystem.setShaderTexture(0, TARGET_BAR);
            GuiComponent.blit(event.getPoseStack(), target_bar_x + 28, 14, 0, 0, target_hp, 25, 209, 25);

            RenderSystem.setShaderTexture(0, HUD);
            GuiComponent.blit(event.getPoseStack(), 0, 0, 0, 0, 203, 82, 203, 82);

            RenderSystem.setShaderTexture(0, EXP_BAR);
            GuiComponent.blit(event.getPoseStack(), 193, 0, 0, 0, w - 203, 10, 1980, 10);
            GuiComponent.blit(event.getPoseStack(), w - 13, 0, 1980 - 13, 0, 13, 10, 1980, 10);

            RenderSystem.setShaderTexture(0, EXP_F);
            GuiComponent.blit(event.getPoseStack(), x,            1,  0,       0,(int) Math.round(exp_w), 8, 1972, 8);
            GuiComponent.blit(event.getPoseStack(), w-14, 1,  1972-8,  0,(int) Math.round(exp_f), 8, 1972, 8);

            RenderSystem.setShaderTexture(0, HP_BAR);
            GuiComponent.blit(event.getPoseStack(), 1, 1, 0, 0, (int) Math.round(hp_w), 19, 197, 19);

            RenderSystem.setShaderTexture(0, MP_BAR);
            GuiComponent.blit(event.getPoseStack(), 1, 21, 0, 0, (int) Math.round(mp_w), 19, 159, 19);


            GuiComponent.drawString(event.getPoseStack(), font, "LVL: " + PDC.getLvl(), 5, 70, -1);

            LivingEntity target = TRACE.getEntityInCrosshair(1.0f, 20.0D);
            if (target != null) {
                if(target_bar_x < target_bar_x_final){
                    target_bar_x +=16;
                }
                target.getCapability(ProviderMDS.MOB_DATA).ifPresent(mds -> {
                    target_hp = (int) ((209*0.01) * (mds.getHp() / (mds.getMax_hp()*0.01)));
                    GuiComponent.drawString(event.getPoseStack(), font, String.valueOf(mds.getHp()), target_bar_x + 53, 25, -1);
                });
                    GuiComponent.drawString(event.getPoseStack(), font, target.getDisplayName(), target_bar_x + 53, 15, -1);
            } else {
                if(target_bar_x > target_bar_start){
                    target_bar_x -=16;
                }
            }

        }
        RenderSystem.depthMask(true);
        RenderSystem.defaultBlendFunc();
        RenderSystem.enableDepthTest();
        RenderSystem.disableBlend();
        RenderSystem.setShaderColor(1, 1, 1, 1);
    }
}
Link to comment
Share on other sites

Most likely because of the event LivingEvent.LivingTickEvent event

all data from mod reseting

in event you used ModMessages.sendToServer

but event going on Server - if(!event.getEntity().getLevel().isClientSide())

 

I propose to rewrite the logic of processing opportunities

in event onAttachCapabilityEntity(AttachCapabilitiesEvent<Entity> event) should check each possibility separately

depending on the entity, set values directly in the check block

this code is called when the mob spawns, so the need to use the event EntityRespawn not need

 

next in the event onEntityHurt(LivingHurtEvent event)

handle all feature changes, and send to all clients INSTANCE.send(PacketDistributor.ALL.noArg(), message);

code in packet handle 


    public boolean handle(Supplier<NetworkEvent.Context> supplier){
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(() -> {
            assert Minecraft.getInstance().player != null;
            Level world = Minecraft.getInstance().player.level;
            final Vec3 _center = new Vec3(this.pos.getX(), this.pos.getY(), this.pos.getZ());
            List<Entity> _entfound = world.getEntitiesOfClass(Entity.class, new AABB(_center, _center).inflate(4 / 2d), e -> true).stream().sorted(Comparator.comparingDouble(_entcnd -> _entcnd.distanceToSqr(_center))).collect(Collectors.toList());
            for (Entity entityiterator : _entfound) {
                if (entityiterator.getUUID().toString().equals(this.entity.toString())){

                    entityiterator.getCapability(ProviderMDS.MOB_DATA).ifPresent(m -> {
                        m.setHp(this.hp);
                    });
                }
            }
        });
        return true;
    }

after that the client sees the changes through the code

Minecraft.getInstance().crosshairPickEntity.getCapability()

 

and write an entity synchronization package that will be sent to clients when a chunk is loaded

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.



×
×
  • Create New...

Important Information

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