Plugin Help Combatlog plugin development

Discussion in 'Plugin Help/Development/Requests' started by bman7842, Dec 25, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    bman7842

    Okay so I have a simplistic class handling something that prevents a user from logging out during a pvp battle (when I say prevents I mean it drops all their stuff when they leave). My problem is simple, if the user is in a safe-zone or no pvp zone even when they punch another user they still get the message saying they are in combat and I don't want this to happen. How would I prevent something like this from happening?

    There may be other problems with this code but please stay on topic with the problem at hand.
    Code:
    package me.bman7842.Main;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class CombatLog implements Listener{
       
        public HashMap<String, Integer> playersinbattle = new HashMap<String, Integer>();
        public ArrayList<String> playersleftinbattle = new ArrayList<String>();
       
        Main main;
       
        public CombatLog(Main main){
            this.main = main;
        }
       
        @EventHandler
        public void onAttack(EntityDamageByEntityEvent e) {
            Player p = (Player)e.getDamager();
            if(e.getDamager() instanceof Player) {
                //if (e.isCancelled()) return;
                if (e.getDamage() == 0) return;
                if (e.getEntity() instanceof Player) {
                    if (playersinbattle.containsKey(p.getName())){
                        Player secp = (Player)e.getEntity();
                        playersinbattle.put(p.getName(), 20);
                        playersinbattle.put(secp.getName(), 20);
                    } else {
                        playersinbattle.put(p.getName(), 20);
                        Player secp = (Player)e.getEntity();
                        playersinbattle.put(secp.getName(), 20);
                        p.sendMessage(main.infobar);
                        p.sendMessage(main.messagestarter + ChatColor.RED + "You are now in combat, please wait 20 seconds before logging off");
                        p.sendMessage(ChatColor.RED + "Or until the message shows up saying you can log off");
                        p.sendMessage(main.infobar);
                    }
                }
            }
        }
       
       
       
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            if (!playersinbattle.containsKey(p.getName())) return;
            playersleftinbattle.add(p.getName());
        }
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if (!playersleftinbattle.contains(p.getName())) return;
           
            Location ploc = p.getLocation();
            for(int i = 0; i < 36;i++){
                if(p.getInventory().getItem(i) != null){
                    p.getWorld().dropItemNaturally(ploc, p.getInventory().getItem(i));
                    p.getInventory().clear(i);
                }
            }
            p.getInventory().setArmorContents(new ItemStack[]{null,null,null,null});
        }
    }
    
     
  2. Offline

    CorrieKay

    Well, based on the limited code, you're not checking if they're in a safe zone.

    Firstly, you need to figure out how to determine how to create a safe zone. You're going to want to declare a 'safe zone' by selecting two locations, and using them as opposite corners of a 3d cube. If you want to get fancy and use shapes other than a cube by utilizing selection areas in the world edit api. Though be aware that for you to do this, you'll need to create a dependency to world edit, and not every server out there will want to have it installed.

    Once you do that, you'll need to figure out how to store it. I would recommend using your plugin configuration, or a custom configuration if you so desire. When the plugin loads, you'll have the system load the safe areas.

    Lastly, you'll need to go to your on-hit event listener, and basically check to see if they're within the bounds of the safe area. If not, cancel the damage, and prevent the combat logging code from firing.
     
  3. Offline

    bman7842

    nonono, safe-zone was an example. The definition for safe-zone is pvp disabled zone. For example, in World Edit there are flags to disable pvp, I don't want to use World Edit's API though, I just want to figure out a way to determine whether or not the user's attack actually did damage to the other player.
     
  4. Offline

    CorrieKay

    Considering you already return when no damage is dealt, I'm not sure what your end goal is anymore.

    e.getDamage() returns the damage done by any attack during the event.
     
  5. Offline

    bman7842

    Yes, but that is the problem that is not working.
     
  6. Offline

    CorrieKay

    What exactly do you mean that its not working? You should probably start by explaining what exactly you want this plugin to do. You mentioned an unrelated example in the OP, so that kinda threw me off, so.. Lets start over. What do you want your plugin to do?
     
  7. Offline

    bman7842

    I want this plugin to drop all the players item if they leave the game while attacking someone. Of course there is a certain delay of 20 seconds you must wait. All players in "combat" are stored inside that HashMap and when a player leaves and they are in that HashMap it will drop all of their stuff at the place they were last seen before logging off. The problem is if the user is in a pvp disabled zone when they punch someone it will still put them in "combat" mode and I don't want this. You pointed out the if statement checking if the damage was more than 0 but like I said, this is not working so something is wrong with it.
     
  8. Offline

    CorrieKay

    Try increasing the priority of your event to "Monitor". It is likely that your code is firing before the anti-pvp code that is lowering the damage.
     
Thread Status:
Not open for further replies.

Share This Page