9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-25 09:39:18 +00:00

Userdata command, API expansion, editor interfaces

This commit is contained in:
William
2022-07-08 01:18:01 +01:00
parent 1c9d74f925
commit b7709f2d6c
43 changed files with 1044 additions and 446 deletions

View File

@@ -124,7 +124,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
}).thenApply(succeeded -> {
// Prepare data editor
if (succeeded) {
dataEditor = new DataEditor();
dataEditor = new DataEditor(locales);
}
return succeeded;
}).thenApply(succeeded -> {
@@ -149,7 +149,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
}).thenApply(succeeded -> {
// Establish connection to the Redis server
if (succeeded) {
this.redisManager = new RedisManager(settings, dataAdapter, logger);
this.redisManager = new RedisManager(this);
getLoggingAdapter().log(Level.INFO, "Attempting to establish connection to the Redis server...");
return this.redisManager.initialize().thenApply(initialized -> {
if (!initialized) {

View File

@@ -7,9 +7,11 @@ import org.jetbrains.annotations.NotNull;
* Commands available on the Bukkit HuskSync implementation
*/
public enum BukkitCommandType {
HUSKSYNC_COMMAND(new HuskSyncCommand(BukkitHuskSync.getInstance())),
HUSKSYNC_INVSEE(new InvseeCommand(BukkitHuskSync.getInstance())),
HUSKSYNC_ECHEST(new EchestCommand(BukkitHuskSync.getInstance()));
INVENTORY_COMMAND(new InventoryCommand(BukkitHuskSync.getInstance())),
ENDER_CHEST_COMMAND(new EnderChestCommand(BukkitHuskSync.getInstance())),
USERDATA_COMMAND(new UserDataCommand(BukkitHuskSync.getInstance()));
public final CommandBase commandBase;

View File

@@ -22,8 +22,8 @@ public class BukkitSerializer {
* @param inventoryContents The contents of the inventory
* @return The serialized inventory contents
*/
public static CompletableFuture<String> serializeItemStackArray(ItemStack[] inventoryContents)
throws DataDeserializationException {
public static CompletableFuture<String> serializeItemStackArray(@NotNull ItemStack[] inventoryContents)
throws DataSerializationException {
return CompletableFuture.supplyAsync(() -> {
// Return an empty string if there is no inventory item data to serialize
if (inventoryContents.length == 0) {
@@ -45,7 +45,7 @@ public class BukkitSerializer {
// Return encoded data, using the encoder from SnakeYaml to get a ByteArray conversion
return Base64Coder.encodeLines(byteOutputStream.toByteArray());
} catch (IOException e) {
throw new DataDeserializationException("Failed to serialize item stack data", e);
throw new DataSerializationException("Failed to serialize item stack data", e);
}
});
}
@@ -53,25 +53,25 @@ public class BukkitSerializer {
/**
* Returns a {@link BukkitInventoryMap} from a serialized array of ItemStacks representing the contents of a player's inventory.
*
* @param serializedPlayerInventory The serialized {@link ItemStack[]} inventory array
* @param serializedPlayerInventory The serialized {@link ItemStack} inventory array
* @return The deserialized ItemStacks, mapped for convenience as a {@link BukkitInventoryMap}
* @throws DataDeserializationException If the serialized item stack array could not be deserialized
* @throws DataSerializationException If the serialized item stack array could not be deserialized
*/
public static CompletableFuture<BukkitInventoryMap> deserializeInventory(@NotNull String serializedPlayerInventory)
throws DataDeserializationException {
throws DataSerializationException {
return CompletableFuture.supplyAsync(() -> new BukkitInventoryMap(deserializeItemStackArray(serializedPlayerInventory).join()));
}
/**
* Returns an array of ItemStacks from serialized inventory data.
*
* @param serializeItemStackArray The serialized {@link ItemStack[]} array
* @param serializeItemStackArray The serialized {@link ItemStack} array
* @return The deserialized array of {@link ItemStack}s
* @throws DataDeserializationException If the serialized item stack array could not be deserialized
* @throws DataSerializationException If the serialized item stack array could not be deserialized
* @implNote Empty slots will be represented by {@code null}
*/
public static CompletableFuture<ItemStack[]> deserializeItemStackArray(String serializeItemStackArray)
throws DataDeserializationException {
public static CompletableFuture<ItemStack[]> deserializeItemStackArray(@NotNull String serializeItemStackArray)
throws DataSerializationException {
return CompletableFuture.supplyAsync(() -> {
// Return empty array if there is no inventory data (set the player as having an empty inventory)
if (serializeItemStackArray.isEmpty()) {
@@ -95,7 +95,7 @@ public class BukkitSerializer {
return inventoryContents;
}
} catch (IOException | ClassNotFoundException e) {
throw new DataDeserializationException("Failed to deserialize item stack data", e);
throw new DataSerializationException("Failed to deserialize item stack data", e);
}
});
}
@@ -107,7 +107,7 @@ public class BukkitSerializer {
* @return The serialized {@link ItemStack}
*/
@Nullable
private static Map<String, Object> serializeItemStack(ItemStack item) {
private static Map<String, Object> serializeItemStack(@Nullable ItemStack item) {
return item != null ? item.serialize() : null;
}
@@ -119,7 +119,7 @@ public class BukkitSerializer {
*/
@SuppressWarnings("unchecked") // Ignore the "Unchecked cast" warning
@Nullable
private static ItemStack deserializeItemStack(Object serializedItemStack) {
private static ItemStack deserializeItemStack(@Nullable Object serializedItemStack) {
return serializedItemStack != null ? ItemStack.deserialize((Map<String, Object>) serializedItemStack) : null;
}
@@ -129,7 +129,7 @@ public class BukkitSerializer {
* @param potionEffects The potion effect array
* @return The serialized potion effects
*/
public static CompletableFuture<String> serializePotionEffects(PotionEffect[] potionEffects) throws DataDeserializationException {
public static CompletableFuture<String> serializePotionEffectArray(@NotNull PotionEffect[] potionEffects) throws DataSerializationException {
return CompletableFuture.supplyAsync(() -> {
// Return an empty string if there are no effects to serialize
if (potionEffects.length == 0) {
@@ -151,7 +151,7 @@ public class BukkitSerializer {
// Return encoded data, using the encoder from SnakeYaml to get a ByteArray conversion
return Base64Coder.encodeLines(byteOutputStream.toByteArray());
} catch (IOException e) {
throw new DataDeserializationException("Failed to serialize potion effect data", e);
throw new DataSerializationException("Failed to serialize potion effect data", e);
}
});
}
@@ -159,10 +159,10 @@ public class BukkitSerializer {
/**
* Returns an array of ItemStacks from serialized potion effect data
*
* @param potionEffectData The serialized {@link PotionEffect[]} array
* @param potionEffectData The serialized {@link PotionEffect} array
* @return The {@link PotionEffect}s
*/
public static CompletableFuture<PotionEffect[]> deserializePotionEffects(String potionEffectData) throws DataDeserializationException {
public static CompletableFuture<PotionEffect[]> deserializePotionEffectArray(@NotNull String potionEffectData) throws DataSerializationException {
return CompletableFuture.supplyAsync(() -> {
// Return empty array if there is no potion effect data (don't apply any effects to the player)
if (potionEffectData.isEmpty()) {
@@ -186,7 +186,7 @@ public class BukkitSerializer {
return potionEffects;
}
} catch (IOException | ClassNotFoundException e) {
throw new DataDeserializationException("Failed to deserialize potion effects", e);
throw new DataSerializationException("Failed to deserialize potion effects", e);
}
});
}
@@ -198,7 +198,7 @@ public class BukkitSerializer {
* @return The serialized {@link ItemStack}
*/
@Nullable
private static Map<String, Object> serializePotionEffect(PotionEffect potionEffect) {
private static Map<String, Object> serializePotionEffect(@Nullable PotionEffect potionEffect) {
return potionEffect != null ? potionEffect.serialize() : null;
}
@@ -210,7 +210,7 @@ public class BukkitSerializer {
*/
@SuppressWarnings("unchecked") // Ignore the "Unchecked cast" warning
@Nullable
private static PotionEffect deserializePotionEffect(Object serializedPotionEffect) {
private static PotionEffect deserializePotionEffect(@Nullable Object serializedPotionEffect) {
return serializedPotionEffect != null ? new PotionEffect((Map<String, Object>) serializedPotionEffect) : null;
}

View File

@@ -7,15 +7,15 @@ import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
public class BukkitDataSavePlayerEvent extends BukkitEvent implements DataSaveEvent, Cancellable {
public class BukkitDataSaveEvent extends BukkitEvent implements DataSaveEvent, Cancellable {
private static final HandlerList HANDLER_LIST = new HandlerList();
private boolean cancelled = false;
private UserData userData;
private final User user;
private final DataSaveCause saveCause;
protected BukkitDataSavePlayerEvent(@NotNull User user, @NotNull UserData userData,
@NotNull DataSaveCause saveCause) {
protected BukkitDataSaveEvent(@NotNull User user, @NotNull UserData userData,
@NotNull DataSaveCause saveCause) {
this.user = user;
this.userData = userData;
this.saveCause = saveCause;

View File

@@ -6,19 +6,28 @@ import net.william278.husksync.player.OnlineUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
public abstract class BukkitEvent extends Event implements net.william278.husksync.event.Event {
protected BukkitEvent() {
}
@Override
public CompletableFuture<net.william278.husksync.event.Event> fire() {
final CompletableFuture<net.william278.husksync.event.Event> eventFireFuture = new CompletableFuture<>();
Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> {
Bukkit.getServer().getPluginManager().callEvent(this);
// Don't fire events while the server is shutting down
if (!BukkitHuskSync.getInstance().isEnabled()) {
eventFireFuture.complete(this);
});
} else {
Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> {
Bukkit.getServer().getPluginManager().callEvent(this);
eventFireFuture.complete(this);
});
}
return eventFireFuture;
}

View File

@@ -22,12 +22,12 @@ public class BukkitEventCannon extends EventCannon {
@Override
public CompletableFuture<Event> fireDataSaveEvent(@NotNull User user, @NotNull UserData userData,
@NotNull DataSaveCause saveCause) {
return new BukkitDataSavePlayerEvent(user, userData, saveCause).fire();
return new BukkitDataSaveEvent(user, userData, saveCause).fire();
}
@Override
public void fireSyncCompleteEvent(@NotNull OnlineUser user) {
new BukkitSyncCompletePlayerEvent(((BukkitPlayer) user).getPlayer()).fire();
new BukkitSyncCompleteEvent(((BukkitPlayer) user).getPlayer()).fire();
}
}

View File

@@ -9,11 +9,12 @@ import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
public abstract class BukkitPlayerEvent extends org.bukkit.event.player.PlayerEvent implements PlayerEvent {
public abstract class BukkitPlayerEvent extends BukkitEvent implements PlayerEvent {
protected final Player player;
public BukkitPlayerEvent(@NotNull Player who) {
super(who);
protected BukkitPlayerEvent(@NotNull Player player) {
this.player = player;
}
@Override

View File

@@ -28,11 +28,6 @@ public class BukkitPreSyncEvent extends BukkitPlayerEvent implements PreSyncEven
this.cancelled = cancelled;
}
@Override
public OnlineUser getUser() {
return BukkitPlayer.adapt(player);
}
@Override
public @NotNull UserData getUserData() {
return userData;

View File

@@ -6,18 +6,13 @@ import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
public class BukkitSyncCompletePlayerEvent extends BukkitPlayerEvent implements SyncCompleteEvent {
public class BukkitSyncCompleteEvent extends BukkitPlayerEvent implements SyncCompleteEvent {
private static final HandlerList HANDLER_LIST = new HandlerList();
protected BukkitSyncCompletePlayerEvent(@NotNull Player player) {
protected BukkitSyncCompleteEvent(@NotNull Player player) {
super(player);
}
@Override
public OnlineUser getUser() {
return BukkitPlayer.adapt(player);
}
@NotNull
@Override
public HandlerList getHandlers() {

View File

@@ -2,7 +2,7 @@ package net.william278.husksync.listener;
import net.william278.husksync.BukkitHuskSync;
import net.william278.husksync.data.BukkitSerializer;
import net.william278.husksync.data.DataDeserializationException;
import net.william278.husksync.data.DataSerializationException;
import net.william278.husksync.data.ItemData;
import net.william278.husksync.player.BukkitPlayer;
import net.william278.husksync.player.OnlineUser;
@@ -57,7 +57,7 @@ public class BukkitEventListener extends EventListener implements Listener {
try {
BukkitSerializer.serializeItemStackArray(event.getInventory().getContents()).thenAccept(
serializedInventory -> super.handleMenuClose(user, new ItemData(serializedInventory)));
} catch (DataDeserializationException e) {
} catch (DataSerializationException e) {
huskSync.getLoggingAdapter().log(Level.SEVERE,
"Failed to serialize inventory data during menu close", e);
}

View File

@@ -5,7 +5,7 @@ import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.william278.husksync.BukkitHuskSync;
import net.william278.husksync.data.*;
import net.william278.husksync.editor.InventoryEditorMenu;
import net.william278.husksync.editor.ItemEditorMenu;
import org.apache.commons.lang.ArrayUtils;
import org.bukkit.*;
import org.bukkit.advancement.Advancement;
@@ -158,13 +158,13 @@ public class BukkitPlayer extends OnlineUser {
@Override
public CompletableFuture<PotionEffectData> getPotionEffects() {
return BukkitSerializer.serializePotionEffects(player.getActivePotionEffects()
return BukkitSerializer.serializePotionEffectArray(player.getActivePotionEffects()
.toArray(new PotionEffect[0])).thenApply(PotionEffectData::new);
}
@Override
public CompletableFuture<Void> setPotionEffects(@NotNull PotionEffectData potionEffectData) {
return BukkitSerializer.deserializePotionEffects(potionEffectData.serializedPotionEffects)
return BukkitSerializer.deserializePotionEffectArray(potionEffectData.serializedPotionEffects)
.thenApplyAsync(effects -> {
final CompletableFuture<Void> potionEffectsSetFuture = new CompletableFuture<>();
Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> {
@@ -328,8 +328,8 @@ public class BukkitPlayer extends OnlineUser {
public CompletableFuture<Void> setStatistics(@NotNull StatisticsData statisticsData) {
return CompletableFuture.runAsync(() -> {
// Set untyped statistics
for (String statistic : statisticsData.untypedStatistic.keySet()) {
player.setStatistic(Statistic.valueOf(statistic), statisticsData.untypedStatistic.get(statistic));
for (String statistic : statisticsData.untypedStatistics.keySet()) {
player.setStatistic(Statistic.valueOf(statistic), statisticsData.untypedStatistics.get(statistic));
}
// Set block statistics
@@ -440,7 +440,7 @@ public class BukkitPlayer extends OnlineUser {
}
@Override
public void showMenu(@NotNull InventoryEditorMenu menu) {
public void showMenu(@NotNull ItemEditorMenu menu) {
BukkitSerializer.deserializeItemStackArray(menu.itemData.serializedItems).thenAccept(inventoryContents -> {
final Inventory inventory = Bukkit.createInventory(player, menu.slotCount,
BaseComponent.toLegacyText(menu.menuTitle.toComponent()));