Jump to content

[1.8][UNSOLVED]Detecting player achievements


Vulon

Recommended Posts

So, I already found code that lets me detect wether a player has an achievement or not. I used Minecraft.getMinecraft().thePlayer.getStatFileWriter.hasAcheivementUnlocked(LostSouls.getAngel);

This is supposed to check if the player has this achieve, and if they have a certain item in their inventory, then take that item and replace it with another item.  There are two instances I check for, one if you have the angel achieve and one if you have the demon achieve. If you have the demon one, it takes angel items and replaces them with a different item. Same for the angel achieve but with demon items instead. It is also supposed to cancel getting the other achieve. While this works great in single player, in multiplayer it goes haywire. Whilst testing the mod in multiplayer with a friend, I tried to get the demon achieve and everything was well- until my friend went for the angel achieve. Not only did this result in the replacement of my items as if I had the angel achieve, which is bad enough, and when I tried to place angel items in my inventory they were also replaced, which is supposed to happen as I had the demon achieve, but I got the angel achieve when I did so. That is not supposed to happen. I suspect the error is in the fact that thePlayer is EntityPlayerSP, but I don't know another way to detect achievements. Can anyone help?

Code:

 

@SubscribeEvent

public void denyDemon(LivingUpdateEvent event){

 

if(event.entityLiving instanceof EntityPlayer ){

EntityPlayer player = (EntityPlayer)event.entityLiving;

 

if(Minecraft.getMinecraft().thePlayer.getStatFileWriter().hasAchievementUnlocked(LostSouls.getAngel)){

if(player.inventory.hasItem(ItemManager.demonIngot)){

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

player.inventory.consumeInventoryItem(ItemManager.demonIngot);

 

}

if(player.inventory.hasItem(ItemManager.angelSword)){

 

player.inventory.consumeInventoryItem(ItemManager.angelSword);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.demonHelmet)){

 

player.inventory.consumeInventoryItem(ItemManager.demonHelmet);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.demonBoots)){

 

player.inventory.consumeInventoryItem(ItemManager.demonBoots);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.demonLegs)){

 

player.inventory.consumeInventoryItem(ItemManager.angelLegs);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.demonChest)){

 

player.inventory.consumeInventoryItem(ItemManager.demonChest);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.demonSword)){

 

player.inventory.consumeInventoryItem(ItemManager.demonSword);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.infusedDemonIngot)){

 

player.inventory.consumeInventoryItem(ItemManager.infusedDemonIngot);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.demonHorn)){

 

player.inventory.consumeInventoryItem(ItemManager.demonHorn);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}if(player.inventory.hasItem(ItemManager.demonLeather)){

 

player.inventory.consumeInventoryItem(ItemManager.demonLeather);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

}

 

}

}

@SubscribeEvent

public void denyDemonAchieve(AchievementEvent event){

if(event.achievement == LostSouls.getDemon){

if(Minecraft.getMinecraft().thePlayer.getStatFileWriter().hasAchievementUnlocked(LostSouls.getAngel)){

event.setCanceled(true);

}

}

}

@SubscribeEvent

public void denyAngel(LivingUpdateEvent event){

 

if(event.entityLiving instanceof EntityPlayer ){

EntityPlayer player= (EntityPlayer)event.entityLiving;

if(Minecraft.getMinecraft().thePlayer.getStatFileWriter().hasAchievementUnlocked(LostSouls.getDemon)){

if(player.inventory.hasItem(ItemManager.angelIngot)){

 

player.inventory.consumeInventoryItem(ItemManager.angelIngot);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.angelSword)){

 

player.inventory.consumeInventoryItem(ItemManager.angelSword);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.angelHelmet)){

 

player.inventory.consumeInventoryItem(ItemManager.angelHelmet);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.angelBoots)){

 

player.inventory.consumeInventoryItem(ItemManager.angelBoots);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.angelLegs)){

 

player.inventory.consumeInventoryItem(ItemManager.angelLegs);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.angelChest)){

 

player.inventory.consumeInventoryItem(ItemManager.angelChest);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.angelSword)){

 

player.inventory.consumeInventoryItem(ItemManager.angelSword);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.infusedAngelIngot)){

 

player.inventory.consumeInventoryItem(ItemManager.infusedAngelIngot);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

if(player.inventory.hasItem(ItemManager.angelSoul)){

 

player.inventory.consumeInventoryItem(ItemManager.angelSoul);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}if(player.inventory.hasItem(ItemManager.angelFeather)){

 

player.inventory.consumeInventoryItem(ItemManager.angelFeather);

player.inventory.addItemStackToInventory(new ItemStack(ItemManager.uselessShnurf));

 

}

}

}

}

 

Link to comment
Share on other sites

Minecraft's thePlayer field can only be used client side. Try getting the world instance from the entity and using that to check if it is remote using the isRemote field. That basically will make it check if it is running the event on client side. Correct me if I'm wrong though.

Link to comment
Share on other sites

I am not sure where and when you need those, so I'll note everything you need to know:

 

While Client.jar runs client and server logical sides, the Dedic.jar runs only server one. Client connected to some other server (dedic or non-self LAN) will only have client logical side.

 

Classes in client packages are client only. That includes Minecraft.class

Using Minecraft.class in ANY PLACE that should be ran on server logical side will break your mod when run on Dedic.jar.

And no - simple IF check is NOT enough. You have to setup proxies, you simply cannot have any reference to client classes in server classes.

 

If you need to access stats on server and based on them - modify player's inventory you will need:

* Cast entity to EntityPlayerMP, and use this.

playerMp.getStatFile()

Then you can do whetever you want on server side. You shouldn't bother with client in this case - stuff like items is set on SERVER, so server check will suffice.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Thanks, Ernio and Spider. Basically I should change all instances of EntityPlayer to EntityPlayerMP? Also, other slightly unrelated question. Could I use UUID to differentiate between players when checking if a player is wearing a full set of my armor and if so giving them effects? Thanks again for you help.

Link to comment
Share on other sites

Basically I should change all instances of EntityPlayer to EntityPlayerMP?

Not really, EntityPlayer is a "base" of player that is on both logical sides (client and player), then server will use 'MP' and client 'SP' which both have some side-specific methods.

There is NO need to cast player to 'MP'/'SP' if you are not using those specific methods. In this case (to get server-side stats) you indeed have to do that.

Keep in mind that to cast player to 'MP' you will need to do check:

!world.isRemote to make sure yo uare running on server side.

 

As to 'SP' - the class is actually @SideOnly(Side.CLIENT).

That means - Dedic.jar doesn't have it (it only exists for Client.jar). Logically - you cannot have ANY reference to in in class that will be ran on server. That's why you need to use proxy if you need 'SP' in e.g tick events (it all really depends on your design, but proxies are common in those cases).

 

Each player has its own "forever" UUID. Yes - basing on UUIDs is data-safe - meaning once you save something with UUID it will always point on one player or entity or whatever.

As to "is it good idea?" - no it's not.

UUIDs are string-like objects - they are simply slow to compare (like any string).

If you can - always pick player instance, if you can't do that - yes, you can use UUID.

1.7.10 is no longer supported by forge, you are on your own.

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.