Add damage metadata system to CombatLog and CustomDamageEvent

This adds a system whereby plugins can associate metadata with individual
damage events and then retrieve that metadata later from the CombatLog.
One example use case for this is Minestrike.  Players are rewarded according
to the weapon that they used to kill another player, however the weapons
are frequently renamed.  Minestrike can now associate a "gunType" key
to the name of the weapon used to kill the player, regardless of the skin
that the gun currently has applied to it.
This commit is contained in:
Nate Mortensen 2016-11-04 10:32:40 -06:00 committed by cnr
parent a05b53c233
commit 21ebb2b4c8
5 changed files with 67 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import mineplex.core.common.util.F;
@ -39,12 +40,12 @@ public class CombatComponent
}
}
public void AddDamage(String source, double dmg, List<DamageChange> mod)
public void AddDamage(String source, double dmg, List<DamageChange> mod, Map<String, Object> metadata)
{
if (source == null)
source = "-";
GetDamage().addFirst(new CombatDamage(source, dmg, mod));
GetDamage().addFirst(new CombatDamage(source, dmg, mod, metadata));
LastDamage = System.currentTimeMillis();
}

View File

@ -1,7 +1,8 @@
package mineplex.minecraft.game.core.combat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mineplex.minecraft.game.core.damage.DamageChange;
@ -10,14 +11,20 @@ public class CombatDamage
private String _name;
private double _dmg;
private long _time;
private List<DamageChange> _mod = new ArrayList<>();
private List<DamageChange> _mod;
private Map<String, Object> _metadata;
public CombatDamage(String name, double dmg, List<DamageChange> mod)
{
this(name, dmg, mod, new HashMap<>());
}
public CombatDamage(String name, double dmg, List<DamageChange> mod, Map<String, Object> metadata)
{
_name = name;
_dmg = dmg;
_time = System.currentTimeMillis();
_mod = mod;
_metadata = metadata;
}
public String GetName()
@ -39,4 +46,17 @@ public class CombatDamage
{
return _mod;
}
/**
* Retrieves metadata that was associated with this damage via
* {@link mineplex.minecraft.game.core.damage.CustomDamageEvent#setMetadata(String, Object)}.
* <p/>
* There is no standardized metadata that should be expected. Metadata is meant to be used
* on a per-minigame basis to store additional information about the damage.
* @return a non-null map containing the metadata
*/
public Map<String, Object> getMetadata()
{
return _metadata;
}
}

View File

@ -3,6 +3,7 @@ package mineplex.minecraft.game.core.combat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilTime;
@ -47,12 +48,17 @@ public class CombatLog
}
public void Attacked(String damagerName, double damage,
LivingEntity damagerEnt, String attackName, List<DamageChange> mod)
LivingEntity damagerEnt, String attackName, List<DamageChange> mod)
{
this.Attacked(damagerName, damage, damagerEnt, attackName, mod, new HashMap<>());
}
public void Attacked(String damagerName, double damage,
LivingEntity damagerEnt, String attackName, List<DamageChange> mod, Map<String, Object> metadata)
{
// Add Attacked
CombatComponent comp = GetEnemy(damagerName, damagerEnt);
comp.AddDamage(attackName, damage, mod);
comp.AddDamage(attackName, damage, mod, metadata);
// Set Last
LastDamager = comp;

View File

@ -307,11 +307,10 @@ public class CombatManager extends MiniPlugin
Get((Player)event.GetDamagerEntity(true)).SetLastCombatEngaged(System.currentTimeMillis());
Get(event.GetDamageePlayer()).SetLastCombatEngaged(System.currentTimeMillis());
}
Get(event.GetDamageePlayer()).Attacked(
UtilEnt.getName(event.GetDamagerEntity(true)),
(int) event.GetDamage(), event.GetDamagerEntity(true),
reason, event.GetDamageMod());
reason, event.GetDamageMod(), event.getMetadata());
}
// Damager is WORLD
else
@ -409,9 +408,9 @@ public class CombatManager extends MiniPlugin
if (event.GetReason() != null)
reason = event.GetReason();
Get(event.GetDamageePlayer()).Attacked(source,
(int) event.GetDamage(), null, reason, event.GetDamageMod());
(int) event.GetDamage(), null, reason, event.GetDamageMod(), event.getMetadata());
}
}

View File

@ -2,7 +2,9 @@ package mineplex.minecraft.game.core.damage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
@ -29,6 +31,7 @@ public class CustomDamageEvent extends Event implements Cancellable
private HashMap<String, Double> _knockbackMod = new HashMap<String, Double>();
private Map<String, Object> _metadata = new HashMap<>();
//Ents
private LivingEntity _damageeEntity;
private Player _damageePlayer;
@ -368,6 +371,34 @@ public class CustomDamageEvent extends Event implements Cancellable
return IsCancelled();
}
/**
* Associates the provided metadata key with the provided value.
* Metadata can later be retrieved from individual {@link
* mineplex.minecraft.game.core.combat.CombatDamage} instances acquired
* from the {@link mineplex.minecraft.game.core.combat.CombatLog}.
*
* @param key non-null key to associate the value with
* @param value nullable value
* @throws IllegalArgumentException if key is null
*/
public void setMetadata(String key, Object value)
{
Validate.notNull(key);
_metadata.put(key, value);
}
/**
* Gets all Metadata associated with this event. There is
* no standardized metadata that should be expected.
*
* @see #setMetadata(String, Object)
* @return non-null map of metadata
*/
public Map<String, Object> getMetadata()
{
return _metadata;
}
@Override
@Deprecated
/**