9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-27 02:29:10 +00:00

Echest, invsee

This commit is contained in:
William
2022-07-05 15:24:26 +01:00
parent 1829526aa7
commit fd08a3e7d0
14 changed files with 433 additions and 21 deletions

View File

@@ -6,10 +6,7 @@ import dev.dejvokep.boostedyaml.settings.dumper.DumperSettings;
import dev.dejvokep.boostedyaml.settings.general.GeneralSettings;
import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings;
import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings;
import net.william278.husksync.command.BukkitCommand;
import net.william278.husksync.command.CommandBase;
import net.william278.husksync.command.HuskSyncCommand;
import net.william278.husksync.command.Permission;
import net.william278.husksync.command.*;
import net.william278.husksync.config.Locales;
import net.william278.husksync.config.Settings;
import net.william278.husksync.data.CompressedDataAdapter;
@@ -17,6 +14,7 @@ import net.william278.husksync.data.DataAdapter;
import net.william278.husksync.data.JsonDataAdapter;
import net.william278.husksync.database.Database;
import net.william278.husksync.database.MySqlDatabase;
import net.william278.husksync.editor.DataEditor;
import net.william278.husksync.listener.BukkitEventListener;
import net.william278.husksync.listener.EventListener;
import net.william278.husksync.player.BukkitPlayer;
@@ -51,6 +49,8 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
private DataAdapter dataAdapter;
private DataEditor dataEditor;
private Settings settings;
private Locales locales;
@@ -97,6 +97,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
return loadedSettings;
}).join();
}).thenApply(succeeded -> {
// Prepare data adapter
if (succeeded) {
if (settings.getBooleanValue(Settings.ConfigOption.SYNCHRONIZATION_COMPRESS_DATA)) {
dataAdapter = new CompressedDataAdapter();
@@ -105,6 +106,12 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
}
}
return succeeded;
}).thenApply(succeeded -> {
// Prepare data editor
if (succeeded) {
dataEditor = new DataEditor();
}
return succeeded;
}).thenApply(succeeded -> {
// Establish connection to the database
this.database = new MySqlDatabase(settings, resourceReader, logger, dataAdapter);
@@ -160,11 +167,10 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
})));
// Register commands
final CommandBase[] commands = new CommandBase[]{new HuskSyncCommand(this)};
for (CommandBase commandBase : commands) {
final PluginCommand pluginCommand = getCommand(commandBase.command);
for (final BukkitCommandType bukkitCommandType : BukkitCommandType.values()) {
final PluginCommand pluginCommand = getCommand(bukkitCommandType.commandBase.command);
if (pluginCommand != null) {
new BukkitCommand(commandBase, this).register(pluginCommand);
new BukkitCommand(bukkitCommandType.commandBase, this).register(pluginCommand);
}
}
getLoggingAdapter().log(Level.INFO, "Successfully registered permissions & commands");
@@ -226,6 +232,11 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
return dataAdapter;
}
@Override
public @NotNull DataEditor getDataEditor() {
return dataEditor;
}
@Override
public @NotNull Settings getSettings() {
return settings;

View File

@@ -67,4 +67,5 @@ public class BukkitCommand implements CommandExecutor, TabExecutor {
}
return Collections.emptyList();
}
}

View File

@@ -0,0 +1,19 @@
package net.william278.husksync.command;
import net.william278.husksync.BukkitHuskSync;
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()));
public final CommandBase commandBase;
BukkitCommandType(@NotNull CommandBase commandBase) {
this.commandBase = commandBase;
}
}

View File

