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

[1.7.10] Need my item to get the players display name


HappyKiller1O1
 Share

Recommended Posts

Heyo! So, my question here today is how would I be able to right click a player with my item and then, grab the players display name to save the the itemstack NBT. Pretty simple so thanks for the input in advance. :P

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

You can override the method itemInteractionForEntity() in your Item class to handle the right clicking of a player. You can use the method getDisplayName() in the EntityPlayer class to get a player's name, and you can save the name to nbt using the nbt methods in ItemStack.

Sample (Not tested):

@Override
public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase livingBase) {
    if (livingBase instanceof EntityPlayer) {
        NBTTagCompound tagCompound = new NBTTagCompound();
        tagCompound.setString("playerName", ((EntityPlayer) livingBase).getDisplayName());
        stack.setTagCompound(tagCompound);
        return true;
    }
    return false;
}

Link to comment
Share on other sites

Now see, I tried that but, when I right clicked a player (tested it on a server) it returned nothing as if it wasn't the player.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Sorry. xD I mean when I right clicked the player it didn't run the code. I'm not sure if you're suppose to use the EntityPlayer provided.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Well, I made a server and had my brother join. xD I did add this though:

 

if(entityLiving.worldObj.isRemote) {
    return false;
}

 

Would that break the code?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Well, basically I have it add information if the stack NBT tag is not null. The information on the item was not set so, it didn't run. I commented out my system print outs by accident before running the server and shit. DERP

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

If I understand you correctly, you're only letting the item be set if it has already been set before (Because an item's nbt tag, by default, is null). You probably want that the other way around.

 

Sorry, it occured to me that you probably aren't talking about setting the tag in itemInteractionForEntity(). Can I see your code for adding the information?

Link to comment
Share on other sites

Actually I am. xD I probably messed something up because I wrote it late at night. :P Here's the two methods:

 

public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase entity) {
    	if(entity.worldObj.isRemote) {
    		return false;
    	}
    	
        if(entity instanceof EntityPlayer) {
        	EntityPlayer victim = (EntityPlayer)entity;
        	
        	NBTTagCompound cmp = new NBTTagCompound();
        	
        	String displayName = victim.getDisplayName();
        	
        	cmp.setString("victim", displayName);
        	
        	stack.setTagCompound(cmp);
        	
        	System.out.println("ORIGINAL DISPLAY NAME: " + displayName);
        	System.out.println("SAVED DISPLAY NAME: " + cmp.getString("victim"));
        	
        	return true;
        }
    	
    	return false;
    }

public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean flag) {
	String defaultDisplay = "No Victim Set";
	String victimDisplay = null;

	if(stack.stackTagCompound != null) {
		victimDisplay = stack.stackTagCompound.getString("victim");
	}

	list.add(EnumChatFormatting.DARK_AQUA + "When right clicked, it fires");
	list.add(EnumChatFormatting.DARK_AQUA + "lightning upon the bound player.");
	list.add("");
	list.add(EnumChatFormatting.RED + "Can only be used four times.");
	list.add("");
	list.add(victimDisplay != null ? victimDisplay : defaultDisplay);
	list.add("");
	list.add(EnumChatFormatting.GREEN + "Hold Shift and Right-Click to");
	list.add(EnumChatFormatting.GREEN + "open the victim set menu.");
}

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

I get the display name because of name changing not being implemented in 1.7.10. Once I update it I'll change the code. :P

No, it is implemented for all versions, if you change your name it will change everywhere. That's why you need to use uuid.

Link to comment
Share on other sites

What I read is that only 1.8 and above will see viewable changes to the display name. But might as well take your advice. How would I go about using UUID for the display name?

 

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Now see, I understand using the UUID for the player. But how would I get the display name? Would I do something like this?:

 

EntityPlayer player = entityLiving.worldObj.getEntityByID(//Player UUID).getDisplayName();

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

So, the reason why it's not displaying the name is because the stack compound tag is null. I'm not sure how considering the server console says it set the tag. Here's my code (Will use UUID after my testing is complete):

public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase entity) {
    	if(entity.worldObj.isRemote) {
    		return false;
    	}
    	
        if(entity instanceof EntityPlayer) {
        	System.out.println("ENTITY GOTTEN");
        	
        	EntityPlayer victim = (EntityPlayer)entity;
        	
        	NBTTagCompound cmp = new NBTTagCompound();
        	
        	String displayName = victim.getDisplayName();
        	
        	cmp.setString("victim", displayName);
        	
        	stack.setTagCompound(cmp);
        	
        	System.out.println("ORIGINAL DISPLAY NAME: " + displayName);
        	System.out.println("SAVED DISPLAY NAME: " + cmp.getString("victim"));
        	
        	return true;
        }else {
        	System.out.println("ENTITY WAS NOT PLAYER");
        }
    	
    	return false;
    }

