Skip to content

Commit

Permalink
Merge pull request #17 from Dzious/dev
Browse files Browse the repository at this point in the history
[FIX] Mob loots + reload command
  • Loading branch information
Dzious authored Dec 30, 2021
2 parents c6320ab + 1db7a1a commit 79f04c6
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

<groupId>com.dzious.bukkit.enchantcontrol</groupId>
<artifactId>EnchantControl_Reforged</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.ApiVersion>1.16.4</project.ApiVersion>
<project.ApiVersion>1.17.1</project.ApiVersion>
</properties>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
plugin.getLogManager().logSevere("Config file does not exists. Reload failed.");
e.printStackTrace();
}
sender.sendMessage("[Enchant Control] Enchantments max level reloaded.");
sender.sendMessage("[Enchant Control] For others config modifications please restart the serve.");
return (true);
} else if (args.length > 0 && args[0].equalsIgnoreCase("event")) {
if (!(sender instanceof Player)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,17 @@ ItemStack[] removeEnchants(ItemStack[] items)
((EnchantmentStorageMeta)meta).addStoredEnchant(enchantment.getKey(), enchantment.getValue(), true);
}
}

plugin.getLogManager().logDebugConsole("Meta (End) : " + meta);


items[i].setItemMeta(meta);
if (!items[i].getItemMeta().getEnchants().isEmpty()) {
plugin.getLogManager().logDebugConsole("Enchantments (Meta) : " + items[i].getItemMeta().getEnchants().toString());
} else {
} else if (meta instanceof EnchantmentStorageMeta ) {
plugin.getLogManager().logDebugConsole("Stored Enchantments (Meta) : " + ((EnchantmentStorageMeta)(items[i].getItemMeta())).getStoredEnchants().toString());
} else {
plugin.getLogManager().logDebugConsole("Item has no more enchantments!");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void onEnable() {
plugin.getServer().getPluginManager().registerEvents(new EnchantmentTableListener(plugin), plugin);
plugin.getServer().getPluginManager().registerEvents(new FishingListener(plugin), plugin);
plugin.getServer().getPluginManager().registerEvents(new LootGenerationListener(plugin), plugin);
plugin.getServer().getPluginManager().registerEvents(new MobsLootListener(plugin), plugin);
plugin.getServer().getPluginManager().registerEvents(new PlayerEnchantmentListener(plugin), plugin);
plugin.getServer().getPluginManager().registerEvents(new VillagerTradeListener(plugin), plugin);

Expand Down
132 changes: 132 additions & 0 deletions src/com/dzious/bukkit/enchantcontrol/listener/MobsLootListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.dzious.bukkit.enchantcontrol.listener;

import java.util.HashMap;
import java.util.Map;

import com.dzious.bukkit.enchantcontrol.EnchantControl;

import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import net.md_5.bungee.api.ChatColor;

public class MobsLootListener implements Listener {
final EnchantControl plugin;

public MobsLootListener(EnchantControl plugin)
{
this.plugin = plugin;
}

@EventHandler(priority = EventPriority.LOWEST)
void onEntityDeath(EntityDeathEvent e) {
ItemStack[] items = new ItemStack[e.getDrops().size()];


for (int i = 0; i < e.getDrops().size(); i++) {
items[i] = e.getDrops().get(i);
}

// e.getDrops().toArray(new ItemStack[0]);

items = removeEnchants(items);

e.getDrops().clear();
for (ItemStack item : items) {
e.getDrops().add(item);
}

return;
}



ItemStack[] removeEnchants(ItemStack[] items)
{
for (int i = 0; i < items.length; i++) {
if (items[i] == null ||
items[i].getType() == Material.SHULKER_BOX ||
items[i].getItemMeta() == null ||
(items[i].getEnchantments().isEmpty() &&
(!(items[i].getItemMeta() instanceof EnchantmentStorageMeta) ||
((EnchantmentStorageMeta)items[i].getItemMeta()).getStoredEnchants().isEmpty()))) {
continue;
}

plugin.getLogManager().logDebugConsole("Item[" + i + "] type : " + items[i].getType());

Map<Enchantment, Integer> enchantments;
if (!items[i].getItemMeta().getEnchants().isEmpty()) {
enchantments = new HashMap<>(items[i].getItemMeta().getEnchants()); ;
} else {
enchantments = new HashMap<>(((EnchantmentStorageMeta)items[i].getItemMeta()).getStoredEnchants());
}

plugin.getLogManager().logDebugConsole("Enchantments (Start) : " + enchantments.toString());

ItemMeta meta = items[i].getItemMeta();
for (Map.Entry<Enchantment, Integer> enchantment : enchantments.entrySet()) {
if (!items[i].getItemMeta().getEnchants().isEmpty() && meta.getEnchants().containsKey(enchantment.getKey())) {
meta.removeEnchant(enchantment.getKey());
} else if (((EnchantmentStorageMeta)meta).getStoredEnchants().containsKey(enchantment.getKey())) {
((EnchantmentStorageMeta)meta).removeStoredEnchant(enchantment.getKey());
}
}

Map<Enchantment, Integer> finalEnchantments = new HashMap<>();
for (Map.Entry<Enchantment, Integer> enchantment : enchantments.entrySet()) {
if (plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey()) <= 0) {
plugin.getLogManager().logDebugConsole("Removed : " + ChatColor.GREEN + enchantment.getKey().getKey());
continue;
} else if (enchantment.getValue() > plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey())) {
PersistentDataContainer container = items[i].getItemMeta().getPersistentDataContainer();
if (container.has(plugin.getNamespacedKey(), PersistentDataType.STRING) && container.get(plugin.getNamespacedKey(), PersistentDataType.STRING).equalsIgnoreCase("event")) {
plugin.getLogManager().logDebugConsole("Skipped : " + ChatColor.GREEN + enchantment.getKey().getKey() + ChatColor.WHITE + ".");
continue;
}
plugin.getLogManager().logDebugConsole("Replaced : " + ChatColor.GREEN + enchantment.getKey().getKey() + ChatColor.WHITE + ". Level was : " + ChatColor.GREEN + enchantment.getValue() + ChatColor.WHITE + " and is now : " + ChatColor.GREEN);
finalEnchantments.put(enchantment.getKey(), plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey()));
} else {
finalEnchantments.put(enchantment.getKey(),enchantment.getValue());
}
}

plugin.getLogManager().logDebugConsole("Enchantments (End) : " + finalEnchantments.toString());

if (finalEnchantments.isEmpty() && items[i].getType() == Material.ENCHANTED_BOOK) {
items[i] = new ItemStack(Material.BOOK);
} else {
for (Map.Entry<Enchantment, Integer> enchantment : finalEnchantments.entrySet()) {
plugin.getLogManager().logDebugConsole("Enchantment (Loop) : " + finalEnchantments.toString());
if (!items[i].getItemMeta().getEnchants().isEmpty()) {
meta.addEnchant(enchantment.getKey(), enchantment.getValue(), true);
} else {
((EnchantmentStorageMeta)meta).addStoredEnchant(enchantment.getKey(), enchantment.getValue(), true);
}
}

plugin.getLogManager().logDebugConsole("Meta (End) : " + meta);


items[i].setItemMeta(meta);
if (!items[i].getItemMeta().getEnchants().isEmpty()) {
plugin.getLogManager().logDebugConsole("Enchantments (Meta) : " + items[i].getItemMeta().getEnchants().toString());
} else if (meta instanceof EnchantmentStorageMeta ) {
plugin.getLogManager().logDebugConsole("Stored Enchantments (Meta) : " + ((EnchantmentStorageMeta)(items[i].getItemMeta())).getStoredEnchants().toString());
} else {
plugin.getLogManager().logDebugConsole("Item has no more enchantments!");
}
}
}
return (items);
}
}

0 comments on commit 79f04c6

Please sign in to comment.