@@ -21,7 +21,7 @@ public class BukkitSerializer {
* @param inventoryContents The contents of the inventory
* @return The serialized inventory contents
*/
public static CompletableFuture<String> serializeInventory(ItemStack[] inventoryContents) {
public static CompletableFuture<String> serializeInventory(ItemStack[] inventoryContents) throws DataDeserializationException {
return CompletableFuture.supplyAsync(() -> {
// Return an empty string if there is no inventory item data to serialize
if (inventoryContents.length == 0) {
@@ -54,7 +54,7 @@ public class BukkitSerializer {
* @param inventoryData The serialized {@link ItemStack[]} array
* @return The inventory contents as an array of {@link ItemStack}s
*/
public static CompletableFuture<ItemStack[]> deserializeInventory(String inventoryData) {
public static CompletableFuture<ItemStack[]> deserializeInventory(String inventoryData) throws DataDeserializationException {
return CompletableFuture.supplyAsync(() -> {
// Return empty array if there is no inventory data (set the player as having an empty inventory)
if (inventoryData.isEmpty()) {
@@ -89,6 +89,7 @@ public class BukkitSerializer {
* @param item The {@link ItemStack} to serialize
* @return The serialized {@link ItemStack}
*/
@Nullable
private static Map<String, Object> serializeItemStack(ItemStack item) {
return item != null ? item.serialize() : null;
}
@@ -100,6 +101,7 @@ public class BukkitSerializer {
* @return The deserialized {@link ItemStack}
*/
@SuppressWarnings("unchecked") // Ignore the "Unchecked cast" warning
@Nullable
private static ItemStack deserializeItemStack(Object serializedItemStack) {
return serializedItemStack != null ? ItemStack.deserialize((Map<String, Object>) serializedItemStack) : null;
}
@@ -110,7 +112,7 @@ public class BukkitSerializer {
* @param potionEffects The potion effect array
* @return The serialized potion effects
*/
public static CompletableFuture<String> serializePotionEffects(PotionEffect[] potionEffects) {
public static CompletableFuture<String> serializePotionEffects(PotionEffect[] potionEffects) throws DataDeserializationException {
return CompletableFuture.supplyAsync(() -> {
// Return an empty string if there are no effects to serialize
if (potionEffects.length == 0) {
@@ -143,7 +145,7 @@ public class BukkitSerializer {
* @param potionEffectData The serialized {@link PotionEffect[]} array
* @return The {@link PotionEffect}s
*/
public static CompletableFuture<PotionEffect[]> deserializePotionEffects(String potionEffectData) {
public static CompletableFuture<PotionEffect[]> deserializePotionEffects(String potionEffectData) throws DataDeserializationException {
return CompletableFuture.supplyAsync(() -> {
// Return empty array if there is no potion effect data (don't apply any effects to the player)
if (potionEffectData.isEmpty()) {

View File

@@ -1,15 +1,29 @@
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.InventoryData;
import net.william278.husksync.player.BukkitPlayer;
import net.william278.husksync.player.OnlineUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.world.WorldSaveEvent;
import org.jetbrains.annotations.NotNull;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class BukkitEventListener extends EventListener implements Listener {
@@ -36,11 +50,60 @@ public class BukkitEventListener extends EventListener implements Listener {
.collect(Collectors.toList()));
}
/*@EventHandler(ignoreCancelled = true)
public void onGenericPlayerEvent(@NotNull PlayerEvent event) {
if (event instanceof Cancellable) {
((Cancellable) event).setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(event.getPlayer())));
@EventHandler(ignoreCancelled = true)
public void onInventoryClose(@NotNull InventoryCloseEvent event) {
if (event.getPlayer() instanceof Player player) {
final OnlineUser user = BukkitPlayer.adapt(player);
if (huskSync.getDataEditor().isEditingInventoryData(user)) {
try {
BukkitSerializer.serializeInventory(event.getInventory().getContents()).thenAccept(
serializedInventory -> super.handleMenuClose(user, new InventoryData(serializedInventory)));
} catch (DataDeserializationException e) {
huskSync.getLoggingAdapter().log(Level.SEVERE,
"Failed to serialize inventory data during menu close", e);
}
}
}
}*/
}
/*
* Events to cancel if the player has not been set yet
*/
@EventHandler(priority = EventPriority.HIGHEST)
public void onDropItem(@NotNull PlayerDropItemEvent event) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(event.getPlayer())));
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onPickupItem(@NotNull EntityPickupItemEvent event) {
if (event.getEntity() instanceof Player player) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(player)));
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerInteract(@NotNull PlayerInteractEvent event) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(event.getPlayer())));
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onBlockPlace(@NotNull BlockPlaceEvent event) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(event.getPlayer())));
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onBlockBreak(@NotNull BlockBreakEvent event) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(event.getPlayer())));
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onInventoryOpen(@NotNull InventoryOpenEvent event) {
if (event.getPlayer() instanceof Player player) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(player)));
}
}
}

View File

@@ -2,8 +2,10 @@ package net.william278.husksync.player;
import de.themoep.minedown.MineDown;
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 org.apache.commons.lang.ArrayUtils;
import org.bukkit.*;
import org.bukkit.advancement.Advancement;
@@ -12,6 +14,7 @@ import org.bukkit.attribute.Attribute;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
@@ -423,6 +426,20 @@ public class BukkitPlayer extends OnlineUser {
return player.hasPermission(node);
}
@Override
public void showMenu(@NotNull InventoryEditorMenu menu) {
BukkitSerializer.deserializeInventory(menu.inventoryData.serializedInventory).thenAccept(inventoryContents -> {
final Inventory inventory = Bukkit.createInventory(player, menu.slotCount,
BaseComponent.toLegacyText(menu.menuTitle.toComponent()));
inventory.setContents(inventoryContents);
Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> {
player.closeInventory();
player.openInventory(inventory);
});
});
}
@Override
public void sendActionBar(@NotNull MineDown mineDown) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, mineDown.toComponent());