Jump to content

[1.12.2] Can't save death location to capability??


SapphireSky

Recommended Posts

I'm trying to save the player's death location to a capability so a custom compass will point there.

Everything seems to work fine, and according to all my console print-outs the capability is being properly synced, but for some reason both the item's tooltip and compass point graphic will only point to the capability's default value of 0,0,0 (BlockPos.ORIGIN, just so it's never null).

 

I don't understand why this isn't working.

 

Capability/death events:

Spoiler

	@SubscribeEvent
	public void onDeath(LivingDeathEvent event)
	{
		if (event.getEntityLiving() instanceof EntityPlayer)
		{
			EntityPlayer player = (EntityPlayer) event.getEntityLiving();
			if (!player.world.isRemote)
			{
				System.out.println("[" + Main.MODID.toUpperCase() + "] DIED AT " + player.posX + ", " + player.posY + ", " + player.posZ);
				System.out.println("[" + Main.MODID.toUpperCase() + "] --> SYNCING DEATH POINT TO CAPABILITY.");
				IDeathSpot cap = DeathSpot.get(player);
				cap.setDeathDimension(player.dimension);
				cap.setDeathSpot(player.getPosition());
				cap.sync(player);
			}
		}
	}

	@SubscribeEvent
	public void attachCapability(AttachCapabilitiesEvent<Entity> event)
	{
		if (event.getObject() instanceof EntityPlayer)
		{
			System.out.println("[" + Main.MODID.toUpperCase() + "] ATTACHING DEATHSPOT CAPABILITY.");
			event.addCapability(Main.DEATHSPOT, new DeathSpot());
		}
	}

	@SubscribeEvent
	public void onLogin(PlayerLoggedInEvent event)
	{
		if (!event.player.world.isRemote)
		{
			System.out.println("[" + Main.MODID.toUpperCase() + "] SYNCING DEATHSPOT CAPABILITY.");
			IDeathSpot cap = DeathSpot.get(event.player);
			cap.sync(event.player);
		}
	}

	@SubscribeEvent
	public void onRespawn(Clone event)
	{
		if (!event.getEntityPlayer().world.isRemote)
		{
			System.out.println("[" + Main.MODID.toUpperCase() + "] RESPAWNING...");
			System.out.println("[" + Main.MODID.toUpperCase() + "] --> COPYING OLD LOCATION OF " + event.getOriginal().posX + ", "
					+ event.getOriginal().posY + ", " + event.getOriginal().posZ);
			System.out.println("[" + Main.MODID.toUpperCase() + "] --> DEATHSPOT LOCATION IS "
					+ DeathSpot.get(event.getOriginal()).getDeathSpot().getX() + ","
					+ DeathSpot.get(event.getOriginal()).getDeathSpot().getY() + ","
					+ DeathSpot.get(event.getOriginal()).getDeathSpot().getZ());

			IDeathSpot old = DeathSpot.get(event.getOriginal());
			IDeathSpot clone = DeathSpot.get(event.getEntityPlayer());
			clone.setDeathSpot(old.getDeathSpot());
			clone.setDeathDimension(old.getDeathDimension());
			clone.sync(event.getEntityPlayer());
			System.out.println("[" + Main.MODID.toUpperCase() + "] CLONING DEATH POINT.");
		}
	}

 

Capability class:

Spoiler


	private int deathdimension;
	private BlockPos deathspot;

	public CapDeathSpot()
	{
		deathdimension = 0;
		deathspot = BlockPos.ORIGIN;
	}

	@Override
	public void setDeathSpot(BlockPos pos)
	{
		deathspot = pos;
	}

	@Override
	public void setDeathDimension(int i)
	{
		deathdimension = i;
	}

	@Override
	public BlockPos getDeathSpot()
	{
		return deathspot;
	}

	@Override
	public int getDeathDimension()
	{
		return deathdimension;
	}

	@Override
	public void sync(EntityPlayer player)
	{
		Main.net.sendTo(new MessageSync(deathspot.getX(), deathspot.getY(), deathspot.getZ(), deathdimension), (EntityPlayerMP) player);
	}

 

Capability IStorage:

Spoiler



		@Override
		public NBTBase writeNBT(Capability<IDeathSpot> capability, IDeathSpot instance, EnumFacing side)
		{
			NBTTagCompound nbt = new NBTTagCompound();
			nbt.setInteger("deathx", instance.getDeathSpot().getX());
			nbt.setInteger("deathy", instance.getDeathSpot().getY());
			nbt.setInteger("deathx", instance.getDeathSpot().getZ());
			nbt.setInteger("deathd", instance.getDeathDimension());
			return nbt;
		}

		@Override
		public void readNBT(Capability<IDeathSpot> capability, IDeathSpot instance, EnumFacing side, NBTBase nbt)
		{
			NBTTagCompound tag = (NBTTagCompound)nbt;
			instance.setDeathSpot(new BlockPos(tag.getInteger("deathx"), tag.getInteger("deathy"), tag.getInteger("deathz")));
			instance.setDeathDimension(tag.getInteger("deathd"));
		}

 

 

Sync packet:

Spoiler



	private int x, y, z, d;

	public MessageSync()
	{
	}

	public MessageSync(int x, int y, int z, int d)
	{
		this.x = x;
		this.y = y;
		this.z = z;
		this.d = d;
	}

	@Override
	public void fromBytes(ByteBuf buf)
	{
		x = buf.readInt();
		y = buf.readInt();
		z = buf.readInt();
		d = buf.readInt();
	}

	@Override
	public void toBytes(ByteBuf buf)
	{
		buf.writeInt(x);
		buf.writeInt(y);
		buf.writeInt(z);
		buf.writeInt(d);
	}

	public static class Handler implements IMessageHandler<MessageSync, IMessage>
	{
		@Override
		public IMessage onMessage(MessageSync message, MessageContext ctx)
		{
			Minecraft.getMinecraft().addScheduledTask(() ->
			{
				IDeathSpot stats = DeathSpot.get(Minecraft.getMinecraft().player);
				stats.setDeathSpot(new BlockPos(message.x, message.y, message.z));
				stats.setDeathDimension(message.d);
				System.out.println("[" + Main.MODID.toUpperCase() + "] SYNCED DEATHSPOT TO " + stats.getDeathSpot().getX() + ", "
						+ stats.getDeathSpot().getY() + ", " + stats.getDeathSpot().getZ());
			});
			return null;
		}
	}

 

And compass item:

Spoiler




	public ObsidianCompass()
	{
		this.setUnlocalizedName("obsidiancompass");
		this.setRegistryName("obsidiancompass");
		this.setMaxStackSize(1);
		this.setMaxDamage(-1);
		this.setCreativeTab(CreativeTabs.TRANSPORTATION);
		this.addPropertyOverride(new ResourceLocation("angle"), new IItemPropertyGetter()
		{
			@SideOnly(Side.CLIENT)
			double rotation;
			@SideOnly(Side.CLIENT)
			double rota;
			@SideOnly(Side.CLIENT)
			long lastUpdateTick;

			@SideOnly(Side.CLIENT)
			public float apply(ItemStack stack, @Nullable World world, @Nullable EntityLivingBase entity)
			{
				if (entity == null || stack.isOnItemFrame() || !(entity instanceof EntityPlayer))
				{
					return 0.0F;
				}
				else
				{
					if (world == null)
					{
						world = entity.world;
					}

					double d0;
					if (world.provider.isSurfaceWorld())
					{
						double d1 = (double) entity.rotationYaw;
						d1 = MathHelper.positiveModulo(d1 / 360.0D, 1.0D);
						double d2 = this.getSpawnToAngle(world, (EntityPlayer) entity) / (Math.PI * 2D);
						d0 = 0.5D - (d1 - 0.25D - d2);
					}
					else
					{
						d0 = Math.random();
					}
					d0 = this.wobble(world, d0);
					return MathHelper.positiveModulo((float) d0, 1.0F);
				}
			}

			@SideOnly(Side.CLIENT)
			private double wobble(World world, double d)
			{
				if (world.getTotalWorldTime() != this.lastUpdateTick)
				{
					this.lastUpdateTick = world.getTotalWorldTime();
					double d0 = d - this.rotation;
					d0 = MathHelper.positiveModulo(d0 + 0.5D, 1.0D) - 0.5D;
					this.rota += d0 * 0.1D;
					this.rota *= 0.8D;
					this.rotation = MathHelper.positiveModulo(this.rotation + this.rota, 1.0D);
				}

				return this.rotation;
			}

			@SideOnly(Side.CLIENT)
			private double getSpawnToAngle(World world, EntityPlayer player)
			{
				BlockPos blockpos = DeathSpot.get(player).getDeathSpot();
				return Math.atan2((double) blockpos.getZ() - player.posZ, (double) blockpos.getX() - player.posX);
			}
		});
	}

	@Override
	public String getItemStackDisplayName(ItemStack stack)
	{
		return "Obsidian Compass";
	}

	@Override
	public void addInformation(ItemStack stack, World world, List<String> list, ITooltipFlag flag)
	{
		list.add(TextFormatting.GRAY + "Points to your");
		list.add(TextFormatting.GRAY + "death location.");
		if (Minecraft.getMinecraft() != null)
		{
			Minecraft mc = Minecraft.getMinecraft();
			if (mc.player != null)
			{
				EntityPlayer player = mc.player;
				if (DeathSpot.get(player) != null)
				{
					BlockPos pos = DeathSpot.get(player).getDeathSpot();
					list.add(TextFormatting.BLUE + "Death Location:");
					list.add(pos == null ? "Empty" : "X:" + (int) pos.getX() + ", Y:" + (int) pos.getY() + ", Z:" + (int) pos.getZ());
				}
			}
		}
	}

 

 

 

 

 

Link to comment
Share on other sites

6 hours ago, diesieben07 said:

Do not call this in the Clone event. That makes no sense. The only thing you should do in the Clone event is copy data from old player to new player.

Then why do all tutorials on capabilities I've seen have it done in the PlayerEvent.Clone event?

PlayerEvent.Clone is also how the Forge documentation says to do it...

Is that something that recently changed?

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
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.