Skip to content

Commit

Permalink
cache data on entities instead of maps
Browse files Browse the repository at this point in the history
  • Loading branch information
JustEli committed Feb 3, 2022
1 parent 569ec38 commit 5b3bf07
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 50 deletions.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,22 @@
</build>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<version>1.14-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-chat</artifactId>
<version>1.16-R0.4</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/justeli/coins/Coins.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void reload ()
this.settings.resetWarningCount();
this.settings.parseConfig();

this.baseCoin = BaseCoin.initialize(this);
this.baseCoin = new BaseCoin(this);
this.createCoin = new CreateCoin(this);
this.coinUtil = new CoinUtil(this);

Expand Down
27 changes: 13 additions & 14 deletions src/main/java/me/justeli/coins/handler/DropHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.justeli.coins.util.SubTitle;
import me.justeli.coins.util.Util;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.block.Block;
Expand All @@ -22,27 +23,28 @@
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.projectiles.ProjectileSource;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.SplittableRandom;
import java.util.UUID;

public final class DropHandler
implements Listener
{
private final Coins coins;
private final NamespacedKey playerDamage;

public DropHandler (Coins coins)
{
this.coins = coins;
this.playerDamage = new NamespacedKey(coins, "coins-player-damage");
}

private final HashMap<Location, Integer> locationAmountCache = new HashMap<>();
private final HashMap<Location, Long> locationLastTimeCache = new HashMap<>();
private final HashMap<UUID, Double> damageCache = new HashMap<>();

private static final SplittableRandom RANDOM = new SplittableRandom();

Expand Down Expand Up @@ -108,7 +110,7 @@ public void entityDeath (LivingEntity entity, Player killer)
AttributeInstance maxHealth = entity.getAttribute(Attribute.GENERIC_MAX_HEALTH);
double hitSetting = Config.PERCENTAGE_PLAYER_HIT;

if (hitSetting > 0 && maxHealth != null && getPlayerDamage(entity.getUniqueId()) / maxHealth.getValue() < hitSetting)
if (hitSetting > 0 && maxHealth != null && getPlayerDamage(entity) / maxHealth.getValue() < hitSetting)
return;
}

Expand Down Expand Up @@ -288,9 +290,9 @@ private void dropCoin (int amount, @NotNull Player player, Location location, bo
}
}

private double getPlayerDamage (UUID uuid)
private double getPlayerDamage (Entity entity)
{
return this.damageCache.computeIfAbsent(uuid, empty -> 0D);
return entity.getPersistentDataContainer().getOrDefault(this.playerDamage, PersistentDataType.DOUBLE, 0D);
}

@EventHandler (priority = EventPriority.LOW)
Expand All @@ -299,14 +301,11 @@ public void registerHits (EntityDamageByEntityEvent event)
if (!(event.getDamager() instanceof Player) && resolvePlayerShooterOrNull(event) == null)
return;

UUID uuid = event.getEntity().getUniqueId();
double playerDamage = this.damageCache.computeIfAbsent(uuid, empty -> 0D);
this.damageCache.put(uuid, playerDamage + event.getFinalDamage());
}

@EventHandler (priority = EventPriority.MONITOR)
public void unregisterHits (EntityDeathEvent event)
{
this.damageCache.remove(event.getEntity().getUniqueId());
double playerDamage = getPlayerDamage(event.getEntity());
event.getEntity().getPersistentDataContainer().set(
this.playerDamage,
PersistentDataType.DOUBLE,
playerDamage + event.getFinalDamage()
);
}
}
37 changes: 10 additions & 27 deletions src/main/java/me/justeli/coins/handler/UnfairMobHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,27 @@
import me.justeli.coins.Coins;
import me.justeli.coins.config.Config;
import me.justeli.coins.util.Util;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.EntityDeathEvent;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.bukkit.persistence.PersistentDataType;

public final class UnfairMobHandler
implements Listener
{
private final Coins coins;
private final NamespacedKey slimeSplit;
private final NamespacedKey spawnerMob;

public UnfairMobHandler (Coins coins)
{
this.coins = coins;
this.slimeSplit = new NamespacedKey(coins, "coins-slime-split");
this.spawnerMob = new NamespacedKey(coins, "coins-spawner-mob");
}

private final Set<UUID> spawnerMobCache = new HashSet<>();
private final Set<UUID> slimeSplitMobCache = new HashSet<>();

@EventHandler
public void preventSpawnerCoin (CreatureSpawnEvent event)
{
Expand All @@ -41,7 +36,7 @@ public void preventSpawnerCoin (CreatureSpawnEvent event)
if (event.getSpawnReason() != SpawnReason.SPAWNER && event.getEntityType() != EntityType.CAVE_SPIDER)
return;

spawnerMobCache.add(event.getEntity().getUniqueId());
event.getEntity().getPersistentDataContainer().set(this.spawnerMob, PersistentDataType.INTEGER, 1);
}

@EventHandler
Expand All @@ -50,28 +45,16 @@ public void splitPrevent (CreatureSpawnEvent event)
if (event.getSpawnReason() != SpawnReason.SLIME_SPLIT || !Config.PREVENT_SPLITS)
return;

slimeSplitMobCache.add(event.getEntity().getUniqueId());
}

@EventHandler (priority = EventPriority.MONITOR)
public void removeDeathEntity (EntityDeathEvent event)
{
removeFromList(event.getEntity());
event.getEntity().getPersistentDataContainer().set(this.slimeSplit, PersistentDataType.INTEGER, 1);
}

public boolean fromSplit (Entity entity)
{
return slimeSplitMobCache.contains(entity.getUniqueId());
return entity.getPersistentDataContainer().has(this.slimeSplit, PersistentDataType.INTEGER);
}

public boolean fromSpawner (Entity entity)
{
return spawnerMobCache.contains(entity.getUniqueId());
}

public void removeFromList (Entity entity)
{
spawnerMobCache.remove(entity.getUniqueId());
slimeSplitMobCache.remove(entity.getUniqueId());
return entity.getPersistentDataContainer().has(this.spawnerMob, PersistentDataType.INTEGER);
}
}
7 changes: 1 addition & 6 deletions src/main/java/me/justeli/coins/item/BaseCoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class BaseCoin
private final MetaBuilder droppedCoin;
private final MetaBuilder otherCoin;

private BaseCoin (Coins coins)
public BaseCoin (Coins coins)
{
String texture = Config.SKULL_TEXTURE;

Expand Down Expand Up @@ -46,11 +46,6 @@ private BaseCoin (Coins coins)
this.otherCoin = coins.meta(baseCoin.clone()).name(Config.DROPPED_COIN_NAME).data(CoinUtil.COINS_TYPE, CoinUtil.TYPE_OTHER);
}

public static BaseCoin initialize (Coins coins)
{
return new BaseCoin(coins);
}

public MetaBuilder dropped ()
{
return this.droppedCoin.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/justeli/coins/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static double getMultiplier (Player player)
if (permission.startsWith("coins.multiplier."))
{
String number = permission.replace("coins.multiplier.", "");
permissions.add(Util.parseDouble(number).orElse(1D));
permissions.add(parseDouble(number).orElse(1D));
}
}
PLAYER_MULTIPLIER.put(player.getUniqueId(), permissions.size() == 0? 1D : Collections.max(permissions));
Expand Down

0 comments on commit 5b3bf07

Please sign in to comment.