Jump to content

My Item Information created from NBT printed on addInformation, gets deleted if not held by a tamed player


Recommended Posts

Posted (edited)

So I tried storing a 3 entities in a dragon whistle using NBTTagList and NBTTAgCompound it was hard and I tried starting on one dragon first with NBTTagCompound. SO I have a problem, so if I tamed a dragon and left click the dragon with the whistle, its entityID will be stored as an NBT Integer. Problem is when printing it, on addInformation it dissapears whenever I quit as if the NBT got deleted the eclipse not just in addInformation game and restart with a different playerName and ID. What I want is if the player is not the owner, it will not open the dragon whistle gui thus not controlling it, and the player name and the dragon name will be printed so the player would understand why.

package com.TheRPGAdventurer.ROTD.client.items;

import java.util.List;
import java.util.UUID;

import javax.annotation.Nullable;

import com.TheRPGAdventurer.ROTD.DragonMounts;
import com.TheRPGAdventurer.ROTD.client.userinput.StatCollector;
import com.TheRPGAdventurer.ROTD.server.entity.EntityTameableDragon;
import com.TheRPGAdventurer.ROTD.util.DMUtils;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemWritableBook;
import net.minecraft.item.ItemWrittenBook;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StringUtils;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import scala.reflect.internal.Trees.Modifiers;

public class ItemDragonWhistle extends Item {

	// private final MessageDragonWhistle dcw = new MessageDragonWhistle();
	// private List<EntityTameableDragon> dragons = new ArrayList();
	// EntityTameableDragon dragon;
	private byte oldIndex;

	public ItemDragonWhistle() {
		this.setUnlocalizedName("dragon_whistle");
		this.setRegistryName(new ResourceLocation(DragonMounts.MODID, "dragon_whistle"));
		this.setMaxStackSize(1);
		this.setCreativeTab(DragonMounts.TAB);
	}

	@Override 
	@SideOnly(Side.CLIENT) 
	public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
		NBTTagCompound nbt = stack.getTagCompound();
		if(stack != null && stack.hasTagCompound() && nbt.hasKey(DragonMounts.MODID.toLowerCase() + "dragon"))  {
		   EntityTameableDragon dragon = (EntityTameableDragon) worldIn.getEntityByID(nbt.getInteger(DragonMounts.MODID.toLowerCase() + "dragon"));
		   if(dragon != null) {
		      String dragonName = dragon.hasCustomName() ? dragon.getCustomNameTag() : dragon.getBreedType().toString().toLowerCase() + " dragon";
		      String ownerName = dragon.getOwner() != null ? dragon.getOwner().getName() : "null";
		      tooltip.add("Name:" + dragonName + " " + " Owner:" + " " + ownerName);
		   }
		}
	}

	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer player, EnumHand hand) {
		ItemStack stack = player.getHeldItem(hand);
		NBTTagCompound nbt = stack.getTagCompound();	
          if (!player.isSneaking() && stack.hasTagCompound()) {
      		  if (worldIn.isRemote) {
				  DragonMounts.proxy.openDragonWhistleGui(nbt.getInteger(DragonMounts.MODID.toLowerCase() + "dragon"),
						new ItemStack(this), worldIn); 
				  return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
			  }
		  }      
        
	   return new ActionResult<ItemStack>(EnumActionResult.FAIL, stack);		
	}
	
	@Override
	public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
		super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected);
	}
	
	@Override
	public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity target) {
		NBTTagCompound nbt = stack.getTagCompound();		

	    if (stack.hasTagCompound()) {
	         nbt = stack.getTagCompound(); 
	    }
	    else
	    {
	         nbt = new NBTTagCompound();
	    }
	       				
        stack.setTagCompound(nbt);
        
		if (target instanceof EntityTameableDragon) {
			EntityTameableDragon dragon = (EntityTameableDragon) target;				
			
			if (dragon.isTamedFor(player)) {	// I blame this, if my guess is right, this means that it only sets the dragon ID integer and removed if the player is untamed
				if(stack.getTagCompound() != null) { 
				   if (nbt.hasKey(DragonMounts.MODID.toLowerCase() + "dragon")) { 		
			          //  nbt.setInteger("dragon", null);
				    } else {
				    	String dragonName = dragon.hasCustomName() ? dragon.getCustomNameTag() : dragon.getBreedType().toString().toLowerCase() + " dragon";
					    String ownerName = dragon.getOwner() != null ? dragon.getOwner().getName() : "NULL";			  
				    	nbt.setInteger(DragonMounts.MODID.toLowerCase() + "dragon", dragon.getEntityId());
				    	player.sendStatusMessage(new TextComponentTranslation("item.whistle.hasDragon" + ":" + dragonName + " for " + ownerName, new Object[0]), true);
				    }
			    }
			//	else { 
			//		return false;
			//	}
			
		//	    EntityTameableDragon storedDragon = (EntityTameableDragon) player.world.getEntityByID(nbt.getInteger("dragon"));
			  //  if(storedDragon.isDead && storedDragon != null && nbt.hasKey("dragon")) {
			 //      nbt.removeTag("dragon");
			//    }
			    
			} else {
	            player.sendStatusMessage(new TextComponentTranslation("item.whistle.notOwned", new Object[0]), true);
			}
			
			return true;
		} else {
			return false;
		}
	}
}

 

