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

    • You cannot do this, this will be shared between all instances of your item, client and server. I would recommend you use getGameTime instead, which will increase by 1 every tick. You can override shouldCauseReequipAnimation in your Item class to control this.
    • Here is the log. 08.12 12:25:29 [Multicraft] Starting server! 08.12 12:25:29 [Multicraft] Loaded config for "Custom JAR (Place "custom.jar" in base directory)" 08.12 12:25:31 [Multicraft] Updating eula.txt file 08.12 12:25:31 [Server] INFO WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap. 08.12 12:25:33 [Server] INFO 2021-12-08 12:25:33,132 main WARN Advanced terminal features are not available in this environment 08.12 12:25:33 [Server] main/INFO [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 35.1.37, --fml.mcpVersion, 20201102.104115, --fml.mcVersion, 1.16.4, --fml.forgeGroup, net.minecraftforge, nogui] 08.12 12:25:33 [Server] main/INFO [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 1.8.0_275 by Oracle Corporation 08.12 12:25:33 [Server] main/INFO [op.OptiFineTransformationService/]: OptiFineTransformationService.onLoad 08.12 12:25:33 [Server] main/INFO [op.OptiFineTransformationService/]: OptiFine ZIP file: /mods/OptiFine-1.16.4_HD_U_G7.jar 08.12 12:25:33 [Server] main/INFO [op.OptiFineTransformer/]: Target.PRE_CLASS is available 08.12 12:25:33 [Server] main/INFO [ne.mi.fm.lo.FixSSL/CORE]: Added Lets Encrypt root certificates as additional trust 08.12 12:25:33 [Server] main/INFO [mixin/]: SpongePowered MIXIN Subsystem Version=0.8.2 Source=file:/libraries/org/spongepowered/mixin/0.8.2/mixin-0.8.2.jar Service=ModLauncher Env=SERVER 08.12 12:25:33 [Server] main/INFO [op.OptiFineTransformationService/]: OptiFineTransformationService.initialize 08.12 12:25:34 [Server] main/INFO [op.OptiFineTransformationService/]: OptiFineTransformationService.transformers 08.12 12:25:34 [Server] main/INFO [op.OptiFineTransformer/]: Targets: 308 08.12 12:25:35 [Server] main/INFO [op.OptiFineTransformationService/]: additionalClassesLocator: [optifine., net.optifine.] 08.12 12:25:35 [Server] main/INFO [mixin/]: Successfully loaded Mixin Connector [vazkii.botania.common.MixinConnector] 08.12 12:25:35 [Server] main/INFO [mixin/]: Successfully loaded Mixin Connector [vazkii.patchouli.common.MixinConnector] 08.12 12:25:35 [Server] main/INFO [cp.mo.mo.LaunchServiceHandler/MODLAUNCHER]: Launching target 'fmlserver' with arguments [--gameDir, ., nogui] 08.12 12:25:36 [Server] main/FATAL [ne.mi.fm.lo.RuntimeDistCleaner/DISTXFORM]: Attempted to load class net/minecraft/client/gui/screen/Screen for invalid dist DEDICATED_SERVER 08.12 12:25:36 [Server] INFO Exception in thread "main" [12:25:36] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1052]: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1052]: at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:39) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1052]: at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1052]: at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1052]: at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1052]: at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1052]: at net.minecraftforge.server.ServerMain$Runner.runLauncher(ServerMain.java:63) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1052]: at net.minecraftforge.server.ServerMain$Runner.access$100(ServerMain.java:60) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1052]: at net.minecraftforge.server.ServerMain.main(ServerMain.java:57) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1061]: Caused by: java.lang.reflect.InvocationTargetException 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1061]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1061]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1061]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1061]: at java.lang.reflect.Method.invoke(Method.java:498) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1061]: at net.minecraftforge.fml.loading.FMLServerLaunchProvider.lambda$launchService$0(FMLServerLaunchProvider.java:51) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1061]: at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.ThreadGroup:uncaughtException:1061]: ... 7 more 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:644]: Caused by: java.lang.ExceptionInInitializerError 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:644]: at net.minecraft.crash.CrashReport.func_71504_g(CrashReport.java:101) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:644]: at net.minecraft.crash.CrashReport.<init>(CrashReport.java:54) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:644]: at net.minecraft.crash.CrashReport.func_230188_h_(CrashReport.java:425) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:644]: at net.minecraft.server.Main.main(Main.java:91) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:644]: ... 13 more 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: Caused by: java.lang.RuntimeException: Attempted to load class net/minecraft/client/gui/screen/Screen for invalid dist DEDICATED_SERVER 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: at net.minecraftforge.fml.loading.RuntimeDistCleaner.processClassWithFlags(RuntimeDistCleaner.java:71) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: at cpw.mods.modlauncher.LaunchPluginHandler.offerClassNodeToPlugins(LaunchPluginHandler.java:85) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: at cpw.mods.modlauncher.ClassTransformer.transform(ClassTransformer.java:120) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: at cpw.mods.modlauncher.TransformingClassLoader$DelegatedClassLoader.findClass(TransformingClassLoader.java:265) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:136) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:98) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: at java.lang.ClassLoader.loadClass(ClassLoader.java:351) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: at net.optifine.reflect.Reflector.<clinit>(Reflector.java:184) 08.12 12:25:36 [Server] main/INFO [STDERR/]: [java.lang.Throwable:printStackTrace:668]: ... 17 more 08.12 12:25:37 [Multicraft] Server shut down (starting) 08.12 12:25:37 [Multicraft] Restarting crashed server in 10 seconds 08.12 12:25:37 [Multicraft] Looks like a crash, check the server console. Return value: 1 08.12 12:25:37 [Multicraft] Server stopped 08.12 12:25:40 [Multicraft] Received stop command 08.12 12:25:40 [Multicraft] Server stopped   The server works without mods, but not with them. Forge is installed on the server.
    • Ah thankyou, yeah sometimes when it's not clear on the stacktrace where the problem started because of nullptrs or lambdas or something I find it quite difficult to work out where to look. Is this what caused the crash and can I simply fix it by adding something like: modEventBus.addListener(EntityAISetupManager::addEntityAttributes); to my LotbMod constructor
    • Hello, i have installed latest version of MC Forge and it don't want to start. It gives me error: somewhere in internet i found information that Forge for MC 1.17.1 don't support Java 17, but from other side Minecraft 1.18 don't support Java 8 (with JDK 17 it worked perfectly) so which version of java should I install to make it work together? When i trying to run MC with Java 8 it gives me error:  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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