Skip to content

Instantly share code, notes, and snippets.

@aikar
Created May 28, 2014 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aikar/34ad5e79756a686ca8a8 to your computer and use it in GitHub Desktop.
Save aikar/34ad5e79756a686ca8a8 to your computer and use it in GitHub Desktop.
public static double processRiskGauge(Player player, Creature entity, double damage) {
final CustomMob type = CustomMob.getType(entity);
if (entity instanceof Animals && type == null) {
return damage;
}
final int playerDifficulty = getPlayerDifficulty(player);
/*
===========
Risk Gauge
===========
If player is hitting entity that has no chance of reaching the player, we will
penalize or teleport the entity, making it fair.
*/
long now = System.currentTimeMillis();
boolean isLowRisk;
boolean isRanged = entity.getType() == EntityType.SKELETON || entity.getType() == EntityType.WITHER ||
entity.getType() == EntityType.GHAST || type == Marlix.MARLIX;
if (player.getGameMode() == GameMode.CREATIVE) {
isLowRisk = false;
} else if (!isRanged) {
EntityMoveApi.setEntityDestination(entity, player);
boolean hasAPath = EntityMoveApi.hasEntityPath(entity);
Location dest = EntityMoveApi.getEntityPathDestination(entity);
boolean canReachPlayer = false;
if (dest != null) {
final double distanceFromDestToPlayer = player.getLocation().distance(dest);
final double distanceFromPlayerToEntity = player.getLocation().distance(entity.getLocation());
canReachPlayer = !(distanceFromDestToPlayer > 2 && distanceFromPlayerToEntity < 4);
}
isLowRisk = !hasAPath || !canReachPlayer;
} else {
isLowRisk = !entity.hasLineOfSight(player);
}
if (isLowRisk) {
final Long lastCheap = Util.getMetadata(entity, TempMetaKey.LAST_CHEAP_HIT);
Long totalCheap = Util.incrementMetadata(entity, TempMetaKey.TOTAL_CHEAP_HITS, 1L);
if (lastCheap != null) {
if (isRanged) {
if (type != null || now - lastCheap < 7000) {
damage = 0;
Util.showError(player, 7000,
"&cIt appears that Monster is having trouble getting to you... It resisted the attack.");
}
} else if (type instanceof MiniBoss || playerDifficulty > 7) {
// 8,9,10 will teleport entity to player if they are not at risk
entity.teleport(player.getLocation().add(0, .5, 0));
Util.showError(player, 7000,
"&cIt appears that Monster is having trouble getting to you... It has been drawn to you.");
} else if (totalCheap > 1) {
// 5,6,7 will nerf the entities score
modifyEntityScore(entity, (-damage) * totalCheap);
if (type != null) {
Util.showError(player, 7000,
"&cIt appears that Monster is having trouble getting to you... Extra rewards chance reduced.");
}
}
}
Util.setMetadata(entity, TempMetaKey.LAST_CHEAP_HIT, now);
}
return damage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment