CachedPlayerHeads/src/main/java/org/warp/cachedplayerheads/Playerheads.java
Andrea Cavalli 9d1f489996 First commit
2021-04-19 20:53:45 +02:00

151 lines
4.6 KiB
Java

package org.warp.cachedplayerheads;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import net.skinsrestorer.api.SkinsRestorerAPI;
import net.skinsrestorer.bukkit.SkinsRestorer;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;
public final class Playerheads extends JavaPlugin {
private FileConfiguration config = getConfig();
static ConcurrentHashMap<String, String> PlayerBase64 = new ConcurrentHashMap<>();
static ConcurrentHashMap<String, String> HeadsPlaced = new ConcurrentHashMap<>();
// Setting definition
static SkinsRestorer skinsRestorer;
@Nullable
static SkinsRestorerAPI skinsRestorerAPI;
static MojangAPI mojangAPI;
static BungeeAPI bungeeAPI;
static FileConfiguration playerDataConfig;
static File playerDataFile;
private static File headBlockDataFile;
boolean dropPlayerOption;
boolean dropOtherDeathOption;
boolean givePermissionOption;
boolean lockdown;
public void onEnable() {
//Connecting to the main SkinsRestorer API
skinsRestorer = JavaPlugin.getPlugin(SkinsRestorer.class);
// Connecting to Bukkit API for applying the skin
skinsRestorerAPI = SkinsRestorerAPI.getApi();
mojangAPI = new MojangAPI();
mojangAPI.setSkinStorage(new BukkitSkinStorage());
bungeeAPI = new BungeeAPI(this);
PluginCommand cmd = getCommand("playerheads");
cmd.setExecutor(new CommandExecutor(this));
cmd.setTabCompleter(new TabCompleter());
new EventListener(this);
createPlayerData();
createHeadBlockData();
reloadHeadBlockDataToHashMap();
this.config.addDefault("Should player heads drop when a player is killed by another player?", Boolean.valueOf(true));
this.config.addDefault("Should player heads drop whenever a player gets killed? (excluding being killed by players)", Boolean.valueOf(false));
this.config.addDefault("Do players need the give permission to use /playerheads give command?)", Boolean.valueOf(true));
this.config.options().copyDefaults(true);
saveConfig();
this.dropPlayerOption = this.config.getBoolean("Should player heads drop when a player is killed by another player?");
this.dropOtherDeathOption = this.config.getBoolean("Should player heads drop whenever a player gets killed? (excluding being killed by players)");
this.givePermissionOption = this.config.getBoolean("Do players need the give permission to use /playerheads give command?)");
getLogger().info("Player Heads Plugin has been enabled");
}
public void onDisable() {
getLogger().info("Player Heads Plugin has been disabled");
}
private static synchronized void createPlayerData() {
try {
File folder = new File("plugins/CachedPlayerHeads");
if (!folder.exists())
folder.mkdirs();
playerDataFile = new File(folder, "PlayerData.yml");
if (!playerDataFile.exists())
playerDataFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
private static synchronized void createHeadBlockData() {
try {
File folder = new File("plugins/CachedPlayerHeads");
if (!folder.exists())
folder.mkdirs();
headBlockDataFile = new File(folder, "HeadBlockData.yml");
if (!headBlockDataFile.exists())
headBlockDataFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
static synchronized void refreshPlayerDatafile() {
playerDataFile.delete();
createPlayerData();
playerDataConfig = (FileConfiguration)YamlConfiguration.loadConfiguration(playerDataFile);
for (String s : PlayerBase64.keySet()) {
String gt = PlayerBase64.get(s);
playerDataConfig.set(s, gt);
}
try {
playerDataConfig.save(playerDataFile);
} catch (IOException e) {
e.printStackTrace();
}
}
static synchronized void refreshHeadBlockDataFile() {
headBlockDataFile.delete();
createPlayerData();
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(headBlockDataFile);
for (String s : HeadsPlaced.keySet()) {
String gt = HeadsPlaced.get(s);
yamlConfiguration.set(s, gt);
}
try {
yamlConfiguration.save(headBlockDataFile);
} catch (IOException e) {
e.printStackTrace();
}
}
private static synchronized void reloadHeadBlockDataToHashMap() {
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(headBlockDataFile);
for (String s : yamlConfiguration.getKeys(false)) {
String gt = yamlConfiguration.getString(s);
HeadsPlaced.put(s, gt);
}
try {
yamlConfiguration.save(headBlockDataFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}