Jump to content

Create Teams where friendly fire is off


Robertusxd

Recommended Posts

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.

Link to comment
Share on other sites

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 by oldcheese
clarification.
  • Like 1
Link to comment
Share on other sites

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 by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

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 :D

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) 

Link to comment
Share on other sites

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/

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.