public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean flag) {
	String defaultDisplay = "No Victim Set";
	String victimDisplay = null;

	if(stack.getTagCompound() != null) {
		System.out.println("STACK COMPOUND WAS NOT NULL");
		victimDisplay = stack.getTagCompound().getString("victim");
	}else {
		System.out.println("STACK COMPOUND WAS NULL");
	}

	list.add(EnumChatFormatting.DARK_AQUA + "When right clicked, it fires");
	list.add(EnumChatFormatting.DARK_AQUA + "lightning upon the bound player.");
	list.add("");
	list.add(EnumChatFormatting.RED + "Can only be used four times.");
	list.add("");
	list.add(victimDisplay != null ? victimDisplay : defaultDisplay);
	list.add("");
	list.add(EnumChatFormatting.GREEN + "Hold Shift and Right-Click to");
	list.add(EnumChatFormatting.GREEN + "open the victim set menu.");
}

 

Any reason why this is caused?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

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

    • https://www.mediafire.com/file/hs6o5snxiwpk1j5/latest.log/file
    • Alright Thanks! So that's why alone what i wrote wasn't enough.
    • Hello,  So i was trying to run forge 1.18 with some mods but it just crashes and gives a -1 error code whenever I click Play World or Create New World. I tried to launch Forge alone without any mods and it worked, so there is some issue with the mod loading.  But in the crash report, it doesn't mention any issues with the mod loading at all, instead it says all mods have loaded. I've attached the crash log with this, it says there's an issue with the render thread? I hope that will help narrow down the issue.   Thanks   Crash file:  
    • How can i make the transparency of an block work just like the SlimeBlock, as shown in the image: By now i'm setting the RenderLayer of the block to RenderType.translucent() and also extending the HalfTransparentBlock in my block.
    • [11:22:34] [main/INFO]: ModLauncher running: args [--username, Amaru782, --version, forge-36.2.19, --gameDir, C:\Users\usuario\curseforge\minecraft\Instances\Better Minecraft [FORGE] - 1.16.5, --assetsDir, C:\Users\usuario\curseforge\minecraft\Install\assets, --assetIndex, 1.16, --uuid, dc27f25f4a0349eca32fbd0abdd36c7c, --accessToken, ????????, --userType, msa, --versionType, release, --width, 1366, --height, 768, --launchTarget, fmlclient, --fml.forgeVersion, 36.2.19, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20210115.111550] [11:22:34] [main/INFO]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 1.8.0_51 by Oracle Corporation [11:22:35] [main/WARN]: LEGACY JDK DETECTED, SECURED JAR HANDLING DISABLED [11:22:36] [main/INFO]: Added Lets Encrypt root certificates as additional trust [11:22:36] [main/INFO]: SpongePowered MIXIN Subsystem Version=0.8.4 Source=file:/C:/Users/usuario/curseforge/minecraft/Install/libraries/org/spongepowered/mixin/0.8.4/mixin-0.8.4.jar Service=ModLauncher Env=CLIENT [11:22:36] [main/WARN]: Configuration file C:\Users\usuario\curseforge\minecraft\Instances\Better Minecraft [FORGE] - 1.16.5\config\fml.toml is not correct. Correcting [11:22:36] [main/WARN]: Incorrect key [defaultConfigPath] was corrected from null to defaultconfigs [11:22:37] [main/FATAL]: Failed to find Minecraft resource version 1.16.5-20210115.111550 at C:\Users\usuario\curseforge\minecraft\Install\libraries\net\minecraftforge\forge\1.16.5-36.2.19\forge-1.16.5-36.2.19-client.jar [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]: java.lang.RuntimeException: Missing minecraft resource! [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLCommonLaunchHandler.lambda$validatePaths$4(FMLCommonLaunchHandler.java:124) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLCommonLaunchHandler$$Lambda$170/292641216.accept(Unknown Source) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLCommonLaunchHandler.validatePaths(FMLCommonLaunchHandler.java:121) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLLoader.setupLaunchHandler(FMLLoader.java:202) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at net.minecraftforge.fml.loading.FMLServiceProvider.initialize(FMLServiceProvider.java:94) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServiceDecorator.onInitialize(TransformationServiceDecorator.java:68) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServicesHandler.lambda$initialiseTransformationServices$7(TransformationServicesHandler.java:107) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServicesHandler$$Lambda$92/30578394.accept(Unknown Source) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at java.util.HashMap$Values.forEach(HashMap.java:972) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServicesHandler.initialiseTransformationServices(TransformationServicesHandler.java:107) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.TransformationServicesHandler.initializeTransformationServices(TransformationServicesHandler.java:59) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.Launcher.run(Launcher.java:76) [11:22:37] [main/INFO]: [java.lang.ThreadGroup:uncaughtException:1052]:     at cpw.mods.modlauncher.Launcher.main(Launcher.java:66)  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.