Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
+ Fix a duplication glitch that could result in users duplicating items from other plugin inventories
+ Updated Spigot API to 1.18.1
  • Loading branch information
Puyodead1 committed Feb 6, 2022
1 parent a7088ea commit 3417a7a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.puyodead1</groupId>
<artifactId>EnchantCrystals</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
<packaging>jar</packaging>

<name>EnchantCrystals</name>
Expand Down Expand Up @@ -37,7 +37,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version>
<version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- <dependency>-->
Expand Down Expand Up @@ -112,7 +112,7 @@
<configuration>
<target>
<copy file="target/${project.name}-${project.version}.jar"
toFile="C:\Users\23562\Desktop\Spigot\1.18\plugins\${project.name}-${project.version}.jar"/>
toFile="C:\Users\23562\Desktop\spigot\1.18.1\plugins\${project.name}-${project.version}.jar"/>
</target>
</configuration>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,44 @@ public class CrystalUseEvent implements Listener {
// TODO: Fix NPath complexity
// TODO: Fix long length
public void crystalUse(InventoryClickEvent e) {
e.setCancelled(true);
final Player player = (Player) e.getWhoClicked();
final Inventory inventory = e.getClickedInventory();
// item to apply to
final ItemStack currentItem = e.getCurrentItem();

// crystal
final ItemStack cursorItem = e.getCursor();
// crystal material
final Material crystalMaterial = Material.valueOf(EnchantCrystals.getPlugin().getConfig().getString("settings.item.material"));

// null checks
if (Objects.isNull(inventory) || Objects.isNull(currentItem) || Objects.isNull(cursorItem)) {
return;
}

// ensure that nothing is null, and that the items are not air, and that the cursorItem is only a nether star, also ignore stacking crystals
if (inventory == null || currentItem == null || cursorItem == null || currentItem.getType().equals(Material.AIR) || cursorItem.getType().equals(Material.AIR) || !cursorItem.getType().equals(Material.valueOf(EnchantCrystals.getPlugin().getConfig().getString("settings.item.material"))) || currentItem.getType().equals(Material.valueOf(EnchantCrystals.getPlugin().getConfig().getString("settings.item.material")))) {
e.setCancelled(false);
final ItemMeta cursorItemMeta = cursorItem.getItemMeta();

// ensure that the items are not air, and that the cursorItem is only a nether star, also ignore stacking crystals
if (Objects.isNull(cursorItemMeta) || currentItem.getType().equals(Material.AIR) || cursorItem.getType().equals(Material.AIR) || !cursorItem.getType().equals(crystalMaterial) || currentItem.getType().equals(crystalMaterial)) {
return;
}

// create nbt item from the cursor item
final NBTItem nbtCursorItem = new NBTItem(cursorItem);

// nbt item null check
if (!EnchantCrystalsUtils.isCrystal(nbtCursorItem)) {
e.setCancelled(false);
return;
}

if (player.getGameMode().equals(GameMode.CREATIVE)) {
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1.0f, 1.0f);
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystals.getPlugin().getConfig().getString("messages.creative_error"));
e.setCancelled(false);
return;
}

final ItemMeta cursorItemMeta = cursorItem.getItemMeta();

if (!cursorItemMeta.hasEnchants()) {
// crystal has no enchants on it, how did we get here?!
EnchantCrystalsUtils.sendPlayer(player, "Crystal does not contain any enchants, Please report this to the developer and include how you managed to do this! x_x");
e.setCancelled(false);
return;
}

Expand All @@ -72,10 +75,9 @@ public void crystalUse(InventoryClickEvent e) {
final Map.Entry<Enchantment, Integer> entry = enchants.entrySet().iterator().next();
final int cursorEnchantLevel = clone.getEnchantmentLevel(entry.getKey());

// check if the enchant can be applied to the item
// check if enchant can be applied to the item
if (!entry.getKey().canEnchantItem(currentItem)) {
rejectInvalidItem(player, currentItem, entry.getKey());
e.setCancelled(false);
return;
}

Expand All @@ -84,38 +86,40 @@ public void crystalUse(InventoryClickEvent e) {
if (currentItem.containsEnchantment(entry.getKey()))
continue; // allow the same enchant for stacking

// check if the enchant conflicts with any of the existing enchants
// check if enchant conflicts with any of the existing enchants
if (currentEntry.getKey().conflictsWith(entry.getKey())) {
refuseConflict(player, entry.getKey(), currentEntry);
e.setCancelled(false);
return;
}
}

// handle stacking
if (currentItem.containsEnchantment(entry.getKey())) {
int currentEnchantLevel = currentItem.getEnchantmentLevel(entry.getKey());
int addedEnchantLevel = currentEnchantLevel + cursorEnchantLevel;
final int currentEnchantLevel = currentItem.getEnchantmentLevel(entry.getKey());
final int addedEnchantLevel = currentEnchantLevel + cursorEnchantLevel;
if (addedEnchantLevel > entry.getKey().getMaxLevel()) {
// its the same enchant but it has a higher level, apply it
// it's the same enchant, and it has a higher level, but it would exceed the enchants maximum, so we can't combine the levels and should just add the max
if (cursorEnchantLevel > currentEnchantLevel) {
e.setCancelled(true);
applyReplace(player, currentItem, clone, entry.getKey(), cursorEnchantLevel);
cursorItem.setAmount(cursorItem.getAmount() - 1);
return;
}

// the enchant level exceeds the enchants max and the level is not higher then the current applied level, exit
// enchant level exceeds the enchants max and the level is not higher than the current applied level, exit
e.setCancelled(true);
rejectExceed(player, entry.getKey());
e.setCancelled(false);
return;
}

// combine the enchant levels
// combine enchant levels
e.setCancelled(true);
applyUpgrade(player, currentItem, clone, entry.getKey(), currentEnchantLevel, addedEnchantLevel);
cursorItem.setAmount(cursorItem.getAmount() - 1);
return;
}

e.setCancelled(true);
apply(player, currentItem, clone, entry.getKey(), cursorEnchantLevel);
cursorItem.setAmount(cursorItem.getAmount() - 1);
} else {
Expand All @@ -126,8 +130,9 @@ public void crystalUse(InventoryClickEvent e) {

// handle a crystal with more than one enchantment
for (final Enchantment enchantment : clone.getEnchantments().keySet()) {
// check if the enchant can be applied to the item
// check if enchant can be applied to the item
if (!enchantment.canEnchantItem(currentItem)) {
e.setCancelled(true);
rejectInvalidItem(player, currentItem, enchantment);
continue;
}
Expand All @@ -137,8 +142,9 @@ public void crystalUse(InventoryClickEvent e) {
if (currentItem.containsEnchantment(enchantment)) {
continue; // allow the same enchant for stacking
}
// check if the enchant conflicts with any of the existing enchants
// check if enchant conflicts with any of the existing enchants
if (currentEntry.getKey().conflictsWith(enchantment)) {
e.setCancelled(true);
refuseConflict(player, enchantment, currentEntry);
}
}
Expand All @@ -151,40 +157,42 @@ public void crystalUse(InventoryClickEvent e) {
if (addedEnchantLevel > enchantment.getMaxLevel()) {
// its the same enchant but it has a higher level, apply it
if (clone.getEnchantmentLevel(enchantment) > currentEnchantLevel) {
e.setCancelled(true);
applyReplace(player, currentItem, clone, enchantment, clone.getEnchantmentLevel(enchantment));
continue;
}

// the enchant level exceeds the enchants max and the level is not higher then the current applied level, exit
e.setCancelled(true);
rejectExceed(player, enchantment);
continue;
}

// combine the enchant levels
// combine enchant levels
e.setCancelled(true);
applyUpgrade(player, currentItem, clone, enchantment, currentEnchantLevel, addedEnchantLevel);
continue;
}

// remove a crystal if its a stack or just remove the crystal
// remove a crystal if it's a stack or just remove the crystal
e.setCancelled(true);
apply(player, currentItem, clone, enchantment, clone.getEnchantmentLevel(enchantment));
}

if (clone.getEnchantments().size() == enchantAmount) {
// no enchants were added to the item
e.setCancelled(false);
} else {
if (clone.getEnchantments().size() != enchantAmount) {
e.setCancelled(true);
cursorItem.setAmount(cursorItem.getAmount() - 1);
}
}
}

private void rejectInvalidItem(final Player player, final ItemStack currentItem, final Enchantment enchantment) {
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1.0f, 1.0f);
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.colorize(EnchantCrystalsUtils.replaceInvalid(EnchantCrystals.getPlugin().getConfig().getString("messages.invalid_item"), enchantment, currentItem)));
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.colorize(EnchantCrystalsUtils.replaceInvalid(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.invalid_item")), enchantment, currentItem)));
}

private void rejectExceed(final Player player, final Enchantment enchantment) {
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.colorize(EnchantCrystalsUtils.replaceExceed(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_max_exceed"), enchantment)));
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.colorize(EnchantCrystalsUtils.replaceExceed(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_max_exceed")), enchantment)));
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1.0f, 1.0f);
}

Expand All @@ -197,7 +205,7 @@ private void apply(final Player player, final ItemStack currentItem, final ItemS
currentItem.addEnchantment(enchantment, cursorEnchantLevel);
cursorItem.removeEnchantment(enchantment);

EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.replace(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_success"), enchantment, cursorEnchantLevel, currentItem));
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.replace(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_success")), enchantment, cursorEnchantLevel, currentItem));
player.getWorld().playSound(player.getLocation(), Sound.BLOCK_ANVIL_USE, 1.0f, 1.0f);
}

Expand All @@ -206,14 +214,14 @@ private void applyUpgrade(final Player player, final ItemStack currentItem, fina
cursorItem.removeEnchantment(enchantment);

player.getWorld().playSound(player.getLocation(), Sound.BLOCK_ANVIL_USE, 1.0f, 1.0f);
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.replaceUpgraded(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_upgraded"), enchantment, currentEnchantLevel, addedEnchantLevel));
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.replaceUpgraded(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_upgraded")), enchantment, currentEnchantLevel, addedEnchantLevel));
}

private void applyReplace(final Player player, final ItemStack currentItem, final ItemStack cursorItem, final Enchantment enchantment, final int cursorEnchantLevel) {
currentItem.addEnchantment(enchantment, cursorItem.getEnchantmentLevel(enchantment));
cursorItem.removeEnchantment(enchantment);

player.getWorld().playSound(player.getLocation(), Sound.BLOCK_ANVIL_USE, 1.0f, 1.0f);
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.replace(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_success"), enchantment, cursorEnchantLevel, currentItem));
EnchantCrystalsUtils.sendPlayer(player, EnchantCrystalsUtils.replace(Objects.requireNonNull(EnchantCrystals.getPlugin().getConfig().getString("messages.enchantment_success")), enchantment, cursorEnchantLevel, currentItem));
}
}

0 comments on commit 3417a7a

Please sign in to comment.