From 404dcb8f1a098f145e3f5f120f2872c59bcc9772 Mon Sep 17 00:00:00 2001 From: Zrips Date: Fri, 16 Jul 2021 16:08:05 +0300 Subject: [PATCH] Option to pay for block broken above original one. Sugarcane, kelp and bamboo --- src/main/java/com/gamingmesh/jobs/Jobs.java | 10 +++- .../jobs/config/GeneralConfigManager.java | 7 ++- .../jobs/container/FastPayment.java | 4 ++ .../jobs/listeners/JobsPaymentListener.java | 55 +++++++++++++++++++ 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index ea553e5b..562aa695 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -159,7 +160,12 @@ public final class Jobs extends JavaPlugin { public static BufferedPaymentThread paymentThread; private static DatabaseSaveThread saveTask; - public static final Map FASTPAYMENT = new HashMap<>(); + private static final int MAX_ENTRIES = 5; + public static final LinkedHashMap FASTPAYMENT = new LinkedHashMap(MAX_ENTRIES + 1, .75F, false) { + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > MAX_ENTRIES; + } + }; protected static VersionChecker versionCheckManager; protected static SelectionManager smanager; @@ -1086,8 +1092,6 @@ public final class Jobs extends JavaPlugin { } } else { - FASTPAYMENT.clear(); - List expiredJobs = new ArrayList<>(); for (JobProgression prog : progression) { if (prog.getJob().isWorldBlackListed(block) || prog.getJob().isWorldBlackListed(block, ent) diff --git a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java index 1f116a85..5ebdbc40 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -100,7 +100,7 @@ public class GeneralConfigManager { hideJobsInfoWithoutPermission, UseTaxes, TransferToServerAccount, TakeFromPlayersPayment, AutoJobJoinUse, AllowDelevel, BossBarEnabled = false, BossBarShowOnEachAction = false, BossBarsMessageByDefault = false, ExploreCompact, DBCleaningJobsUse, DBCleaningUsersUse, DisabledWorldsUse, UseAsWhiteListWorldList, PaymentMethodsMoney, PaymentMethodsPoints, PaymentMethodsExp, MythicMobsEnabled, - LoggingUse, payForCombiningItems, BlastFurnacesReassign = false, SmokerReassign = false, payForStackedEntities, + LoggingUse, payForCombiningItems, BlastFurnacesReassign = false, SmokerReassign = false, payForStackedEntities, payForAbove = false, payForEachVTradeItem, allowEnchantingBoostedItems; public ItemStack guiBackButton, guiNextButton; @@ -472,6 +472,9 @@ public class GeneralConfigManager { "This option is only related to: sugar_cane, cactus, kelp, bamboo"); preventCropResizePayment = c.get("prevent-crop-resize-payment", false);*/ + c.addComment("pay-for-above", "When enabled we will try to pay player for blocks above broken ones. This only applies to sugarcane, bamboo and kelp"); + payForAbove = c.get("pay-for-above", false); + c.addComment("pay-for-stacked-entities", "Allows to pay for stacked entities for each one. Requires StackMob or WildStacker."); payForStackedEntities = c.get("pay-for-stacked-entities", false); @@ -832,7 +835,7 @@ public class GeneralConfigManager { MythicMobsEnabled = c.get("ExploitProtections.MythicMobs.enabled", true); c.addComment("ExploitProtections.Spawner.PreventSlimeSplit", "Prevent slime splitting when they are from spawner", - "Protects agains exploiting as new splited slimes is treated as naturally spawned and not from spawner"); + "Protects against exploiting as new splitted slimes is treated as naturally spawned and not from spawner"); PreventSlimeSplit = c.get("ExploitProtections.Spawner.PreventSlimeSplit", true); c.addComment("ExploitProtections.Spawner.PreventMagmaCubeSplit", "Prevent magmacube splitting when they are from spawner"); PreventMagmaCubeSplit = c.get("ExploitProtections.Spawner.PreventMagmaCubeSplit", true); diff --git a/src/main/java/com/gamingmesh/jobs/container/FastPayment.java b/src/main/java/com/gamingmesh/jobs/container/FastPayment.java index e1538511..bce7cd4a 100644 --- a/src/main/java/com/gamingmesh/jobs/container/FastPayment.java +++ b/src/main/java/com/gamingmesh/jobs/container/FastPayment.java @@ -36,4 +36,8 @@ public class FastPayment { public Long getTime() { return time; } + + public void setTime(Long time) { + this.time = time; + } } diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index 5ca6659f..5aacf280 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -18,6 +18,8 @@ package com.gamingmesh.jobs.listeners; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; @@ -28,6 +30,7 @@ import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.block.BrewingStand; import org.bukkit.block.Furnace; import org.bukkit.block.data.Ageable; @@ -47,6 +50,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPhysicsEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.enchantment.EnchantItemEvent; import org.bukkit.event.entity.CreatureSpawnEvent; @@ -119,6 +123,7 @@ import com.google.common.cache.CacheBuilder; import net.Zrips.CMILib.CMILib; import net.Zrips.CMILib.ActionBar.CMIActionBar; import net.Zrips.CMILib.Colors.CMIChatColor; +import net.Zrips.CMILib.Container.CMILocation; import net.Zrips.CMILib.Entities.CMIEntityType; import net.Zrips.CMILib.Items.CMIItemStack; import net.Zrips.CMILib.Items.CMIMaterial; @@ -450,6 +455,7 @@ public final class JobsPaymentListener implements Listener { }*/ Jobs.action(Jobs.getPlayerManager().getJobsPlayer(player), bInfo, block); + breakCache.put(CMILocation.toString(block.getLocation(), ":", true, true), player.getUniqueId()); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @@ -1761,4 +1767,53 @@ public final class JobsPaymentListener implements Listener { return true; } + + private static final int MAX_ENTRIES = 50; + LinkedHashMap breakCache = new LinkedHashMap(MAX_ENTRIES + 1, .75F, false) { + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > MAX_ENTRIES; + } + }; + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onEvent(BlockPhysicsEvent event) { + if (!Jobs.getGCManager().payForAbove) + return; + if (event.getBlock().getType().equals(Material.AIR)) + return; + final Block block = event.getBlock(); + + CMIMaterial mat = CMIMaterial.get(block); + + if (!mat.equals(CMIMaterial.SUGAR_CANE) && !mat.equals(CMIMaterial.BAMBOO) && !mat.equals(CMIMaterial.KELP_PLANT)) + return; + + if (!Jobs.getGCManager().canPerformActionInWorld(block.getWorld())) + return; + + if (event.getSourceBlock().equals(event.getBlock())) + return; + + if (event.getBlock().getLocation().getBlockY() <= event.getSourceBlock().getLocation().getBlockY()) + return; + + Location loc = event.getSourceBlock().getLocation().clone(); + UUID uuid = breakCache.get(CMILocation.toString(loc, ":", true, true)); + if (uuid == null) + return; + + BlockActionInfo bInfo = new BlockActionInfo(block, ActionType.BREAK); + FastPayment fp = Jobs.FASTPAYMENT.get(uuid); + if (fp == null) + return; + if (!fp.getInfo().getType().equals(ActionType.BREAK) || !fp.getInfo().getNameWithSub().equals(bInfo.getNameWithSub())) + return; + + if (fp.getTime() > System.currentTimeMillis() - 50L && (fp.getInfo().getName().equalsIgnoreCase(bInfo.getName()) || + fp.getInfo().getNameWithSub().equalsIgnoreCase(bInfo.getNameWithSub()))) { + Jobs.perform(fp.getPlayer(), fp.getInfo(), fp.getPayment(), fp.getJob()); + breakCache.put(CMILocation.toString(block.getLocation(), ":", true, true), uuid); + fp.setTime(System.currentTimeMillis() + 45); + } + } }