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