Jump to content

[1.6.4] Detect if a player is using an item always


XVicarious

Recommended Posts

I was wondering how I'd detect if a player is using a pickaxe or something of the like.  I know how to do it (getItemInUse()) but how would I go about detecting it.  Like some kind of listener? Also I know how to use the player interact event, but how do I have it detect that?

Link to comment
Share on other sites

I was just about to say I had figured that out, thanks!

 

Edit: I seem to get a nullpointer

 

	if (event.entityPlayer.getItemInUse().getItem() instanceof ItemPickaxe ) {
		//ItemPickaxe itemInUsePickaxe = (ItemPickaxe)itemInUse.getItem();
		((ItemPickaxe)event.entityPlayer.getItemInUse().getItem()).efficiencyOnProperMaterial = (float) (((ItemPickaxe)event.entityPlayer.getItemInUse().getItem()).efficiencyOnProperMaterial * 3.0);
		System.out.printf("RUNNING");
	}

Link to comment
Share on other sites

I was just about to say I had figured that out, thanks!

 

Edit: I seem to get a nullpointer

 

 

Yes.  What happens if you have nothing in your hand?  Wouldn't "event.entityPlayer.getItemInUse()" return null?

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

Thanks, I ended up figuring that out.

 

One last problem that is holding me up.  I know it has something to with either sync, or nbt (or both i suppose).

When I get an item out of my custom block/tileentity it does not save that item in my inventory, nor does it save the experience I take from the player (also it always seems to set it to 11 no matter how much you have).

 

edit: also nbt does not seem to be saving

 

LevelUpStationGUI.java

package us.xvicario.rpglevel;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

public class LevelUpStationGUI extends GuiContainer {

public static final ResourceLocation textureGUI = new ResourceLocation("rpglevel", "textures/gui/levelUpGui.png");
public TileEntityLevelUpStation levelUpStation;
public static EntityPlayer player;

public LevelUpStationGUI(EntityPlayer player,InventoryPlayer inventoryPlayer, TileEntityLevelUpStation entity) {
	super(new LevelUpStationContainer(player, inventoryPlayer,entity));
	this.levelUpStation = entity;
	this.player = player;
	this.xSize = 176;
	this.ySize = 166;
}

public void drawGuiContainerForegroundLayer(int par1, int par2) {
	String name = this.levelUpStation.isInvNameLocalized() ? this.levelUpStation.getInvName() : I18n.getString(this.levelUpStation.getInvName());
	this.fontRenderer.drawString(name, this.xSize / 2 - this.fontRenderer.getStringWidth(name) / 2, 6, 4210752);
	this.fontRenderer.drawString(I18n.getString("container.inventory"), 8, this.ySize-96+2, 4210752);
}

@Override
public void drawGuiContainerBackgroundLayer(float f, int i, int j) {
	GL11.glColor4f(1f, 1f, 1f, 1f);
	//int texture = mc.renderEngine.getTexture("/gui/levelUpGui.png");
	//this.mc.renderEngine.bindTexture(texture);
	this.mc.renderEngine.bindTexture(textureGUI);
	this.buttonList.add(new GuiButton(0,guiLeft+8,this.ySize-112,20,20, "Mining"));
	this.buttonList.add(new GuiButton(1,guiLeft+8,this.ySize-90,20,20, "Attack"));
	this.buttonList.add(new GuiButton(2,guiLeft+30,this.ySize-112,20,20, "Defense"));
	this.buttonList.add(new GuiButton(3,guiLeft+30,this.ySize-90,20,20, "Archery"));
	//new GuiButton()
	drawTexturedModalRect((width-xSize)/2,(height-ySize)/2,0,0,xSize,ySize);
}

public void actionPerformed(GuiButton button) {
	if (this.player.experienceLevel >= 20) {
		ItemStack levelUpBook;
		switch(button.id) {
		case 0:
			levelUpBook = new ItemStack(RPGLevel.itemLevelUpBook,1,0);
			this.levelUpStation.setInventorySlotContents(0, levelUpBook);
			break;
		case 1:
			levelUpBook = new ItemStack(RPGLevel.itemLevelUpBook,1,1);
			this.levelUpStation.setInventorySlotContents(0, levelUpBook);
			break;
		case 2:
			levelUpBook = new ItemStack(RPGLevel.itemLevelUpBook,1,2);
			this.levelUpStation.setInventorySlotContents(0,levelUpBook);
			break;
		case 3:
			levelUpBook = new ItemStack(RPGLevel.itemLevelUpBook,1,3);
			this.levelUpStation.setInventorySlotContents(0, levelUpBook);
			break;
		}
		this.player.addExperienceLevel(-10);
	}
}

}

 

BlockLevelUpStation.java

package us.xvicario.rpglevel;

