mirror of
https://github.com/WiIIiam278/HuskSync.git
synced 2026-01-06 15:41:56 +00:00
Initial project setup, building, setup planning, readme & redis
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
package me.william278.crossserversync;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class CrossServerSyncSpigot extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Plugin startup logic
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package me.william278.crossserversync.bukkit;
|
||||
|
||||
import me.william278.crossserversync.Settings;
|
||||
import me.william278.crossserversync.redis.RedisListener;
|
||||
import me.william278.crossserversync.redis.RedisMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class BukkitRedisListener extends RedisListener {
|
||||
|
||||
private static final CrossServerSyncBukkit plugin = CrossServerSyncBukkit.getInstance();
|
||||
|
||||
// Initialize the listener on the bukkit server
|
||||
public BukkitRedisListener() {
|
||||
listen();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming {@link RedisMessage}
|
||||
*
|
||||
* @param message The {@link RedisMessage} to handle
|
||||
*/
|
||||
@Override
|
||||
public void handleMessage(RedisMessage message) {
|
||||
// Ignore messages for proxy servers
|
||||
if (message.getMessageTarget().targetServerType() != Settings.ServerType.BUKKIT) {
|
||||
return;
|
||||
}
|
||||
// Handle the message for the player
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (player.getUniqueId() == message.getMessageTarget().targetPlayerName()) {
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log to console
|
||||
*
|
||||
* @param level The {@link Level} to log
|
||||
* @param message Message to log
|
||||
*/
|
||||
@Override
|
||||
public void log(Level level, String message) {
|
||||
plugin.getLogger().log(level, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package me.william278.crossserversync.bukkit;
|
||||
|
||||
import me.william278.crossserversync.bukkit.config.ConfigLoader;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class CrossServerSyncBukkit extends JavaPlugin {
|
||||
|
||||
private static CrossServerSyncBukkit instance;
|
||||
public static CrossServerSyncBukkit getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Plugin startup logic
|
||||
|
||||
// Load the config file
|
||||
getConfig().options().copyDefaults(true);
|
||||
saveDefaultConfig();
|
||||
saveConfig();
|
||||
reloadConfig();
|
||||
ConfigLoader.loadSettings(getConfig());
|
||||
|
||||
// Initialize the redis listener
|
||||
new BukkitRedisListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package me.william278.crossserversync.bukkit;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.util.io.BukkitObjectInputStream;
|
||||
import org.bukkit.util.io.BukkitObjectOutputStream;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Class for serializing and deserializing player inventories and Ender Chests contents ({@link ItemStack[]}) as base64 strings.
|
||||
* Based on https://gist.github.com/graywolf336/8153678 by graywolf336
|
||||
* Modified for 1.16 via https://gist.github.com/graywolf336/8153678#gistcomment-3551376 by efindus
|
||||
*
|
||||
* @author efindus
|
||||
* @author graywolf336
|
||||
*/
|
||||
public final class InventorySerializer {
|
||||
|
||||
/**
|
||||
* Converts the player inventory to a Base64 encoded string.
|
||||
*
|
||||
* @param player whose inventory will be turned into an array of strings.
|
||||
* @return string with serialized Inventory
|
||||
* @throws IllegalStateException in the event the item stacks cannot be saved
|
||||
*/
|
||||
public static String getSerializedInventoryContents(Player player) throws IllegalStateException {
|
||||
// This contains contents, armor and offhand (contents are indexes 0 - 35, armor 36 - 39, offhand - 40)
|
||||
return itemStackArrayToBase64(player.getInventory().getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the player inventory to a Base64 encoded string.
|
||||
*
|
||||
* @param player whose Ender Chest will be turned into an array of strings.
|
||||
* @return string with serialized Ender Chest
|
||||
* @throws IllegalStateException in the event the item stacks cannot be saved
|
||||
*/
|
||||
public static String getSerializedEnderChestContents(Player player) throws IllegalStateException {
|
||||
// This contains all slots (0-27) in the player's Ender Chest
|
||||
return itemStackArrayToBase64(player.getEnderChest().getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a player's inventory from a set of {@link ItemStack}s
|
||||
*
|
||||
* @param player The player to set the inventory of
|
||||
* @param items The array of {@link ItemStack}s to set
|
||||
*/
|
||||
public static void setPlayerItems(Player player, ItemStack[] items) {
|
||||
setInventoryItems(player.getInventory(), items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a player's ender chest from a set of {@link ItemStack}s
|
||||
*
|
||||
* @param player The player to set the inventory of
|
||||
* @param items The array of {@link ItemStack}s to set
|
||||
*/
|
||||
public static void setPlayerEnderChest(Player player, ItemStack[] items) {
|
||||
setInventoryItems(player.getEnderChest(), items);
|
||||
}
|
||||
|
||||
// Clears, then fills an inventory's items correctly.
|
||||
private static void setInventoryItems(Inventory inventory, ItemStack[] items) {
|
||||
inventory.clear();
|
||||
int index = 0;
|
||||
for (ItemStack item : items) {
|
||||
if (item != null) {
|
||||
inventory.setItem(index, item);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A method to serialize an {@link ItemStack} array to Base64 String.
|
||||
*
|
||||
* @param items to turn into a Base64 String.
|
||||
* @return Base64 string of the items.
|
||||
* @throws IllegalStateException in the event the item stacks cannot be saved
|
||||
*/
|
||||
public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException {
|
||||
try {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
try (BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream)) {
|
||||
dataOutput.writeInt(items.length);
|
||||
|
||||
for (ItemStack item : items) {
|
||||
if (item != null) {
|
||||
dataOutput.writeObject(item.serialize());
|
||||
} else {
|
||||
dataOutput.writeObject(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Unable to save item stacks.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of ItemStacks from Base64 string.
|
||||
*
|
||||
* @param data Base64 string to convert to ItemStack array.
|
||||
* @return ItemStack array created from the Base64 string.
|
||||
* @throws IOException in the event the class type cannot be decoded
|
||||
*/
|
||||
public static ItemStack[] itemStackArrayFromBase64(String data) throws IOException {
|
||||
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data))) {
|
||||
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
||||
ItemStack[] items = new ItemStack[dataInput.readInt()];
|
||||
|
||||
for (int Index = 0; Index < items.length; Index++) {
|
||||
@SuppressWarnings("unchecked") // Ignore the unchecked cast here
|
||||
Map<String, Object> stack = (Map<String, Object>) dataInput.readObject();
|
||||
|
||||
if (stack != null) {
|
||||
items[Index] = ItemStack.deserialize(stack);
|
||||
} else {
|
||||
items[Index] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IOException("Unable to decode class type.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package me.william278.crossserversync.bukkit.config;
|
||||
|
||||
import me.william278.crossserversync.Settings;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
public class ConfigLoader {
|
||||
|
||||
public static void loadSettings(FileConfiguration config) throws IllegalArgumentException {
|
||||
Settings.serverType = Settings.ServerType.BUKKIT;
|
||||
Settings.redisHost = config.getString("redis_settings.host", "localhost");
|
||||
Settings.redisPort = config.getInt("redis_settings.port", 6379);
|
||||
Settings.redisPassword = config.getString("redis_settings.password", "");
|
||||
}
|
||||
|
||||
}
|
||||
4
bukkit/src/main/resources/config.yml
Normal file
4
bukkit/src/main/resources/config.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
redis_settings:
|
||||
host: 'localhost'
|
||||
port: 6379
|
||||
password: ''
|
||||
@@ -1,6 +0,0 @@
|
||||
name: CrossServerSync
|
||||
version: '${version}'
|
||||
main: me.william278.crossserversync.CrossServerSyncSpigot
|
||||
api-version: 1.16
|
||||
author: William278
|
||||
description: '${properties.description}'
|
||||
Reference in New Issue
Block a user