Advertisement
Grexxity

T2Limiter

Jun 28th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.96 KB | None | 0 0
  1. package com.dragoncraft.T2Limiter;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.UUID;
  8. import net.md_5.bungee.api.ChatColor;
  9. import org.bukkit.Location;
  10. import org.bukkit.block.Block;
  11. import org.bukkit.command.Command;
  12. import org.bukkit.command.CommandExecutor;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.configuration.ConfigurationSection;
  15. import org.bukkit.configuration.file.FileConfiguration;
  16. import org.bukkit.configuration.file.YamlConfiguration;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.event.EventHandler;
  19. import org.bukkit.event.Listener;
  20. import org.bukkit.event.block.BlockBreakEvent;
  21. import org.bukkit.event.block.BlockPlaceEvent;
  22. import org.bukkit.plugin.Plugin;
  23. import org.bukkit.plugin.java.JavaPlugin;
  24.  
  25. public class Main extends JavaPlugin implements Listener, CommandExecutor {
  26.   private FileConfiguration playerDataConfig;
  27.  
  28.   private File playerDataFile;
  29.  
  30.   private Map<String, Map<String, Integer>> limiters;
  31.  
  32.   private Map<Location, UUID> placedBlocks;
  33.  
  34.   public void onEnable() {
  35.     this.limiters = new HashMap<>();
  36.     this.placedBlocks = new HashMap<>();
  37.     loadConfig();
  38.     loadPlayerData();
  39.     getServer().getPluginManager().registerEvents(this, (Plugin)this);
  40.     getCommand("t2limiter").setExecutor(this);
  41.   }
  42.  
  43.   private void loadConfig() {
  44.     saveDefaultConfig();
  45.     FileConfiguration config = getConfig();
  46.     ConfigurationSection limitersSection = config.getConfigurationSection("limiters");
  47.     if (limitersSection != null)
  48.       for (String limiter : limitersSection.getKeys(false)) {
  49.         Map<String, Integer> limiterLimits = new HashMap<>();
  50.         ConfigurationSection limitsSection = limitersSection.getConfigurationSection(limiter);
  51.         if (limitsSection != null)
  52.           for (String materialString : limitsSection.getKeys(false)) {
  53.             int limit = limitsSection.getInt(materialString);
  54.             limiterLimits.put(materialString, Integer.valueOf(limit));
  55.           }  
  56.         int weight = limitsSection.getInt("weight", 0);
  57.         limiterLimits.put("weight", Integer.valueOf(weight));
  58.         this.limiters.put(limiter, limiterLimits);
  59.       }  
  60.   }
  61.  
  62.   private void loadPlayerData() {
  63.     this.playerDataFile = new File(getDataFolder(), "player_data.yml");
  64.     if (!this.playerDataFile.exists())
  65.       saveResource("player_data.yml", false);
  66.     this.playerDataConfig = (FileConfiguration)YamlConfiguration.loadConfiguration(this.playerDataFile);
  67.   }
  68.  
  69.   private void savePlayerData() {
  70.     try {
  71.       this.playerDataConfig.save(this.playerDataFile);
  72.     } catch (IOException e) {
  73.       getLogger().severe("Failed to save player data: " + e.getMessage());
  74.     }
  75.   }
  76.  
  77.   @EventHandler
  78.   public void onBlockPlace(BlockPlaceEvent event) {
  79.     Block placedBlock = event.getBlockPlaced();
  80.     String placedMaterial = placedBlock.getType().name();
  81.     UUID playerId = event.getPlayer().getUniqueId();
  82.     int currentCount = getPlayerBlockCount(playerId, placedMaterial);
  83.     Integer limit = getLimiterLimit(event.getPlayer(), placedMaterial);
  84.     if (limit != null)
  85.       if (currentCount >= limit.intValue()) {
  86.         event.setCancelled(true);
  87.         event.getPlayer().sendMessage(ChatColor.RED + "You can't place any more of this item!");
  88.       } else {
  89.         setPlayerBlockCount(playerId, placedMaterial, currentCount + 1);
  90.         setPlayerBlockCount(playerId, "total", getPlayerBlockCount(playerId, "total") + 1);
  91.         this.placedBlocks.put(placedBlock.getLocation(), playerId);
  92.       }  
  93.     savePlayerData();
  94.   }
  95.  
  96.   @EventHandler
  97.   public void onBlockBreak(BlockBreakEvent event) {
  98.     Block brokenBlock = event.getBlock();
  99.     String brokenMaterial = brokenBlock.getType().name();
  100.     UUID playerId = this.placedBlocks.get(brokenBlock.getLocation());
  101.     if (playerId != null) {
  102.       int currentCount = getPlayerBlockCount(playerId, brokenMaterial);
  103.       Integer limit = getLimiterLimit(event.getPlayer(), brokenMaterial);
  104.       if (limit != null &&
  105.         currentCount > 0) {
  106.         setPlayerBlockCount(playerId, brokenMaterial, currentCount - 1);
  107.         setPlayerBlockCount(playerId, "total", getPlayerBlockCount(playerId, "total") - 1);
  108.       }
  109.       this.placedBlocks.remove(brokenBlock.getLocation());
  110.     }
  111.     savePlayerData();
  112.   }
  113.  
  114.   private int getPlayerBlockCount(UUID playerId, String material) {
  115.     ConfigurationSection playerSection = this.playerDataConfig.getConfigurationSection(playerId.toString());
  116.     if (playerSection != null)
  117.       return playerSection.getInt(material, 0);
  118.     return 0;
  119.   }
  120.  
  121.   private void setPlayerBlockCount(UUID playerId, String material, int count) {
  122.     ConfigurationSection playerSection = this.playerDataConfig.getConfigurationSection(playerId.toString());
  123.     if (playerSection == null)
  124.       playerSection = this.playerDataConfig.createSection(playerId.toString());
  125.     playerSection.set(material, Integer.valueOf(count));
  126.   }
  127.  
  128.   private Integer getLimiterLimit(Player player, String material) {
  129.     int highestWeight = -1;
  130.     Integer highestLimit = null;
  131.     for (String limiter : this.limiters.keySet()) {
  132.       Map<String, Integer> limiterLimits = this.limiters.get(limiter);
  133.       if (limiterLimits.containsKey(material)) {
  134.         int weight = ((Integer)limiterLimits.getOrDefault("weight", Integer.valueOf(0))).intValue();
  135.         boolean hasPermission = player.hasPermission("tekkit2limiter.limiter." + limiter.toLowerCase());
  136.         if (hasPermission && weight > highestWeight) {
  137.           highestWeight = weight;
  138.           highestLimit = limiterLimits.get(material);
  139.         }
  140.       }
  141.     }
  142.     if (this.limiters.containsKey("default") && ((Map)this.limiters.get("default")).containsKey(material)) {
  143.       Integer defaultLimit = (Integer)((Map)this.limiters.get("default")).get(material);
  144.       if (defaultLimit != null && (highestLimit == null || defaultLimit.intValue() > highestLimit.intValue()))
  145.         highestLimit = defaultLimit;
  146.     }
  147.     return highestLimit;
  148.   }
  149.  
  150.   public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  151.     if (cmd.getName().equalsIgnoreCase("t2limiter")) {
  152.       if (args.length == 3 && args[0].equalsIgnoreCase("reset") && sender.hasPermission("t2limiter.reset")) {
  153.         String playerName = args[1];
  154.         Player player = getServer().getPlayer(playerName);
  155.         if (player == null) {
  156.           sender.sendMessage(ChatColor.RED + "Player not found or not online.");
  157.           return true;
  158.         }
  159.         String materialName = args[2];
  160.         UUID playerId = player.getUniqueId();
  161.         setPlayerBlockCount(playerId, materialName, 0);
  162.         sender.sendMessage(ChatColor.GREEN + "Block limit reset successfully for player: " + playerName);
  163.         savePlayerData();
  164.         return true;
  165.       }
  166.       if (args.length == 3 && args[0].equalsIgnoreCase("remove") && sender.hasPermission("t2limiter.remove")) {
  167.         String playerName = args[1];
  168.         Player player = getServer().getPlayer(playerName);
  169.         if (player == null) {
  170.           sender.sendMessage(ChatColor.RED + "Player not found or not online.");
  171.           return true;
  172.         }
  173.         String materialName = args[2];
  174.         UUID playerId = player.getUniqueId();
  175.         int currentCount = getPlayerBlockCount(playerId, materialName);
  176.         if (currentCount > 0) {
  177.           setPlayerBlockCount(playerId, materialName, currentCount - 1);
  178.           int totalLimit = getPlayerBlockCount(playerId, "total");
  179.           if (totalLimit > 0)
  180.             setPlayerBlockCount(playerId, "total", totalLimit - 1);
  181.           sender.sendMessage(ChatColor.GREEN + "Block limit removed successfully for player: " + playerName);
  182.           savePlayerData();
  183.         } else {
  184.           sender.sendMessage(ChatColor.RED + "Player does not have any blocks of that type to remove.");
  185.         }
  186.         return true;
  187.       }
  188.     }
  189.     return false;
  190.   }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement