From 6f1b5f8defe17c8737885c9636cf5468a1cc8e90 Mon Sep 17 00:00:00 2001 From: Sefiraat Date: Mon, 17 Jul 2023 11:16:19 +0100 Subject: [PATCH 01/19] BlockLimiter start --- pom.xml | 2 +- .../dev/j3fftw/headlimiter/HeadLimiter.java | 39 ++++++++-- .../blocklimiter/BlockLimiter.java | 72 +++++++++++++++++++ .../blocklimiter/BlockListener.java | 72 +++++++++++++++++++ .../blocklimiter/ChunkContent.java | 47 ++++++++++++ src/main/resources/config.yml | 14 +++- 6 files changed, 237 insertions(+), 9 deletions(-) create mode 100644 src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java create mode 100644 src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java create mode 100644 src/main/java/dev/j3fftw/headlimiter/blocklimiter/ChunkContent.java diff --git a/pom.xml b/pom.xml index 984af2f..2570be0 100644 --- a/pom.xml +++ b/pom.xml @@ -108,7 +108,7 @@ com.github.thebusybiscuit Slimefun4 - 2c4f886fe4 + RC-35 provided diff --git a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java index 50cf15d..d892570 100644 --- a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java @@ -1,8 +1,9 @@ package dev.j3fftw.headlimiter; -import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; -import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; -import io.github.thebusybiscuit.slimefun4.libraries.dough.updater.GitHubBuildsUpdater; +import java.io.File; + +import javax.annotation.Nonnull; + import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -12,17 +13,25 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.plugin.java.JavaPlugin; -import java.io.File; +import com.palmergames.bukkit.towny.regen.block.BlockLocation; + +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; +import io.github.thebusybiscuit.slimefun4.libraries.dough.updater.GitHubBuildsUpdater; + +import dev.j3fftw.headlimiter.blocklimiter.BlockLimiter; public final class HeadLimiter extends JavaPlugin implements Listener { private static HeadLimiter instance; + private BlockLimiter blockLimiter; @Override public void onEnable() { instance = this; - if (!new File(getDataFolder(), "config.yml").exists()) + if (!new File(getDataFolder(), "config.yml").exists()) { saveDefaultConfig(); + } Utils.loadPermissions(); @@ -35,6 +44,8 @@ public void onEnable() { if (getConfig().getBoolean("auto-update", true) && getDescription().getVersion().startsWith("DEV - ")) { new GitHubBuildsUpdater(this, getFile(), "J3fftw1/HeadLimiter/master").start(); } + + this.blockLimiter = new BlockLimiter(this); } @Override @@ -64,13 +75,27 @@ public void onPlace(BlockPlaceEvent e) { && isCargo(sfItem) ) { final int maxAmount = Utils.getMaxHeads(player); - Utils.count(block.getChunk(), - result -> Utils.onCheck(player, block, maxAmount, result.getTotal(), sfItem)); + Utils.count( + block.getChunk(), + result -> Utils.onCheck(player, block, maxAmount, result.getTotal(), sfItem) + ); } } } + public BlockLimiter getBlockLimiter() { + return blockLimiter; + } + public static HeadLimiter getInstance() { return instance; } + + public static int getSlimefunItemLimit(@Nonnull SlimefunItem slimefunItem) { + return getSlimefunItemLimit(slimefunItem.getId()); + } + + public static int getSlimefunItemLimit(@Nonnull String itemId) { + return instance.getConfig().getInt("block-limits." + itemId); + } } diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java new file mode 100644 index 0000000..ee3cc7e --- /dev/null +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java @@ -0,0 +1,72 @@ +package dev.j3fftw.headlimiter.blocklimiter; + +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; + +import com.google.common.base.Preconditions; + +import io.github.thebusybiscuit.slimefun4.libraries.dough.blocks.ChunkPosition; + +import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; +import me.mrCookieSlime.Slimefun.api.BlockStorage; + +import dev.j3fftw.headlimiter.HeadLimiter; + +public final class BlockLimiter { + + private static BlockLimiter instance; + private final Map contentMap = new HashMap<>(); + + public BlockLimiter(@Nonnull HeadLimiter headLimiter) { + Preconditions.checkArgument(instance == null, "Cannot create a new instance of the BlockLimiter"); + instance = this; + new BlockListener(headLimiter); + headLimiter.getServer().getScheduler().runTaskLater(headLimiter, this::loadBlockStorage, 1); + } + + private void loadBlockStorage() { + for (World world : Bukkit.getWorlds()) { + BlockStorage worldStorage = BlockStorage.getStorage(world); + if (worldStorage == null) { + return; + } else { + for (Map.Entry entry : worldStorage.getRawStorage().entrySet()) { + Location location = entry.getKey(); + String id = entry.getValue().getString("id"); + ChunkPosition chunkPosition = new ChunkPosition(location.getChunk()); + ChunkContent content = contentMap.get(chunkPosition); + if (content == null) { + content = new ChunkContent(); + content.incrementAmount(id); + contentMap.put(chunkPosition, content); + } else { + content.incrementAmount(id); + } + } + } + + } + } + + @Nullable + public ChunkContent getChunkContent(@Nonnull ChunkPosition chunkPosition) { + return contentMap.get(chunkPosition); + } + + @Nullable + public ChunkContent setChunkContent(@Nonnull ChunkPosition chunkPosition, @Nonnull ChunkContent content) { + return contentMap.put(chunkPosition, content); + } + + @Nonnull + public static BlockLimiter getInstance() { + return instance; + } +} diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java new file mode 100644 index 0000000..92075eb --- /dev/null +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java @@ -0,0 +1,72 @@ +package dev.j3fftw.headlimiter.blocklimiter; + +import javax.annotation.Nonnull; + +import org.bukkit.ChatColor; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import io.github.thebusybiscuit.slimefun4.api.events.SlimefunBlockBreakEvent; +import io.github.thebusybiscuit.slimefun4.api.events.SlimefunBlockPlaceEvent; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.libraries.dough.blocks.ChunkPosition; + +import dev.j3fftw.headlimiter.HeadLimiter; + +public class BlockListener implements Listener { + + public BlockListener(@Nonnull HeadLimiter headLimiter) { + headLimiter.getServer().getPluginManager().registerEvents(this, headLimiter); + } + + @EventHandler + public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { + SlimefunItem slimefunItem = event.getSlimefunItem(); + String id = slimefunItem.getId(); + int definedLimit = HeadLimiter.getSlimefunItemLimit(id); + if (definedLimit == 0) { + // No limit has been set, nothing required for HeadLimiter + return; + } + + ChunkPosition chunkPosition = new ChunkPosition(event.getBlockPlaced().getChunk()); + ChunkContent content = BlockLimiter.getInstance().getChunkContent(chunkPosition); + + if (content == null) { + // Content is null so no blocks are currently in this chunk, lets set one up - event can continue + content = new ChunkContent(); + content.incrementAmount(id); + BlockLimiter.getInstance().setChunkContent(chunkPosition, content); + } else if (content.getCurrentAmount(id) < definedLimit) { + // This chunk can take more of the specified item type + content.incrementAmount(id); + } else { + // Chunk has hit its limit for this type, time to deny the placement + event.setCancelled(true); + event.getPlayer().sendMessage(ChatColor.RED + "You cannot place any more of this item within this chunk."); + } + } + + @EventHandler + public void onSlimefunItemBroken(@Nonnull SlimefunBlockBreakEvent event) { + SlimefunItem slimefunItem = event.getSlimefunItem(); + String id = slimefunItem.getId(); + int definedLimit = HeadLimiter.getSlimefunItemLimit(id); + if (definedLimit == 0) { + // No limit has been set, nothing required for HeadLimiter + return; + } + + ChunkPosition chunkPosition = new ChunkPosition(event.getBlockBroken().getChunk()); + ChunkContent content = BlockLimiter.getInstance().getChunkContent(chunkPosition); + + if (content == null) { + // Content is null so no blocks are currently in this chunk, shouldn't be possible, but never mind + return; + } else { + // This chunk can take more of the specified item type + content.decrementAmount(id); + } + } + +} diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/ChunkContent.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/ChunkContent.java new file mode 100644 index 0000000..0a7f103 --- /dev/null +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/ChunkContent.java @@ -0,0 +1,47 @@ +package dev.j3fftw.headlimiter.blocklimiter; + +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.Nonnull; + +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; + +public class ChunkContent { + + private final Map contentMap = new HashMap<>(); + + public int getCurrentAmount(@Nonnull SlimefunItem slimefunItem) { + return getCurrentAmount(slimefunItem.getId()); + } + + public int getCurrentAmount(@Nonnull String itemId) { + return this.contentMap.getOrDefault(itemId, 0); + } + + public void incrementAmount(@Nonnull SlimefunItem slimefunItem) { + incrementAmount(slimefunItem.getId()); + } + + public void incrementAmount(@Nonnull String itemId) { + int amount = getCurrentAmount(itemId); + setAmount(itemId, amount + 1); + } + + public void decrementAmount(@Nonnull SlimefunItem slimefunItem) { + incrementAmount(slimefunItem.getId()); + } + + public void decrementAmount(@Nonnull String itemId) { + int amount = getCurrentAmount(itemId); + setAmount(itemId, Math.max(0, amount - 1)); + } + + public void setAmount(@Nonnull SlimefunItem slimefunItem, int amount) { + setAmount(slimefunItem.getId(), amount); + } + + public void setAmount(@Nonnull String itemId, int amount) { + contentMap.put(itemId, amount); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 157fe55..459d1fe 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -25,4 +25,16 @@ permission: ## walshy: 20 ## alessio: 15 ## jeff: 10 - noob: 5 \ No newline at end of file + noob: 5 +## Put the SlimefunItem ID (found in Items.yml) followed by the maximum number of those blocks you want to allow in +## a chunk. Please note this will not remove blocks retroactively only limit further placement. +## Example: +## CARGO_INPUT_NODE: 25 +## NTW_BRIDGE: 100 +## JEFFS_WEIRD_BOX: 0 +block-limits: + CARGO_NODE_INPUT: 25 + CARGO_NODE_OUTPUT: 25 + CARGO_NODE_OUTPUT_ADVANCED: 25 + CARGO_NODE: 25 + CARGO_MANAGER: 25 \ No newline at end of file From 081b00620317622f900810fbc757bf73eb0df992 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 17 Jul 2023 12:34:50 +0200 Subject: [PATCH 02/19] changed some behaviour --- .../dev/j3fftw/headlimiter/HeadLimiter.java | 2 -- .../blocklimiter/BlockLimiter.java | 5 ++-- .../blocklimiter/BlockListener.java | 25 +++++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java index d892570..f700d1c 100644 --- a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java @@ -13,8 +13,6 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.plugin.java.JavaPlugin; -import com.palmergames.bukkit.towny.regen.block.BlockLocation; - import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.libraries.dough.updater.GitHubBuildsUpdater; diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java index ee3cc7e..2da5ab0 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java @@ -60,9 +60,8 @@ public ChunkContent getChunkContent(@Nonnull ChunkPosition chunkPosition) { return contentMap.get(chunkPosition); } - @Nullable - public ChunkContent setChunkContent(@Nonnull ChunkPosition chunkPosition, @Nonnull ChunkContent content) { - return contentMap.put(chunkPosition, content); + public void setChunkContent(@Nonnull ChunkPosition chunkPosition, @Nonnull ChunkContent content) { + contentMap.put(chunkPosition, content); } @Nonnull diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java index 92075eb..32361d8 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java @@ -24,10 +24,6 @@ public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { SlimefunItem slimefunItem = event.getSlimefunItem(); String id = slimefunItem.getId(); int definedLimit = HeadLimiter.getSlimefunItemLimit(id); - if (definedLimit == 0) { - // No limit has been set, nothing required for HeadLimiter - return; - } ChunkPosition chunkPosition = new ChunkPosition(event.getBlockPlaced().getChunk()); ChunkContent content = BlockLimiter.getInstance().getChunkContent(chunkPosition); @@ -40,11 +36,17 @@ public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { } else if (content.getCurrentAmount(id) < definedLimit) { // This chunk can take more of the specified item type content.incrementAmount(id); - } else { - // Chunk has hit its limit for this type, time to deny the placement - event.setCancelled(true); - event.getPlayer().sendMessage(ChatColor.RED + "You cannot place any more of this item within this chunk."); } + + // We want to still add to the map but do not cancel if definedLimit is 0. + if (definedLimit == 0) { + // No limit has been set, nothing required for HeadLimiter + return; + } + + // Chunk has hit its limit for this type, time to deny the placement + event.setCancelled(true); + event.getPlayer().sendMessage(ChatColor.RED + "You cannot place any more of this item within this chunk."); } @EventHandler @@ -63,10 +65,11 @@ public void onSlimefunItemBroken(@Nonnull SlimefunBlockBreakEvent event) { if (content == null) { // Content is null so no blocks are currently in this chunk, shouldn't be possible, but never mind return; - } else { - // This chunk can take more of the specified item type - content.decrementAmount(id); } + + // This chunk can take more of the specified item type + content.decrementAmount(id); + } } From ac200d64966f609fc0da124e4190c5f9f7887033 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 17 Jul 2023 12:50:31 +0200 Subject: [PATCH 03/19] updated java in the workflow --- .github/workflows/java.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/java.yml b/.github/workflows/java.yml index b5ec63d..3ea9a48 100644 --- a/.github/workflows/java.yml +++ b/.github/workflows/java.yml @@ -22,10 +22,10 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v2 - - name: Set up JDK 1.8 + - name: Set up JDK 16 uses: actions/setup-java@v2 with: - java-version: 8 + java-version: 16 distribution: adopt - name: Build with Maven run: mvn package --file pom.xml From ed892f08bf32cdd544062db2a3a22d50301c5420 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 17 Jul 2023 20:41:01 +0200 Subject: [PATCH 04/19] add new config --- .../dev/j3fftw/headlimiter/HeadLimiter.java | 12 ++++++ .../headlimiter/blocklimiter/Group.java | 29 +++++++++++++++ src/main/resources/config.yml | 37 +++++++++++++------ 3 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java diff --git a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java index f700d1c..e8ba8b2 100644 --- a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java @@ -1,11 +1,14 @@ package dev.j3fftw.headlimiter; import java.io.File; +import java.util.HashSet; import javax.annotation.Nonnull; +import dev.j3fftw.headlimiter.blocklimiter.Group; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -23,6 +26,7 @@ public final class HeadLimiter extends JavaPlugin implements Listener { private static HeadLimiter instance; private BlockLimiter blockLimiter; + private final HashSet groups = new HashSet<>(); @Override public void onEnable() { @@ -44,6 +48,7 @@ public void onEnable() { } this.blockLimiter = new BlockLimiter(this); + loadConfig(); } @Override @@ -96,4 +101,11 @@ public static int getSlimefunItemLimit(@Nonnull SlimefunItem slimefunItem) { public static int getSlimefunItemLimit(@Nonnull String itemId) { return instance.getConfig().getInt("block-limits." + itemId); } + + public void loadConfig() { + ConfigurationSection configurationSection = instance.getConfig().getConfigurationSection("block-limits"); + for (String key : configurationSection.getKeys(false)) { + groups.add(new Group(configurationSection.getConfigurationSection(key))); + } + } } diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java new file mode 100644 index 0000000..4ef3281 --- /dev/null +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java @@ -0,0 +1,29 @@ +package dev.j3fftw.headlimiter.blocklimiter; + +import org.bukkit.configuration.ConfigurationSection; + +import java.util.HashMap; +import java.util.HashSet; + +public class Group { + + private final String group; + private final int itemAmount; + private final HashSet items; + private final HashMap permissionAmount; + + + public Group(ConfigurationSection configurationSection) { + this.group = configurationSection.getName(); + this.itemAmount = configurationSection.getInt("items-amount"); + this.items = new HashSet<>(configurationSection.getStringList("items")); + this.permissionAmount = new HashMap<>(); + + ConfigurationSection permissionAmountSection = + configurationSection.getConfigurationSection("permission-amount"); + + for (String key : permissionAmountSection.getKeys(false)) { + permissionAmount.put(key, permissionAmountSection.getInt(key)); + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 459d1fe..eb2b8be 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -26,15 +26,30 @@ permission: ## alessio: 15 ## jeff: 10 noob: 5 -## Put the SlimefunItem ID (found in Items.yml) followed by the maximum number of those blocks you want to allow in -## a chunk. Please note this will not remove blocks retroactively only limit further placement. -## Example: -## CARGO_INPUT_NODE: 25 -## NTW_BRIDGE: 100 -## JEFFS_WEIRD_BOX: 0 + +## Item groups +## You can make your own item group see the example. Make sure that the example doesnt match up with any item ID's +## highly recommend prefixing or suffixing them. For example CARGO_GROUP or GROUP_CARGO. +## +## cargo_group: +## items-amount: 25 +## items: +## - CARGO_NODE_INPUT +## - CARGO_NODE_OUTPUT +## - CARGO_NODE_OUTPUT_ADVANCED +## - CARGO_NODE +## - CARGO_MANAGER +## block-limits: - CARGO_NODE_INPUT: 25 - CARGO_NODE_OUTPUT: 25 - CARGO_NODE_OUTPUT_ADVANCED: 25 - CARGO_NODE: 25 - CARGO_MANAGER: 25 \ No newline at end of file + cargo_group: + items-amount: 25 + items: + - CARGO_NODE_INPUT + - CARGO_NODE_OUTPUT + - CARGO_NODE_OUTPUT_ADVANCED + - CARGO_NODE + - CARGO_MANAGER + permission-amount: + example_1: 50 + example_2: 100 + example_3: 150 \ No newline at end of file From 9db8b827c457f39c3577b14d3245670ebbc8d08c Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 17 Jul 2023 21:54:56 +0200 Subject: [PATCH 05/19] done something --- .../dev/j3fftw/headlimiter/HeadLimiter.java | 25 +++++++++++---- .../blocklimiter/BlockListener.java | 32 ++++++++++--------- .../headlimiter/blocklimiter/Group.java | 18 +++++++++++ 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java index e8ba8b2..c762a64 100644 --- a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java @@ -2,8 +2,8 @@ import java.io.File; import java.util.HashSet; - -import javax.annotation.Nonnull; +import java.util.Map; +import java.util.Set; import dev.j3fftw.headlimiter.blocklimiter.Group; import org.bukkit.Material; @@ -94,12 +94,25 @@ public static HeadLimiter getInstance() { return instance; } - public static int getSlimefunItemLimit(@Nonnull SlimefunItem slimefunItem) { - return getSlimefunItemLimit(slimefunItem.getId()); + public Set getGroupSet() { + return groups; } - public static int getSlimefunItemLimit(@Nonnull String itemId) { - return instance.getConfig().getInt("block-limits." + itemId); + public int getLimitForItem(String slimefunItemId, Player player) { + Set groupSet = HeadLimiter.getInstance().getGroupSet(); + for (Group group : groupSet) { + if (group.getItems().contains(slimefunItemId)) { + if (!group.getPermissionAmount().isEmpty()) { + for (Map.Entry entry : group.getPermissionAmount().entrySet()) { + if (player.hasPermission(entry.getKey())) { + return entry.getValue(); + } + } + } + return group.getItemAmount(); + } + } + return -1; } public void loadConfig() { diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java index 32361d8..e75345e 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java @@ -13,6 +13,9 @@ import dev.j3fftw.headlimiter.HeadLimiter; +import java.util.Map; +import java.util.Set; + public class BlockListener implements Listener { public BlockListener(@Nonnull HeadLimiter headLimiter) { @@ -22,8 +25,13 @@ public BlockListener(@Nonnull HeadLimiter headLimiter) { @EventHandler public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { SlimefunItem slimefunItem = event.getSlimefunItem(); - String id = slimefunItem.getId(); - int definedLimit = HeadLimiter.getSlimefunItemLimit(id); + String slimefunItemId = slimefunItem.getId(); + int definedLimit = HeadLimiter.getInstance().getLimitForItem(slimefunItemId, event.getPlayer()); + + if (definedLimit == -1) { + // No limit has been set, nothing required for HeadLimiter + return; + } ChunkPosition chunkPosition = new ChunkPosition(event.getBlockPlaced().getChunk()); ChunkContent content = BlockLimiter.getInstance().getChunkContent(chunkPosition); @@ -31,17 +39,11 @@ public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { if (content == null) { // Content is null so no blocks are currently in this chunk, lets set one up - event can continue content = new ChunkContent(); - content.incrementAmount(id); + content.incrementAmount(slimefunItemId); BlockLimiter.getInstance().setChunkContent(chunkPosition, content); - } else if (content.getCurrentAmount(id) < definedLimit) { + } else if (content.getCurrentAmount(slimefunItemId) < definedLimit) { // This chunk can take more of the specified item type - content.incrementAmount(id); - } - - // We want to still add to the map but do not cancel if definedLimit is 0. - if (definedLimit == 0) { - // No limit has been set, nothing required for HeadLimiter - return; + content.incrementAmount(slimefunItemId); } // Chunk has hit its limit for this type, time to deny the placement @@ -52,9 +54,9 @@ public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { @EventHandler public void onSlimefunItemBroken(@Nonnull SlimefunBlockBreakEvent event) { SlimefunItem slimefunItem = event.getSlimefunItem(); - String id = slimefunItem.getId(); - int definedLimit = HeadLimiter.getSlimefunItemLimit(id); - if (definedLimit == 0) { + String slimefunItemId = slimefunItem.getId(); + int definedLimit = HeadLimiter.getInstance().getLimitForItem(slimefunItemId, event.getPlayer()); + if (definedLimit == -1) { // No limit has been set, nothing required for HeadLimiter return; } @@ -68,7 +70,7 @@ public void onSlimefunItemBroken(@Nonnull SlimefunBlockBreakEvent event) { } // This chunk can take more of the specified item type - content.decrementAmount(id); + content.decrementAmount(slimefunItemId); } diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java index 4ef3281..1b77fa0 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java @@ -4,6 +4,8 @@ import java.util.HashMap; import java.util.HashSet; +import java.util.Map; +import java.util.Set; public class Group { @@ -26,4 +28,20 @@ public Group(ConfigurationSection configurationSection) { permissionAmount.put(key, permissionAmountSection.getInt(key)); } } + + public String getGroup() { + return group; + } + + public int getItemAmount() { + return itemAmount; + } + + public Set getItems() { + return items; + } + + public Map getPermissionAmount() { + return permissionAmount; + } } From 25cf2bfa8be37dc6ad05482bb8e10e8a248f3b99 Mon Sep 17 00:00:00 2001 From: Sefiraat Date: Tue, 18 Jul 2023 08:20:46 +0100 Subject: [PATCH 06/19] Restore else branch --- .../headlimiter/blocklimiter/BlockListener.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java index e75345e..2ef530e 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java @@ -13,9 +13,6 @@ import dev.j3fftw.headlimiter.HeadLimiter; -import java.util.Map; -import java.util.Set; - public class BlockListener implements Listener { public BlockListener(@Nonnull HeadLimiter headLimiter) { @@ -44,11 +41,11 @@ public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { } else if (content.getCurrentAmount(slimefunItemId) < definedLimit) { // This chunk can take more of the specified item type content.incrementAmount(slimefunItemId); + } else { + // Chunk has hit its limit for this type, time to deny the placement + event.setCancelled(true); + event.getPlayer().sendMessage(ChatColor.RED + "You cannot place any more of this item within this chunk."); } - - // Chunk has hit its limit for this type, time to deny the placement - event.setCancelled(true); - event.getPlayer().sendMessage(ChatColor.RED + "You cannot place any more of this item within this chunk."); } @EventHandler From a7a521020cff7c601322f5407f55d4c35f72d23c Mon Sep 17 00:00:00 2001 From: Sefiraat Date: Tue, 18 Jul 2023 08:21:12 +0100 Subject: [PATCH 07/19] Rename fields and add defaults --- .../headlimiter/blocklimiter/Group.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java index 1b77fa0..c0ac7a5 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java @@ -9,39 +9,39 @@ public class Group { - private final String group; - private final int itemAmount; + private final String groupName; + private final int defaultAmount; private final HashSet items; - private final HashMap permissionAmount; - + private final HashMap permissionAmounts; public Group(ConfigurationSection configurationSection) { - this.group = configurationSection.getName(); - this.itemAmount = configurationSection.getInt("items-amount"); + this.groupName = configurationSection.getName(); + this.defaultAmount = configurationSection.getInt("items-amount", 0); this.items = new HashSet<>(configurationSection.getStringList("items")); - this.permissionAmount = new HashMap<>(); + this.permissionAmounts = new HashMap<>(); - ConfigurationSection permissionAmountSection = - configurationSection.getConfigurationSection("permission-amount"); + ConfigurationSection permissionSection = configurationSection.getConfigurationSection("permission-amount"); - for (String key : permissionAmountSection.getKeys(false)) { - permissionAmount.put(key, permissionAmountSection.getInt(key)); + if (permissionSection != null) { + for (String key : permissionSection.getKeys(false)) { + permissionAmounts.put(key, permissionSection.getInt(key, 0)); + } } } - public String getGroup() { - return group; + public String getGroupName() { + return groupName; } - public int getItemAmount() { - return itemAmount; + public int getDefaultAmount() { + return defaultAmount; } public Set getItems() { return items; } - public Map getPermissionAmount() { - return permissionAmount; + public Map getPermissionAmounts() { + return permissionAmounts; } } From cc3eb2f4367d375d07e24cf0303d8696060b0fff Mon Sep 17 00:00:00 2001 From: Sefiraat Date: Tue, 18 Jul 2023 08:21:33 +0100 Subject: [PATCH 08/19] Rename fields and add defaults --- src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java index c762a64..7651b55 100644 --- a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java @@ -102,14 +102,14 @@ public int getLimitForItem(String slimefunItemId, Player player) { Set groupSet = HeadLimiter.getInstance().getGroupSet(); for (Group group : groupSet) { if (group.getItems().contains(slimefunItemId)) { - if (!group.getPermissionAmount().isEmpty()) { - for (Map.Entry entry : group.getPermissionAmount().entrySet()) { + if (!group.getPermissionAmounts().isEmpty()) { + for (Map.Entry entry : group.getPermissionAmounts().entrySet()) { if (player.hasPermission(entry.getKey())) { return entry.getValue(); } } } - return group.getItemAmount(); + return group.getDefaultAmount(); } } return -1; From c81404dbad1ddb03859ca56c4e2110b3f5802e3b Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Tue, 18 Jul 2023 10:38:20 +0200 Subject: [PATCH 09/19] updated permissions --- .DS_Store | Bin 0 -> 6148 bytes .../java/dev/j3fftw/headlimiter/HeadLimiter.java | 6 ++++-- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ff446f64f29745394d8861270ec8809074604ea0 GIT binary patch literal 6148 zcmeHKO>fgc5S>lZ#%V#y0i+5E$r9Hzgce$%UeYu=-3^Kz$`K*hk#^sHyqWbp%e!7860J%2fT&7D4hmyw3)L@-=eev| z!HnDh3XX9a%U%%60DTT<%_?9O_`4P0Z?{Bs8c;-O?)yE)T>ZN;l|*^TqjQ>2kYj(& zvA2G*BScR~(un$5A8|M5vjLxoLh6&BV}8sr?d|}J_6{G_-2KCYdCi?Y-fPs{hx-TfdBwSX@BYzQ_aeSb9gcoTvxzoSKSJ6o{ zHQL%LU=_$JP}JQjpZ}ff@BdjR+p`K-1^y`oM5*nyTS&>At!v5gS?i*_MPXy!LZfm) kWsYOj;G=j4MH%K?ZUCDG7aB1Fvp)h#23uJL{-^>!03DFd?*IS* literal 0 HcmV?d00001 diff --git a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java index 7651b55..2769a8b 100644 --- a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java @@ -103,11 +103,13 @@ public int getLimitForItem(String slimefunItemId, Player player) { for (Group group : groupSet) { if (group.getItems().contains(slimefunItemId)) { if (!group.getPermissionAmounts().isEmpty()) { + int highestPermissionAmount = 0; for (Map.Entry entry : group.getPermissionAmounts().entrySet()) { - if (player.hasPermission(entry.getKey())) { - return entry.getValue(); + if (player.hasPermission(entry.getKey()) || player.isOp()) { + highestPermissionAmount = Math.max(highestPermissionAmount, entry.getValue()); } } + return highestPermissionAmount; } return group.getDefaultAmount(); } From 3948f05d5bf564c1815f2807df420fdd23165746 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Tue, 18 Jul 2023 10:43:10 +0200 Subject: [PATCH 10/19] updated gitignore --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 1 + 2 files changed, 1 insertion(+) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index ff446f64f29745394d8861270ec8809074604ea0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKO>fgc5S>lZ#%V#y0i+5E$r9Hzgce$%UeYu=-3^Kz$`K*hk#^sHyqWbp%e!7860J%2fT&7D4hmyw3)L@-=eev| z!HnDh3XX9a%U%%60DTT<%_?9O_`4P0Z?{Bs8c;-O?)yE)T>ZN;l|*^TqjQ>2kYj(& zvA2G*BScR~(un$5A8|M5vjLxoLh6&BV}8sr?d|}J_6{G_-2KCYdCi?Y-fPs{hx-TfdBwSX@BYzQ_aeSb9gcoTvxzoSKSJ6o{ zHQL%LU=_$JP}JQjpZ}ff@BdjR+p`K-1^y`oM5*nyTS&>At!v5gS?i*_MPXy!LZfm) kWsYOj;G=j4MH%K?ZUCDG7aB1Fvp)h#23uJL{-^>!03DFd?*IS* diff --git a/.gitignore b/.gitignore index cf6c107..c1e3877 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ *.sh dependency-reduced-pom.xml /dependency-reduced-pom.xml +*.DS_Store \ No newline at end of file From 9efc8c1cb11eadc7671b570949a9411a44ccea95 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Tue, 18 Jul 2023 10:58:35 +0200 Subject: [PATCH 11/19] fixed permissions --- src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java index 2769a8b..d85f161 100644 --- a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java @@ -103,10 +103,12 @@ public int getLimitForItem(String slimefunItemId, Player player) { for (Group group : groupSet) { if (group.getItems().contains(slimefunItemId)) { if (!group.getPermissionAmounts().isEmpty()) { - int highestPermissionAmount = 0; + int highestPermissionAmount = -1; for (Map.Entry entry : group.getPermissionAmounts().entrySet()) { if (player.hasPermission(entry.getKey()) || player.isOp()) { highestPermissionAmount = Math.max(highestPermissionAmount, entry.getValue()); + } else { + return group.getDefaultAmount(); } } return highestPermissionAmount; From 50de235e12a9c9ed50acd6c804777d42dba5415d Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Wed, 19 Jul 2023 16:54:52 +0200 Subject: [PATCH 12/19] deleted old stuff added new docs --- src/main/resources/config.yml | 51 +++++++++++------------------------ 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index eb2b8be..475946a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,37 +1,14 @@ -## Amount of Cargo machines per chunk (25 by default) -amount: 25 - -## Auto updater (true by default) -auto-update: true - -## Size of the thread pool (4 by default) -## Only touch this setting when you know what you are doing! -thread-pool-size: 4 - -## If a claiming plugin is enabled, this setting prevents players from placing cargo outside of claimed areas -## This makes it impossible to "extend" existing networks from just outside the border in order to steal from claims -block-wilderness-cargo: false - -## Permissions (false by default) -## Permission node: headlimiter.permission. -## Example: headlimiter.permission.noob +## Block Limits +## You can limit all Slimefun Blocks including all addons. +## You will have to make a group per item or item set. ## -## When permissions have been set to true, you can use the values here to set the amount of heads checked. -## You can add as many entries as you want. -## Order has to be reversed or breaky breaky -permissions: false -permission: - ## sefi: 25 - ## walshy: 20 - ## alessio: 15 - ## jeff: 10 - noob: 5 - -## Item groups -## You can make your own item group see the example. Make sure that the example doesnt match up with any item ID's -## highly recommend prefixing or suffixing them. For example CARGO_GROUP or GROUP_CARGO. -## -## cargo_group: +## Down here you have an example for Cargo. +## cargo: is the group name +## items-amount: is the default amount the group is limited to +## items: will have a list of items you want to block. These are all Slimefun Items IDs (Addons included) +## permission-amount: this will have a list of permissions. This is completely optional and doesn't have to be there, +## If you do not want permissions you can just remove it or not add it to the group. +## cargo: ## items-amount: 25 ## items: ## - CARGO_NODE_INPUT @@ -39,9 +16,13 @@ permission: ## - CARGO_NODE_OUTPUT_ADVANCED ## - CARGO_NODE ## - CARGO_MANAGER -## +## permission-amount: +## example_1: 50 +## example_2: 100 +## example_3: 150 + block-limits: - cargo_group: + cargo: items-amount: 25 items: - CARGO_NODE_INPUT From 9e9f502867b57e721d0172311d06f5ce2b8ea4a9 Mon Sep 17 00:00:00 2001 From: Sefiraat Date: Sun, 30 Jul 2023 13:12:43 +0100 Subject: [PATCH 13/19] Move to group values --- .../dev/j3fftw/headlimiter/HeadLimiter.java | 35 ++----------- .../blocklimiter/BlockLimiter.java | 36 +++++++++++++ .../blocklimiter/BlockListener.java | 6 +-- .../blocklimiter/ChunkContent.java | 21 ++++++++ .../headlimiter/blocklimiter/Group.java | 51 +++++++++++++++++++ 5 files changed, 116 insertions(+), 33 deletions(-) diff --git a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java index d85f161..3d7600e 100644 --- a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java @@ -1,9 +1,6 @@ package dev.j3fftw.headlimiter; import java.io.File; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; import dev.j3fftw.headlimiter.blocklimiter.Group; import org.bukkit.Material; @@ -26,7 +23,7 @@ public final class HeadLimiter extends JavaPlugin implements Listener { private static HeadLimiter instance; private BlockLimiter blockLimiter; - private final HashSet groups = new HashSet<>(); + @Override public void onEnable() { @@ -94,35 +91,13 @@ public static HeadLimiter getInstance() { return instance; } - public Set getGroupSet() { - return groups; - } - - public int getLimitForItem(String slimefunItemId, Player player) { - Set groupSet = HeadLimiter.getInstance().getGroupSet(); - for (Group group : groupSet) { - if (group.getItems().contains(slimefunItemId)) { - if (!group.getPermissionAmounts().isEmpty()) { - int highestPermissionAmount = -1; - for (Map.Entry entry : group.getPermissionAmounts().entrySet()) { - if (player.hasPermission(entry.getKey()) || player.isOp()) { - highestPermissionAmount = Math.max(highestPermissionAmount, entry.getValue()); - } else { - return group.getDefaultAmount(); - } - } - return highestPermissionAmount; - } - return group.getDefaultAmount(); - } - } - return -1; - } - public void loadConfig() { ConfigurationSection configurationSection = instance.getConfig().getConfigurationSection("block-limits"); + if (configurationSection == null) { + throw new IllegalStateException("No configuration for groups is available."); + } for (String key : configurationSection.getKeys(false)) { - groups.add(new Group(configurationSection.getConfigurationSection(key))); + BlockLimiter.getInstance().getGroups().add(new Group(configurationSection.getConfigurationSection(key))); } } } diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java index 2da5ab0..09b5c96 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java @@ -1,7 +1,9 @@ package dev.j3fftw.headlimiter.blocklimiter; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -9,9 +11,11 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.entity.Player; import com.google.common.base.Preconditions; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.libraries.dough.blocks.ChunkPosition; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; @@ -22,6 +26,7 @@ public final class BlockLimiter { private static BlockLimiter instance; + private final HashSet groups = new HashSet<>(); private final Map contentMap = new HashMap<>(); public BlockLimiter(@Nonnull HeadLimiter headLimiter) { @@ -60,6 +65,37 @@ public ChunkContent getChunkContent(@Nonnull ChunkPosition chunkPosition) { return contentMap.get(chunkPosition); } + public Group getGroupByItem(@Nonnull SlimefunItem slimefunItem) { + return getGroupByItem(slimefunItem.getId()); + } + + @Nullable + public Group getGroupByItem(@Nonnull String itemId) { + for (Group group : this.groups) { + if (group.contains(itemId)) { + return group; + } + } + return null; + } + + public int getPlayerLimitByItem(@Nonnull Player player, @Nonnull SlimefunItem slimefunItem) { + return getPlayerLimitByItem(player, slimefunItem.getId()); + } + + public int getPlayerLimitByItem(@Nonnull Player player, @Nonnull String itemId) { + Group group = getGroupByItem(itemId); + if (group == null) { + return -1; + } else { + return group.getPermissibleAmount(player); + } + } + + public Set getGroups() { + return groups; + } + public void setChunkContent(@Nonnull ChunkPosition chunkPosition, @Nonnull ChunkContent content) { contentMap.put(chunkPosition, content); } diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java index 2ef530e..698220f 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java @@ -23,7 +23,7 @@ public BlockListener(@Nonnull HeadLimiter headLimiter) { public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { SlimefunItem slimefunItem = event.getSlimefunItem(); String slimefunItemId = slimefunItem.getId(); - int definedLimit = HeadLimiter.getInstance().getLimitForItem(slimefunItemId, event.getPlayer()); + int definedLimit = BlockLimiter.getInstance().getPlayerLimitByItem(event.getPlayer(), slimefunItem); if (definedLimit == -1) { // No limit has been set, nothing required for HeadLimiter @@ -38,7 +38,7 @@ public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { content = new ChunkContent(); content.incrementAmount(slimefunItemId); BlockLimiter.getInstance().setChunkContent(chunkPosition, content); - } else if (content.getCurrentAmount(slimefunItemId) < definedLimit) { + } else if (content.getGroupTotal(slimefunItemId) < definedLimit) { // This chunk can take more of the specified item type content.incrementAmount(slimefunItemId); } else { @@ -52,7 +52,7 @@ public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { public void onSlimefunItemBroken(@Nonnull SlimefunBlockBreakEvent event) { SlimefunItem slimefunItem = event.getSlimefunItem(); String slimefunItemId = slimefunItem.getId(); - int definedLimit = HeadLimiter.getInstance().getLimitForItem(slimefunItemId, event.getPlayer()); + int definedLimit = BlockLimiter.getInstance().getPlayerLimitByItem(event.getPlayer(), slimefunItem); if (definedLimit == -1) { // No limit has been set, nothing required for HeadLimiter return; diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/ChunkContent.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/ChunkContent.java index 0a7f103..30adb45 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/ChunkContent.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/ChunkContent.java @@ -2,6 +2,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Set; import javax.annotation.Nonnull; @@ -19,6 +20,26 @@ public int getCurrentAmount(@Nonnull String itemId) { return this.contentMap.getOrDefault(itemId, 0); } + public int getGroupTotal(@Nonnull SlimefunItem slimefunItem) { + return getGroupTotal(slimefunItem.getId()); + } + + public int getGroupTotal(@Nonnull String itemId) { + Set groupSet = BlockLimiter.getInstance().getGroups(); + + for (Group group : groupSet) { + if (group.contains(itemId)) { + int amount = 0; + for (String item : group.getItems()) { + amount += this.contentMap.get(item); + } + return amount; + } + } + + return -1; + } + public void incrementAmount(@Nonnull SlimefunItem slimefunItem) { incrementAmount(slimefunItem.getId()); } diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java index c0ac7a5..fb12500 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/Group.java @@ -1,12 +1,19 @@ package dev.j3fftw.headlimiter.blocklimiter; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import javax.annotation.Nonnull; + +import com.google.common.base.Objects; + +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; + public class Group { private final String groupName; @@ -44,4 +51,48 @@ public Set getItems() { public Map getPermissionAmounts() { return permissionAmounts; } + + public boolean contains(@Nonnull SlimefunItem slimefunItem) { + return contains(slimefunItem.getId()); + } + + public boolean contains(@Nonnull String itemId) { + return this.items.contains(itemId); + } + + public int getPermissibleAmount(@Nonnull Player player) { + int allowable = defaultAmount; + if (!this.permissionAmounts.isEmpty()) { + for (Map.Entry entry : this.permissionAmounts.entrySet()) { + String permission = entry.getKey(); + if (player.hasPermission(permission)) { + allowable = Math.max(entry.getValue(), allowable); + } + } + } + return allowable; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Group group = (Group) o; + return defaultAmount == group.defaultAmount && Objects.equal( + groupName, + group.groupName + ) && Objects.equal(items, group.items) && Objects.equal( + permissionAmounts, + group.permissionAmounts + ); + } + + @Override + public int hashCode() { + return Objects.hashCode(groupName, defaultAmount, items, permissionAmounts); + } } From 42aba9f7c0193a10cde7dd8d9c212acd9982582d Mon Sep 17 00:00:00 2001 From: ybw0014 Date: Tue, 1 Aug 2023 13:22:27 -0400 Subject: [PATCH 14/19] chore: follow up changes --- .../java/dev/j3fftw/headlimiter/CountCommand.java | 6 +++--- src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java | 11 ++++++++++- src/main/resources/config.yml | 3 ++- src/main/resources/plugin.yml | 3 ++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/dev/j3fftw/headlimiter/CountCommand.java b/src/main/java/dev/j3fftw/headlimiter/CountCommand.java index 70398ed..5d19c3b 100644 --- a/src/main/java/dev/j3fftw/headlimiter/CountCommand.java +++ b/src/main/java/dev/j3fftw/headlimiter/CountCommand.java @@ -27,7 +27,7 @@ public boolean onCommand(CommandSender sender, Command command, String s, String } message.append(ChatColor.GOLD) - .append("当前数量: ") + .append("当前数量:") .append(result.getTotal()) .append("/") .append(Utils.getMaxHeads(player)) @@ -38,7 +38,7 @@ public boolean onCommand(CommandSender sender, Command command, String s, String message.append(" ") .append(ChatColor.GRAY) .append(entry.getKey()) - .append(": ") + .append(":") .append(ChatColor.YELLOW) .append(entry.getValue()) .append('\n'); @@ -48,7 +48,7 @@ public boolean onCommand(CommandSender sender, Command command, String s, String }); } else { sender.sendMessage(ChatColor.GOLD + "/hl count" - + ChatColor.GRAY + " - 统计该区块中有多少个头颅" + + ChatColor.GRAY + " - 统计该区块中有多少个方块" ); } diff --git a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java index 0d55b06..e711d5f 100644 --- a/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java @@ -1,8 +1,10 @@ package dev.j3fftw.headlimiter; import java.io.File; +import java.util.logging.Level; import dev.j3fftw.headlimiter.blocklimiter.Group; +import net.guizhanss.guizhanlibplugin.updater.GuizhanUpdater; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.configuration.ConfigurationSection; @@ -15,7 +17,6 @@ import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; -import io.github.thebusybiscuit.slimefun4.libraries.dough.updater.GitHubBuildsUpdater; import dev.j3fftw.headlimiter.blocklimiter.BlockLimiter; @@ -28,6 +29,14 @@ public final class HeadLimiter extends JavaPlugin implements Listener { @Override public void onEnable() { instance = this; + + if (!getServer().getPluginManager().isPluginEnabled("GuizhanLibPlugin")) { + getLogger().log(Level.SEVERE, "本插件需要 鬼斩前置库插件(GuizhanLibPlugin) 才能运行!"); + getLogger().log(Level.SEVERE, "从此处下载: https://50L.cc/gzlib"); + getServer().getPluginManager().disablePlugin(this); + return; + } + if (!new File(getDataFolder(), "config.yml").exists()) { saveDefaultConfig(); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index f013252..5afb64a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,7 +7,8 @@ ## items-amount:每个区块内最多可以放置多少方块。(组内的所有方块叠加起来的上限) ## items:方块的粘液物品ID列表(支持附属)。 ## permission-amount:你可以在此增加权限节点,以设置不同的数量限制。 -## 如果你不需要这一项,你可以删除。 +## 如果你不需要这一项,可以删除。 +## 对应的权限节点为:headlimiter.permission.<权限> ## cargo: ## items-amount: 25 ## items: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a3c4862..eeeccd6 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,8 @@ name: HeadLimiter version: ${project.version} main: dev.j3fftw.headlimiter.HeadLimiter -depend: [Slimefun, GuizhanLibPlugin] +depend: [Slimefun] +softdepend: [GuizhanLibPlugin] api-version: 1.16 commands: From 687de17a0761ba20f573f8b363f63b19f97e2edb Mon Sep 17 00:00:00 2001 From: ybw0014 Date: Fri, 4 Aug 2023 21:03:43 -0400 Subject: [PATCH 15/19] chore: use new db controller --- .gitignore | 3 +- pom.xml | 7 +-- .../blocklimiter/BlockLimiter.java | 45 ++++++++++--------- .../blocklimiter/BlockListener.java | 2 +- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index c1e3877..de031b0 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ *.sh dependency-reduced-pom.xml /dependency-reduced-pom.xml -*.DS_Store \ No newline at end of file +*.DS_Store +/local/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index 6dfc5b1..fb191ad 100644 --- a/pom.xml +++ b/pom.xml @@ -107,10 +107,11 @@ - com.github.Slimefun + com.github.StarWishsama Slimefun4 - RC-35 - provided + 230804-4c8bd2b-Insider + system + ${project.basedir}/local/Slimefun-230804-4c8bd2b-Insider.jar diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java index 09b5c96..57437cb 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java @@ -8,8 +8,11 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import com.xzavier0722.mc.plugin.slimefun4.storage.callback.IAsyncReadCallback; +import com.xzavier0722.mc.plugin.slimefun4.storage.controller.BlockDataController; +import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunBlockData; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; @@ -18,9 +21,6 @@ import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.libraries.dough.blocks.ChunkPosition; -import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; -import me.mrCookieSlime.Slimefun.api.BlockStorage; - import dev.j3fftw.headlimiter.HeadLimiter; public final class BlockLimiter { @@ -37,26 +37,29 @@ public BlockLimiter(@Nonnull HeadLimiter headLimiter) { } private void loadBlockStorage() { + BlockDataController controller = Slimefun.getDatabaseManager().getBlockDataController(); for (World world : Bukkit.getWorlds()) { - BlockStorage worldStorage = BlockStorage.getStorage(world); - if (worldStorage == null) { - return; - } else { - for (Map.Entry entry : worldStorage.getRawStorage().entrySet()) { - Location location = entry.getKey(); - String id = entry.getValue().getString("id"); - ChunkPosition chunkPosition = new ChunkPosition(location.getChunk()); - ChunkContent content = contentMap.get(chunkPosition); - if (content == null) { - content = new ChunkContent(); - content.incrementAmount(id); - contentMap.put(chunkPosition, content); - } else { - content.incrementAmount(id); - } + controller.getAllBlockDataAsync(world, new IAsyncReadCallback<>() { + @Override + public void onResult(Set result) { + loadWorldStorage(result); } - } + }); + } + } + private void loadWorldStorage(Set blockDataSet) { + for (SlimefunBlockData blockData : blockDataSet) { + String id = blockData.getSfId(); + ChunkPosition chunkPosition = new ChunkPosition(blockData.getLocation().getChunk()); + ChunkContent content = contentMap.get(chunkPosition); + if (content == null) { + content = new ChunkContent(); + content.incrementAmount(id); + contentMap.put(chunkPosition, content); + } else { + content.incrementAmount(id); + } } } diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java index 698220f..b34291c 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java @@ -44,7 +44,7 @@ public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { } else { // Chunk has hit its limit for this type, time to deny the placement event.setCancelled(true); - event.getPlayer().sendMessage(ChatColor.RED + "You cannot place any more of this item within this chunk."); + event.getPlayer().sendMessage(ChatColor.RED + "你不能在该区块中放置更多。"); } } From c6e45500e9a5ec10a02561becd355ce9d8d346a2 Mon Sep 17 00:00:00 2001 From: ybw0014 Date: Tue, 8 Aug 2023 12:39:37 -0400 Subject: [PATCH 16/19] chore(pom): use private repo --- pom.xml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index fb191ad..f6f10a7 100644 --- a/pom.xml +++ b/pom.xml @@ -96,6 +96,13 @@ bstats-repo https://repo.codemc.org/repository/maven-public + + github + https://maven.pkg.github.com/StarWishsama/Slimefun-Secret + + true + + @@ -107,11 +114,10 @@ - com.github.StarWishsama - Slimefun4 - 230804-4c8bd2b-Insider - system - ${project.basedir}/local/Slimefun-230804-4c8bd2b-Insider.jar + com.github.slimefun + slimefun + 230805-4d8c3d8-Insider + provided From e28887309580487f0b7810c898fc669dae2a339b Mon Sep 17 00:00:00 2001 From: ybw0014 Date: Thu, 10 Aug 2023 11:42:26 -0400 Subject: [PATCH 17/19] fix: use chunk load event --- pom.xml | 2 +- .../blocklimiter/BlockLimiter.java | 28 ------------------- .../blocklimiter/BlockListener.java | 22 +++++++++++++++ 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index f6f10a7..9af48d2 100644 --- a/pom.xml +++ b/pom.xml @@ -116,7 +116,7 @@ com.github.slimefun slimefun - 230805-4d8c3d8-Insider + 230809-4ea6c8b-Insider provided diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java index 57437cb..fce5a90 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java @@ -33,34 +33,6 @@ public BlockLimiter(@Nonnull HeadLimiter headLimiter) { Preconditions.checkArgument(instance == null, "Cannot create a new instance of the BlockLimiter"); instance = this; new BlockListener(headLimiter); - headLimiter.getServer().getScheduler().runTaskLater(headLimiter, this::loadBlockStorage, 1); - } - - private void loadBlockStorage() { - BlockDataController controller = Slimefun.getDatabaseManager().getBlockDataController(); - for (World world : Bukkit.getWorlds()) { - controller.getAllBlockDataAsync(world, new IAsyncReadCallback<>() { - @Override - public void onResult(Set result) { - loadWorldStorage(result); - } - }); - } - } - - private void loadWorldStorage(Set blockDataSet) { - for (SlimefunBlockData blockData : blockDataSet) { - String id = blockData.getSfId(); - ChunkPosition chunkPosition = new ChunkPosition(blockData.getLocation().getChunk()); - ChunkContent content = contentMap.get(chunkPosition); - if (content == null) { - content = new ChunkContent(); - content.incrementAmount(id); - contentMap.put(chunkPosition, content); - } else { - content.incrementAmount(id); - } - } } @Nullable diff --git a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java index b34291c..4b8cb21 100644 --- a/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java +++ b/src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java @@ -2,7 +2,11 @@ import javax.annotation.Nonnull; +import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunBlockData; +import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunChunkData; +import com.xzavier0722.mc.plugin.slimefun4.storage.event.SlimefunChunkDataLoadEvent; import org.bukkit.ChatColor; +import org.bukkit.Chunk; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -19,6 +23,24 @@ public BlockListener(@Nonnull HeadLimiter headLimiter) { headLimiter.getServer().getPluginManager().registerEvents(this, headLimiter); } + @EventHandler + public void onSlimefunChunkLoad(@Nonnull SlimefunChunkDataLoadEvent event) { + BlockLimiter blockLimiter = HeadLimiter.getInstance().getBlockLimiter(); + ChunkPosition chunkPos = new ChunkPosition(event.getChunk()); + + for (SlimefunBlockData blockData : event.getChunkData().getAllBlockData()) { + String id = blockData.getSfId(); + ChunkContent content = blockLimiter.getChunkContent(chunkPos); + if (content == null) { + content = new ChunkContent(); + content.incrementAmount(id); + blockLimiter.setChunkContent(chunkPos, content); + } else { + content.incrementAmount(id); + } + } + } + @EventHandler public void onSlimefunItemPlaced(@Nonnull SlimefunBlockPlaceEvent event) { SlimefunItem slimefunItem = event.getSlimefunItem(); From 2a60594e914ebd81f1f6eeb2fa2b43dd6ab3d910 Mon Sep 17 00:00:00 2001 From: ybw0014 Date: Fri, 11 Aug 2023 11:43:34 -0400 Subject: [PATCH 18/19] fix(pom): use slimefun beta --- pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 9af48d2..0e6782d 100644 --- a/pom.xml +++ b/pom.xml @@ -114,9 +114,9 @@ - com.github.slimefun - slimefun - 230809-4ea6c8b-Insider + com.github.StarWishsama + Slimefun4 + eb20e5911f provided From 8fd181a630fabb39283c1bb5a58f7e2221628901 Mon Sep 17 00:00:00 2001 From: ybw0014 Date: Fri, 11 Aug 2023 11:44:04 -0400 Subject: [PATCH 19/19] fix(pom): remove private repo --- pom.xml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pom.xml b/pom.xml index 0e6782d..d8ce874 100644 --- a/pom.xml +++ b/pom.xml @@ -96,13 +96,6 @@ bstats-repo https://repo.codemc.org/repository/maven-public - - github - https://maven.pkg.github.com/StarWishsama/Slimefun-Secret - - true - -