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

Item in inventory?


RiceAndCheese
 Share

Recommended Posts

So, I have this code.
What I wanna do is that when the item is in my inventory, the player is set on fire.

It isn't working... Any Idea on what is the problem (Eclipse shows me no problem)

I almost forgot to say, I used ACATIA_BUTTON just to test and I'm kinda new at this.

 

package com.riceandcheese.jademod.common.items;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraftforge.event.TickEvent.PlayerTickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class SpecialItem4 extends Item {

    public SpecialItem4(Properties properties) {
        super(properties);
    }
    @SubscribeEvent
    public void onTickPlayerEvent(PlayerTickEvent event){

        if(event.player instanceof PlayerEntity){
            PlayerEntity player = (PlayerEntity) event.player;
            if(player.inventory.hasItemStack(Items.ACACIA_BUTTON.getDefaultInstance())) {
                player.forceFireTicks(21);
            }
        }
    }
}

 

Edited by RiceAndCheese
Link to comment
Share on other sites

Several things don't make sense about your code.

  • Do you want to use an item that is not added by your mod (e.g. the acacia button)? If so, why do you have a class that extends Item then?
  • If you want to do it for an item added by your mod (e.g. the "SpecialItem4"), why do you use an event for this? Override inventoryTick in your Item class instead.
Link to comment
Share on other sites

 

3 hours ago, diesieben07 said:

Several things don't make sense about your code.

  • Do you want to use an item that is not added by your mod (e.g. the acacia button)? If so, why do you have a class that extends Item then?
  • If you want to do it for an item added by your mod (e.g. the "SpecialItem4"), why do you use an event for this? Override inventoryTick in your Item class instead.

Ok so, this is my EventHandler class:

package com.riceandcheese.jademod.core.event;

import com.riceandcheese.jademod.Jademod;
import com.riceandcheese.jademod.core.init.Iteminit;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraftforge.event.TickEvent.PlayerTickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;

@EventBusSubscriber(modid = Jademod.MOD_ID, bus = Bus.FORGE)
public class EventHandler {
	@SubscribeEvent
	public void onTickPlayerEvent(PlayerTickEvent event){

		if(event.player instanceof PlayerEntity){
			PlayerEntity player = (PlayerEntity) event.player;
			if(player.inventory.hasItemStack(Iteminit.fire_fragment.get().getDefaultInstance())) {
				player.forceFireTicks(21);
			}
		}
	}
}

It's still not working (fire_fragment is the item that's gonna make it happen)
 

Link to comment
Share on other sites

12 minutes ago, RiceAndCheese said:

fire_fragment is the item that's gonna make it happen

 

3 hours ago, diesieben07 said:
  • If you want to do it for an item added by your mod (e.g. the "SpecialItem4"), why do you use an event for this? Override inventoryTick in your Item class instead.

*Squint*

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.

Link to comment
Share on other sites

Worked thanks!
If anyone wants the code:

package com.riceandcheese.jademod.common.items;

import com.riceandcheese.jademod.core.init.Iteminit;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class SpecialItem4 extends Item {

	public SpecialItem4(Properties properties) {
		super(properties);
	}
	@Override
	public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
		if(entityIn instanceof PlayerEntity){
			PlayerEntity player = (PlayerEntity) entityIn;
			if(player.inventory.hasItemStack(Iteminit.fire_fragment.get().getDefaultInstance())) {
				player.forceFireTicks(30);
			}
		}
		super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
	}
}

 

Link to comment
Share on other sites

16 hours ago, RiceAndCheese said:

if(player.inventory.hasItemStack(Iteminit.fire_fragment.get().getDefaultInstance())) {

This is pointless... The whole point of inventoryTick is that it will called if the item is in the inventory. So there is no need checking if it is in the inventory.

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

    • I didn't put in credentials, I used it only for testing installing forge. But still the problem mentioned above.
    • I am trying to use Forge 1.18.2, I have checked my firewall, redownloaded Forge, deleted the fml.toml file (which keeps reappearing) and looked at countless help pages. I am just trying to download and use forge so that I can play with my friends and it will not download. It tells me there may be an issue with the Java runtime config and that the exit code is one, and I have copied the exit code.   
    • Okay, i think i understand but im getting issues, i create another packet to sync entity to another players like this package com.naim.belloto.networking.packet; import com.naim.belloto.networking.handler.ClientSyncHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.FriendlyByteBuf; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.network.NetworkEvent; import java.util.Objects; import java.util.function.Supplier; public class ClientSyncPacket { public ClientSyncPacket() { } public ClientSyncPacket(FriendlyByteBuf buf) { } public void toBytes(FriendlyByteBuf buf) { } public boolean handle(Supplier<NetworkEvent.Context> ctx) { NetworkEvent.Context context = ctx.get(); context.enqueueWork(() -> { assert Minecraft.getInstance().player != null; int idplayer = Minecraft.getInstance().player.getId(); DistExecutor.unsafeCallWhenOn(Dist.CLIENT,() -> () -> ClientSyncHandler.handlePacket(ctx, idplayer)); }); return true; } } And in the handler: public class ClientSyncHandler { public static boolean handlePacket(Supplier<NetworkEvent.Context> ctx, int idPlayer) { Minecraft.getInstance().level.getEntity(idPlayer); return true; } } But is not working :// 
    • I want to make a modded server on mac but I cant run a .bat file and it does not generate other .jar files so how can i make the server work (its 1.18.2 forge version 40.1.73)
  • Topics

×
×
  • Create New...

Important Information

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