Posted October 9, 20177 yr So i been looking around a little but didnt really find a way to do this. Im basicially creating a gamemode where two teams are playing against each other and within these teams the players should not be abe to attack each other. So what i need is to turn off PvP between some Players but towards other Players it still has to be turned on. I hope there is a way to do that and thanks in advance.
October 9, 20177 yr Don't turn off PVP. What you want to do is create a modifier for the players to make them belong to a team. You could add a custom Capability that lists their Teamname for example. Now what you want to do is create an forge event https://mcforge.readthedocs.io/en/latest/events/intro/ in which you subscribe to the LivingHurtEvent, this event fires whenever a player is ABOUT to take damage. Now you make it only listen to players taking damage, you do this with a simple if statement. if(event.getEntity() instanceof EntityPlayer) Now you have an Event that will listen to the damage event. You can now use event.getSource() to get the damage source. Most damage sources in the game (Projectiles, sword attacks etc.) Will have a parameter that tells you who actually fired the arrow/used the sword. While the code below probably wouldn't work, since I removed the part about capabilities with a comment and you probably need a few null checks to make sure the damagesource has an Entity attached. This is what I imagine the event would end up looking like: @SubscribeEvent public void event(LivingHurtEvent event){ if(event.getEntity() instanceof EntityPlayer && event.getSource().getSourceOfDamage() instanceof EntityPlayer){ EntityPlayer hitter = (EntityPlayer)event.getSource().getSourceOfDamage(); //returns an Entity who caused the damage. EntityPlayer target = (EntityPlayer)event.getEntity(); //returns Entity who got hit. We already established this is a player. //Code to check the user's Capabilities and see if they have the same team here if(hitterCapability.getTeam == targetCapability.getTeam){ event.setCanceled(true); } } } now, adding capabilities to players is documented here: https://mcforge.readthedocs.io/en/latest/datastorage/capabilities/ and if you want to synch this up to the server side you probably want to use a packet, unless players don't get a team untill after they log in. Sorry for being a bit long while talking, I'm messaging away from home. I hope this helps a bit, if you have questions feel free to ask. Edit: Not every source of damage has a player source like I said before, make sure that the code you end up making takes this into account. If the source of the damage is an TNT block you might want to block the damage alltogether. The most basic use of blocking damage with a player source should block sources like hits, throwables, throwing potions(?) etc. But the things that are NOT blocked are mostly stuff with big AOE's, which means that that could possibly count as friendly fire when blowing stuff up. Edited October 9, 20177 yr by oldcheese clarification.
October 9, 20177 yr One thing to look at is the scoreboard class already allows you to create teams, and it already has a field to enable/disable friendly fire. Not sure if that works for what you want, but you might want to look at it. At least you can check out how it works. Edited October 9, 20177 yr by jabelar Check out my tutorials here: http://jabelarminecraft.blogspot.com/
October 9, 20177 yr Author thats the first thing i found when i googled it but im not a 100% sure on how to use it. i tried it out but i have no real way to test it in single player
October 9, 20177 yr 6 minutes ago, Robertusxd said: thats the first thing i found when i googled it but im not a 100% sure on how to use it. i tried it out but i have no real way to test it in single player Using whatever client you're using to code you should be able to launch a test server and two clients (which will by default have different usernames)
October 9, 20177 yr 12 minutes ago, Robertusxd said: im using eclipse, not quite sure how to do that Edit: I actually installed Eclipse for some reason just to check. Edited October 9, 20177 yr by oldcheese I have too much time on my hands.
October 9, 20177 yr Author thats how i start the server, but how do i get two clients on that server? when i start 2 clients afterwards they have the same name and one of them joining kicks the other one Edited October 9, 20177 yr by Robertusxd
October 9, 20177 yr Really? Did you specifically configure your run to use your own minecraft account? If you don't, it should logon with a random username listed as player###, which should both be different.
October 10, 20177 yr Actually it is usually good to have your user account info in the run configuration because otherwise during testing the various inventory stuff, any ownership data (like tamed animals), spawn points, and so forth aren't consistent. But in this case you should create a copy of the run configuration without the user account information (you probably entered it in the program arguments in the run configuration editor). Then you can either run with or without account information as you need to. But the main point is yes you should be able to run multiple instances of Minecraft from Eclipse, a server and a couple clients, whenever it is useful. Check out my tutorials here: http://jabelarminecraft.blogspot.com/
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.