2018-12-29_22.28.53.png

Edited by TheRPGAdventurer
Posted

reason i change it to ID because i got confused

	@Override 
	@SideOnly(Side.CLIENT) 
	public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
		NBTTagCompound nbt = stack.getTagCompound();
		if(stack != null && stack.hasTagCompound() && nbt.hasKey(DragonMounts.MODID.toLowerCase() + "dragon"))  {
		   EntityTameableDragon dragon = (EntityTameableDragon) worldIn.getEntityByID(nbt.getInteger(DragonMounts.MODID.toLowerCase() + "dragon"));
		   if(dragon != null) {
		      String dragonName = dragon.hasCustomName() ? dragon.getCustomNameTag() : dragon.getBreedType().toString().toLowerCase() + " dragon";
		      String ownerName = dragon.getOwner() != null ? dragon.getOwner().getName() : "null";
		      tooltip.add("Dragon:" + dragonName + " " + " Owner:" + " " + ownerName);
		   }
		}
	}

gettingUUID from an entity is a worldserver right? would it work here which is a client?

Posted

IIRC Unique ids are unique, and are the same on the client and sever 

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
package com.TheRPGAdventurer.ROTD.client.items;

import java.util.List;
import java.util.UUID;

import javax.annotation.Nullable;

import com.TheRPGAdventurer.ROTD.DragonMounts;
import com.TheRPGAdventurer.ROTD.client.userinput.StatCollector;
import com.TheRPGAdventurer.ROTD.server.entity.EntityTameableDragon;
import com.TheRPGAdventurer.ROTD.util.DMUtils;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemWritableBook;
import net.minecraft.item.ItemWrittenBook;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StringUtils;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import scala.reflect.internal.Trees.Modifiers;

public class ItemDragonWhistle extends Item {

	// private final MessageDragonWhistle dcw = new MessageDragonWhistle();
	// private List<EntityTameableDragon> dragons = new ArrayList();
	// EntityTameableDragon dragon;
	private byte oldIndex;

	public ItemDragonWhistle() {
		this.setUnlocalizedName("dragon_whistle");
		this.setRegistryName(new ResourceLocation(DragonMounts.MODID, "dragon_whistle"));
		this.setMaxStackSize(1);
		this.setCreativeTab(DragonMounts.TAB);
	}