import cpw.mods.fml.common.network.FMLNetworkHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlockLevelUpStation extends BlockContainer {

protected BlockLevelUpStation(int par1, Material par2Material, boolean active) {
	super(par1, par2Material);
	setCreativeTab(CreativeTabs.tabDecorations);
}

@Override
public TileEntity createNewTileEntity(World world) {
	return new TileEntityLevelUpStation();
}

public void onBlockAdded(World par1World,int x, int y, int z) {
	super.onBlockAdded(par1World, x, y, z);
	setDefaultDirection(par1World,x,y,z);
	par1World.markBlockForUpdate(x, y, z);
}

private void setDefaultDirection(World par1World, int x, int y, int z) {
	TileEntity blockEntity = par1World.getBlockTileEntity(x, y, z);
	if (par1World.isRemote) {
		return;
	}
	int i = par1World.getBlockId(x, y, z-1);
	int j = par1World.getBlockId(x,y,z+1);
	int k = par1World.getBlockId(x-1, y, z);
	int l = par1World.getBlockId(x+1, y, z);
	byte byte0 = 3;
	if (Block.opaqueCubeLookup[i] && !Block.opaqueCubeLookup[j]) {
	byte0 = 3;
	}
	if (Block.opaqueCubeLookup[j] && !Block.opaqueCubeLookup[i]) {
	byte0 = 2;
	}
	if (Block.opaqueCubeLookup[k] && !Block.opaqueCubeLookup[l]) {
	byte0 = 5;
	}
	if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[k]) {
	byte0 = 4;
	}
	((TileEntityLevelUpStation)blockEntity).setFrontDirection(byte0);
}

public boolean renderAsNormalBlock() {
	return true;
}

@SideOnly(Side.CLIENT) public void registerIcons(IconRegister icon) {
	this.blockIcon = icon.registerIcon("rpglevel:levelup");
}

@SideOnly(Side.CLIENT) public Icon getIcon(int side, int metadata) {
	return this.blockIcon;
}

@Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
	if (!world.isRemote) {
		TileEntityLevelUpStation levelUpStation = (TileEntityLevelUpStation)world.getBlockTileEntity(x, z, z);
		FMLNetworkHandler.openGui(player, RPGLevel.instance, RPGLevel.guiID, world, x, y, z);
	}
	return true;
}

}

 

LevelUpGUIHandler.java

package us.xvicario.rpglevel;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.network.NetworkRegistry;

public class LevelUpGUIHandler implements IGuiHandler {

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	TileEntity entity = world.getBlockTileEntity(x, y, z);
	if (entity != null) {
		switch(ID) {
			case RPGLevel.guiID:
				if (entity instanceof TileEntityLevelUpStation) {
					return new LevelUpStationContainer(player, player.inventory,(TileEntityLevelUpStation) entity);
				}

		}
	}
	return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	TileEntity entity = world.getBlockTileEntity(x, y, z);
	if (entity != null) {
		switch(ID) {
			case RPGLevel.guiID:
				if (entity instanceof TileEntityLevelUpStation) {
					return new LevelUpStationGUI(player ,player.inventory,(TileEntityLevelUpStation) entity);
				}

		}
	}
	return null;
}

}

ClientProxy.java

package us.xvicario.rpglevel.proxy;

import us.xvicario.rpglevel.LevelUpStationContainer;
import us.xvicario.rpglevel.TileEntityLevelUpStation;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class ClientProxy extends CommonProxy {

@Override public void registerRenderers() {

}

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
	if (tileEntity != null) {
		switch(ID) {
		case 0: return new LevelUpStationContainer(player, player.inventory, (TileEntityLevelUpStation)tileEntity);
		}
	}
	return null;

}

}

LevelUpStationContainer.java

package us.xvicario.rpglevel;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class LevelUpStationContainer extends Container {

private TileEntityLevelUpStation levelUpStation;

public LevelUpStationContainer(EntityPlayer player, InventoryPlayer inventory, TileEntityLevelUpStation entity) {
	this.levelUpStation = entity;
	this.addSlotToContainer(new Slot(entity,0,145,35));
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 9; j++) {
			this.addSlotToContainer(new Slot(inventory,j+i*9+9,8+j*18,84+i*18));
		}
	}
	for (int i = 0; i < 9; i++) {
		this.addSlotToContainer(new Slot(inventory,i,8+(i*18),142));
	}
}

@Override
public boolean canInteractWith(EntityPlayer entityplayer) {
	return this.levelUpStation.isUseableByPlayer(entityplayer);
}

public void detectAndSendChanges() {
	super.detectAndSendChanges();
	for (int i = 0; i < this.crafters.size(); i++) {
		ICrafting icrafting = (ICrafting) this.crafters.get(i);
	}
}

