This commit is contained in:
parent
e147e995bb
commit
cde1c48b7a
8 changed files with 645 additions and 1 deletions
|
@ -0,0 +1,39 @@
|
|||
package me.unurled.sacredrealms.sr.components.clientbuild;
|
||||
|
||||
import java.util.HashMap;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ClientBuild {
|
||||
private final String name;
|
||||
private final HashMap<Location, BlockData> blocks = new HashMap<>();
|
||||
|
||||
public ClientBuild(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public HashMap<Location, BlockData> getBlocks() {
|
||||
return this.blocks;
|
||||
}
|
||||
|
||||
public void addBlock(Location loc, BlockData state) {
|
||||
this.blocks.put(loc, state);
|
||||
}
|
||||
|
||||
public void removeBlock(@NotNull Location loc) {
|
||||
this.blocks.remove(loc);
|
||||
}
|
||||
|
||||
public void clearBlocks() {
|
||||
this.blocks.clear();
|
||||
}
|
||||
|
||||
public boolean containsBlock(@NotNull Location loc, @NotNull BlockData data) {
|
||||
return this.blocks.containsKey(loc) && this.blocks.get(loc).matches(data);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,241 @@
|
|||
package me.unurled.sacredrealms.sr.components.clientbuild;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import me.unurled.sacredrealms.sr.SR;
|
||||
import me.unurled.sacredrealms.sr.commands.admin.ClientBuildCommand;
|
||||
import me.unurled.sacredrealms.sr.data.DataHandler;
|
||||
import me.unurled.sacredrealms.sr.data.DataManager;
|
||||
import me.unurled.sacredrealms.sr.data.gson.ClientBuildDeserializer;
|
||||
import me.unurled.sacredrealms.sr.data.gson.ClientBuildSerializer;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ClientBuildManager extends Manager {
|
||||
|
||||
private final List<ClientBuild> builds = new ArrayList<>();
|
||||
|
||||
/** a map of players that have ClientBuild displayed */
|
||||
private final HashMap<Player, List<String>> playerBlocks = new HashMap<>();
|
||||
|
||||
private final HashMap<UUID, HashMap<Location, BlockData>> temporaryBlocks = new HashMap<>();
|
||||
private final HashMap<UUID, HashMap<Location, BlockData>> temporaryPreviousBlocks =
|
||||
new HashMap<>();
|
||||
|
||||
/** Load the manager */
|
||||
@Override
|
||||
public void load() {
|
||||
super.load();
|
||||
Bukkit.getPluginManager().registerEvents(new ClientBuildCommand(), SR.getInstance());
|
||||
}
|
||||
|
||||
public List<ClientBuild> getBuilds() {
|
||||
return this.builds;
|
||||
}
|
||||
|
||||
/** Save the data */
|
||||
@Override
|
||||
public void saveData() {
|
||||
// save client side builds to redis
|
||||
|
||||
DataManager dataManager = DataManager.getInstance(DataManager.class);
|
||||
DataHandler dh = dataManager.getDataHandler();
|
||||
|
||||
Gson gson =
|
||||
new GsonBuilder()
|
||||
.registerTypeAdapter(ClientBuild.class, new ClientBuildSerializer())
|
||||
.create();
|
||||
for (ClientBuild build : builds) {
|
||||
String json = gson.toJson(build);
|
||||
dh.set("sr.clientbuild." + build.getName(), json);
|
||||
}
|
||||
|
||||
for (Entry<Player, List<String>> entry : playerBlocks.entrySet()) {
|
||||
for (String name : entry.getValue()) {
|
||||
// save for player
|
||||
dh.set("sr.players.clientbuild." + entry.getKey().getUniqueId() + "." + name, "true");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Load the data */
|
||||
@Override
|
||||
public void loadData() {
|
||||
// load client side builds from redis
|
||||
DataManager dataManager = DataManager.getInstance(DataManager.class);
|
||||
DataHandler dh = dataManager.getDataHandler();
|
||||
|
||||
if (dh == null) {
|
||||
error("Failed to get DataHandler instance, can't load client builds.");
|
||||
error("Retrying in 10 seconds.");
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(SR.getInstance(), this::loadData, 200L);
|
||||
return;
|
||||
}
|
||||
List<String> keys = dh.getKeysAll("sr.clientbuild");
|
||||
log("Loading " + keys.size() + " client builds.");
|
||||
|
||||
Gson gson =
|
||||
new GsonBuilder()
|
||||
.registerTypeAdapter(ClientBuild.class, new ClientBuildDeserializer())
|
||||
.create();
|
||||
|
||||
for (String key : keys) {
|
||||
String json = dh.get(key);
|
||||
ClientBuild build = gson.fromJson(json, ClientBuild.class);
|
||||
if (build == null) {
|
||||
error("Failed to load client build " + key + " " + json);
|
||||
}
|
||||
builds.add(build);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(@NotNull PlayerJoinEvent e) {
|
||||
DataHandler dh = DataManager.getInstance(DataManager.class).getDataHandler();
|
||||
Player p = e.getPlayer();
|
||||
|
||||
List<String> names =
|
||||
new ArrayList<>(dh.getKeysAll("sr.players.clientbuild." + p.getUniqueId()));
|
||||
|
||||
playerBlocks.put(p, names);
|
||||
|
||||
for (String name : names) {
|
||||
ClientBuild build = getBuild(name);
|
||||
if (build != null) {
|
||||
displayToPlayer(p, build);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(@NotNull PlayerQuitEvent e) {
|
||||
Player p = e.getPlayer();
|
||||
List<String> names = playerBlocks.get(p);
|
||||
if (names == null) {
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler()
|
||||
.runTaskAsynchronously(
|
||||
SR.getInstance(),
|
||||
() -> {
|
||||
for (String name : names) {
|
||||
ClientBuild build = getBuild(name);
|
||||
if (build != null) {
|
||||
removeDisplayFromPlayer(p, build);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// save the player's displayed builds
|
||||
|
||||
DataManager dataManager = DataManager.getInstance(DataManager.class);
|
||||
DataHandler dh = dataManager.getDataHandler();
|
||||
|
||||
dh.remove("sr.players.clientbuild." + p.getUniqueId());
|
||||
|
||||
for (String name : names) {
|
||||
dh.set("sr.players.clientbuild." + p.getUniqueId() + "." + name, "true");
|
||||
}
|
||||
|
||||
playerBlocks.get(p).clear();
|
||||
|
||||
playerBlocks.remove(p);
|
||||
}
|
||||
|
||||
public List<String> getBuildNames() {
|
||||
List<String> names = new ArrayList<>();
|
||||
for (ClientBuild build : builds) {
|
||||
names.add(build.getName());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
public void addBuild(ClientBuild build) {
|
||||
builds.add(build);
|
||||
}
|
||||
|
||||
public void removeBuild(String name) {
|
||||
for (ClientBuild build : builds) {
|
||||
if (build.getName().equals(name)) {
|
||||
builds.remove(build);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ClientBuild getBuild(@NotNull String name) {
|
||||
for (ClientBuild build : builds) {
|
||||
if (build.getName().equals(name)) {
|
||||
return build;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<String> getPlayerBuilds(Player p) {
|
||||
return playerBlocks.get(p);
|
||||
}
|
||||
|
||||
public void displayToPlayer(@NotNull Player p, @NotNull ClientBuild build) {
|
||||
playerBlocks.get(p).add(build.getName());
|
||||
for (Entry<Location, BlockData> entry : build.getBlocks().entrySet()) {
|
||||
p.sendBlockChange(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void removeDisplayFromPlayer(@NotNull Player p, @NotNull ClientBuild build) {
|
||||
playerBlocks.get(p).remove(build.getName());
|
||||
for (Entry<Location, BlockData> entry : build.getBlocks().entrySet()) {
|
||||
p.sendBlockChange(entry.getKey(), entry.getKey().getBlock().getBlockData());
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<Location, BlockData> getTemporaryBlocks(@NotNull Player p) {
|
||||
return temporaryBlocks.get(p.getUniqueId());
|
||||
}
|
||||
|
||||
public void setTemporaryBlocks(@NotNull Player p, HashMap<Location, BlockData> blocks) {
|
||||
temporaryBlocks.put(p.getUniqueId(), blocks);
|
||||
}
|
||||
|
||||
public boolean hasTemporaryBlocks(@NotNull Player p) {
|
||||
return temporaryBlocks.containsKey(p.getUniqueId());
|
||||
}
|
||||
|
||||
public void removeTemporaryBlocks(@NotNull Player p) {
|
||||
temporaryBlocks.remove(p.getUniqueId());
|
||||
}
|
||||
|
||||
public HashMap<Location, BlockData> getTemporaryPreviousBlocks(@NotNull Player p) {
|
||||
return temporaryPreviousBlocks.get(p.getUniqueId());
|
||||
}
|
||||
|
||||
public void setTemporaryPreviousBlocks(@NotNull Player p, HashMap<Location, BlockData> blocks) {
|
||||
temporaryPreviousBlocks.put(p.getUniqueId(), blocks);
|
||||
}
|
||||
|
||||
public boolean hasTemporaryPreviousBlocks(@NotNull Player p) {
|
||||
return temporaryPreviousBlocks.containsKey(p.getUniqueId());
|
||||
}
|
||||
|
||||
public void removeTemporaryPreviousBlocks(@NotNull Player p) {
|
||||
temporaryPreviousBlocks.remove(p.getUniqueId());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue