Merge branch 'development' into 'master'
Development See merge request Songoda/epicheads!11
This commit is contained in:
commit
30baf9f2d2
|
@ -4,7 +4,7 @@ stages:
|
|||
variables:
|
||||
name: "EpicHeads"
|
||||
path: "/builds/$CI_PROJECT_PATH"
|
||||
version: "3.0.1"
|
||||
version: "3.0.2"
|
||||
|
||||
build:
|
||||
stage: build
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.songoda.epicheads.head.Head;
|
|||
import com.songoda.epicheads.head.HeadManager;
|
||||
import com.songoda.epicheads.head.Tag;
|
||||
import com.songoda.epicheads.listeners.ItemListeners;
|
||||
import com.songoda.epicheads.listeners.LoginListeners;
|
||||
import com.songoda.epicheads.players.EPlayer;
|
||||
import com.songoda.epicheads.players.PlayerManager;
|
||||
import com.songoda.epicheads.utils.Methods;
|
||||
|
@ -85,6 +86,7 @@ public class EpicHeads extends JavaPlugin {
|
|||
// Register Listeners
|
||||
AbstractGUI.initializeListeners(this);
|
||||
Bukkit.getPluginManager().registerEvents(new ItemListeners(this), this);
|
||||
Bukkit.getPluginManager().registerEvents(new LoginListeners(this), this);
|
||||
|
||||
// Download Heads
|
||||
downloadHeads();
|
||||
|
@ -93,7 +95,7 @@ public class EpicHeads extends JavaPlugin {
|
|||
loadHeads();
|
||||
|
||||
// Load Favorites
|
||||
Bukkit.getScheduler().runTaskLater(this, this::loadData, 10);
|
||||
loadData();
|
||||
|
||||
int timeout = SettingsManager.Setting.AUTOSAVE.getInt() * 60 * 20;
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, timeout, timeout);
|
||||
|
@ -156,6 +158,8 @@ public class EpicHeads extends JavaPlugin {
|
|||
|
||||
private boolean loadHeads() {
|
||||
try {
|
||||
this.headManager.clear();
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONArray jsonArray = (JSONArray) parser.parse(new FileReader(getDataFolder() + "/heads.json"));
|
||||
|
||||
|
@ -165,7 +169,7 @@ public class EpicHeads extends JavaPlugin {
|
|||
String tags = (String) jsonObject.get("tags");
|
||||
Optional<Tag> tagOptional = headManager.getTags().stream().filter(t -> t.getName().equalsIgnoreCase(tags)).findFirst();
|
||||
|
||||
Tag tag = tagOptional.orElseGet(() -> new Tag(tags, -1));
|
||||
Tag tag = tagOptional.orElseGet(() -> new Tag(tags));
|
||||
|
||||
Head head = new Head(Integer.parseInt((String) jsonObject.get("id")),
|
||||
(String) jsonObject.get("name"),
|
||||
|
@ -181,8 +185,25 @@ public class EpicHeads extends JavaPlugin {
|
|||
headManager.addHead(head);
|
||||
}
|
||||
|
||||
for (Tag tag : headManager.getTags()) {
|
||||
tag.setCount(Math.toIntExact(headManager.getHeads().stream().filter(head -> head.getTag() == tag).count()));
|
||||
if (storage.containsGroup("local")) {
|
||||
for (StorageRow row : storage.getRowsByGroup("local")) {
|
||||
String tagStr = row.get("category").asString();
|
||||
|
||||
Optional<Tag> tagOptional = headManager.getTags().stream()
|
||||
.filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst();
|
||||
|
||||
Tag tag = tagOptional.orElseGet(() -> new Tag(tagStr));
|
||||
|
||||
Head head = new Head(row.get("id").asInt(),
|
||||
row.get("name").asString(),
|
||||
row.get("url").asString(),
|
||||
tag,
|
||||
(byte) 0);
|
||||
|
||||
if (!tagOptional.isPresent())
|
||||
headManager.addTag(tag);
|
||||
headManager.addLocalHead(head);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("loaded " + headManager.getHeads().size() + " Heads.");
|
||||
|
@ -229,10 +250,14 @@ public class EpicHeads extends JavaPlugin {
|
|||
}
|
||||
|
||||
public void reload() {
|
||||
saveToFile();
|
||||
locale.reloadMessages();
|
||||
references = new References();
|
||||
this.setupConfig();
|
||||
saveConfig();
|
||||
saveToFile();
|
||||
downloadHeads();
|
||||
loadHeads();
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
|
|
|
@ -64,7 +64,7 @@ public class GUIOverview extends AbstractGUI {
|
|||
|
||||
Tag tag = plugin.getHeadManager().getTags().get(i);
|
||||
|
||||
if (!player.hasPermission("epicheads.category." + tag.getName())) continue;
|
||||
if (!player.hasPermission("epicheads.category." + tag.getName().replace(" ", "_"))) continue;
|
||||
|
||||
createButton(i + 10 + add, Methods.addTexture(new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13)
|
||||
? Material.PLAYER_HEAD : Material.valueOf("SKULL_ITEM"), 1, (byte) 3),
|
||||
|
|
|
@ -2,10 +2,12 @@ package com.songoda.epicheads.head;
|
|||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class HeadManager {
|
||||
|
||||
private static final Set<Head> registeredHeads = new HashSet<>();
|
||||
private static final List<Head> localRegisteredHeads = new ArrayList<>();
|
||||
private static final List<Tag> registeredTags = new ArrayList<>();
|
||||
|
||||
public Head addHead(Head head) {
|
||||
|
@ -21,8 +23,20 @@ public class HeadManager {
|
|||
registeredHeads.addAll(heads);
|
||||
}
|
||||
|
||||
public void addLocalHeads(Head... heads) {
|
||||
localRegisteredHeads.addAll(Arrays.asList(heads));
|
||||
}
|
||||
|
||||
public void addLocalHead(Head head) {
|
||||
localRegisteredHeads.add(head);
|
||||
}
|
||||
|
||||
public void addLocalHeads(Collection<Head> heads) {
|
||||
localRegisteredHeads.addAll(heads);
|
||||
}
|
||||
|
||||
public Head getHead(String name) {
|
||||
return getHeads().stream().filter(head -> head.getName().equals(name)).findFirst().orElse(null);
|
||||
return getHeads().stream().filter(head -> head.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
public List<Head> getHeadsByQuery(String query) {
|
||||
|
@ -43,12 +57,25 @@ public class HeadManager {
|
|||
}
|
||||
|
||||
public List<Head> getHeads() {
|
||||
return Collections.unmodifiableList(registeredHeads.stream()
|
||||
return Collections.unmodifiableList(Stream.concat(registeredHeads.stream(), localRegisteredHeads.stream())
|
||||
.sorted(Comparator.comparing(Head::getName))
|
||||
.sorted(Comparator.comparingInt(head -> (head.getStaffPicked() == 1 ? 0 : 1)))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public Integer getNextLocalId() {
|
||||
if (localRegisteredHeads.isEmpty()) return 1;
|
||||
return localRegisteredHeads.get(localRegisteredHeads.size() - 1).getId() + 1;
|
||||
}
|
||||
|
||||
public List<Head> getLocalHeads() {
|
||||
return Collections.unmodifiableList(localRegisteredHeads);
|
||||
}
|
||||
|
||||
public void removeLocalHead(Head head) {
|
||||
localRegisteredHeads.remove(head);
|
||||
}
|
||||
|
||||
public Tag addTag(Tag tag) {
|
||||
registeredTags.add(tag);
|
||||
return tag;
|
||||
|
@ -57,4 +84,10 @@ public class HeadManager {
|
|||
public List<Tag> getTags() {
|
||||
return Collections.unmodifiableList(registeredTags);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
registeredHeads.clear();
|
||||
localRegisteredHeads.clear();
|
||||
registeredTags.clear();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package com.songoda.epicheads.head;
|
||||
|
||||
import com.songoda.epicheads.EpicHeads;
|
||||
|
||||
public class Tag {
|
||||
|
||||
private final String name;
|
||||
private int count;
|
||||
|
||||
public Tag(String name, int count) {
|
||||
public Tag(String name) {
|
||||
this.name = name;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
@ -15,10 +15,7 @@ public class Tag {
|
|||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
return Math.toIntExact(EpicHeads.getInstance().getHeadManager()
|
||||
.getHeads().stream().filter(head -> head.getTag() == this).count());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package com.songoda.epicheads.listeners;
|
||||
|
||||
import com.songoda.epicheads.EpicHeads;
|
||||
import com.songoda.epicheads.head.Head;
|
||||
import com.songoda.epicheads.head.HeadManager;
|
||||
import com.songoda.epicheads.head.Tag;
|
||||
import com.songoda.epicheads.utils.Methods;
|
||||
import com.songoda.epicheads.utils.ServerVersion;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class LoginListeners implements Listener {
|
||||
|
||||
private final EpicHeads plugin;
|
||||
|
||||
public LoginListeners(EpicHeads plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void loginEvent(PlayerLoginEvent event) {
|
||||
HeadManager headManager = plugin.getHeadManager();
|
||||
|
||||
ItemStack item = new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13)
|
||||
? Material.PLAYER_HEAD : Material.valueOf("SKULL_ITEM"), 1, (byte) 3);
|
||||
|
||||
Player player = event.getPlayer();
|
||||
SkullMeta meta = (SkullMeta) item.getItemMeta();
|
||||
meta.setOwningPlayer(player);
|
||||
item.setItemMeta(meta);
|
||||
|
||||
String url = Methods.getDecodedTexture(item);
|
||||
|
||||
String tagStr = "Player Heads";
|
||||
|
||||
Optional<Tag> tagOptional = headManager.getTags()
|
||||
.stream().filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst();
|
||||
|
||||
Tag tag = tagOptional.orElseGet(() -> new Tag(tagStr));
|
||||
|
||||
if (!tagOptional.isPresent())
|
||||
headManager.addTag(tag);
|
||||
|
||||
Optional<Head> optional = headManager.getLocalHeads().stream()
|
||||
.filter(h -> h.getName().equalsIgnoreCase(event.getPlayer().getName())).findFirst();
|
||||
|
||||
int id = headManager.getNextLocalId();
|
||||
|
||||
if (optional.isPresent()) {
|
||||
Head head = optional.get();
|
||||
id = head.getId();
|
||||
headManager.removeLocalHead(head);
|
||||
}
|
||||
|
||||
headManager.addLocalHeads(new Head(id, player.getName(), url, tag, (byte) 0));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.songoda.epicheads.utils.storage;
|
||||
|
||||
import com.songoda.epicheads.EpicHeads;
|
||||
import com.songoda.epicheads.head.Head;
|
||||
import com.songoda.epicheads.players.EPlayer;
|
||||
import com.songoda.epicheads.utils.ConfigWrapper;
|
||||
|
||||
|
@ -31,6 +32,13 @@ public abstract class Storage {
|
|||
prepareSaveItem("players", new StorageItem("uuid", player.getUuid().toString()),
|
||||
new StorageItem("favorites", player.getFavorites()));
|
||||
}
|
||||
|
||||
for (Head head : instance.getHeadManager().getLocalHeads()) {
|
||||
prepareSaveItem("local", new StorageItem("url", head.getURL()),
|
||||
new StorageItem("name", head.getName()),
|
||||
new StorageItem("id", head.getId()),
|
||||
new StorageItem("category", head.getTag().getName()));
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void doSave();
|
||||
|
|
Loading…
Reference in New Issue