Skip to content

Commit

Permalink
Allow ignoring enchantments, gamemodes and worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
RealTriassic committed Aug 12, 2024
1 parent d57e563 commit 1f216d4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.stupidcraft.randomcart.listeners;

import org.stupidcraft.randomcart.RandomCart;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
Expand All @@ -10,13 +9,15 @@
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.StorageMinecart;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.stupidcraft.randomcart.RandomCart;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -29,24 +30,46 @@ public class BlockBreakListener implements Listener {
private final Random random;
private final Configuration config;
private final List<Material> allowedBlocks;
private final int summonChance;
private final double summonChance;
private final List<String> disabledWorlds;
private final List<String> disabledGamemodes;
private final List<Enchantment> disabledEnchantments = new ArrayList<>();

public BlockBreakListener(Configuration config, Logger logger, Random random) {
this.config = config;
this.logger = logger;
this.random = random;
this.allowedBlocks = RandomCart.loadAllowedBlocks(config, logger);
this.summonChance = config.getInt("randomcart.chance");
this.summonChance = config.getDouble("randomcart.chance");
this.disabledWorlds = config.getStringList("randomcart.disabled-worlds");
this.disabledGamemodes = config.getStringList("randomcart.disabled-gamemodes");

for (String ench : config.getStringList("randomcart.disabled-enchantments")) {
this.disabledEnchantments.add(Enchantment.getByKey(NamespacedKey.minecraft(ench.toLowerCase())));
}
}

@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
Block block = event.getBlock();
if (!allowedBlocks.contains(block.getType()) || event.isCancelled()) {
Player player = event.getPlayer();
ItemStack itemInHand = player.getInventory().getItemInMainHand();

if (disabledWorlds.contains(player.getWorld().getName()) || disabledGamemodes.contains(player.getGameMode().name()) || !allowedBlocks.contains(block.getType()) || event.isCancelled()) {
return;
}

if (random.nextDouble() * 100 >= summonChance) {
// Check for disabled enchantments
if (itemInHand.getType() != Material.AIR) {
boolean hasDisabledEnchantments = itemInHand.getEnchantments().keySet().stream()
.anyMatch(disabledEnchantments::contains);

if (hasDisabledEnchantments) {
return;
}
}

if (random.nextDouble() >= summonChance) {
return;
}

Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@


randomcart:
chance: 0.5 # The chance that a RandomCart spawns.
chance: 0.01 # The chance that a RandomCart spawns. Default value is 0.01, meaning 1% chance.
# Blocks that have the ability to spawn RandomCarts.
allowed-blocks:
- STONE
- DIRT
- GRASS_BLOCK
# If player is in any of these worlds, do not summon.
disabled-worlds:
- disabled_world
# If player is in any of these gamemodes, do not summon.
disabled-gamemodes:
- CREATIVE
- SPECTATOR
# If tool that player used has any of these enchants, do not summon.
disabled-enchantments:
- SILK_TOUCH

minecarts:
# miner-cart: # Identifier used internally by the plugin.
Expand Down

0 comments on commit 1f216d4

Please sign in to comment.