Jump to content

Recommended Posts

Posted

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());
				}
			}
		}
	}

 

 

 

 

 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • *** buffer overflow detected ***: terminated /usr/bin/forge-1.12.2d: line 162: 15787 Aborted                 (core dumped) ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null Starting server...There are several suitable screens on:     14630.forge-1.12.2    (Detached)     15796.forge-1.12.2    (Detached) Use -S to specify a session How do i fix this issue?
    • Oh I forgot to update the title. I figured out the issue and I'm rather embarrassed to say that it was a file path issue. The game already knew I was accessing the achievements so I wasn't suppose to include advancements in the file path. Minecraft file paths have always confused me a little bit... ResourceLocation advancementId = new ResourceLocation( TheDeadRise.MODID,"adventure/spawntrigger");
    • Can someone help my with this? My forge server won't open and I'm not that good with this stuff. It gave me this error message:   C:\Users\apbeu\Desktop\Forge server>java -Xmx4G -Xms1G -jar server.jar nogui 2024-12-11 18:21:01,054 main WARN Advanced terminal features are not available in this environment [18:21:01] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 36.2.34, --fml.mcpVersion, 20210115.111550, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge, nogui] [18:21:01] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher 8.1.3+8.1.3+main-8.1.x.c94d18ec starting: java version 21.0.4 by Oracle Corporation Exception in thread "main" java.lang.IllegalAccessError: class cpw.mods.modlauncher.SecureJarHandler (in unnamed module @0x402e37bc) cannot access class sun.security.util.ManifestEntryVerifier (in module java.base) because module java.base does not export sun.security.util to unnamed module @0x402e37bc         at cpw.mods.modlauncher.SecureJarHandler.lambda$static$1(SecureJarHandler.java:45)         at cpw.mods.modlauncher.api.LamdbaExceptionUtils.uncheck(LamdbaExceptionUtils.java:95)         at cpw.mods.modlauncher.SecureJarHandler.<clinit>(SecureJarHandler.java:45)         at cpw.mods.modlauncher.Launcher.lambda$new$6(Launcher.java:55)         at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708)         at cpw.mods.modlauncher.api.TypesafeMap.computeIfAbsent(TypesafeMap.java:52)         at cpw.mods.modlauncher.api.TypesafeMap.computeIfAbsent(TypesafeMap.java:47)         at cpw.mods.modlauncher.Environment.computePropertyIfAbsent(Environment.java:62)         at cpw.mods.modlauncher.Launcher.<init>(Launcher.java:55)         at cpw.mods.modlauncher.Launcher.main(Launcher.java:66)         at net.minecraftforge.server.ServerMain$Runner.runLauncher(ServerMain.java:63)         at net.minecraftforge.server.ServerMain$Runner.access$100(ServerMain.java:60)         at net.minecraftforge.server.ServerMain.main(ServerMain.java:57) C:\Users\apbeu\Desktop\Forge server>pause
    • Here is the url for the crash report if anyone can help me, please. https://mclo.gs/KGn5LWy  
    • Every single time I try and open my modpack it crashes before the game fully opens and I don't know what is wrong. I open the crash logs but I don't understand what any of it means. What do I do?
  • Topics

×
×
  • Create New...

Important Information

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