Optimize ChunkUnloadEvent & Partial rewrite to COTW entity tracking + some tweaks to COTW entity removal

This commit is contained in:
nossr50
2020-12-31 15:25:21 -08:00
parent aed4cb87be
commit 2664ae4bd6
10 changed files with 294 additions and 156 deletions

View File

@ -37,8 +37,6 @@ import org.bukkit.entity.*;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
@ -397,7 +395,7 @@ public class FishingManager extends SkillManager {
if (treasure != null) {
if(treasure instanceof FishingTreasureBook) {
treasureDrop = createEnchantBook((FishingTreasureBook) treasure);
treasureDrop = ItemUtils.createEnchantBook((FishingTreasureBook) treasure);
} else {
treasureDrop = treasure.getDrop().clone(); // Not cloning is bad, m'kay?
@ -461,29 +459,6 @@ public class FishingManager extends SkillManager {
applyXpGain(fishXp + treasureXp, XPGainReason.PVE);
}
private @NotNull ItemStack createEnchantBook(@NotNull FishingTreasureBook fishingTreasureBook) {
ItemStack itemStack = fishingTreasureBook.getDrop().clone();
EnchantmentWrapper enchantmentWrapper = getRandomEnchantment(fishingTreasureBook.getLegalEnchantments());
ItemMeta itemMeta = itemStack.getItemMeta();
if(itemMeta == null) {
return itemStack;
}
EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) itemMeta;
enchantmentStorageMeta.addStoredEnchant(enchantmentWrapper.getEnchantment(), enchantmentWrapper.getEnchantmentLevel(), ExperienceConfig.getInstance().allowUnsafeEnchantments());
itemStack.setItemMeta(enchantmentStorageMeta);
return itemStack;
}
private @NotNull EnchantmentWrapper getRandomEnchantment(@NotNull List<EnchantmentWrapper> enchantmentWrappers) {
Collections.shuffle(enchantmentWrappers, Misc.getRandom());
int randomIndex = Misc.getRandom().nextInt(enchantmentWrappers.size());
return enchantmentWrappers.get(randomIndex);
}
/**
* Handle the vanilla XP boost for Fishing
*

View File

@ -33,9 +33,7 @@ import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class TamingManager extends SkillManager {
//TODO: Temporary static cache, will be changed in 2.2
@ -43,8 +41,6 @@ public class TamingManager extends SkillManager {
private static HashMap<CallOfTheWildType, TamingSummon> cotwSummonDataProperties;
private long lastSummonTimeStamp;
private HashMap<CallOfTheWildType, List<TrackedTamingEntity>> playerSummonedEntities;
public TamingManager(McMMOPlayer mcMMOPlayer) {
super(mcMMOPlayer, PrimarySkillType.TAMING);
init();
@ -56,20 +52,12 @@ public class TamingManager extends SkillManager {
lastSummonTimeStamp = 0L;
//Init per-player tracking of summoned entities
initPerPlayerSummonTracking();
mcMMO.getTransientEntityTracker().initPlayer(mmoPlayer.getPlayer().getUniqueId());
//Hacky stuff used as a band-aid
initStaticCaches();
}
private void initPerPlayerSummonTracking() {
playerSummonedEntities = new HashMap<>();
for(CallOfTheWildType callOfTheWildType : CallOfTheWildType.values()) {
playerSummonedEntities.put(callOfTheWildType, new ArrayList<>());
}
}
private void initStaticCaches() {
//TODO: Temporary static cache, will be changed in 2.2
//This is shared between instances of TamingManager
@ -500,62 +488,16 @@ public class TamingManager extends SkillManager {
* @param itemStack target ItemStack
* @return true if it is used for any COTW
*/
public boolean isCOTWItem(ItemStack itemStack) {
public boolean isCOTWItem(@NotNull ItemStack itemStack) {
return summoningItems.containsKey(itemStack.getType());
}
//TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
private int getAmountCurrentlySummoned(CallOfTheWildType callOfTheWildType) {
//The tracker is unreliable so validate its contents first
recalibrateTracker();
return playerSummonedEntities.get(callOfTheWildType).size();
private int getAmountCurrentlySummoned(@NotNull CallOfTheWildType callOfTheWildType) {
return mcMMO.getTransientEntityTracker().getAmountCurrentlySummoned(getPlayer().getUniqueId(), callOfTheWildType);
}
//TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
private void addToTracker(LivingEntity livingEntity, CallOfTheWildType callOfTheWildType) {
TrackedTamingEntity trackedEntity = new TrackedTamingEntity(livingEntity, callOfTheWildType, this);
playerSummonedEntities.get(callOfTheWildType).add(trackedEntity);
}
//TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
public List<TrackedTamingEntity> getTrackedEntities(CallOfTheWildType callOfTheWildType) {
return playerSummonedEntities.get(callOfTheWildType);
}
//TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
public void removeFromTracker(TrackedTamingEntity trackedEntity) {
playerSummonedEntities.get(trackedEntity.getCallOfTheWildType()).remove(trackedEntity);
NotificationManager.sendPlayerInformationChatOnly(getPlayer(), "Taming.Summon.COTW.TimeExpired", StringUtils.getPrettyEntityTypeString(trackedEntity.getLivingEntity().getType()));
}
/**
* Builds a new tracked list by determining which tracked things are still valid
*/
//TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
private void recalibrateTracker() {
for(CallOfTheWildType callOfTheWildType : CallOfTheWildType.values()) {
ArrayList<TrackedTamingEntity> validEntities = getValidTrackedEntities(callOfTheWildType);
playerSummonedEntities.put(callOfTheWildType, validEntities); //Replace the old list with the new list
}
}
//TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
private ArrayList<TrackedTamingEntity> getValidTrackedEntities(CallOfTheWildType callOfTheWildType) {
ArrayList<TrackedTamingEntity> validTrackedEntities = new ArrayList<>();
for(TrackedTamingEntity trackedTamingEntity : getTrackedEntities(callOfTheWildType)) {
LivingEntity livingEntity = trackedTamingEntity.getLivingEntity();
//Remove from existence
if(livingEntity != null && livingEntity.isValid()) {
validTrackedEntities.add(trackedTamingEntity);
}
}
return validTrackedEntities;
private void addToTracker(@NotNull LivingEntity livingEntity, @NotNull CallOfTheWildType callOfTheWildType) {
mcMMO.getTransientEntityTracker().registerEntity(getPlayer().getUniqueId(), new TrackedTamingEntity(livingEntity, callOfTheWildType, getPlayer()));
}
/**
@ -564,20 +506,6 @@ public class TamingManager extends SkillManager {
*/
//TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
public void cleanupAllSummons() {
for(List<TrackedTamingEntity> trackedTamingEntities : playerSummonedEntities.values()) {
for(TrackedTamingEntity trackedTamingEntity : trackedTamingEntities) {
LivingEntity livingEntity = trackedTamingEntity.getLivingEntity();
//Remove from existence
if(livingEntity != null && livingEntity.isValid()) {
mcMMO.getCompatibilityManager().getPersistentDataLayer().removeMobFlags(livingEntity);
livingEntity.setHealth(0);
livingEntity.remove();
}
}
//Clear the list
trackedTamingEntities.clear();
}
mcMMO.getTransientEntityTracker().cleanupPlayer(getPlayer());
}
}

View File

@ -4,61 +4,40 @@ import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.skills.subskills.taming.CallOfTheWildType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.skills.ParticleEffectUtils;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
public class TrackedTamingEntity extends BukkitRunnable {
private final LivingEntity livingEntity;
private final CallOfTheWildType callOfTheWildType;
private final UUID id;
private int length;
private final TamingManager tamingManagerRef;
private final @NotNull LivingEntity livingEntity;
private final @NotNull CallOfTheWildType callOfTheWildType;
private final @NotNull Player player;
protected TrackedTamingEntity(LivingEntity livingEntity, CallOfTheWildType callOfTheWildType, TamingManager tamingManagerRef) {
this.tamingManagerRef = tamingManagerRef;
protected TrackedTamingEntity(@NotNull LivingEntity livingEntity, @NotNull CallOfTheWildType callOfTheWildType, @NotNull Player player) {
this.player = player;
this.callOfTheWildType = callOfTheWildType;
this.livingEntity = livingEntity;
this.id = livingEntity.getUniqueId();
int tamingCOTWLength = Config.getInstance().getTamingCOTWLength(callOfTheWildType.getConfigEntityTypeEntry());
if (tamingCOTWLength > 0) {
this.length = tamingCOTWLength * Misc.TICK_CONVERSION_FACTOR;
int length = tamingCOTWLength * Misc.TICK_CONVERSION_FACTOR;
this.runTaskLater(mcMMO.p, length);
}
}
@Override
public void run() {
if (livingEntity.isValid()) {
Location location = livingEntity.getLocation();
location.getWorld().playSound(location, Sound.BLOCK_FIRE_EXTINGUISH, 0.8F, 0.8F);
ParticleEffectUtils.playCallOfTheWildEffect(livingEntity);
if(tamingManagerRef != null)
tamingManagerRef.removeFromTracker(this);
livingEntity.setHealth(0);
livingEntity.remove();
}
mcMMO.getTransientEntityTracker().removeSummon(this.getLivingEntity(), player, true);
this.cancel();
}
public CallOfTheWildType getCallOfTheWildType() {
public @NotNull CallOfTheWildType getCallOfTheWildType() {
return callOfTheWildType;
}
public LivingEntity getLivingEntity() {
public @NotNull LivingEntity getLivingEntity() {
return livingEntity;
}
public UUID getID() {
return id;
}
}