Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dragoncraft.T2Limiter;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- import net.md_5.bungee.api.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.block.Block;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.ConfigurationSection;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.block.BlockPlaceEvent;
- import org.bukkit.plugin.Plugin;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Main extends JavaPlugin implements Listener, CommandExecutor {
- private FileConfiguration playerDataConfig;
- private File playerDataFile;
- private Map<String, Map<String, Integer>> limiters;
- private Map<Location, UUID> placedBlocks;
- public void onEnable() {
- this.limiters = new HashMap<>();
- this.placedBlocks = new HashMap<>();
- loadConfig();
- loadPlayerData();
- getServer().getPluginManager().registerEvents(this, (Plugin)this);
- getCommand("t2limiter").setExecutor(this);
- }
- private void loadConfig() {
- saveDefaultConfig();
- FileConfiguration config = getConfig();
- ConfigurationSection limitersSection = config.getConfigurationSection("limiters");
- if (limitersSection != null)
- for (String limiter : limitersSection.getKeys(false)) {
- Map<String, Integer> limiterLimits = new HashMap<>();
- ConfigurationSection limitsSection = limitersSection.getConfigurationSection(limiter);
- if (limitsSection != null)
- for (String materialString : limitsSection.getKeys(false)) {
- int limit = limitsSection.getInt(materialString);
- limiterLimits.put(materialString, Integer.valueOf(limit));
- }
- int weight = limitsSection.getInt("weight", 0);
- limiterLimits.put("weight", Integer.valueOf(weight));
- this.limiters.put(limiter, limiterLimits);
- }
- }
- private void loadPlayerData() {
- this.playerDataFile = new File(getDataFolder(), "player_data.yml");
- if (!this.playerDataFile.exists())
- saveResource("player_data.yml", false);
- this.playerDataConfig = (FileConfiguration)YamlConfiguration.loadConfiguration(this.playerDataFile);
- }
- private void savePlayerData() {
- try {
- this.playerDataConfig.save(this.playerDataFile);
- } catch (IOException e) {
- getLogger().severe("Failed to save player data: " + e.getMessage());
- }
- }
- @EventHandler
- public void onBlockPlace(BlockPlaceEvent event) {
- Block placedBlock = event.getBlockPlaced();
- String placedMaterial = placedBlock.getType().name();
- UUID playerId = event.getPlayer().getUniqueId();
- int currentCount = getPlayerBlockCount(playerId, placedMaterial);
- Integer limit = getLimiterLimit(event.getPlayer(), placedMaterial);
- if (limit != null)
- if (currentCount >= limit.intValue()) {
- event.setCancelled(true);
- event.getPlayer().sendMessage(ChatColor.RED + "You can't place any more of this item!");
- } else {
- setPlayerBlockCount(playerId, placedMaterial, currentCount + 1);
- setPlayerBlockCount(playerId, "total", getPlayerBlockCount(playerId, "total") + 1);
- this.placedBlocks.put(placedBlock.getLocation(), playerId);
- }
- savePlayerData();
- }
- @EventHandler
- public void onBlockBreak(BlockBreakEvent event) {
- Block brokenBlock = event.getBlock();
- String brokenMaterial = brokenBlock.getType().name();
- UUID playerId = this.placedBlocks.get(brokenBlock.getLocation());
- if (playerId != null) {
- int currentCount = getPlayerBlockCount(playerId, brokenMaterial);
- Integer limit = getLimiterLimit(event.getPlayer(), brokenMaterial);
- if (limit != null &&
- currentCount > 0) {
- setPlayerBlockCount(playerId, brokenMaterial, currentCount - 1);
- setPlayerBlockCount(playerId, "total", getPlayerBlockCount(playerId, "total") - 1);
- }
- this.placedBlocks.remove(brokenBlock.getLocation());
- }
- savePlayerData();
- }
- private int getPlayerBlockCount(UUID playerId, String material) {
- ConfigurationSection playerSection = this.playerDataConfig.getConfigurationSection(playerId.toString());
- if (playerSection != null)
- return playerSection.getInt(material, 0);
- return 0;
- }
- private void setPlayerBlockCount(UUID playerId, String material, int count) {
- ConfigurationSection playerSection = this.playerDataConfig.getConfigurationSection(playerId.toString());
- if (playerSection == null)
- playerSection = this.playerDataConfig.createSection(playerId.toString());
- playerSection.set(material, Integer.valueOf(count));
- }
- private Integer getLimiterLimit(Player player, String material) {
- int highestWeight = -1;
- Integer highestLimit = null;
- for (String limiter : this.limiters.keySet()) {
- Map<String, Integer> limiterLimits = this.limiters.get(limiter);
- if (limiterLimits.containsKey(material)) {
- int weight = ((Integer)limiterLimits.getOrDefault("weight", Integer.valueOf(0))).intValue();
- boolean hasPermission = player.hasPermission("tekkit2limiter.limiter." + limiter.toLowerCase());
- if (hasPermission && weight > highestWeight) {
- highestWeight = weight;
- highestLimit = limiterLimits.get(material);
- }
- }
- }
- if (this.limiters.containsKey("default") && ((Map)this.limiters.get("default")).containsKey(material)) {
- Integer defaultLimit = (Integer)((Map)this.limiters.get("default")).get(material);
- if (defaultLimit != null && (highestLimit == null || defaultLimit.intValue() > highestLimit.intValue()))
- highestLimit = defaultLimit;
- }
- return highestLimit;
- }
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if (cmd.getName().equalsIgnoreCase("t2limiter")) {
- if (args.length == 3 && args[0].equalsIgnoreCase("reset") && sender.hasPermission("t2limiter.reset")) {
- String playerName = args[1];
- Player player = getServer().getPlayer(playerName);
- if (player == null) {
- sender.sendMessage(ChatColor.RED + "Player not found or not online.");
- return true;
- }
- String materialName = args[2];
- UUID playerId = player.getUniqueId();
- setPlayerBlockCount(playerId, materialName, 0);
- sender.sendMessage(ChatColor.GREEN + "Block limit reset successfully for player: " + playerName);
- savePlayerData();
- return true;
- }
- if (args.length == 3 && args[0].equalsIgnoreCase("remove") && sender.hasPermission("t2limiter.remove")) {
- String playerName = args[1];
- Player player = getServer().getPlayer(playerName);
- if (player == null) {
- sender.sendMessage(ChatColor.RED + "Player not found or not online.");
- return true;
- }
- String materialName = args[2];
- UUID playerId = player.getUniqueId();
- int currentCount = getPlayerBlockCount(playerId, materialName);
- if (currentCount > 0) {
- setPlayerBlockCount(playerId, materialName, currentCount - 1);
- int totalLimit = getPlayerBlockCount(playerId, "total");
- if (totalLimit > 0)
- setPlayerBlockCount(playerId, "total", totalLimit - 1);
- sender.sendMessage(ChatColor.GREEN + "Block limit removed successfully for player: " + playerName);
- savePlayerData();
- } else {
- sender.sendMessage(ChatColor.RED + "Player does not have any blocks of that type to remove.");
- }
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement