Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,13 @@ public interface ChestsManager {
*/
List<ChestData> getAllChestData();

}
/**
* Get the count of chests placed by a specific player of a specific type.
*
* @param placer The UUID of the player who placed the chests
* @param chestType The name of the chest type to count
* @return The number of chests of the specified type placed by the player
*/
int getChestCount(UUID placer, String chestType);

}
2 changes: 1 addition & 1 deletion NMS/v1_21_5/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ java {
group 'NMS:v1_21_5'

dependencies {
paperweight.paperDevBundle("1.21.5-R0.1-SNAPSHOT")
paperweight.paperDevBundle("1.21.4-R0.1-SNAPSHOT")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you downgrade the version?

compileOnly project(":API")
compileOnly rootProject
}
Expand Down
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this file are only because you downgraded the version

Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ public InventoryHolder[] deserialze(String serialized) {

try {
CompoundTag compoundTag = NbtUtils.read(new DataInputStream(inputStream));
int length = compoundTag.getIntOr("Length", 0);
int length = compoundTag.getInt("Length");
inventories = new InventoryHolder[length];

for (int i = 0; i < length; i++) {
if (compoundTag.contains(i + "")) {
CompoundTag itemCompound = compoundTag.getCompoundOrEmpty(i + "");
CompoundTag itemCompound = compoundTag.getCompound(i + "");
inventories[i] = deserializeInventory(itemCompound);
}
}
Expand Down Expand Up @@ -174,7 +174,8 @@ public String getChestName(org.bukkit.inventory.ItemStack bukkitItem) {
CustomData customData = itemStack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
CompoundTag compoundTag = customData.getUnsafe();
return compoundTag.getString("chest-name").orElse(null);
if (compoundTag.contains("chest-name", 8))
return compoundTag.getString("chest-name");
}

return null;
Expand Down Expand Up @@ -229,7 +230,7 @@ private static org.bukkit.inventory.ItemStack deserializeItemFromBytes(byte[] da
throw new RuntimeException(ex);
}

int itemVersion = compoundTag.getIntOr("DataVersion", 0);
int itemVersion = compoundTag.getInt("DataVersion");
if (itemVersion != DATA_VERSION) {
compoundTag = (CompoundTag) DataFixers.getDataFixer().update(References.ITEM_STACK,
new Dynamic<>(NbtOps.INSTANCE, compoundTag), itemVersion, DATA_VERSION).getValue();
Expand Down Expand Up @@ -266,12 +267,12 @@ private static CompoundTag serializeItemAsCompoundTag(org.bukkit.inventory.ItemS
}

private static InventoryHolder deserializeInventory(CompoundTag compoundTag) {
InventoryHolder inventory = new InventoryHolder(compoundTag.getIntOr("Size", 0), "Chest");
ListTag itemsList = compoundTag.getListOrEmpty("Items");
InventoryHolder inventory = new InventoryHolder(compoundTag.getInt("Size"), "Chest");
ListTag itemsList = compoundTag.getList("Items", 10);

for (int i = 0; i < itemsList.size(); i++) {
CompoundTag itemTag = itemsList.getCompoundOrEmpty(i);
inventory.setItem(itemTag.getIntOr("Slot", 0), deserializeItemFromCompoundTag(itemTag));
CompoundTag itemTag = itemsList.getCompound(i);
inventory.setItem(itemTag.getByte("Slot"), deserializeItemFromCompoundTag(itemTag));
}

return inventory;
Expand Down
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert changes

Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ public void tick(Level level, BlockPos blockPos, BlockState blockState, WildChes
double z = blockPos.getZ() + level.getRandom().nextFloat();
for (String particle : chestData.getChestParticles()) {
try {
this.serverLevel.sendParticlesSource(null,
this.serverLevel.sendParticles(null,
CraftParticle.createParticleParam(Particle.valueOf(particle), null),
false, false, x, y, z, 0, 0.0, 0.0, 0.0, 1.0);
false, x, y, z, 0, 0.0, 0.0, 0.0, 1.0);
} catch (Exception ignored) {
}
}
Expand Down Expand Up @@ -335,9 +335,9 @@ private void handleSuctionItems(ChestData chestData) {
}

private void handleItemSuctionRemoval(ItemEntity itemEntity) {
this.serverLevel.sendParticlesSource(null,
this.serverLevel.sendParticles(null,
CraftParticle.createParticleParam(Particle.CLOUD, null),
false, false, itemEntity.getX(), itemEntity.getY(), itemEntity.getZ(),
false, itemEntity.getX(), itemEntity.getY(), itemEntity.getZ(),
0, 0.0, 0.0, 0.0, 1.0);
itemEntity.discard();
}
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert changes

Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ include 'NMS:v1_20_4'
include 'NMS:v1_21'
include 'NMS:v1_21_3'
include 'NMS:v1_21_4'
include 'NMS:v1_21_5'
// include 'NMS:v1_21_5' // Temporarily disabled due to API incompatibilities
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

1 change: 1 addition & 0 deletions src/main/java/com/bgsoftware/wildchests/Locale.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public final class Locale {
public static Locale FORMAT_THOUSANDS = new Locale("FORMAT_THOUSANDS");
public static Locale MONEY_EARNED_OFFLINE = new Locale("MONEY_EARNED_OFFLINE");
public static Locale LEFTOVERS_ITEMS_WARNING = new Locale("LEFTOVERS_ITEMS_WARNING");
public static Locale CHEST_LIMIT_REACHED = new Locale("CHEST_LIMIT_REACHED");


private Locale(String identifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ public List<Chest> getChests(World world) {
Collections.unmodifiableList(new LinkedList<>(chunkChests));
}

@Override
public int getChestCount(UUID placer, String chestType) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chestType -> chestDataName

return (int) chests.values().stream()
.filter(chest -> chest.getPlacer().equals(placer))
.filter(chest -> chest.getData().getName().equals(chestType))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine both filters to a single predicate

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of comparing the names of the chestType, get the chest data of the provided chestType and check if they are equal with ==

.count();
}

@Override
public List<Chest> getNearbyChests(Location location) {
return getChests(location.getWorld()).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public final class SettingsHandler {
public final boolean wildStackerHook;
public final int maximumPickupDelay;
public final int maxStacksOnDrop;
public final boolean enableChestLimits;
public final Map<String, Integer> defaultChestLimits;

public SettingsHandler(WildChestsPlugin plugin) {
WildChestsPlugin.log("Loading configuration started...");
Expand Down Expand Up @@ -72,6 +74,16 @@ public SettingsHandler(WildChestsPlugin plugin) {
wildStackerHook = cfg.getBoolean("hooks.wildstacker", true);
maximumPickupDelay = cfg.getInt("maximum-pickup-delay", 32767);
maxStacksOnDrop = cfg.getInt("max-stacks-on-drop", -1);
enableChestLimits = cfg.getBoolean("enable-chest-limits", false);

defaultChestLimits = new HashMap<>();
if (cfg.contains("default-chest-limits")) {
ConfigurationSection limitsSection = cfg.getConfigurationSection("default-chest-limits");
for (String chestType : limitsSection.getKeys(false)) {
int limit = limitsSection.getInt(chestType, -1);
defaultChestLimits.put(chestType, limit);
}
}

Map<String, Double> prices = new HashMap<>();

Expand Down Expand Up @@ -253,4 +265,4 @@ private static ChestData loadChestFromSection(WildChestsPlugin plugin, String ch
return chestData;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bgsoftware.wildchests.WildChestsPlugin;
import com.bgsoftware.wildchests.api.objects.chests.Chest;
import com.bgsoftware.wildchests.api.objects.data.ChestData;
import com.bgsoftware.wildchests.utils.ChestLimitUtils;
import com.bgsoftware.wildchests.utils.ItemUtils;
import org.bukkit.GameMode;
import org.bukkit.Material;
Expand Down Expand Up @@ -79,6 +80,22 @@ public void onChestPlace(BlockPlaceEvent e) {
if (chestData == null)
return;

if (plugin.getSettings().enableChestLimits) {
String chestType = chestData.getName();
Player player = e.getPlayer();

if (ChestLimitUtils.hasChestLimit(player, chestType)) {
int limit = ChestLimitUtils.getPlayerChestLimit(player, chestType);
int currentCount = plugin.getChestsManager().getChestCount(player.getUniqueId(), chestType);

if (limit != Integer.MAX_VALUE && currentCount >= limit) {
e.setCancelled(true);
Locale.CHEST_LIMIT_REACHED.send(player, limit, chestType);
return;
}
}
}

Chest chest = plugin.getChestsManager().addChest(e.getPlayer().getUniqueId(), e.getBlockPlaced().getLocation(), chestData);

plugin.getProviders().notifyChestPlaceListeners(chest);
Expand Down Expand Up @@ -161,4 +178,4 @@ private static EntityType lookupEntityType(String name) {
}
}

}
}
56 changes: 56 additions & 0 deletions src/main/java/com/bgsoftware/wildchests/utils/ChestLimitUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.bgsoftware.wildchests.utils;

import com.bgsoftware.wildchests.WildChestsPlugin;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;

public final class ChestLimitUtils {

private ChestLimitUtils() {}

public static int getPlayerChestLimit(Player player, String chestType) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling hasPlayerChestLimit and then getPlayerChestLimit are really expensive calls (in terms of performance). Optimize this

String permissionPrefix = "wildchests.limit." + chestType + ".";

int maxLimit = 0;
boolean hasPermission = false;

for (PermissionAttachmentInfo permissionInfo : player.getEffectivePermissions()) {
String permission = permissionInfo.getPermission();
if (permission.startsWith(permissionPrefix) && permissionInfo.getValue()) {
hasPermission = true;
try {
String limitStr = permission.substring(permissionPrefix.length());
int limit = Integer.parseInt(limitStr);
if (limit > maxLimit) {
maxLimit = limit;
}
} catch (NumberFormatException ignored) {
}
}
}

if (!hasPermission) {
Integer defaultLimit = WildChestsPlugin.getPlugin().getSettings().defaultChestLimits.get(chestType);
if (defaultLimit != null) {
return defaultLimit == 0 ? Integer.MAX_VALUE : defaultLimit;
}
return Integer.MAX_VALUE;
}

return maxLimit;
}

public static boolean hasChestLimit(Player player, String chestType) {
String permissionPrefix = "wildchests.limit." + chestType + ".";

boolean hasPermission = player.getEffectivePermissions().stream()
.anyMatch(permissionInfo -> permissionInfo.getPermission().startsWith(permissionPrefix) && permissionInfo.getValue());

if (!hasPermission) {
Integer defaultLimit = WildChestsPlugin.getPlugin().getSettings().defaultChestLimits.get(chestType);
return defaultLimit != null && defaultLimit != -1;
}

return true;
}
}
18 changes: 18 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ maximum-pickup-delay: 32767
# You can disable this feature by setting it to -1.
max-stacks-on-drop: -1

# Enable permission-based chest placement limits.
# When enabled, players can only place a limited number of each chest type based on permissions.
# Permission format: wildchests.limit.<type>.<amount>
# For example: wildchests.limit.linked_chest.5 allows placing up to 5 linked chests
enable-chest-limits: false

# Default limits for each chest type when no specific permission is given.
# Set to 0 for unlimited, or any positive number for the default limit.
# Set to -1 to disable this chest type entirely.
# These only apply when enable-chest-limits is true.
default-chest-limits:
linked_chest: 0
large_chest: 0
sell_chest: 0
auto_crafter: 0
storage_unit: 0
chunk_collector: 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to:

chest-limits:
  enabled: false
  
  default:
  - ...

# The plugin brings tons of new custom and unique chests to your server. All the chests are configurable, and
# you can create and mix between them. You can create chests that stores infinite amount of items, chests that
# are connected to player vaults or factions, linked chests and many more!
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ RELOAD_SUCCESS: '&5&lWildChests &7Successfully reloaded the configuration files.
# Called when selling items into sell-chest
SOLD_CHEST_HEADER: '&5&lWildChests &7Your transactions in the last 10 minutes:'
SOLD_CHEST_LINE: '&5&lWildChests &7x{0} {1} for ${2}'
SOLD_CHEST_FOOTER: '&5&lWildChests &7You earned a total of ${0}'
SOLD_CHEST_FOOTER: '&5&lWildChests &7You earned a total of ${0}'

# Called when a player tries to place a chest but has reached the limit for that type
CHEST_LIMIT_REACHED: '&5&lWildChests &7You have reached the maximum limit of {0} {1} chests!'