diff --git a/eco-extensions/autosell/build.gradle b/eco-extensions/autosell/build.gradle new file mode 100644 index 00000000..fb664c6c --- /dev/null +++ b/eco-extensions/autosell/build.gradle @@ -0,0 +1,12 @@ +group 'com.willfp' +version '1.0.0' +description = 'Autosell Extension' + +shadowJar { + archiveFileName = project.getDescription() + " v" + project.version + ".jar" +} + +dependencies { + compileOnly 'com.github.brcdev-minecraft:shopgui-api:2.2.0' + compileOnly 'com.github.MilkBowl:VaultAPI:1.7' +} \ No newline at end of file diff --git a/eco-extensions/autosell/src/main/java/com/willfp/ecoenchants/autosell/Autosell.java b/eco-extensions/autosell/src/main/java/com/willfp/ecoenchants/autosell/Autosell.java new file mode 100644 index 00000000..78f8d2f4 --- /dev/null +++ b/eco-extensions/autosell/src/main/java/com/willfp/ecoenchants/autosell/Autosell.java @@ -0,0 +1,112 @@ +package com.willfp.ecoenchants.autosell; + + +import com.willfp.eco.core.drops.DropQueue; +import com.willfp.eco.core.integrations.antigrief.AntigriefManager; +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import com.willfp.ecoenchants.enchantments.EcoEnchants; +import com.willfp.ecoenchants.enchantments.meta.EnchantmentType; +import com.willfp.ecoenchants.enchantments.util.EnchantChecks; +import net.brcdev.shopgui.ShopGuiPlusApi; +import org.bukkit.GameMode; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.Container; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Item; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.block.BlockDropItemEvent; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +public class Autosell extends EcoEnchant { + private static final Set FORTUNE_MATERIALS = new HashSet<>( + Arrays.asList( + Material.GOLD_INGOT, + Material.IRON_INGOT + ) + ); + + public Autosell() { + super( + "autosell", EnchantmentType.NORMAL + ); + } + + @EventHandler + public void infernalTouchBreak(@NotNull final BlockDropItemEvent event) { + Player player = event.getPlayer(); + Block block = event.getBlock(); + + if (!this.areRequirementsMet(player)) { + return; + } + + if (!EnchantChecks.mainhand(player, this)) { + return; + } + + if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) { + return; + } + + if (event.getBlockState() instanceof Container) { + return; + } + + if (event.isCancelled()) { + return; + } + + if (!AntigriefManager.canBreakBlock(player, block)) { + return; + } + + if (this.getDisabledWorlds().contains(player.getWorld())) { + return; + } + + Collection drops = new ArrayList<>(); + + for (Item item : event.getItems()) { + drops.add(item.getItemStack()); + } + + int experience = 0; + int fortune = EnchantChecks.getMainhandLevel(player, Enchantment.LOOT_BONUS_BLOCKS); + + for (ItemStack itemStack : drops) { + if (fortune > 0 && FORTUNE_MATERIALS.contains(itemStack.getType())) { + itemStack.setAmount((int) Math.round((Math.random() * ((double) fortune - 1)) + 1.1)); + } + + double price = ShopGuiPlusApi.getItemStackPriceSell(player, itemStack); + if (price <= 0) { + continue; + } + + EconomyHandler.getInstance().depositPlayer(player, price); + + drops.remove(itemStack); + } + + event.getItems().clear(); + + if (!this.getConfig().getBool(EcoEnchants.CONFIG_LOCATION + "drop-xp")) { + experience = 0; + } + + new DropQueue(player) + .setLocation(block.getLocation()) + .addItems(drops) + .addXP(experience) + .push(); + } +} diff --git a/eco-extensions/autosell/src/main/java/com/willfp/ecoenchants/autosell/AutosellMain.java b/eco-extensions/autosell/src/main/java/com/willfp/ecoenchants/autosell/AutosellMain.java new file mode 100644 index 00000000..67e16881 --- /dev/null +++ b/eco-extensions/autosell/src/main/java/com/willfp/ecoenchants/autosell/AutosellMain.java @@ -0,0 +1,27 @@ +package com.willfp.ecoenchants.autosell; + +import com.willfp.eco.core.EcoPlugin; +import com.willfp.eco.core.extensions.Extension; +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import org.jetbrains.annotations.NotNull; + +public class AutosellMain extends Extension { + /** + * Autosell enchantment. + */ + public static final EcoEnchant AUTOSELL = new Autosell(); + + public AutosellMain(@NotNull final EcoPlugin plugin) { + super(plugin); + } + + @Override + public void onEnable() { + EconomyHandler.setEnabled(EconomyHandler.init()); + } + + @Override + public void onDisable() { + // Handled by super + } +} diff --git a/eco-extensions/autosell/src/main/java/com/willfp/ecoenchants/autosell/EconomyHandler.java b/eco-extensions/autosell/src/main/java/com/willfp/ecoenchants/autosell/EconomyHandler.java new file mode 100644 index 00000000..c3c09a13 --- /dev/null +++ b/eco-extensions/autosell/src/main/java/com/willfp/ecoenchants/autosell/EconomyHandler.java @@ -0,0 +1,35 @@ +package com.willfp.ecoenchants.autosell; + +import lombok.Getter; +import lombok.Setter; +import lombok.experimental.UtilityClass; +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Bukkit; +import org.bukkit.plugin.RegisteredServiceProvider; + +@UtilityClass +public class EconomyHandler { + /** + * The instance. + */ + @Getter + private static Economy instance = null; + + @Getter + @Setter + private static boolean enabled = false; + + /** + * Initialize the economy manager. + * + * @return If was successful. + */ + public boolean init() { + RegisteredServiceProvider rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class); + if (rsp == null) { + return false; + } + instance = rsp.getProvider(); + return true; + } +} diff --git a/eco-extensions/autosell/src/main/resources/enchants/normal/autosell.yml b/eco-extensions/autosell/src/main/resources/enchants/normal/autosell.yml new file mode 100644 index 00000000..6a5fd80b --- /dev/null +++ b/eco-extensions/autosell/src/main/resources/enchants/normal/autosell.yml @@ -0,0 +1,29 @@ +# +# Autosell EcoEnchant +# + +name: "Autosell" +description: Drops are instantly sold to the shop +enabled: true + +obtaining: + table: true + villager: true + loot: true + rarity: legendary + +general-config: + flags: [ ] + targets: + - pickaxe + - axe + grindstoneable: true + disabled-in-worlds: [ ] + requirements: + list: [ ] + not-met-lore: [ ] + conflicts: + - silk_touch + +config: + drop-xp: true \ No newline at end of file diff --git a/eco-extensions/autosell/src/main/resources/extension.yml b/eco-extensions/autosell/src/main/resources/extension.yml new file mode 100644 index 00000000..8cd01083 --- /dev/null +++ b/eco-extensions/autosell/src/main/resources/extension.yml @@ -0,0 +1,4 @@ +name: Autosell +main: com.willfp.ecoenchants.autosell.AutosellMain +version: ${projectVersion} +author: Auxilor \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 2392821c..d9cdba7a 100644 --- a/settings.gradle +++ b/settings.gradle @@ -11,6 +11,7 @@ include ':eco-core:core-plugin' // Extensions include ':eco-extensions' include ':eco-extensions:alchemy' +include ':eco-extensions:autosell' include ':eco-extensions:biomes' include ':eco-extensions:citizen' include ':eco-extensions:countereffects'