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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Your drivers are in a broken state, follow the guide in the FAQ: https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/#:~:text=How do I update my drivers%3F
    • A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffc3fe22b60, pid=15228, tid=5140 # # JRE version: OpenJDK Runtime Environment Microsoft-8035246 (17.0.8+7) (build 17.0.8+7-LTS) # Java VM: OpenJDK 64-Bit Server VM Microsoft-8035246 (17.0.8+7-LTS, mixed mode, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64) # Problematic frame: # C [atio6axx.dll+0x192b60] # # No core dump will be written. Minidumps are not enabled by default on client versions of Windows # # If you would like to submit a bug report, please visit: # https://aka.ms/minecraftjavacrashes # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug i have no idea why please help
    • Quick-Books is great with regards to coordinating your monetary data. You can decide to live talk with a specialist at Quick-Books to get the answer for your questions. You will actually want to get to the talk going to the landing page and call us +1855-210-1428.
    • Update: I managed to make the item not disappear after crafting, but it doesn't remove any durability. import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.Enchantments; import net.minecraft.enchantment.UnbreakingEnchantment; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; public class NuggetHammer extends Item { private boolean damage; public NuggetHammer(Properties p_i48487_1_) { super(p_i48487_1_); } @Override public int getMaxDamage(ItemStack stack) { return 54 - 1; } public boolean isBarVisible(ItemStack stack) { return false; } @Override public ItemStack getContainerItem(ItemStack stack) { ItemStack copy = stack.copy(); copy.setCount(1); if (!this.damage) return copy; int unbreaking = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.UNBREAKING, stack); for (int i = 0; i < unbreaking; i++) { if (UnbreakingEnchantment.shouldIgnoreDurabilityDrop(stack, unbreaking, random)) return copy; } copy.setDamageValue(stack.getDamageValue() + 1); if (copy.getDamageValue() > stack.getMaxDamage()) return ItemStack.EMPTY; return copy; } }  
  • Topics

×
×
  • Create New...

Important Information

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