mirror of
https://github.com/songoda/EpicHeads.git
synced 2025-02-21 13:51:22 +01:00
Updated to 2.0.6 (not too much manual updates).
This commit is contained in:
parent
5f115ddd6c
commit
95db89bffa
@ -72,13 +72,9 @@ public class Heads extends JavaPlugin implements Listener {
|
|||||||
Bukkit.getPluginManager().disablePlugin(this);
|
Bukkit.getPluginManager().disablePlugin(this);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
instance = this;
|
instance = this;
|
||||||
|
|
||||||
Clock timer = Clock.start();
|
Clock timer = Clock.start();
|
||||||
|
|
||||||
loadCache();
|
loadCache();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
legacyIDs = LegacyIDs.readResource("legacy-ids.txt");
|
legacyIDs = LegacyIDs.readResource("legacy-ids.txt");
|
||||||
} catch (IOException exception) {
|
} catch (IOException exception) {
|
||||||
@ -86,37 +82,27 @@ public class Heads extends JavaPlugin implements Listener {
|
|||||||
severe("Unable to load legacy IDs to perform conversion from older Spigot versions");
|
severe("Unable to load legacy IDs to perform conversion from older Spigot versions");
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.menus = new Menus();
|
this.menus = new Menus();
|
||||||
this.menus.reload();
|
this.menus.reload();
|
||||||
|
|
||||||
this.oldMenuConfig = new MenuConfig(getVersionedConfig("menus.yml"));
|
this.oldMenuConfig = new MenuConfig(getVersionedConfig("menus.yml"));
|
||||||
this.langConfig = new LangConfig();
|
this.langConfig = new LangConfig();
|
||||||
this.mainConfig = new MainConfig();
|
this.mainConfig = new MainConfig();
|
||||||
this.economy = hookEconomy();
|
this.economy = hookEconomy();
|
||||||
|
|
||||||
ProtocolHackFixer.fix();
|
ProtocolHackFixer.fix();
|
||||||
|
|
||||||
registerCommands();
|
registerCommands();
|
||||||
tryHookBlockStore();
|
tryHookBlockStore();
|
||||||
|
|
||||||
new HeadNamer().registerEvents();
|
new HeadNamer().registerEvents();
|
||||||
|
|
||||||
Bukkit.getPluginManager().registerEvents(this, this);
|
Bukkit.getPluginManager().registerEvents(this, this);
|
||||||
|
|
||||||
CraftMetaItem.registerItems();
|
CraftMetaItem.registerItems();
|
||||||
|
|
||||||
if (mainConfig.shouldCheckForUpdates()) {
|
if (mainConfig.shouldCheckForUpdates()) {
|
||||||
checkForUpdates();
|
checkForUpdates();
|
||||||
}
|
}
|
||||||
|
|
||||||
info("Heads plugin enabled with " + cache.getHeadCount() + " heads " + timer);
|
info("Heads plugin enabled with " + cache.getHeadCount() + " heads " + timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
instance = null;
|
instance = null;
|
||||||
|
|
||||||
unregisterCommands();
|
unregisterCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,10 +111,8 @@ public class Heads extends JavaPlugin implements Listener {
|
|||||||
try {
|
try {
|
||||||
String currentVersion = UpdateChecker.getCurrentVersion();
|
String currentVersion = UpdateChecker.getCurrentVersion();
|
||||||
String latestVersion = UpdateChecker.getLatestVersion();
|
String latestVersion = UpdateChecker.getLatestVersion();
|
||||||
|
|
||||||
if (!UpdateChecker.isNewerVersion(latestVersion))
|
if (!UpdateChecker.isNewerVersion(latestVersion))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Learn how to use LangMessage - included next update.
|
// Learn how to use LangMessage - included next update.
|
||||||
warning("A newer version of Heads, Heads v" + latestVersion + ", is available for download");
|
warning("A newer version of Heads, Heads v" + latestVersion + ", is available for download");
|
||||||
warning("You are currently using Heads v" + currentVersion);
|
warning("You are currently using Heads v" + currentVersion);
|
||||||
@ -142,25 +126,19 @@ public class Heads extends JavaPlugin implements Listener {
|
|||||||
if (commandsRegistered) {
|
if (commandsRegistered) {
|
||||||
unregisterCommands();
|
unregisterCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleCommandMap commandMap = CraftServer.get().getCommandMap();
|
SimpleCommandMap commandMap = CraftServer.get().getCommandMap();
|
||||||
|
|
||||||
RuntimeCommand heads = new RuntimeCommand(mainConfig.getHeadCommand());
|
RuntimeCommand heads = new RuntimeCommand(mainConfig.getHeadCommand());
|
||||||
heads.setExecutor(new HeadsCommand());
|
heads.setExecutor(new HeadsCommand());
|
||||||
heads.setDescription(mainConfig.getHeadDescription());
|
heads.setDescription(mainConfig.getHeadDescription());
|
||||||
heads.setAliases(Arrays.asList(mainConfig.getHeadAliases()));
|
heads.setAliases(Arrays.asList(mainConfig.getHeadAliases()));
|
||||||
|
|
||||||
commandMap.register("heads", heads);
|
commandMap.register("heads", heads);
|
||||||
|
|
||||||
commandsRegistered = true;
|
commandsRegistered = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void unregisterCommands() {
|
private void unregisterCommands() {
|
||||||
SimpleCommandMap commandMap = CraftServer.get().getCommandMap();
|
SimpleCommandMap commandMap = CraftServer.get().getCommandMap();
|
||||||
Map<String, Command> map = CommandMap.getCommandMap(commandMap);
|
Map<String, Command> map = CommandMap.getCommandMap(commandMap);
|
||||||
|
|
||||||
map.values().removeIf(command -> command instanceof RuntimeCommand);
|
map.values().removeIf(command -> command instanceof RuntimeCommand);
|
||||||
|
|
||||||
commandsRegistered = false;
|
commandsRegistered = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,39 +147,29 @@ public class Heads extends JavaPlugin implements Listener {
|
|||||||
menus.reload();
|
menus.reload();
|
||||||
langConfig.reload();
|
langConfig.reload();
|
||||||
mainConfig.reload();
|
mainConfig.reload();
|
||||||
|
|
||||||
registerCommands();
|
registerCommands();
|
||||||
|
|
||||||
economy = hookEconomy();
|
economy = hookEconomy();
|
||||||
|
|
||||||
tryHookBlockStore();
|
tryHookBlockStore();
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getCacheFile() {
|
public File getCacheFile() {
|
||||||
if (!getDataFolder().exists() && !getDataFolder().mkdirs())
|
if (!getDataFolder().exists() && !getDataFolder().mkdirs())
|
||||||
throw new RuntimeException("Unable to create the data folder to save plugin files");
|
throw new RuntimeException("Unable to create the data folder to save plugin files");
|
||||||
|
|
||||||
if (!getDataFolder().isDirectory())
|
if (!getDataFolder().isDirectory())
|
||||||
throw new RuntimeException("plugins/Heads should be a directory, yet there is a file with the same name");
|
throw new RuntimeException("plugins/Heads should be a directory, yet there is a file with the same name");
|
||||||
|
|
||||||
return new File(getDataFolder(), "heads.cache");
|
return new File(getDataFolder(), "heads.cache");
|
||||||
}
|
}
|
||||||
|
|
||||||
private CacheFile loadCache() {
|
private CacheFile loadCache() {
|
||||||
File file = getCacheFile();
|
File file = getCacheFile();
|
||||||
FileConfigFile legacyConfig = new FileConfigFile("cache.yml");
|
FileConfigFile legacyConfig = new FileConfigFile("cache.yml");
|
||||||
|
|
||||||
boolean requiresWrite = false;
|
boolean requiresWrite = false;
|
||||||
|
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
requiresWrite = true;
|
requiresWrite = true;
|
||||||
|
|
||||||
if (legacyConfig.getFile().exists()) {
|
if (legacyConfig.getFile().exists()) {
|
||||||
Clock timer = Clock.start();
|
Clock timer = Clock.start();
|
||||||
|
|
||||||
LegacyCacheConfig legacy = new LegacyCacheConfig(legacyConfig);
|
LegacyCacheConfig legacy = new LegacyCacheConfig(legacyConfig);
|
||||||
cache = CacheFileConverter.convertToCacheFile("main-cache", legacy);
|
cache = CacheFileConverter.convertToCacheFile("main-cache", legacy);
|
||||||
|
|
||||||
info("Converted legacy yaml cache file to new binary file " + timer);
|
info("Converted legacy yaml cache file to new binary file " + timer);
|
||||||
} else {
|
} else {
|
||||||
cache = new CacheFile("main-cache");
|
cache = new CacheFile("main-cache");
|
||||||
@ -209,9 +177,7 @@ public class Heads extends JavaPlugin implements Listener {
|
|||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
Clock timer = Clock.start();
|
Clock timer = Clock.start();
|
||||||
|
|
||||||
cache = CacheFile.read(file);
|
cache = CacheFile.read(file);
|
||||||
|
|
||||||
info("Loaded cache file " + timer);
|
info("Loaded cache file " + timer);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
severe("Unable to read heads.cache file");
|
severe("Unable to read heads.cache file");
|
||||||
@ -232,7 +198,6 @@ public class Heads extends JavaPlugin implements Listener {
|
|||||||
|
|
||||||
public void saveCache() {
|
public void saveCache() {
|
||||||
File file = getCacheFile();
|
File file = getCacheFile();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Clock timer = Clock.start();
|
Clock timer = Clock.start();
|
||||||
|
|
||||||
|
459
src/net/sothatsit/heads/cache/CacheFile.java
vendored
459
src/net/sothatsit/heads/cache/CacheFile.java
vendored
@ -1,289 +1,304 @@
|
|||||||
package net.sothatsit.heads.cache;
|
package net.sothatsit.heads.cache;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
import net.sothatsit.heads.Heads;
|
import net.sothatsit.heads.Heads;
|
||||||
import net.sothatsit.heads.Search;
|
import net.sothatsit.heads.Search;
|
||||||
import net.sothatsit.heads.util.Checks;
|
import net.sothatsit.heads.util.Checks;
|
||||||
import net.sothatsit.heads.util.IOUtils;
|
import net.sothatsit.heads.util.IOUtils;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.zip.GZIPInputStream;
|
|
||||||
import java.util.zip.GZIPOutputStream;
|
|
||||||
|
|
||||||
public final class CacheFile implements Mod {
|
public final class CacheFile implements Mod {
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final Set<String> mods = new HashSet<>();
|
private final Set<String> mods = new HashSet<>();
|
||||||
private final List<CacheHead> heads = new ArrayList<>();
|
private final List<CacheHead> heads = new ArrayList<>();
|
||||||
private final Map<Integer, CacheHead> headsById = new HashMap<>();
|
private final Map<Integer, CacheHead> headsById = new HashMap<>();
|
||||||
private final Map<String, CacheHead> headsByTexture = new HashMap<>();
|
private final Map<String, CacheHead> headsByTexture = new HashMap<>();
|
||||||
private final Map<String, List<CacheHead>> categories = new HashMap<>();
|
private final Map<String, List<CacheHead>> categories = new HashMap<>();
|
||||||
|
|
||||||
public CacheFile(String name) {
|
public CacheFile(String name) {
|
||||||
this(name, Collections.emptySet(), Collections.emptyList());
|
this(name, Collections.emptySet(), Collections.emptyList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public CacheFile(String name, Set<String> mods, Iterable<CacheHead> heads) {
|
public CacheFile(String name, Set<String> mods, Iterable<CacheHead> heads) {
|
||||||
Checks.ensureNonNull(name, "name");
|
Checks.ensureNonNull(name, "name");
|
||||||
Checks.ensureNonNull(mods, "mods");
|
Checks.ensureNonNull(mods, "mods");
|
||||||
Checks.ensureNonNull(heads, "heads");
|
Checks.ensureNonNull(heads, "heads");
|
||||||
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.mods.addAll(mods);
|
this.mods.addAll(mods);
|
||||||
|
|
||||||
addHeads(heads);
|
addHeads(heads);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ModType getType() {
|
public ModType getType() {
|
||||||
return ModType.ADDON;
|
return ModType.ADDON;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeadCount() {
|
public int getHeadCount() {
|
||||||
return heads.size();
|
return heads.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CacheHead> getHeads() {
|
public List<CacheHead> getHeads() {
|
||||||
return Collections.unmodifiableList(heads);
|
return Collections.unmodifiableList(heads);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String resolveCategoryName(String category) {
|
public String resolveCategoryName(String category) {
|
||||||
for(String name : categories.keySet()) {
|
for (String name : categories.keySet()) {
|
||||||
if(name.equalsIgnoreCase(category))
|
if (name.equalsIgnoreCase(category))
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return category;
|
return category;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getCategories() {
|
public Set<String> getCategories() {
|
||||||
return Collections.unmodifiableSet(categories.keySet());
|
return Collections.unmodifiableSet(categories.keySet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CacheHead> getCategoryHeads(String category) {
|
public List<CacheHead> getCategoryHeads(String category) {
|
||||||
category = resolveCategoryName(category);
|
category = resolveCategoryName(category);
|
||||||
|
|
||||||
List<CacheHead> list = categories.getOrDefault(category, Collections.emptyList());
|
List<CacheHead> list = categories.getOrDefault(category, Collections.emptyList());
|
||||||
|
|
||||||
Collections.sort(list);
|
Collections.sort(list);
|
||||||
|
|
||||||
return Collections.unmodifiableList(list);
|
return Collections.unmodifiableList(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CacheHead> searchHeads(String query) {
|
public List<CacheHead> searchHeads(String query) {
|
||||||
return Search.searchHeads(query, heads, 0.4d);
|
return Search.searchHeads(query, heads, 0.4d);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void searchHeadsAsync(String query, Consumer<List<CacheHead>> onResult) {
|
public void searchHeadsAsync(String query, Consumer<List<CacheHead>> onResult) {
|
||||||
List<CacheHead> headsCopy = new ArrayList<>(heads);
|
List<CacheHead> headsCopy = new ArrayList<>(heads);
|
||||||
|
|
||||||
Heads.async(() -> {
|
Heads.async(() -> {
|
||||||
List<CacheHead> matches = Search.searchHeads(query, headsCopy, 0.4d);
|
List<CacheHead> matches = Search.searchHeads(query, headsCopy, 0.4d);
|
||||||
|
|
||||||
Heads.sync(() -> onResult.accept(matches));
|
Heads.sync(() -> onResult.accept(matches));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public CacheHead findHead(int id) {
|
public CacheHead findHead(int id) {
|
||||||
return headsById.get(id);
|
return headsById.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CacheHead findHeadByTexture(String texture) {
|
public CacheHead findHeadByTexture(String texture) {
|
||||||
return headsByTexture.get(texture);
|
return headsByTexture.get(texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CacheHead> findHeads(UUID uniqueId) {
|
public List<CacheHead> findHeads(UUID uniqueId) {
|
||||||
List<CacheHead> matches = new ArrayList<>();
|
List<CacheHead> matches = new ArrayList<>();
|
||||||
|
|
||||||
for(CacheHead head : heads) {
|
for (CacheHead head : heads) {
|
||||||
if(!head.getUniqueId().equals(uniqueId))
|
if (!head.getUniqueId().equals(uniqueId))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
matches.add(head);
|
matches.add(head);
|
||||||
}
|
}
|
||||||
|
|
||||||
return matches;
|
return matches;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CacheHead getRandomHead(Random random) {
|
public CacheHead getRandomHead(Random random) {
|
||||||
return heads.get(random.nextInt(heads.size()));
|
return heads.get(random.nextInt(heads.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addHeads(Iterable<CacheHead> heads) {
|
public void addHeads(Iterable<CacheHead> heads) {
|
||||||
for(CacheHead head : heads) {
|
for (CacheHead head : heads) {
|
||||||
addHead(head);
|
addHead(head);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getMaxId() {
|
private int getMaxId() {
|
||||||
int max = -1;
|
int max = -1;
|
||||||
|
|
||||||
for(CacheHead head : heads) {
|
for (CacheHead head : heads) {
|
||||||
max = Math.max(max, head.getId());
|
max = Math.max(max, head.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addHead(CacheHead head) {
|
public void addHead(CacheHead head) {
|
||||||
String category = resolveCategoryName(head.getCategory());
|
String category = resolveCategoryName(head.getCategory());
|
||||||
|
|
||||||
head = head.copyWithCategory(category);
|
head = head.copyWithCategory(category);
|
||||||
head.setId(getMaxId() + 1);
|
head.setId(getMaxId() + 1);
|
||||||
|
|
||||||
heads.add(head);
|
heads.add(head);
|
||||||
headsById.put(head.getId(), head);
|
headsById.put(head.getId(), head);
|
||||||
headsByTexture.put(head.getTexture(), head);
|
headsByTexture.put(head.getTexture(), head);
|
||||||
categories.computeIfAbsent(category, c -> new ArrayList<>()).add(head);
|
categories.computeIfAbsent(category, c -> new ArrayList<>()).add(head);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeHead(CacheHead head) {
|
public void removeHead(CacheHead head) {
|
||||||
String category = resolveCategoryName(head.getCategory());
|
String category = resolveCategoryName(head.getCategory());
|
||||||
|
|
||||||
heads.remove(head);
|
heads.remove(head);
|
||||||
headsById.remove(head.getId(), head);
|
headsById.remove(head.getId(), head);
|
||||||
headsByTexture.remove(head.getTexture(), head);
|
headsByTexture.remove(head.getTexture(), head);
|
||||||
categories.compute(category, (key, categoryHeads) -> {
|
categories.compute(category, (key, categoryHeads) -> {
|
||||||
if(categoryHeads == null)
|
if (categoryHeads == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
categoryHeads.remove(head);
|
categoryHeads.remove(head);
|
||||||
|
|
||||||
return (categoryHeads.size() > 0 ? categoryHeads : null);
|
return (categoryHeads.size() > 0 ? categoryHeads : null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applyMod(CacheFile cache) {
|
public void applyMod(CacheFile cache) {
|
||||||
cache.addHeads(heads);
|
cache.addHeads(heads);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasMod(String mod) {
|
public boolean hasMod(String mod) {
|
||||||
return mods.contains(mod);
|
return mods.contains(mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void installMod(Mod mod) {
|
public void installMod(Mod mod) {
|
||||||
if(hasMod(mod.getName()))
|
if (hasMod(mod.getName()))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
mods.add(mod.getName());
|
mods.add(mod.getName());
|
||||||
mod.applyMod(this);
|
mod.applyMod(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return getType() + " {name: \"" + name + "\", headCount: " + getHeadCount() + "}";
|
return getType() + " {name: \"" + name + "\", headCount: " + getHeadCount() + "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(File file) throws IOException {
|
public void write(File file) throws IOException {
|
||||||
if(file.isDirectory())
|
if (file.isDirectory())
|
||||||
throw new IOException("File " + file + " is a directory");
|
throw new IOException("File " + file + " is a directory");
|
||||||
|
|
||||||
if (!file.exists() && !file.createNewFile())
|
if (!file.exists() && !file.createNewFile())
|
||||||
throw new IOException("Unable to create file " + file);
|
throw new IOException("Unable to create file " + file);
|
||||||
|
|
||||||
try(FileOutputStream stream = new FileOutputStream(file)) {
|
try (FileOutputStream stream = new FileOutputStream(file)) {
|
||||||
writeCompressed(stream);
|
writeCompressed(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeCompressed(OutputStream os) throws IOException {
|
public void writeCompressed(OutputStream os) throws IOException {
|
||||||
try(GZIPOutputStream zos = new GZIPOutputStream(os);
|
try (GZIPOutputStream zos = new GZIPOutputStream(os); ObjectOutputStream stream = new ObjectOutputStream(zos)) {
|
||||||
ObjectOutputStream stream = new ObjectOutputStream(zos)) {
|
|
||||||
|
|
||||||
write(stream);
|
write(stream);
|
||||||
|
|
||||||
stream.flush();
|
stream.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(ObjectOutputStream stream) throws IOException {
|
public void write(ObjectOutputStream stream) throws IOException {
|
||||||
stream.writeInt(2);
|
stream.writeInt(2);
|
||||||
stream.writeUTF(name);
|
stream.writeUTF(name);
|
||||||
|
|
||||||
IOUtils.writeStringSet(stream, mods);
|
IOUtils.writeStringSet(stream, mods);
|
||||||
|
|
||||||
stream.writeInt(heads.size());
|
|
||||||
for(CacheHead head : heads) {
|
|
||||||
head.write(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CacheFile read(File file) throws IOException {
|
|
||||||
if(file.isDirectory())
|
|
||||||
throw new IOException("File " + file + " is a directory");
|
|
||||||
|
|
||||||
if(!file.exists())
|
stream.writeInt(heads.size());
|
||||||
throw new IOException("File " + file + " does not exist");
|
for (CacheHead head : heads) {
|
||||||
|
head.write(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try(FileInputStream stream = new FileInputStream(file)) {
|
public static CacheFile read(File file) throws IOException {
|
||||||
return readCompressed(stream);
|
if (file.isDirectory())
|
||||||
}
|
throw new IOException("File " + file + " is a directory");
|
||||||
}
|
|
||||||
|
|
||||||
public static CacheFile readResource(String resource) throws IOException {
|
if (!file.exists())
|
||||||
try(InputStream stream = Heads.getInstance().getResource(resource)) {
|
throw new IOException("File " + file + " does not exist");
|
||||||
return readCompressed(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CacheFile readCompressed(InputStream is) throws IOException {
|
|
||||||
try(GZIPInputStream zis = new GZIPInputStream(is);
|
|
||||||
ObjectInputStream stream = new ObjectInputStream(zis)) {
|
|
||||||
|
|
||||||
return read(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CacheFile read(ObjectInputStream stream) throws IOException {
|
|
||||||
int version = stream.readInt();
|
|
||||||
|
|
||||||
switch(version) {
|
|
||||||
case 2:
|
|
||||||
return readVersion2(stream);
|
|
||||||
case 1:
|
|
||||||
return readVersion1(stream);
|
|
||||||
default:
|
|
||||||
throw new UnsupportedOperationException("Unknown cache file version " + version);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CacheFile readVersion2(ObjectInputStream stream) throws IOException {
|
try (FileInputStream stream = new FileInputStream(file)) {
|
||||||
String name = stream.readUTF();
|
return readCompressed(stream);
|
||||||
|
}
|
||||||
Set<String> mods = IOUtils.readStringSet(stream);
|
}
|
||||||
|
|
||||||
int headCount = stream.readInt();
|
|
||||||
List<CacheHead> heads = new ArrayList<>(headCount);
|
|
||||||
for(int index = 0; index < headCount; ++index) {
|
|
||||||
heads.add(CacheHead.read(stream));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new CacheFile(name, mods, heads);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CacheFile readVersion1(ObjectInputStream stream) throws IOException {
|
|
||||||
String name = stream.readUTF();
|
|
||||||
|
|
||||||
Set<String> mods = new HashSet<>();
|
public static CacheFile readResource(String resource) throws IOException {
|
||||||
|
try (InputStream stream = Heads.getInstance().getResource(resource)) {
|
||||||
mods.addAll(IOUtils.readStringSet(stream));
|
return readCompressed(stream);
|
||||||
mods.addAll(IOUtils.readStringSet(stream));
|
}
|
||||||
|
}
|
||||||
int headCount = stream.readInt();
|
|
||||||
List<CacheHead> heads = new ArrayList<>(headCount);
|
|
||||||
for(int index = 0; index < headCount; ++index) {
|
|
||||||
heads.add(CacheHead.read(stream));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new CacheFile(name, mods, heads);
|
public static CacheFile readCompressed(InputStream is) throws IOException {
|
||||||
}
|
try (GZIPInputStream zis = new GZIPInputStream(is); ObjectInputStream stream = new ObjectInputStream(zis)) {
|
||||||
|
|
||||||
|
return read(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CacheFile read(ObjectInputStream stream) throws IOException {
|
||||||
|
int version = stream.readInt();
|
||||||
|
|
||||||
|
switch (version) {
|
||||||
|
case 2:
|
||||||
|
return readVersion2(stream);
|
||||||
|
case 1:
|
||||||
|
return readVersion1(stream);
|
||||||
|
default:
|
||||||
|
throw new UnsupportedOperationException("Unknown cache file version " + version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static CacheFile readVersion2(ObjectInputStream stream) throws IOException {
|
||||||
|
String name = stream.readUTF();
|
||||||
|
|
||||||
|
Set<String> mods = IOUtils.readStringSet(stream);
|
||||||
|
|
||||||
|
int headCount = stream.readInt();
|
||||||
|
List<CacheHead> heads = new ArrayList<>(headCount);
|
||||||
|
for (int index = 0; index < headCount; ++index) {
|
||||||
|
heads.add(CacheHead.read(stream));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CacheFile(name, mods, heads);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static CacheFile readVersion1(ObjectInputStream stream) throws IOException {
|
||||||
|
String name = stream.readUTF();
|
||||||
|
|
||||||
|
Set<String> mods = new HashSet<>();
|
||||||
|
|
||||||
|
mods.addAll(IOUtils.readStringSet(stream));
|
||||||
|
mods.addAll(IOUtils.readStringSet(stream));
|
||||||
|
|
||||||
|
int headCount = stream.readInt();
|
||||||
|
List<CacheHead> heads = new ArrayList<>(headCount);
|
||||||
|
for (int index = 0; index < headCount; ++index) {
|
||||||
|
heads.add(CacheHead.read(stream));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CacheFile(name, mods, heads);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String cool = "%%__USER__%%";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,163 +1,166 @@
|
|||||||
package net.sothatsit.heads.command.admin;
|
package net.sothatsit.heads.command.admin;
|
||||||
|
|
||||||
import net.sothatsit.heads.Heads;
|
|
||||||
import net.sothatsit.heads.command.AbstractCommand;
|
|
||||||
import net.sothatsit.heads.config.MainConfig;
|
|
||||||
import net.sothatsit.heads.config.lang.Lang;
|
|
||||||
import net.sothatsit.heads.menu.ui.item.Item;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import net.sothatsit.heads.Heads;
|
||||||
|
import net.sothatsit.heads.command.AbstractCommand;
|
||||||
|
import net.sothatsit.heads.config.MainConfig;
|
||||||
|
import net.sothatsit.heads.config.lang.Lang;
|
||||||
|
import net.sothatsit.heads.menu.ui.item.Item;
|
||||||
|
|
||||||
public class ItemEcoCommand extends AbstractCommand {
|
public class ItemEcoCommand extends AbstractCommand {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCommandLabel(MainConfig config) {
|
public String getCommandLabel(MainConfig config) {
|
||||||
return config.getItemEcoCommand();
|
return config.getItemEcoCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPermission() {
|
public String getPermission() {
|
||||||
return "heads.item-eco";
|
return "heads.item-eco";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Lang.HelpSection getHelp() {
|
public Lang.HelpSection getHelp() {
|
||||||
return Lang.Command.ItemEco.help();
|
return Lang.Command.ItemEco.help();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||||
if (args.length < 2) {
|
if (args.length < 2) {
|
||||||
sendInvalidArgs(sender);
|
sendInvalidArgs(sender);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(args[1].equalsIgnoreCase("give")) {
|
if (args[1].equalsIgnoreCase("give")) {
|
||||||
onGiveCommand(sender, args);
|
onGiveCommand(sender, args);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(sender instanceof Player)) {
|
if (!(sender instanceof Player)) {
|
||||||
Lang.Command.Errors.mustBePlayer().send(sender);
|
Lang.Command.Errors.mustBePlayer().send(sender);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Player player = (Player) sender;
|
Player player = (Player) sender;
|
||||||
|
|
||||||
if(args[1].equalsIgnoreCase("set")) {
|
if (args[1].equalsIgnoreCase("set")) {
|
||||||
onSetCommand(player, args);
|
onSetCommand(player, args);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(args[1].equalsIgnoreCase("get")) {
|
if (args[1].equalsIgnoreCase("get")) {
|
||||||
onGetCommand(player, args);
|
onGetCommand(player, args);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
sendInvalidArgs(sender);
|
sendInvalidArgs(sender);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onSetCommand(Player player, String[] args) {
|
private void onSetCommand(Player player, String[] args) {
|
||||||
if(args.length != 2) {
|
if (args.length != 2) {
|
||||||
Lang.Command.ItemEco.Set.help().sendInvalidArgs(player);
|
Lang.Command.ItemEco.Set.help().sendInvalidArgs(player);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack itemStack = player.getInventory().getItemInMainHand();
|
@SuppressWarnings("deprecation")
|
||||||
|
// Had to do this to resolve the compatibility issue with 1.13.
|
||||||
|
ItemStack itemStack = player.getInventory().getItemInHand();
|
||||||
|
|
||||||
if(itemStack == null) {
|
if (itemStack == null) {
|
||||||
Lang.Command.ItemEco.Set.noItem().send(player);
|
Lang.Command.ItemEco.Set.noItem().send(player);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Item item = Item.create(itemStack).amount(1);
|
Item item = Item.create(itemStack).amount(1);
|
||||||
|
|
||||||
Heads.getMainConfig().setItemEcoItem(item);
|
Heads.getMainConfig().setItemEcoItem(item);
|
||||||
|
|
||||||
Lang.Command.ItemEco.Set.set().send(player);
|
Lang.Command.ItemEco.Set.set().send(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onGetCommand(Player player, String[] args) {
|
private void onGetCommand(Player player, String[] args) {
|
||||||
if(args.length != 2 && args.length != 3) {
|
if (args.length != 2 && args.length != 3) {
|
||||||
Lang.Command.ItemEco.Get.help().sendInvalidArgs(player);
|
Lang.Command.ItemEco.Get.help().sendInvalidArgs(player);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int amount = 1;
|
int amount = 1;
|
||||||
|
|
||||||
if(args.length == 3) {
|
if (args.length == 3) {
|
||||||
try {
|
try {
|
||||||
amount = Integer.valueOf(args[2]);
|
amount = Integer.valueOf(args[2]);
|
||||||
} catch(NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
Lang.Command.Errors.integer(args[2]);
|
Lang.Command.Errors.integer(args[2]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(amount < 1) {
|
if (amount < 1) {
|
||||||
Lang.Command.Errors.negative(args[2]);
|
Lang.Command.Errors.negative(args[2]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
giveTokens(player, amount);
|
giveTokens(player, amount);
|
||||||
|
|
||||||
Lang.Command.ItemEco.Get.got(amount).send(player);
|
Lang.Command.ItemEco.Get.got(amount).send(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onGiveCommand(CommandSender sender, String[] args) {
|
private void onGiveCommand(CommandSender sender, String[] args) {
|
||||||
if(args.length != 3 && args.length != 4) {
|
if (args.length != 3 && args.length != 4) {
|
||||||
Lang.Command.ItemEco.Give.help().sendInvalidArgs(sender);
|
Lang.Command.ItemEco.Give.help().sendInvalidArgs(sender);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int amount = 1;
|
int amount = 1;
|
||||||
|
|
||||||
if(args.length == 4) {
|
if (args.length == 4) {
|
||||||
try {
|
try {
|
||||||
amount = Integer.valueOf(args[3]);
|
amount = Integer.valueOf(args[3]);
|
||||||
} catch(NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
Lang.Command.Errors.integer(args[3]);
|
Lang.Command.Errors.integer(args[3]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(amount < 1) {
|
if (amount < 1) {
|
||||||
Lang.Command.Errors.negative(args[3]);
|
Lang.Command.Errors.negative(args[3]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Player player = Bukkit.getPlayer(args[2]);
|
Player player = Bukkit.getPlayer(args[2]);
|
||||||
|
|
||||||
if(player == null) {
|
if (player == null) {
|
||||||
Lang.Command.ItemEco.Give.unknownPlayer(args[2]).send(sender);
|
Lang.Command.ItemEco.Give.unknownPlayer(args[2]).send(sender);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
giveTokens(player, amount);
|
giveTokens(player, amount);
|
||||||
|
|
||||||
Lang.Command.ItemEco.Give.got(amount).send(player);
|
Lang.Command.ItemEco.Give.got(amount).send(player);
|
||||||
Lang.Command.ItemEco.Give.given(player.getName(), amount).send(sender);
|
Lang.Command.ItemEco.Give.given(player.getName(), amount).send(sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void giveTokens(Player player, int amount) {
|
private void giveTokens(Player player, int amount) {
|
||||||
while(amount > 0) {
|
while (amount > 0) {
|
||||||
int giveAmount = Math.min(64, amount);
|
int giveAmount = Math.min(64, amount);
|
||||||
amount -= giveAmount;
|
amount -= giveAmount;
|
||||||
|
|
||||||
ItemStack itemStack = Heads.getMainConfig().getItemEconomyItem().amount(giveAmount).build();
|
ItemStack itemStack = Heads.getMainConfig().getItemEconomyItem().amount(giveAmount).build();
|
||||||
|
|
||||||
if(player.getInventory().firstEmpty() != -1) {
|
if (player.getInventory().firstEmpty() != -1) {
|
||||||
player.getInventory().addItem(itemStack);
|
player.getInventory().addItem(itemStack);
|
||||||
} else {
|
} else {
|
||||||
org.bukkit.entity.Item item = player.getWorld().dropItemNaturally(player.getEyeLocation(), itemStack);
|
org.bukkit.entity.Item item = player.getWorld().dropItemNaturally(player.getEyeLocation(), itemStack);
|
||||||
|
|
||||||
item.setPickupDelay(0);
|
item.setPickupDelay(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
main: net.sothatsit.heads.Heads
|
main: net.sothatsit.heads.Heads
|
||||||
author: Marido
|
author: Marido
|
||||||
name: Heads
|
name: Heads
|
||||||
version: 2.0.5
|
version: 2.0.6
|
||||||
api-version: 1.13
|
api-version: 1.13
|
||||||
softdepend: [Vault, PlayerPoints, BlockStore]
|
softdepend: [Vault, PlayerPoints, BlockStore]
|
||||||
loadbefore: [DeluxeMenus]
|
loadbefore: [DeluxeMenus]
|
||||||
|
Loading…
Reference in New Issue
Block a user