Untitled

mail@pastecode.io avatar
unknown
java
3 years ago
3.2 kB
20
Indexable
Never
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.List;


public class AutoMiner {
    public static class Statusplayer extends BukkitRunnable {


        private static boolean isPlayerInRegion(Player player) {

            final Location location = player.getLocation();
            World world = location.getWorld();

            if (world == null) {
                return false;
            }
            final RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer();
            final RegionManager regionManager = regionContainer.get(BukkitAdapter.adapt(location.getWorld()));


            if (regionManager == null) {
                return false;
            }
            final List<String> regionList = (regionManager).getApplicableRegionsIDs(BlockVector3.at(location.getX(), location.getY(), location.getZ()));
            for (String region : regionList) {
                if (region.equals("Autominer")) return true;
            }
            return false;
        }
        Plugin plugin = Bukkit.getPluginManager().getPlugin("AutoMiner");
        public void run() {
           NamespacedKey autoMinerTime = new NamespacedKey(plugin, "autoMinerTime");
            for (Player player : Bukkit.getOnlinePlayers()) {
               Integer getTime = player.getPersistentDataContainer().get(autoMinerTime, PersistentDataType.INTEGER);
                if (isPlayerInRegion(player)) {
                    String playerName = player.getDisplayName();
                    int amount = plugin.getConfig().getInt("Amount");
                    PersistentDataContainer persistentDataContainer = player.getPersistentDataContainer();
                    if (persistentDataContainer.has(autoMinerTime, PersistentDataType.INTEGER)) {


                        if (getTime != null) {
                            if (getTime > 0) {
                                player.getPersistentDataContainer().set(autoMinerTime, PersistentDataType.INTEGER, getTime - 1);
                                ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
                                String command = "tokens add" + playerName + amount;
                                Bukkit.dispatchCommand(console, command);
                            }
                        }
                    } else {
                        player.getPersistentDataContainer().set(autoMinerTime, PersistentDataType.INTEGER, 0);
                    }
                }
            }


        }


    }
}