forked from Slimefun-Addon-Community/HeadLimiter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from SlimefunGuguProject/merge-upstream
- Loading branch information
Showing
10 changed files
with
435 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ | |
*.sh | ||
dependency-reduced-pom.xml | ||
/dependency-reduced-pom.xml | ||
*.DS_Store | ||
/local/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockLimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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 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.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 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); | ||
} | ||
|
||
@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; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/dev/j3fftw/headlimiter/blocklimiter/BlockListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package dev.j3fftw.headlimiter.blocklimiter; | ||
|
||
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; | ||
|
||
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 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(); | ||
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 + "你不能在该区块中放置更多。"); | ||
} | ||
} | ||
|
||
@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); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.