Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package de.minebench.mbutils.bukkit.modules;
import com.Acrobot.ChestShop.Events.ItemInfoEvent;
import de.minebench.mbutils.bukkit.MbUtils;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.player.PlayerItemMendEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
/*
* MendingNerf
* Copyright (C) 2017 Phoenix616 aka Max Lee (max@themoep.de)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
@ModuleInfo(commands = {"mendingnerf", "repaircost"})
public class MendingNerf extends MbModule implements CommandExecutor, Listener {
private final NamespacedKey REPAIR_COUNT_KEY;
private final NamespacedKey STORED_EXP_KEY;
private boolean repairNerf;
private int maxRepairCost;
private double increaseCostEachModifier;
private double repairCostModifier;
private Set<UUID> debugPlayers = new HashSet<>();
public MendingNerf(MbUtils plugin) {
super(plugin);
loadConfig();
REPAIR_COUNT_KEY = new NamespacedKey(plugin, "repair-count");
STORED_EXP_KEY = new NamespacedKey(plugin, "stored-exp");
}
private void loadConfig() {
repairNerf = getConfig().getBoolean("repair-nerf");
maxRepairCost = getConfig().getInt("max-repair-cost");
increaseCostEachModifier = getConfig().getInt("increase-cost-step-modifier");
repairCostModifier = getConfig().getDouble("repair-cost-modifier");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if ("mendingnerf".equalsIgnoreCase(cmd.getName())) {
return onCommandMendingNerf(sender, cmd, label, args);
} else if ("repaircost".equalsIgnoreCase(cmd.getName())) {
return onCommandRepairCost(sender, cmd, label, args);
}
return false;
}
private boolean onCommandRepairCost(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Can only be executed by a player!");
return true;
}
ItemStack itemStack = ((Player) sender).getInventory().getItemInMainHand();
if (itemStack.getType().isAir()) {
sender.sendMessage(ChatColor.RED + "Du musst einen Gegenstand in der Hand halten!");
return true;
}
ItemMeta meta = itemStack.getItemMeta();
if (!(meta instanceof Repairable)) {
sender.sendMessage(ChatColor.RED + "Du musst einen Gegenstand in der Hand halten der reparierbar ist!");
return true;
}
if (args.length > 0 && sender.hasPermission(cmd.getPermission() + ".set")) {
try {
int oldCost = ((Repairable) meta).getRepairCost();
((Repairable) meta).setRepairCost(Integer.parseInt(args[0]));
itemStack.setItemMeta(meta);
sender.sendMessage(ChatColor.YELLOW + "Reparaturkosten auf " + ChatColor.AQUA + ((Repairable) meta).getRepairCost() + ChatColor.YELLOW + " gesetzt! (Vorher: " + oldCost + ")");
} catch (NumberFormatException e) {
sender.sendMessage(args[0] + ChatColor.RED + " ist keine valide Zahl für die Reparaturkosten!");
}
} else {
sender.sendMessage(ChatColor.YELLOW + "Reparaturkosten: " + ChatColor.AQUA + ((Repairable) meta).getRepairCost());
}
return true;
}
private boolean onCommandMendingNerf(CommandSender sender, Command cmd, String label, String[] args) {
if (args.length > 0) {
if ("enable".equalsIgnoreCase(args[0])) {
plugin.reloadConfig();
getConfig().set("enabled", true);
loadConfig();
plugin.saveConfig();
sender.sendMessage(org.bukkit.ChatColor.GREEN + "MendingNerf enabled!");
return true;
} else if("disable".equalsIgnoreCase(args[0])) {
setEnabled(false);
plugin.reloadConfig();
getConfig().set("enabled", false);
plugin.saveConfig();
sender.sendMessage(org.bukkit.ChatColor.RED + "MendingNerf disabled!");
return true;
} else if("reload".equalsIgnoreCase(args[0])) {
boolean enabledBefore = isEnabled();
plugin.reloadConfig();
loadConfig();
sender.sendMessage(org.bukkit.ChatColor.YELLOW + "MendingNerf reloaded!");
if(enabledBefore != isEnabled()) {
sender.sendMessage(org.bukkit.ChatColor.YELLOW + "MendingNerf is now " + (isEnabled() ? "enabled" : "disabled"));
}
return true;
} else if ("debug".equalsIgnoreCase(args[0])) {
Player target;
if (args.length > 1) {
target = plugin.getServer().getPlayer(args[1]);
if (target == null) {
sender.sendMessage(ChatColor.RED + "No player with the name " + args[1] + " found online?");
return true;
}
} else if (sender instanceof Player) {
target = (Player) sender;
} else {
sender.sendMessage(ChatColor.RED + "Run /" + label + " debug <playername> from the console!");
return true;
}
if (debugPlayers.contains(target.getUniqueId())) {
debugPlayers.remove(target.getUniqueId());
} else {
debugPlayers.add(target.getUniqueId());
}
sender.sendMessage("Debug mode " + (debugPlayers.contains(target.getUniqueId()) ? ChatColor.GREEN + "enabled" : ChatColor.RED + "disabled") + (target == sender ? "" : " for " + target.getName()));
return true;
}
} else {
sender.sendMessage(org.bukkit.ChatColor.YELLOW + "MendingNerf is " + (isEnabled() ? "enabled" : "disabled"));
}
return false;
}
@EventHandler(ignoreCancelled = true)
public void onAnvilPrepare(PrepareAnvilEvent event) {
if (!isEnabled()
|| event.getResult() == null
|| event.getViewers().get(0).getGameMode() == GameMode.CREATIVE
|| event.getViewers().get(0).hasPermission("mbadmin.mendingnerf.anvilbypass")) {
return;
}
for (Map.Entry<Enchantment, Integer> ench : event.getResult().getEnchantments().entrySet()) {
if (ench.getValue() > ench.getKey().getMaxLevel()) {
event.setResult(null);
for (HumanEntity viewer : event.getViewers()) {
if (viewer instanceof Player) {
((Player) viewer).updateInventory();
}
}
break;
}
}
}
@EventHandler(ignoreCancelled = true)
public void onItemMend(PlayerItemMendEvent event) {
if (!isEnabled() || !repairNerf || !(event.getItem().getItemMeta() instanceof Repairable)) {
return;
}
ItemMeta meta = event.getItem().getItemMeta();
PersistentDataContainer dataContainer = meta.getPersistentDataContainer();
int itemRepairCount = dataContainer.getOrDefault(REPAIR_COUNT_KEY, PersistentDataType.INTEGER, 0);
int increaseCostEach = (int) (event.getItem().getType().getMaxDurability() * increaseCostEachModifier);
if (((Repairable) meta).getRepairCost() >= maxRepairCost && itemRepairCount > increaseCostEach) {
event.setRepairAmount(0);
return;
}
double expNeeded = 0.5 + ((Repairable) meta).getRepairCost() * repairCostModifier;
double availableExp = dataContainer.getOrDefault(STORED_EXP_KEY, PersistentDataType.DOUBLE, 0.0) + event.getExperienceOrb().getExperience();
boolean updated = false;
debug(event.getPlayer(), "repairAmount: " + event.getRepairAmount() + " exp: " + event.getExperienceOrb().getExperience() + " repairCost: " + ((Repairable) meta).getRepairCost());
debug(event.getPlayer(), "expNeeded: " + expNeeded + " itemRepairCount: " + itemRepairCount + " availableExp: " + availableExp);
if (availableExp >= expNeeded) {
int repairTimes = (int) (availableExp / expNeeded);
int maxRepair = ((Damageable) meta).getDamage();
repairTimes = repairTimes > maxRepair ? maxRepair : repairTimes;
event.setRepairAmount(repairTimes);
boolean increaseRepairCost = itemRepairCount % increaseCostEach + repairTimes >= increaseCostEach;
itemRepairCount += repairTimes;
dataContainer.set(REPAIR_COUNT_KEY, PersistentDataType.INTEGER, itemRepairCount);
availableExp = availableExp - repairTimes * expNeeded;
debug(event.getPlayer(), "repairTimes: " + repairTimes + " increaseCostEach: " + increaseCostEach + " availableExp: " + availableExp);
if (repairTimes == maxRepair) {
event.getPlayer().giveExp((int) availableExp);
availableExp = 0;
}
if (increaseRepairCost) {
((Repairable) meta).setRepairCost(((Repairable) meta).getRepairCost() + 1);
debug(event.getPlayer(), "increaseCost: " + 1 + " itemRepairCount: " + itemRepairCount);
}
updated = true;
} else {
event.setRepairAmount(0);
}
event.getExperienceOrb().setExperience(0);
if (availableExp == 0) {
if (dataContainer.has(STORED_EXP_KEY, PersistentDataType.DOUBLE)) {
dataContainer.remove(STORED_EXP_KEY);
updated = true;
}
} else {
dataContainer.set(STORED_EXP_KEY, PersistentDataType.DOUBLE, availableExp);
updated = true;
}
if (updated) {
event.getItem().setItemMeta(meta);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onChestshopItemInfo(ItemInfoEvent event) {
if (!isEnabled() || !repairNerf || !(event.getItem().getItemMeta() instanceof Repairable)) {
return;
}
boolean canBeRepaired = true;
if (event.getItem().getItemMeta().getPersistentDataContainer().has(REPAIR_COUNT_KEY, PersistentDataType.INTEGER)) {
int itemRepairCount = event.getItem().getItemMeta().getPersistentDataContainer().getOrDefault(REPAIR_COUNT_KEY, PersistentDataType.INTEGER, 0);
event.getSender().sendMessage(ChatColor.WHITE + "Repariert: " + ChatColor.GRAY + itemRepairCount + "x");
int increaseCostEach = (int) (event.getItem().getType().getMaxDurability() * increaseCostEachModifier);
if (((Repairable) event.getItem().getItemMeta()).getRepairCost() >= maxRepairCost && itemRepairCount > increaseCostEach) {
event.getSender().sendMessage(ChatColor.RED + "Kann nichtmehr mit Mending repariert werden!");
canBeRepaired = false;
}
}
if (canBeRepaired && event.getItem().getItemMeta().hasEnchant(Enchantment.MENDING)) {
event.getSender().sendMessage(ChatColor.WHITE + "Erfahrung für Reparatur: " + ChatColor.GRAY + (0.5 + ((Repairable) event.getItem().getItemMeta()).getRepairCost() * repairCostModifier));
}
}
private void debug(Player player, String string) {
if (debugPlayers.contains(player.getUniqueId())) {
log(Level.INFO, ChatColor.GRAY + "[DEBUG] " + string);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment