Small Optimizations

This commit is contained in:
Warrior 2023-05-19 18:19:57 +02:00
parent 133a60c4bf
commit 1c307d4f4c
9 changed files with 98 additions and 97 deletions

14
pom.xml
View File

@ -154,6 +154,7 @@
<include>net.kyori:adventure-text-serializer-craftbukkit</include>
<include>net.kyori:adventure-text-serializer-gson-legacy-impl</include>
<include>co.aikar:acf-bukkit</include>
<include>io.papermc:paperlib</include>
</includes>
</artifactSet>
<relocations>
@ -189,6 +190,10 @@
<pattern>org.bstats</pattern>
<shadedPattern>com.gmail.nossr50.mcmmo.metrics.bstats</shadedPattern>
</relocation>
<relocation>
<pattern>io.papermc.lib</pattern>
<shadedPattern>com.gmail.nossr50.mcmmo.paperlib</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
@ -241,6 +246,10 @@
<id>sonatype-oss-snapshots1</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<!-- ... -->
<!-- ... -->
</repositories>
@ -362,5 +371,10 @@
<version>31.1-jre</version> <!-- At this time Spigot is including 29.0 Guava classes that we are using -->
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.papermc</groupId>
<artifactId>paperlib</artifactId>
<version>1.0.8</version>
</dependency>
</dependencies>
</project>

View File

