Skip to content

Commit

Permalink
Merge pull request #16 from Slimefun-Addon-Community/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
J3fftw1 authored Aug 1, 2023
2 parents 0a02e2f + 9e9f502 commit 66d18f5
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
*.sh
dependency-reduced-pom.xml
/dependency-reduced-pom.xml
*.DS_Store
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<dependency>
<groupId>com.github.thebusybiscuit</groupId>
<artifactId>Slimefun4</artifactId>
<version>2c4f886fe4</version>
<version>RC-35</version>
<scope>provided</scope>
</dependency>

Expand Down
41 changes: 34 additions & 7 deletions src/main/java/dev/j3fftw/headlimiter/HeadLimiter.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
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 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;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
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();

Expand All @@ -35,6 +43,9 @@ 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);
loadConfig();
}

@Override
Expand Down Expand Up @@ -64,13 +75,29 @@ 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 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)) {
BlockLimiter.getInstance().getGroups().add(new Group(configurationSection.getConfigurationSection(key)));
}
}
}
107 changes: 107 additions & 0 deletions src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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;

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;
import me.mrCookieSlime.Slimefun.api.BlockStorage;

import dev.j3fftw.headlimiter.HeadLimiter;

public final class BlockLimiter {

private static BlockLimiter instance;
private final HashSet<Group> groups = new HashSet<>();
private final Map<ChunkPosition, ChunkContent> 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<Location, Config> 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);
}

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<Group> getGroups() {
return groups;
}

public void setChunkContent(@Nonnull ChunkPosition chunkPosition, @Nonnull ChunkContent content) {
contentMap.put(chunkPosition, content);
}

@Nonnull
public static BlockLimiter getInstance() {
return instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 slimefunItemId = slimefunItem.getId();
int definedLimit = BlockLimiter.getInstance().getPlayerLimitByItem(event.getPlayer(), slimefunItem);

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);

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(slimefunItemId);
BlockLimiter.getInstance().setChunkContent(chunkPosition, content);
} else if (content.getGroupTotal(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.");
}
}

@EventHandler
public void onSlimefunItemBroken(@Nonnull SlimefunBlockBreakEvent event) {
SlimefunItem slimefunItem = event.getSlimefunItem();
String slimefunItemId = slimefunItem.getId();
int definedLimit = BlockLimiter.getInstance().getPlayerLimitByItem(event.getPlayer(), slimefunItem);
if (definedLimit == -1) {
// 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;
}

// This chunk can take more of the specified item type
content.decrementAmount(slimefunItemId);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dev.j3fftw.headlimiter.blocklimiter;

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

import javax.annotation.Nonnull;

import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;

public class ChunkContent {

private final Map<String, Integer> 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 int getGroupTotal(@Nonnull SlimefunItem slimefunItem) {
return getGroupTotal(slimefunItem.getId());
}

public int getGroupTotal(@Nonnull String itemId) {
Set<Group> 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());
}

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);
}
}
Loading

0 comments on commit 66d18f5

Please sign in to comment.