MC服务器每日挖矿限制 DailyMineLimit

兰科勒布劳恩斯基
2025-01-16 / 0 评论 / 197 阅读 / 正在检测是否收录...

想必兰科的朋友们都知道在2个月前我开了一个MC服务器,那么只用市面上有的插件怎么行,有些功能还没实现,那么就有了这样一个插件——DailyMineLimit
为什么我要开发它?
当时在我的服务器,生存区能挖到钻石矿的概率比较高,我还额外做了一个矿场出来,矿率比生存区(主世界)低,但是许多玩家都在生存区挖矿,这个插件的目的就是一定程度限制在生存区的职业挖矿,可以灵活调整各种矿石挖掘的每日限额,来避免大型服务器玩家对生存区地形破坏和经济问题。
插件在哪里?
目前这个插件已经开源到兰科的Github页面,你可以进行二次修改和商用,如果要装在你的服务器上,请访问SpigotMC页面,这个插件只对1.9-1.12.2之间的版本有效,因为1.13后更改了方块监听逻辑
Github页面: https://github.com/xmlans/DailyMineLimit
SpigotMC页面: https://www.spigotmc.org/resources/dailyminelimit.121876/
插件源代码
注释为什么要加?方便二次修改,你可以把它汉化并重新打包。

package me.yourplugin.dailymine;

/**
 * DailyMineLimit
 * Limit Daily Mining
 * By Star Dream Studio
 * https://xmc.tw
 */

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DailyMineLimit extends JavaPlugin implements Listener {
    // Stores the mining counts for each player
    private Map<UUID, Map<Material, Integer>> playerMiningLimits = new HashMap<>();
    // Stores the last mining time for each player
    private Map<UUID, Long> playerLastMineTime = new HashMap<>();
    private FileConfiguration config;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        config = getConfig();
        // Register the event listener
        Bukkit.getPluginManager().registerEvents(this, this);
        getLogger().info("DailyMineLimit plugin has been enabled!");
    }

    @Override
    public void onDisable() {
        getLogger().info("DailyMineLimit plugin has been disabled!");
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        World world = player.getWorld();
        Material blockType = event.getBlock().getType();
        UUID playerId = player.getUniqueId();

        // Skip if the current world is not in the restricted worlds list
        if (!config.getStringList("restricted-worlds").contains(world.getName())) {
            return;
        }

        // Get the current time (in milliseconds)
        long currentTime = System.currentTimeMillis();
        long lastMineTime = playerLastMineTime.getOrDefault(playerId, 0L);

        // Check if 24 hours have passed, if so, reset the mining count
        if (currentTime - lastMineTime > 86400000L) { // 86400000 milliseconds = 24 hours
            playerMiningLimits.put(playerId, new HashMap<>());
        }

        // Get the mining limit for the current block type from the config
        int limit = config.getInt("limits." + blockType.name(), -1);
        if (limit == -1) {
            return; // If no limit is set for this block type, skip
        }

        // Get the player's mining record
        playerMiningLimits.putIfAbsent(playerId, new HashMap<>());
        Map<Material, Integer> minedBlocks = playerMiningLimits.get(playerId);
        int minedCount = minedBlocks.getOrDefault(blockType, 0);

        // If the mining count has reached or exceeded the limit, cancel the mining
        if (minedCount >= limit) {
            player.sendMessage("You have reached the daily mining limit for " + blockType.name() + "!");
            event.setCancelled(true);
        } else {
            // If the limit is not reached, increase the mining count for this block type
            minedBlocks.put(blockType, minedCount + 1);
        }

        // Update the last mining time for the player
        playerLastMineTime.put(playerId, currentTime);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        // Handle /dailymine reload command to reload the configuration
        if (command.getName().equalsIgnoreCase("dailymine")) {
            if (args.length > 0 && args[0].equalsIgnoreCase("reload")) {
                if (sender.hasPermission("dailymine.reload")) {
                    reloadConfig();
                    config = getConfig();
                    sender.sendMessage("DailyMineLimit configuration has been reloaded!");
                } else {
                    sender.sendMessage("You do not have permission to execute this command.");
                }
                return true;
            }
        }
        return false;
    }
}

唯一的插件命令

/dailymine reload //重新加载每日挖矿限制插件

你可以修改config.json文件来让这个插件适配你的服务器,改各种矿石的挖矿上限和应用该插件的世界。
兰科其实并不是写java的料,初三的时候认识过一个会写go语言的学姐,在开发人员里面我最佩服她了,但是java和go我都学不来,想着学python吧,唉!

0

评论 (0)

取消