Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ configure(apiDependencies) {
}

dependencies {
// Server API
// TODO make our custom plugin target paper instead of spigot
externalPlugin 'io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT'

// Core
// TODO update to correct version once we have it published
externalPlugin 'org.mvplugins.multiverse.core:multiverse-core:5.0.0-SNAPSHOT'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.mvplugins.multiverse.inventories.profile.container.ProfileContainerStoreProvider;
import org.mvplugins.multiverse.inventories.profile.group.WorldGroupManager;
import org.mvplugins.multiverse.inventories.share.Sharables;
import org.mvplugins.multiverse.inventories.util.ItemStackConverter;
import org.mvplugins.multiverse.inventories.util.Perm;
import org.bukkit.Bukkit;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
Expand Down Expand Up @@ -96,6 +97,8 @@ public final void onEnable() {
initializeDependencyInjection();
Sharables.init(this);
Perm.register(this);
ItemStackConverter.init(this);
Logging.fine("ItemStackConverter is using byte serialization: " + ItemStackConverter.hasByteSerializeSupport);
this.reloadConfig();
inventoriesConfig.get().save().onFailure(e -> Logging.severe("Failed to save config file!"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ public Try<Void> setApplyLastLocationForAllTeleports(boolean applyLastLocationFo
return this.configHandle.set(configNodes.applyLastLocationForAllTeleports, applyLastLocationForAllTeleports);
}

public boolean getUseByteSerializationForInventoryData() {
return this.configHandle.get(configNodes.useByteSerializationForInventoryData);
}

public Try<Void> setUseByteSerializationForInventoryData(boolean useByteSerializationForInventoryData) {
return this.configHandle.set(configNodes.useByteSerializationForInventoryData, useByteSerializationForInventoryData);
}

/**
* Tells whether Multiverse-Inventories should save on player logout.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ public Object serialize(Shares sharables, Class<Shares> aClass) {
.name("apply-last-location-for-all-teleports")
.build());

final ConfigNode<Boolean> useByteSerializationForInventoryData = node(ConfigNode.builder("sharables.use-byte-serialization-for-inventory-data", Boolean.class)
.comment("")
.comment("When enabled, we will use byte serialization for inventory data.")
.defaultValue(false)
.name("use-byte-serialization-for-inventory-data")
.build());

private final ConfigHeaderNode performanceHeader = node(ConfigHeaderNode.builder("performance")
.comment("")
.comment("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private void updatePersistingProfile(PersistingProfile persistingProfile, Profil
}

private void logHandlingComplete(double timeTaken, ShareHandlingEvent event) {
Logging.fine("=== %s complete for %s | time taken: %4.4f ms ===", player.getName(), event.getEventName(), timeTaken);
Logging.fine("=== %s complete for %s | \u001B[32mtime taken: %4.4f ms\u001B[0m ===",
player.getName(), event.getEventName(), timeTaken);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mvplugins.multiverse.inventories.share;

import org.mvplugins.multiverse.inventories.util.ItemStackConverter;
import org.mvplugins.multiverse.inventories.util.MinecraftTools;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -29,11 +30,14 @@ public Object serialize(ItemStack[] itemStacks) {
return mapSlots(itemStacks);
}

private Map<String, ItemStack> mapSlots(ItemStack[] itemStacks) {
Map<String, ItemStack> result = new HashMap<>(itemStacks.length);
private Map<String, Object> mapSlots(ItemStack[] itemStacks) {
Map<String, Object> result = new HashMap<>(itemStacks.length);
for (int i = 0; i < itemStacks.length; i++) {
if (itemStacks[i] != null && itemStacks[i].getType() != Material.AIR) {
result.put(Integer.toString(i), itemStacks[i]);
Object serialize = ItemStackConverter.serialize(itemStacks[i]);
if (serialize != null) {
result.put(Integer.toString(i), serialize);
}
}
}
return result;
Expand All @@ -46,7 +50,16 @@ private ItemStack[] unmapSlots(Object obj) {
}
for (int i = 0; i < inventory.length; i++) {
Object value = invMap.get(Integer.toString(i));
inventory[i] = value instanceof ItemStack item ? item : new ItemStack(Material.AIR);
if (value == null) {
inventory[i] = new ItemStack(Material.AIR);
continue;
}
ItemStack item = ItemStackConverter.deserialize(value);
if (item == null) {
inventory[i] = new ItemStack(Material.AIR);
continue;
}
inventory[i] = item;
}
return inventory;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mvplugins.multiverse.inventories.share;

import org.bukkit.inventory.ItemStack;
import org.mvplugins.multiverse.inventories.util.ItemStackConverter;

final class ItemStackSerializer implements SharableSerializer<ItemStack> {

@Override
public ItemStack deserialize(Object obj) {
return ItemStackConverter.deserialize(obj);
}

@Override
public Object serialize(ItemStack itemStack) {
return ItemStackConverter.serialize(itemStack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public boolean updatePlayer(Player player, ProfileData profile) {
return true;
}
}).serializer(new ProfileEntry(false, DataStrings.PLAYER_OFF_HAND_ITEM),
new DefaultSerializer<>(ItemStack.class)).altName("shield").build();
new ItemStackSerializer()).altName("shield").build();

/**
* Sharing Max Health.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.mvplugins.multiverse.inventories.util;

import com.dumptruckman.minecraft.util.Logging;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import org.mvplugins.multiverse.external.vavr.control.Try;
import org.mvplugins.multiverse.inventories.MultiverseInventories;
import org.mvplugins.multiverse.inventories.config.InventoriesConfig;

import java.util.Base64;

public final class ItemStackConverter {

public final static boolean hasByteSerializeSupport;

static {
hasByteSerializeSupport = Try.run(() -> ItemStack.class.getMethod("deserializeBytes", byte[].class))
.map(ignore -> true)
.recover(ignore -> false)
.getOrElse(false);
}

private static InventoriesConfig config = null;

public static void init(MultiverseInventories plugin) {
config = plugin.getServiceLocator().getService(InventoriesConfig.class);
}

@Nullable
public static ItemStack deserialize(Object obj) {
if (obj instanceof ItemStack itemStack) {
// Already handled by ConfigurationSerialization
return itemStack;
}
if (hasByteSerializeSupport && obj instanceof String string) {
byte[] bytes = Base64.getDecoder().decode(string);
return ItemStack.deserializeBytes(bytes);
}
return null;
}

@Nullable
public static Object serialize(ItemStack itemStack) {
if (config != null && config.getUseByteSerializationForInventoryData() && hasByteSerializeSupport) {
if (itemStack.getType() == Material.AIR) {
return null;
}
return Try.of(() -> Base64.getEncoder().encodeToString(itemStack.serializeAsBytes()))
.onFailure(e -> Logging.severe("Could not serialize item stack: %s", e.getMessage()))
.getOrNull();
}
// let ConfigurationSerialization handle it
return itemStack;
}

private ItemStackConverter() {
// no instantiation
}
}
1 change: 1 addition & 0 deletions src/test/resources/config/fresh_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sharables:
use-improved-respawn-location-detection: true
reset-last-location-on-death: false
apply-last-location-for-all-teleports: true
use-byte-serialization-for-inventory-data: false

performance:
save-playerdata-on-quit: false
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/config/migrated_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sharables:
use-improved-respawn-location-detection: true
reset-last-location-on-death: false
apply-last-location-for-all-teleports: true
use-byte-serialization-for-inventory-data: false

performance:
save-playerdata-on-quit: true
Expand Down
Loading