From a45d31afea74e01ab4befb163b8ce5cbd3bd49b7 Mon Sep 17 00:00:00 2001 From: Leaf Date: Sat, 2 Dec 2023 16:39:40 +1300 Subject: [PATCH] Improve flying scrolls performance --- .../enigmaticlegacy/items/FabulousScroll.java | 49 +++++++------------ .../enigmaticlegacy/items/HeavenScroll.java | 39 ++++++++++----- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/aizistral/enigmaticlegacy/items/FabulousScroll.java b/src/main/java/com/aizistral/enigmaticlegacy/items/FabulousScroll.java index c54c3d60..5c5b1597 100644 --- a/src/main/java/com/aizistral/enigmaticlegacy/items/FabulousScroll.java +++ b/src/main/java/com/aizistral/enigmaticlegacy/items/FabulousScroll.java @@ -53,44 +53,29 @@ public void curioTick(SlotContext context, ItemStack stack) { return; if (context.entity() instanceof Player player) { + //don't check in range every tick as it is expensive. Particularly for multiple people. + //instead, check once per second, based on the player's tick count + if (player.tickCount % 20 != 0) + return; + boolean inRange = SuperpositionHandler.isInBeaconRange(player); - if (!SuperpositionHandler.isInBeaconRange(player)) - if (Math.random() <= (this.baseXpConsumptionProbability*8) * xpCostModifier.getValue() && player.getAbilities().flying) { - ExperienceHelper.drainPlayerXP(player, 1); - } + final int fabConsumptionProbMod = 8; + //only check xp drain if flying. + //because we're only checking once per 20 ticks, increase the probability by 20 + if (shouldCheckXpDrain(player) && !inRange && Math.random() <= ((this.baseXpConsumptionProbability * fabConsumptionProbMod) * 20)) { + //cost modifier hooked up to drain xp cost + ExperienceHelper.drainPlayerXP(player, (int) (xpCostModifier.getValue())); + } - this.handleFabulousFlight(player, inRange); + this.handleFlight(player, inRange); } } - protected void handleFabulousFlight(Player player, boolean inRange) { - try { - if (ExperienceHelper.getPlayerXP(player) > 0 || inRange) { - - if (!player.getAbilities().mayfly) { - player.getAbilities().mayfly = true; - } - - player.onUpdateAbilities(); - this.flyMap.put(player, 100); - - } else if (this.flyMap.get(player) > 1) { - this.flyMap.put(player, this.flyMap.get(player)-1); - } else if (this.flyMap.get(player) == 1) { - if (!player.isCreative()) { - player.getAbilities().mayfly = false; - player.getAbilities().flying = false; - player.onUpdateAbilities(); - player.addEffect(new MobEffectInstance(MobEffects.SLOW_FALLING, 200, 0, true, false)); - } - - this.flyMap.put(player, 0); - } - - } catch (NullPointerException ex) { - this.flyMap.put(player, 0); - } + @Override + protected boolean canFly(Player player, boolean inRangeCheckedAndSucceeded) + { + return inRangeCheckedAndSucceeded || ExperienceHelper.getPlayerXP(player) > 0 || (SuperpositionHandler.isInBeaconRange(player)); } @Override diff --git a/src/main/java/com/aizistral/enigmaticlegacy/items/HeavenScroll.java b/src/main/java/com/aizistral/enigmaticlegacy/items/HeavenScroll.java index da0d6dfa..faac577f 100644 --- a/src/main/java/com/aizistral/enigmaticlegacy/items/HeavenScroll.java +++ b/src/main/java/com/aizistral/enigmaticlegacy/items/HeavenScroll.java @@ -70,29 +70,46 @@ public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List 0 && (inRangeCheckedAndSucceeded || SuperpositionHandler.isInBeaconRange(player)); + } + @Override public void curioTick(SlotContext context, ItemStack stack) { if (context.entity().level.isClientSide) return; if (context.entity() instanceof Player player) { - if (Math.random() <= (this.baseXpConsumptionProbability * xpCostModifier.getValue()) && player.getAbilities().flying) { - ExperienceHelper.drainPlayerXP(player, 1); + //since we don't need to check if in range of beacon here, we can run this xp check every frame. + if (shouldCheckXpDrain(player) && Math.random() <= this.baseXpConsumptionProbability) { + //hook into xp cost modifier from config. + ExperienceHelper.drainPlayerXP(player, (int) xpCostModifier.getValue()); } - this.handleFlight(player); + // we pass false here, because we only want heaven scroll to check beacon + // range once per 20 ticks which is handled in the below function. + this.handleFlight(player, false); } - } - protected void handleFlight(Player player) { - try { - if (ExperienceHelper.getPlayerXP(player) > 0 && SuperpositionHandler.isInBeaconRange(player)) { - - if (!player.getAbilities().mayfly) { - player.getAbilities().mayfly = true; - } + protected void handleFlight(Player player, boolean inRangeCheckedAndSucceeded) { + //don't check in range every tick as it is expensive. Particularly for multiple people. + //instead, check once per second, based on the player's tick count + //If we're here from fabulous scroll, we automatically know we can skip this + if (player.tickCount % 20 != 0) + return; + try { + if (canFly(player, inRangeCheckedAndSucceeded)) { + player.getAbilities().mayfly = true; + //since we're only updating once per 20 ticks, we can leave this here as we won't be spamming update packets player.onUpdateAbilities(); this.flyMap.put(player, 100);