Skip to content

Commit 0c397bf

Browse files
committed
Simplify implementation of preventing axolotl damage
1 parent 1c9a003 commit 0c397bf

File tree

2 files changed

+12
-36
lines changed

2 files changed

+12
-36
lines changed

src/com/palmergames/bukkit/towny/listeners/TownyEntityListener.java

+6-18
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@
4343
*
4444
*/
4545
public class TownyEntityListener implements Listener {
46-
47-
/**
48-
* A Map that stores the target of Axolotls which has ever been cancelled
49-
* attack damage by Towny, where key is {@link Fish} (defender) and value is
50-
* a set of {@link Axolotl} (attacker).
51-
*/
52-
public static final Map<Entity, Set<Entity>> axolotlTargetExempt = new WeakHashMap<>();
53-
5446
private final Towny plugin;
5547

5648
public TownyEntityListener(Towny instance) {
@@ -127,21 +119,17 @@ public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
127119
}
128120

129121
/**
130-
* Prevent axolotl from targeting protected mobs if Towny has ever cancelled
131-
* the damage previously. Note that axolotls would still make the first attack
132-
* due to this listener relies on the states produced in {@link
133-
* CombatUtil#preventDamageCall(Entity, Entity, DamageCause)}.
122+
* Prevent axolotl from targeting protected mobs.
134123
*
135124
* @param event - EntityTargetLivingEntityEvent
136125
*/
137126
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
138127
public void onAxolotlTarget(EntityTargetLivingEntityEvent event) {
139-
var attacker = event.getEntity();
140-
var defender = event.getTarget();
141-
if (attacker.getType() == EntityType.AXOLOTL && axolotlTargetExempt.containsKey(defender) && axolotlTargetExempt.get(defender).contains(attacker)) {
142-
event.setTarget(null);
143-
event.setCancelled(true);
144-
((Axolotl) attacker).setMemory(MemoryKey.HAS_HUNTING_COOLDOWN, true);
128+
if (event.getEntity() instanceof Axolotl axolotl &&
129+
event.getTarget() instanceof Mob target &&
130+
CombatUtil.preventDamageCall(axolotl, target, DamageCause.ENTITY_ATTACK)) {
131+
axolotl.setTarget(null);
132+
axolotl.setMemory(MemoryKey.HAS_HUNTING_COOLDOWN, true);
145133
}
146134
}
147135

src/com/palmergames/bukkit/towny/utils/CombatUtil.java

+6-18
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33
import com.palmergames.bukkit.towny.*;
44
import com.palmergames.bukkit.towny.event.damage.*;
55
import com.palmergames.bukkit.towny.event.executors.TownyActionEventExecutor;
6-
import com.palmergames.bukkit.towny.listeners.TownyEntityListener;
76
import com.palmergames.bukkit.towny.object.*;
87
import com.palmergames.bukkit.towny.object.TownyPermission.ActionType;
98
import com.palmergames.bukkit.util.BukkitTools;
109
import org.bukkit.Bukkit;
1110
import org.bukkit.Material;
1211
import org.bukkit.block.Block;
1312
import org.bukkit.entity.*;
13+
import org.bukkit.entity.memory.MemoryKey;
1414
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
1515
import org.bukkit.projectiles.BlockProjectileSource;
1616
import org.jetbrains.annotations.NotNull;
1717

18-
import java.util.Collections;
19-
import java.util.List;
20-
import java.util.Set;
21-
import java.util.WeakHashMap;
18+
import java.util.*;
2219

2320
/**
2421
*
@@ -275,20 +272,11 @@ private static boolean preventDamageCall(TownyWorld world, Entity attackingEntit
275272
}
276273

277274
/*
278-
* Actually "clear" axolotl's target.
279-
* See TownyEntityListener#onAxolotlTarget for more information.
280-
* This also makes it possible to make a nice fish tank where
281-
* the axolotls won't have attack behaviours on other fish
282-
* if Fish is specified in the protection.mob_types list.
275+
* Clear axolotl's target.
283276
* */
284-
if (attackingEntity.getType() == EntityType.AXOLOTL && EntityTypeUtil.isInstanceOfAny(TownySettings.getProtectedEntityTypes(), defendingEntity)) {
285-
if (TownyEntityListener.axolotlTargetExempt.containsKey(defendingEntity)) {
286-
TownyEntityListener.axolotlTargetExempt.get(defendingEntity).add(attackingEntity);
287-
} else {
288-
Set<Entity> axolotlSet = Collections.newSetFromMap(new WeakHashMap<>()); // axolotl attackers
289-
axolotlSet.add(attackingEntity);
290-
TownyEntityListener.axolotlTargetExempt.put(defendingEntity, axolotlSet);
291-
}
277+
if (attackingEntity instanceof Axolotl axolotl && EntityTypeUtil.isInstanceOfAny(TownySettings.getProtectedEntityTypes(), defendingEntity)) {
278+
axolotl.setTarget(null);
279+
axolotl.setMemory(MemoryKey.HAS_HUNTING_COOLDOWN, true);
292280
return true;
293281
}
294282
}

0 commit comments

Comments
 (0)