	@Override 
	@SideOnly(Side.CLIENT) 
	public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
		NBTTagCompound nbt = stack.getTagCompound();
		if(stack != null && stack.hasTagCompound() && nbt.hasKey(DragonMounts.MODID.toLowerCase() + "dragon"))  {
		   if(worldIn instanceof WorldServer) {
			 WorldServer worldServer = (WorldServer) worldIn; 
		     EntityTameableDragon dragon = (EntityTameableDragon) worldServer.getEntityFromUuid(nbt.getUniqueId(DragonMounts.MODID.toLowerCase() + "dragon"));
		     if(dragon != null) {
		        String dragonName = dragon.hasCustomName() ? dragon.getCustomNameTag() : dragon.getBreedType().toString().toLowerCase() + " dragon";
		        String ownerName = dragon.getOwner() != null ? dragon.getOwner().getName() : "null";
		        tooltip.add("Dragon:" + dragonName + " " + " Owner:" + " " + ownerName);
		      }
		   }
		}
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer player, EnumHand hand) {
		ItemStack stack = player.getHeldItem(hand);
		NBTTagCompound nbt = stack.getTagCompound();		

	    if (stack.hasTagCompound()) {
	         nbt = stack.getTagCompound(); 
	    }
	    else
	    {
	         nbt = new NBTTagCompound();
	    }
	       				
        stack.setTagCompound(nbt);
          if (!player.isSneaking() && stack.hasTagCompound()) {
      		  if (worldIn.isRemote) {
				  DragonMounts.proxy.openDragonWhistleGui(nbt.getUniqueId(DragonMounts.MODID.toLowerCase() + "dragon"),
						new ItemStack(this), worldIn); 
				  return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
			  }
		  }      
        
	   return new ActionResult<ItemStack>(EnumActionResult.FAIL, stack);		
	}
	
	@Override
	public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
		super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected);
	}
	
	@Override
	public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity target) {
		NBTTagCompound nbt = stack.getTagCompound();		

	    if (stack.hasTagCompound()) {
	         nbt = stack.getTagCompound(); 
	    }
	    else
	    {
	         nbt = new NBTTagCompound();
	    }
	       				
        stack.setTagCompound(nbt);
        
		if (target instanceof EntityTameableDragon) {
			EntityTameableDragon dragon = (EntityTameableDragon) target;				
			
			if (dragon.isTamedFor(player)) {	// I blame this, if my guess is right, this means that it only sets the dragon ID integer and removed if the player is untamed
				if(stack.getTagCompound() != null) { 
				   if (nbt.hasKey(DragonMounts.MODID.toLowerCase() + "dragon")) { 		
			          //  nbt.setInteger("dragon", null);
				    } else {
				    	String dragonName = dragon.hasCustomName() ? dragon.getCustomNameTag() : dragon.getBreedType().toString().toLowerCase() + " dragon";
					    String ownerName = dragon.getOwner() != null ? dragon.getOwner().getName() : "NULL";			  
				    	nbt.setUniqueId(DragonMounts.MODID.toLowerCase() + "dragon", dragon.getUniqueID());
				    	player.sendStatusMessage(new TextComponentTranslation("item.whistle.hasDragon" + ":" + dragonName + " for " + ownerName, new Object[0]), true);
				    }
			    }
			//	else { 
			//		return false;
			//	}
			
		//	    EntityTameableDragon storedDragon = (EntityTameableDragon) player.world.getEntityByID(nbt.getInteger("dragon"));
			  //  if(storedDragon.isDead && storedDragon != null && nbt.hasKey("dragon")) {
			 //      nbt.removeTag("dragon");
			//    }
			    
			} else {
	            player.sendStatusMessage(new TextComponentTranslation("item.whistle.notOwned", new Object[0]), true);
			}
			
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public boolean hasEffect(ItemStack stack) {
		NBTTagCompound nbt = stack.getTagCompound();		

	    if (stack.hasTagCompound()) {
	         nbt = stack.getTagCompound(); 
	    }
	    else
	    {
	         nbt = new NBTTagCompound();
	    }
	       				
        stack.setTagCompound(nbt);
        if(nbt.hasKey("dragon")) {
        	return true;
        } else {
	    	return super.hasEffect(stack);
        }
	}
}

 

Posted

when I use UUID it sends a different UUID 

I printed it out with a logger 

 

[14:59:35] [main/INFO] [dragonmounts]: Current State at 527561dd-f8a1-4e39-8ee1-a26897e4ce36
[14:59:38] [main/INFO] [dragonmounts]: Current State at 7020e729-f0b5-4b60-ae91-def0d5b83d26

Posted

Ok so i tried using getEntityId for this but it keeps dissappearing and dieseben071 guy said to use UUID, the id is stored in an nbt for a dragon whistle item which when right clicked will open that gui, and the gui will set some command data by pressing the buttons fly,come,circle etc. but since guis are client, it causes a nullPointer by worldServer.getEntityByUUid. Is there any way around this?

package com.TheRPGAdventurer.ROTD.client.gui;

import java.util.UUID;

import javax.annotation.Nullable;

import org.lwjgl.input.Keyboard;

import com.TheRPGAdventurer.ROTD.DragonMounts;
import com.TheRPGAdventurer.ROTD.client.sound.ModSounds;
import com.TheRPGAdventurer.ROTD.server.entity.EntityTameableDragon;
import com.TheRPGAdventurer.ROTD.server.network.MessageDragonWhistle;
import com.TheRPGAdventurer.ROTD.util.DMUtils;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.SoundCategory;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;

public class GuiDragonWhistle extends GuiScreen {
	
	Entity entity;
	private final MessageDragonWhistle dcw = new MessageDragonWhistle();
	private float mousePosX;
	private float mousePosY;
	World world;
	
	ItemStack whistle;
	UUID uuid;
	GuiButton nothing;
	GuiButton circle;
	GuiButton followFlying;
	GuiButton come;
	GuiButton sit;
	EntityTameableDragon owner;
	
	

	public GuiDragonWhistle(World world, UUID uuid, ItemStack whistle) {
		super();
		this.whistle = whistle;
		this.uuid = uuid;
		this.world = world;
		
	}
	
	@Override
	public void initGui() {
		buttonList.clear();
		
		Keyboard.enableRepeatEvents(true);
		
		nothing =      new GuiButton(0, width / 2 - 50, height / 2 + 10, 
	                   98, 20, I18n.format("gui.nothing", new Object[0]));
		
		circle =       new GuiButton(0, width / 2 + 100 - 50, height / 2 + 10, 
	                   98, 20, I18n.format("gui.circle", new Object[0]));
		
		followFlying = new GuiButton(0, width / 2 - 100 - 50, height / 2 + 10, 
	                   98, 20, I18n.format("gui.followFlying", new Object[0]));
		
		come =   new GuiButton(0, width / 2 - 50, height / 2 - 15, 
	                   98, 20, I18n.format("gui.goToPlayer", new Object[0]));
		
		buttonList.add(nothing);
		buttonList.add(circle);
		buttonList.add(followFlying);
		buttonList.add(come);
	}
	
    @Nullable
    public EntityTameableDragon getDragon() { //tried using this
        if (this.owner == null && this.uuid != null && this.world instanceof WorldServer)
        {
            Entity entity = ((WorldServer)this.world).getEntityFromUuid(this.uuid);

            if (entity instanceof EntityLivingBase)
            {
                this.owner = (EntityTameableDragon)entity;
            }
        }

        return this.owner;
    }
    
	@Override
	protected void actionPerformed(GuiButton button) {
		EntityTameableDragon dragon = (EntityTameableDragon)((WorldServer) world).getEntityFromUuid(uuid); // causes nullpointer
		DMUtils.getLogger().info("Current State at " + dragon.getUniqueID().toString() + "" + dragon.getPosition() +""+uuid);
		
		if(dragon != null) {
		   byte previousState = dragon.getWhistleState();
		   dragon.come(button == come);
		   dragon.circle(button == circle);
		   dragon.follow(button == followFlying);
		   dragon.nothing(button == nothing);
		   dragon.world.playSound(this.mc.player, this.mc.player.getPosition(), ModSounds.DRAGON_WHISTLE, SoundCategory.PLAYERS, 5, 1);
		   byte controlState = dragon.getWhistleState();
		   
		   if (controlState != previousState) {
					DragonMounts.NETWORK_WRAPPER
							.sendToServer(new MessageDragonWhistle(dragon.getUniqueID(), controlState));
		    }
		   
		  //   }
		} 
	}
	
	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks) {
		this.mousePosX = mouseX;
		this.mousePosY = mouseY;
		super.drawScreen(mouseX, mouseY, partialTicks);
	}
	
	@Override
	public boolean doesGuiPauseGame() {
		return false;
	}

}

 

Posted (edited)
Just now, diesieben07 said:

You need to use the UUID for storing in NBT.

You need to use the entity ID when talking to the client.

 

This is not difficult. The two are separate concepts, you cannot mix them.

I know now, what Im confused is the client and server stuff, sorry for making too many threads btw. SO gui is client right? and getting the entity uuid is server, so how am I supposed to get the dragon with the uuid? if i do however a nullpointer happens

Edited by TheRPGAdventurer
Posted (edited)

Oh sorry about that

package com.TheRPGAdventurer.ROTD.client.gui;

import java.util.UUID;

import javax.annotation.Nullable;

import org.lwjgl.input.Keyboard;

import com.TheRPGAdventurer.ROTD.DragonMounts;
import com.TheRPGAdventurer.ROTD.client.sound.ModSounds;
import com.TheRPGAdventurer.ROTD.server.entity.EntityTameableDragon;
import com.TheRPGAdventurer.ROTD.server.network.MessageDragonWhistle;
import com.TheRPGAdventurer.ROTD.util.DMUtils;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.SoundCategory;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;

public class GuiDragonWhistle extends GuiScreen {
	
	Entity entity;
	private final MessageDragonWhistle dcw = new MessageDragonWhistle();
	private float mousePosX;
	private float mousePosY;
	World world;
	