public ItemStack slotClick(int par1, int par2, int par3, EntityPlayer par4EntityPlayer) {
	super.slotClick(par1, par2, par3, par4EntityPlayer);
	return new ItemStack(Item.wheat);
}


    @Override
    public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
            ItemStack stack = null;
            Slot slotObject = (Slot) inventorySlots.get(slot);

            //null checks and checks if the item can be stacked (maxStackSize > 1)
            if (slotObject != null && slotObject.getHasStack()) {
                    ItemStack stackInSlot = slotObject.getStack();
                    stack = stackInSlot.copy();

                    //merges the item into player inventory since its in the tileEntity
                    if (slot < 9) {
                            if (!this.mergeItemStack(stackInSlot, 0, 35, true)) {
                                    return null;
                            }
                    }
                    //places it into the tileEntity is possible since its in the player inventory
                    else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) {
                            return null;
                    }

                    if (stackInSlot.stackSize == 0) {
                            slotObject.putStack(null);
                    } else {
                            slotObject.onSlotChanged();
                    }

                    if (stackInSlot.stackSize == stack.stackSize) {
                            return null;
                    }
                    slotObject.onPickupFromSlot(player, stackInSlot);
            }
            return stack;
    }


}

TileEntityLevelUpStation.java

package us.xvicario.rpglevel;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityLevelUpStation extends TileEntity implements IInventory {

public int front;
private ItemStack[] levelUpItemStacks;

public TileEntityLevelUpStation() {
	levelUpItemStacks = new ItemStack[1];
}

public void setFrontDirection(int f) {
	this.front = f;
}

public int getFrontDirection() {
	return this.front;
}

@Override public int getSizeInventory() {
	return levelUpItemStacks.length;
}

@Override
public void closeChest() {}

@Override
public ItemStack decrStackSize(int i, int j) {
	if (levelUpItemStacks[i] != null) {
		if (levelUpItemStacks[i].stackSize <= j) {
			ItemStack itemstack = levelUpItemStacks[i];
			levelUpItemStacks[i] = null;
			return itemstack;
		}
		ItemStack itemstack1 = levelUpItemStacks[i].splitStack(j);
		if (levelUpItemStacks[i].stackSize == 0) {
			levelUpItemStacks[i] = null;
		}
		return itemstack1;
	} else {
		return null;
	}
}

@Override
public String getInvName() {
	return "container.levelUp";
}

@Override
public int getInventoryStackLimit() {
	return 1;
}


@Override
public ItemStack getStackInSlot(int i) {
	return levelUpItemStacks[i];
}

@Override
public ItemStack getStackInSlotOnClosing(int i) {
	if (levelUpItemStacks[i] != null) {
		ItemStack itemstack = levelUpItemStacks[i];
		levelUpItemStacks[i] = null;
		return itemstack;
	} else {
		return null;
	}
}

@Override
public boolean isInvNameLocalized() {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
	if (worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this) {
		return false;
	}
	return entityplayer.getDistanceSq((double)xCoord+0.5D,(double)yCoord+0.5D,(double)zCoord+0.5D) <= 64D;
}

@Override
public void openChest() {}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
	levelUpItemStacks[0] = itemstack;
}

public void readFromNBT(NBTTagCompound par1NBTTagCompound) {
	super.readFromNBT(par1NBTTagCompound);
	NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items");
	levelUpItemStacks = new ItemStack[getSizeInventory()];
	for (int i = 0; i < nbttaglist.tagCount(); i++) {
		NBTTagCompound nbttagcompound = (NBTTagCompound)nbttaglist.tagAt(i);
		byte byte0 = nbttagcompound.getByte("Slot");
		if (byte0 >= 0 && byte0 < levelUpItemStacks.length) {
			levelUpItemStacks[byte0] = ItemStack.loadItemStackFromNBT(nbttagcompound);
		}
	}
	front = par1NBTTagCompound.getInteger("FrontDirection");
}

public void writeToNBT(NBTTagCompound par1NBTTagCompound) {
	super.writeToNBT(par1NBTTagCompound);
	par1NBTTagCompound.setInteger("FrontDirection", (int)front);
	NBTTagList nbttaglist = new NBTTagList();
	for (int i = 0; i < levelUpItemStacks.length; i++) {
		if (levelUpItemStacks[i] != null) { 
			NBTTagCompound nbttagcompound = new NBTTagCompound();
			nbttagcompound.setByte("Slot", (byte)i);
			levelUpItemStacks[i].writeToNBT(nbttagcompound);
			nbttaglist.appendTag(nbttagcompound);
		}
	}
	par1NBTTagCompound.setTag("Items", nbttaglist);
}

}

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • i tried downloading the drivers but it says no AMD graphics hardware
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
  • Topics

×
×
  • Create New...

Important Information

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