@ -13,6 +13,7 @@ import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
import com.gmail.nossr50.events.fake.FakeEvent;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.runnables.skills.AlchemyBrewTask;
import com.gmail.nossr50.skills.alchemy.Alchemy;
import com.gmail.nossr50.skills.excavation.ExcavationManager;
import com.gmail.nossr50.skills.herbalism.HerbalismManager;
@ -27,6 +28,7 @@ import com.gmail.nossr50.util.sounds.SoundManager;
import com.gmail.nossr50.util.sounds.SoundType;
import com.gmail.nossr50.worldguard.WorldGuardManager;
import com.gmail.nossr50.worldguard.WorldGuardUtils;
import io.papermc.lib.PaperLib;
import org.bukkit.*;
import org.bukkit.block.*;
import org.bukkit.entity.Item;
@ -36,8 +38,10 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.MetadataValue;
import java.util.HashSet;
import java.util.List;
public class BlockListener implements Listener {
private final mcMMO plugin;
@ -104,8 +108,9 @@ public class BlockListener implements Listener {
}
}
if (event.getBlock().getMetadata(MetadataConstants.METADATA_KEY_BONUS_DROPS).size() > 0) {
BonusDropMeta bonusDropMeta = (BonusDropMeta) event.getBlock().getMetadata(MetadataConstants.METADATA_KEY_BONUS_DROPS).get(0);
List<MetadataValue> metadata = event.getBlock().getMetadata(MetadataConstants.METADATA_KEY_BONUS_DROPS);
if (metadata.size() > 0) {
BonusDropMeta bonusDropMeta = (BonusDropMeta) metadata.get(0);
int bonusCount = bonusDropMeta.asInt();
for (int i = 0; i < bonusCount; i++) {
@ -115,7 +120,6 @@ public class BlockListener implements Listener {
}
}
if(event.getBlock().hasMetadata(MetadataConstants.METADATA_KEY_BONUS_DROPS))
event.getBlock().removeMetadata(MetadataConstants.METADATA_KEY_BONUS_DROPS, plugin);
}
@ -339,7 +343,7 @@ public class BlockListener implements Listener {
}
}
BlockState blockState = block.getState();
BlockState blockState = PaperLib.getBlockState(block, false).getState();
Location location = blockState.getLocation();
// if (!BlockUtils.shouldBeWatched(blockState)) {
@ -347,8 +351,10 @@ public class BlockListener implements Listener {
// }
/* ALCHEMY - Cancel any brew in progress for that BrewingStand */
if (blockState instanceof BrewingStand && Alchemy.brewingStandMap.containsKey(location)) {
Alchemy.brewingStandMap.get(location).cancelBrew();
if (blockState instanceof BrewingStand) {
AlchemyBrewTask task = Alchemy.brewingStandMap.get(location);
if (task != null)
task.cancelBrew();
}
Player player = event.getPlayer();
@ -605,17 +611,11 @@ public class BlockListener implements Listener {
Player player = event.getPlayer();
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
//Profile not loaded
if(UserManager.getPlayer(player) == null)
{
if (mcMMOPlayer == null)
return;
}
ItemStack heldItem = player.getInventory().getItemInMainHand();
Block block = event.getBlock();
@ -659,37 +659,30 @@ public class BlockListener implements Listener {
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
//Profile not loaded
if(UserManager.getPlayer(player) == null)
{
if (mcMMOPlayer == null)
return;
}
BlockState blockState = event.getBlock().getState();
BlockState blockState = PaperLib.getBlockState(event.getBlock(), false).getState();
ItemStack heldItem = player.getInventory().getItemInMainHand();
cleanupAbilityTools(player, mcMMOPlayer, blockState, heldItem);
debugStickDump(player, blockState);
debugStickDump(player, mcMMOPlayer, blockState);
}
//TODO: Rewrite this
//TODO: Convert into locale strings
private void debugStickDump(Player player, BlockState blockState) {
//Profile not loaded
if(UserManager.getPlayer(player) == null)
{
return;
}
private void debugStickDump(Player player, McMMOPlayer mcmmoPlayer, BlockState blockState) {
if(UserManager.getPlayer(player).isDebugMode())
if(mcmmoPlayer.isDebugMode())
{
if(mcMMO.getPlaceStore().isTrue(blockState))
player.sendMessage("[mcMMO DEBUG] This block is not natural and does not reward treasures/XP");
else
{
player.sendMessage("[mcMMO DEBUG] This block is considered natural by mcMMO");
UserManager.getPlayer(player).getExcavationManager().printExcavationDebug(player, blockState);
mcmmoPlayer.getExcavationManager().printExcavationDebug(player, blockState);
}
if(WorldGuardUtils.isWorldGuardLoaded())

View File

@ -7,6 +7,7 @@ import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.events.fake.FakeBrewEvent;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.runnables.player.PlayerUpdateInventoryTask;
import com.gmail.nossr50.runnables.skills.AlchemyBrewTask;
import com.gmail.nossr50.skills.alchemy.Alchemy;
import com.gmail.nossr50.skills.alchemy.AlchemyPotionBrewer;
import com.gmail.nossr50.util.ItemUtils;
@ -16,6 +17,7 @@ import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.skills.SkillUtils;
import com.gmail.nossr50.worldguard.WorldGuardManager;
import com.gmail.nossr50.worldguard.WorldGuardUtils;
import io.papermc.lib.PaperLib;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
@ -54,11 +56,8 @@ public class InventoryListener implements Listener {
Furnace furnace = (Furnace) furnaceState;
OfflinePlayer offlinePlayer = mcMMO.getSmeltingTracker().getFurnaceOwner(furnace);
Player player;
if(offlinePlayer != null && offlinePlayer.isOnline() && offlinePlayer instanceof Player) {
player = (Player) offlinePlayer;
if(offlinePlayer != null && offlinePlayer.isOnline() && offlinePlayer instanceof Player player) {
if (!Permissions.isSubSkillEnabled(player, SubSkillType.SMELTING_FUEL_EFFICIENCY)) {
return;
}
@ -91,7 +90,7 @@ public class InventoryListener implements Listener {
if(WorldBlacklist.isWorldBlacklisted(event.getBlock().getWorld()))
return;
BlockState blockState = event.getBlock().getState(); //Furnaces can only be cast from a BlockState not a Block
BlockState blockState = PaperLib.getBlockState(event.getBlock(), false).getState(); //Furnaces can only be cast from a BlockState not a Block
ItemStack smelting = event.getSource();
if (!ItemUtils.isSmeltable(smelting)) {
@ -120,12 +119,12 @@ public class InventoryListener implements Listener {
if(WorldBlacklist.isWorldBlacklisted(event.getPlayer().getWorld()))
return;
BlockState furnaceBlock = event.getBlock().getState();
if (!ItemUtils.isSmelted(new ItemStack(event.getItemType(), event.getItemAmount()))) {
return;
}
BlockState furnaceBlock = PaperLib.getBlockState(event.getBlock(), false).getState();
Player player = event.getPlayer();
if(furnaceBlock instanceof Furnace) {
@ -136,18 +135,14 @@ public class InventoryListener implements Listener {
return;
}
if (!UserManager.hasPlayerDataKey(player) || !Permissions.vanillaXpBoost(player, PrimarySkillType.SMELTING)) {
return;
}
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
//Profile not loaded
if(UserManager.getPlayer(player) == null)
{
if (mcMMOPlayer == null || !Permissions.vanillaXpBoost(player, PrimarySkillType.SMELTING)) {
return;
}
int xpToDrop = event.getExpToDrop();
int exp = UserManager.getPlayer(player).getSmeltingManager().vanillaXPBoost(xpToDrop);
int exp = mcMMOPlayer.getSmeltingManager().vanillaXPBoost(xpToDrop);
event.setExpToDrop(exp);
}
}
@ -164,7 +159,7 @@ public class InventoryListener implements Listener {
Inventory inventory = event.getInventory();
Player player = ((Player) event.getWhoClicked()).getPlayer();
Player player = ((Player) event.getWhoClicked());
if(event.getInventory() instanceof FurnaceInventory)
{
@ -181,7 +176,7 @@ public class InventoryListener implements Listener {
return;
}
InventoryHolder holder = inventory.getHolder();
InventoryHolder holder = PaperLib.getHolder(inventory, false).getHolder();
if (!(holder instanceof BrewingStand stand)) {
return;
@ -287,13 +282,17 @@ public class InventoryListener implements Listener {
if(WorldBlacklist.isWorldBlacklisted(event.getWhoClicked().getWorld()))
return;
Inventory inventory = event.getInventory();
if (!(inventory instanceof BrewerInventory)) {
if (!event.getInventorySlots().contains(Alchemy.INGREDIENT_SLOT)) {
return;
}
InventoryHolder holder = inventory.getHolder();
Inventory inventory = event.getInventory();
if (!(inventory instanceof BrewerInventory brewerInventory)) {
return;
}
InventoryHolder holder = PaperLib.getHolder(inventory, false).getHolder();
if (!(holder instanceof BrewingStand)) {
return;
@ -305,12 +304,8 @@ public class InventoryListener implements Listener {
return;
}
if (!event.getInventorySlots().contains(Alchemy.INGREDIENT_SLOT)) {
return;
}
ItemStack cursor = event.getCursor();
ItemStack ingredient = ((BrewerInventory) inventory).getIngredient();
ItemStack ingredient = brewerInventory.getIngredient();
if (AlchemyPotionBrewer.isEmpty(ingredient) || ingredient.isSimilar(cursor)) {
Player player = (Player) whoClicked;
@ -344,8 +339,9 @@ public class InventoryListener implements Listener {
if (event instanceof FakeBrewEvent)
return;
Location location = event.getBlock().getLocation();
if (Alchemy.brewingStandMap.containsKey(location)) {
Alchemy.brewingStandMap.get(location).finishImmediately();
AlchemyBrewTask task = Alchemy.brewingStandMap.get(location);
if (task != null) {
task.finishImmediately();
event.setCancelled(true);
}
}
@ -364,7 +360,7 @@ public class InventoryListener implements Listener {
return;
}
InventoryHolder holder = inventory.getHolder();
InventoryHolder holder = PaperLib.getHolder(inventory, false).getHolder();
if (!(holder instanceof BrewingStand)) {
return;

View File

@ -103,8 +103,7 @@ public final class LocaleLoader {
public static String formatString(String string, Object... messageArguments) {
if (messageArguments != null) {
MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(string.replace("'", "''"));
MessageFormat formatter = new MessageFormat(string.replace("'", "''"));
string = formatter.format(messageArguments);
}
@ -115,8 +114,7 @@ public final class LocaleLoader {
public static @NotNull TextComponent formatComponent(@NotNull String string, Object... messageArguments) {
if (messageArguments != null) {
MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(string.replace("'", "''"));
MessageFormat formatter = new MessageFormat(string.replace("'", "''"));
string = formatter.format(messageArguments);
}

View File

@ -73,19 +73,15 @@ public class ItemMetadataService {
ItemMeta itemMeta = itemStack.getItemMeta();
if(itemMeta != null) {
//TODO: can be optimized
if (itemMeta.hasEnchant(Enchantment.DIG_SPEED)) {
itemMeta.removeEnchant(Enchantment.DIG_SPEED);
}
if (originalSpeed > 0) {
itemMeta.addEnchant(Enchantment.DIG_SPEED, originalSpeed, true);
} else {
itemMeta.removeEnchant(Enchantment.DIG_SPEED);
}
PersistentDataContainer dataContainer = itemMeta.getPersistentDataContainer();
dataContainer.remove(NSK_SUPER_ABILITY_BOOSTED_ITEM); //Remove persistent data
//TODO: needed?
itemStack.setItemMeta(itemMeta);
}
}

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.runnables.skills;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.events.skills.alchemy.McMMOPlayerBrewEvent;
@ -12,7 +13,6 @@ import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.BrewingStand;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
@ -21,7 +21,7 @@ public class AlchemyBrewTask extends BukkitRunnable {
private static final double DEFAULT_BREW_SPEED = 1.0;
private static final int DEFAULT_BREW_TICKS = 400;
private final BlockState brewingStand;
private final BrewingStand brewingStand;
private final Location location;
private double brewSpeed;
private double brewTimer;
@ -29,7 +29,7 @@ public class AlchemyBrewTask extends BukkitRunnable {
private int fuel;
private boolean firstRun = true;
public AlchemyBrewTask(BlockState brewingStand, Player player) {
public AlchemyBrewTask(BrewingStand brewingStand, Player player) {
this.brewingStand = brewingStand;
this.location = brewingStand.getLocation();
this.player = player;
@ -37,12 +37,14 @@ public class AlchemyBrewTask extends BukkitRunnable {
brewSpeed = DEFAULT_BREW_SPEED;
brewTimer = DEFAULT_BREW_TICKS;
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
if (player != null
&& !Misc.isNPCEntityExcludingVillagers(player)
&& Permissions.isSubSkillEnabled(player, SubSkillType.ALCHEMY_CATALYSIS)
&& UserManager.getPlayer(player) != null) {
&& mcMMOPlayer != null) {
double catalysis = UserManager.getPlayer(player).getAlchemyManager().calculateBrewSpeed(Permissions.lucky(player, PrimarySkillType.ALCHEMY));
double catalysis = mcMMOPlayer.getAlchemyManager().calculateBrewSpeed(Permissions.lucky(player, PrimarySkillType.ALCHEMY));
McMMOPlayerCatalysisEvent event = new McMMOPlayerCatalysisEvent(player, catalysis);
mcMMO.p.getServer().getPluginManager().callEvent(event);
@ -52,13 +54,14 @@ public class AlchemyBrewTask extends BukkitRunnable {
}
}
if (Alchemy.brewingStandMap.containsKey(location)) {
Alchemy.brewingStandMap.get(location).cancel();
AlchemyBrewTask existing = Alchemy.brewingStandMap.get(location);
if (existing != null) {
existing.cancel();
}
fuel = ((BrewingStand) brewingStand).getFuelLevel();
fuel = brewingStand.getFuelLevel();
if (((BrewingStand) brewingStand).getBrewingTime() == -1) // Only decrement on our end if it isn't a vanilla ingredient.
if (brewingStand.getBrewingTime() == -1) // Only decrement on our end if it isn't a vanilla ingredient.
fuel--;
Alchemy.brewingStandMap.put(location, this);
@ -67,10 +70,8 @@ public class AlchemyBrewTask extends BukkitRunnable {
@Override
public void run() {
if (player == null || !player.isValid() || brewingStand == null || brewingStand.getType() != Material.BREWING_STAND || !AlchemyPotionBrewer.isValidIngredient(player, ((BrewingStand) brewingStand).getInventory().getContents()[Alchemy.INGREDIENT_SLOT])) {
if (Alchemy.brewingStandMap.containsKey(location)) {
if (player == null || !player.isValid() || location.getBlock().getType() != Material.BREWING_STAND || !AlchemyPotionBrewer.isValidIngredient(player, brewingStand.getInventory().getContents()[Alchemy.INGREDIENT_SLOT])) {
Alchemy.brewingStandMap.remove(location);
}
this.cancel();
@ -79,7 +80,7 @@ public class AlchemyBrewTask extends BukkitRunnable {
if (firstRun) {
firstRun = false;
((BrewingStand) brewingStand).setFuelLevel(fuel);
brewingStand.setFuelLevel(fuel);
}
brewTimer -= brewSpeed;
@ -90,7 +91,7 @@ public class AlchemyBrewTask extends BukkitRunnable {
finish();
}
else {
((BrewingStand) brewingStand).setBrewingTime((int) brewTimer);
brewingStand.setBrewingTime((int) brewTimer);
}
}

View File

@ -50,24 +50,19 @@ public class ExperienceBarManager {
|| !ExperienceConfig.getInstance().isExperienceBarEnabled(primarySkillType))
return;
//Init Bar
if(experienceBars.get(primarySkillType) == null)
experienceBars.put(primarySkillType, new ExperienceBarWrapper(primarySkillType, mcMMOPlayer));
//Get Bar
ExperienceBarWrapper experienceBarWrapper = experienceBars.get(primarySkillType);
// Init or get the bar
ExperienceBarWrapper bar = experienceBars.computeIfAbsent(primarySkillType, k -> new ExperienceBarWrapper(primarySkillType, mcMMOPlayer));
//Update Progress
experienceBarWrapper.setProgress(mcMMOPlayer.getProgressInCurrentSkillLevel(primarySkillType));
bar.setProgress(mcMMOPlayer.getProgressInCurrentSkillLevel(primarySkillType));
//Show Bar
experienceBarWrapper.showExperienceBar();
bar.showExperienceBar();
//Setup Hide Bar Task
if(experienceBarHideTaskHashMap.get(primarySkillType) != null)
{
experienceBarHideTaskHashMap.get(primarySkillType).cancel();
}
ExperienceBarHideTask task = experienceBarHideTaskHashMap.get(primarySkillType);
if (task != null)
task.cancel();
scheduleHideTask(primarySkillType, plugin);
}

View File

@ -8,12 +8,14 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
public final class UserManager {
@ -137,9 +139,13 @@ public final class UserManager {
* @return McMMOPlayer object for this player, null if Player has not been loaded
*/
public static @Nullable McMMOPlayer getPlayer(@Nullable Player player) {
if (player == null)
return null;
List<MetadataValue> metadata = player.getMetadata(MetadataConstants.METADATA_KEY_PLAYER_DATA);
//Avoid Array Index out of bounds
if(player != null && player.hasMetadata(MetadataConstants.METADATA_KEY_PLAYER_DATA))
return (McMMOPlayer) player.getMetadata(MetadataConstants.METADATA_KEY_PLAYER_DATA).get(0).value();
if (!metadata.isEmpty())
return (McMMOPlayer) metadata.get(0).value();
else
return null;
}

View File

@ -4,6 +4,7 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.player.UserManager;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
@ -80,7 +81,7 @@ public class SmeltingTracker {
return null;
}
return (Furnace) inventory.getHolder();
return (Furnace) PaperLib.getHolder(inventory, false).getHolder();
}
public boolean isFurnaceOwned(Furnace furnace) {
@ -92,8 +93,9 @@ public class SmeltingTracker {
return;
//Don't swap ownership if its the same player
if(getFurnaceOwner(furnace) != null) {
if(getFurnaceOwner(furnace).getUniqueId().equals(player.getUniqueId()))
OfflinePlayer owner = getFurnaceOwner(furnace);
if(owner != null) {
if(owner.getUniqueId().equals(player.getUniqueId()))
return;
}