	ItemStack whistle;
	UUID uuid;
	GuiButton nothing;
	GuiButton circle;
	GuiButton followFlying;
	GuiButton come;
	GuiButton sit;
	EntityTameableDragon owner;
	
	

	public GuiDragonWhistle(World world, UUID uuid, ItemStack whistle) {
		super();
		this.whistle = whistle;
		this.uuid = uuid;
		this.world = world;
		
	}
	
	@Override
	public void initGui() {
		buttonList.clear();
		
		Keyboard.enableRepeatEvents(true);
		
		nothing =      new GuiButton(0, width / 2 - 50, height / 2 + 10, 
	                   98, 20, I18n.format("gui.nothing", new Object[0]));
		
		circle =       new GuiButton(0, width / 2 + 100 - 50, height / 2 + 10, 
	                   98, 20, I18n.format("gui.circle", new Object[0]));
		
		followFlying = new GuiButton(0, width / 2 - 100 - 50, height / 2 + 10, 
	                   98, 20, I18n.format("gui.followFlying", new Object[0]));
		
		come =   new GuiButton(0, width / 2 - 50, height / 2 - 15, 
	                   98, 20, I18n.format("gui.goToPlayer", new Object[0]));
		
		buttonList.add(nothing);
		buttonList.add(circle);
		buttonList.add(followFlying);
		buttonList.add(come);
	}
	
    @Nullable
    public EntityTameableDragon getDragon() { //tried using this
        if (this.owner == null && this.uuid != null && this.world instanceof WorldServer)
        {
            Entity entity = ((WorldServer)this.world).getEntityFromUuid(this.uuid);

            if (entity instanceof EntityLivingBase)
            {
                this.owner = (EntityTameableDragon)entity;
            }
        }

        return this.owner;
    }
    
	@Override
	protected void actionPerformed(GuiButton button) {
		EntityTameableDragon dragon = (EntityTameableDragon)((WorldServer) world).getEntityFromUuid(uuid); // causes nullpointer
		
		if(dragon != null) {
		   byte previousState = dragon.getWhistleState();
		   dragon.come(button == come);
		   dragon.circle(button == circle);
		   dragon.follow(button == followFlying);
		   dragon.nothing(button == nothing);
		   dragon.world.playSound(this.mc.player, this.mc.player.getPosition(), ModSounds.DRAGON_WHISTLE, SoundCategory.PLAYERS, 5, 1);
		   byte controlState = dragon.getWhistleState();
		   
		   if (controlState != previousState) {
					DragonMounts.NETWORK_WRAPPER
							.sendToServer(new MessageDragonWhistle(dragon.getUniqueID(), controlState));
		    }
		   
		  //   }
		} 
	}
	
	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks) {
		this.mousePosX = mouseX;
		this.mousePosY = mouseY;
		super.drawScreen(mouseX, mouseY, partialTicks);
	}
	
	@Override
	public boolean doesGuiPauseGame() {
		return false;
	}

}

 

Edited by TheRPGAdventurer
Posted
Just now, diesieben07 said:

And you are trying to use WorldServer from a gui, this makes zero sense.

Exactly why it makes zero sense, thats why im confused right now :D, I really should use getEntityID there because its clientside,  but I only stored the uuid on the Item nbt(should I store the ID integer too as well?), or do I have to convert the UUId to id? and how ? Message Packets?

Posted (edited)
Just now, diesieben07 said:

WorldServer is the server-side world. GUIs are purely client side. // I get this,

 

You tell the client to open the GUI with a packet, right? Before you send that packet, obtain the entity on the server using the UUID and then send the integer ID to the client.

	@Override
	protected void actionPerformed(GuiButton button) {
		if(dragon != null) {
		   byte previousState = dragon.getWhistleState();
		   dragon.come(button == come);
		   dragon.circle(button == circle);
		   dragon.follow(button == followFlying);
		   dragon.nothing(button == nothing);
		   this.mc.player.world.playSound(this.mc.player, this.mc.player.getPosition(), ModSounds.DRAGON_WHISTLE, SoundCategory.PLAYERS, 5, 1);
		   byte controlState = dragon.getWhistleState();
		   
		   if (controlState != previousState) {
					DragonMounts.NETWORK_WRAPPER
							.sendToServer(new MessageDragonWhistle(uuid, controlState));
		    }
		  //   }
		} 
	}

so that would mean the come and other commands shouldnt be called on using the dragon but on the packet itself?, the commands are in statefields i forgot to mention that.

	public boolean nothing() {
		return (dataManager.get(WHISTLE_STATE).byteValue() & 1) == 1;
	}

	public boolean follow() {
		return (dataManager.get(WHISTLE_STATE).byteValue() >> 1 & 1) == 1;
	}

	public boolean circle() {
		return (dataManager.get(WHISTLE_STATE).byteValue() >> 2 & 1) == 1;
	}

	public boolean come() {
		return (dataManager.get(WHISTLE_STATE).byteValue() >> 3 & 1) == 1;
	}


	@Override
	public void nothing(boolean nothing) {
		setStateField(0, nothing);
	}

	@Override
	public void follow(boolean follow) {
		setStateField(1, follow);
	}

	@Override
	public void circle(boolean circle) {
		setStateField(2, circle);
	}

	@Override
	public void come(boolean come) {
		setStateField(3, come);
	}

	/**
	 * @TheRPGAdventurer thanks AlexThe666
	 * @param 
	 * @param 
	 */
	private void setStateField(int i, boolean newState) {
		byte prevState = dataManager.get(WHISTLE_STATE).byteValue();
		if (newState) {
			dataManager.set(WHISTLE_STATE, (byte) (prevState | (1 << i)));			
		} else {
			dataManager.set(WHISTLE_STATE, (byte) (prevState & ~(1 << i)));
		}
	}

 

Edited by TheRPGAdventurer
Posted
Just now, diesieben07 said:

I can't think of an easy solution for that on the top of my head.

public class MessageDragonWhistle extends AbstractMessage<MessageDragonWhistle> {

	public UUID dragonId;
	public byte controlState;
	EntityTameableDragon dragon;

	public MessageDragonWhistle(UUID dragonId, byte controlState) {
		this.dragonId = dragonId;
		this.controlState = controlState;
	}

	public MessageDragonWhistle() {
	}

	@Override
	public void fromBytes(ByteBuf buf) {
		PacketBuffer packetBuf = new PacketBuffer(buf);
	    dragonId = packetBuf.readUniqueId();
		controlState = buf.readByte();
	}

	@Override
	public void toBytes(ByteBuf buf) {
		PacketBuffer packetBuf = new PacketBuffer(buf);
		packetBuf.writeUniqueId(dragonId);
		buf.writeByte(controlState);

	}
	
	@Override
	@SideOnly(Side.CLIENT)
	public void onClientReceived(Minecraft client, MessageDragonWhistle message, EntityPlayer player, MessageContext messageContext) {

	}

	@Override
	public void onServerReceived(MinecraftServer server, MessageDragonWhistle message, EntityPlayer player, MessageContext messageContext) {
		World world = player.world; 
	//	if(world instanceof WorldServer) {
		  WorldServer worldServer = (WorldServer) world;
	      Entity entity = server.getEntityFromUuid(dragonId);
		  if (entity != null && entity instanceof EntityTameableDragon) {
			  EntityTameableDragon dragon = (EntityTameableDragon) entity;
			  this.setDragon(dragon);
			  if (dragon.isOwner(player)) {
				  dragon.setWhistleState(message.controlState);
			  }
			}			
	//	} 
	}

	public EntityTameableDragon getDragon() {
	    return dragon;
    }

    public void setDragon(EntityTameableDragon dragon) {
	    this.dragon = dragon;
    }

would just using a setter and getter in a dragon would work?

	@Override 
	@SideOnly(Side.CLIENT) 
	public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
		NBTTagCompound nbt = stack.getTagCompound();
		if(stack != null && stack.hasTagCompound() && nbt.hasKey(DragonMounts.MODID.toLowerCase() + "dragon"))  {
            MessageDragonWhistle whistle = new MessageDragonWhistle();
		     EntityTameableDragon dragon = whistle.getDragon();
		     if(dragon != null) {
		        String dragonName = dragon.hasCustomName() ? dragon.getCustomNameTag() : dragon.getBreedType().toString().toLowerCase() + " dragon";
		        String ownerName = dragon.getOwner() != null ? dragon.getOwner().getName() : "null";
		        tooltip.add("Dragon:" + dragonName + " " + " Owner:" + " " + ownerName);
		    }
		}
	}

I tried but it didnt.

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.

Announcements



×
×
  • Create New...

Important Information

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