Skip to content

Commit

Permalink
Nearly done with separate inventories
Browse files Browse the repository at this point in the history
  • Loading branch information
hsyyid committed Feb 25, 2016
1 parent 44dd2a0 commit 3f23215
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@
package io.github.hsyyid.essentialcmds.listeners;

import io.github.hsyyid.essentialcmds.utils.Utils;

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.entity.DestructEntityEvent;
import org.spongepowered.api.event.filter.cause.First;

public class PlayerDeathListener
{
@Listener
public void onPlayerDeath(DestructEntityEvent event, @First Player died)
public void onPlayerDeath(DestructEntityEvent event)
{
Utils.setLastTeleportOrDeathLocation(died.getUniqueId(), died.getLocation());
if (event.getTargetEntity() instanceof Player)
{
Player died = (Player) event.getTargetEntity();

Utils.savePlayerInventory(died, died.getWorld().getUniqueId());
Utils.setLastTeleportOrDeathLocation(died.getUniqueId(), died.getLocation());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void onPlayerMove(DisplaceEntityEvent event, @First Player player)
World newWorld = event.getToTransform().getExtent();

Utils.savePlayerInventory(player, oldWorld.getUniqueId());
Utils.updatePlayerInventory(player, newWorld.getUniqueId());

if (!Utils.doShareInventories(oldWorld.getName(), newWorld.getName()))
{
Utils.updatePlayerInventory(player, newWorld.getUniqueId());
}

player.offer(Keys.GAME_MODE, newWorld.getProperties().getGameMode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public void save()
@Override
public void populate()
{
get().getNode("world", "inventory", "separate").setValue(true).setComment("Toggles separate inventories between worlds.");
get().getNode("world", "inventory", "groups").setComment("Contains all world inventory groups.");
get().getNode("world", "inventory", "groups", "example").setValue("world, DIM1, DIM-1").setComment("Example world inventory group.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ public static Object serializeItemStack(ItemStack itemStack)
public static Optional<ItemStack> readItemStack(ConfigurationNode node, Object number)
{
DataView view = ConfigurateTranslator.instance().translateFrom(node);
view = (DataView) view.get(DataQuery.of(String.valueOf(number))).get();

if (view.get(DataQuery.of(String.valueOf(number))).isPresent())
view = (DataView) view.get(DataQuery.of(String.valueOf(number))).get();
else
System.out.println(view.toString());

return Sponge.getDataManager().deserialize(ItemStack.class, view);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,24 @@
*/
package io.github.hsyyid.essentialcmds.utils;

import com.google.common.collect.Lists;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.type.CarriedInventory;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class PlayerInventory
{
private UUID playerUuid;
private UUID worldUuid;
private List<ItemStack> slots;
private List<ItemStack> armorSlots;

public PlayerInventory(UUID playerUuid, UUID worldUuid, List<ItemStack> slots)
public PlayerInventory(UUID playerUuid, UUID worldUuid, List<ItemStack> slots, List<ItemStack> armorSlots)
{
this.playerUuid = playerUuid;
this.worldUuid = worldUuid;
this.slots = slots;
}

public PlayerInventory(CarriedInventory<Player> inventory, UUID worldUuid)
{
this.slots = Lists.newArrayList();
this.worldUuid = worldUuid;

if (inventory.getCarrier().isPresent())
this.playerUuid = inventory.getCarrier().get().getUniqueId();

for (Inventory slot : inventory.slots())
{
Optional<ItemStack> stack = slot.peek();

if (stack.isPresent())
this.slots.add(stack.get());
}
this.armorSlots = armorSlots;
}

public UUID getPlayerUuid()
Expand All @@ -78,4 +58,9 @@ public List<ItemStack> getSlots()
{
return slots;
}

public List<ItemStack> getArmorSlots()
{
return armorSlots;
}
}
149 changes: 134 additions & 15 deletions src/main/java/io/github/hsyyid/essentialcmds/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.github.hsyyid.essentialcmds.managers.config.RulesConfig;
import io.github.hsyyid.essentialcmds.managers.config.SpawnConfig;
import io.github.hsyyid.essentialcmds.managers.config.WarpConfig;
import io.github.hsyyid.essentialcmds.managers.config.WorldConfig;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import org.spongepowered.api.Game;
Expand Down Expand Up @@ -94,8 +95,11 @@ public class Utils
private static Configurable spawnConfig = SpawnConfig.getConfig();
private static Configurable playerDataConfig = PlayerDataConfig.getConfig();
private static Configurable inventoryConfig = InventoryConfig.getConfig();
private static Configurable worldConfig = WorldConfig.getConfig();
private static ConfigManager configManager = new ConfigManager();

public static final String[] ARMOR = new String[] { "helmet", "chestplate", "leggings", "boots" };

public static void setSQLPort(String value)
{
Configs.setValue(mainConfig, new Object[] { "mysql", "port" }, value);
Expand Down Expand Up @@ -1241,22 +1245,81 @@ public static void deleteWarp(String warpName)
Configs.removeChild(warpsConfig, new Object[] { "warps" }, warpName);
}

public static void savePlayerInventory(Player player, UUID worldUuid)
public static void savePlayerInventory(Player player, UUID worldUUID)
{
List<Inventory> slots = Lists.newArrayList(player.getInventory().slots());
if (!Utils.areSeparateWorldInventoriesEnabled())
return;

String worldName = Sponge.getServer().getWorld(worldUUID).get().getName();
Set<UUID> worlds = Sets.newHashSet();

for (World world : Sponge.getServer().getWorlds())
{
if (Utils.doShareInventories(worldName, world.getName()))
{
if(!worlds.contains(world.getUniqueId()))
worlds.add(world.getUniqueId());
}
}

for (int i = 0; i < slots.size(); i++)
for (UUID worldUuid : worlds)
{
Optional<ItemStack> stack = slots.get(i).peek();
List<Inventory> slots = Lists.newArrayList(player.getInventory().slots());

for (int i = 0; i < slots.size(); i++)
{
Optional<ItemStack> stack = slots.get(i).peek();

if (stack.isPresent())
{
Object object = ItemStackSerializer.serializeItemStack(stack.get());
Configs.setValue(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots", String.valueOf(i) }, object);
}
else if (Configs.getConfig(inventoryConfig).getNode("inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots").getChildrenMap().containsKey(String.valueOf(i)))
{
Configs.removeChild(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots" }, String.valueOf(i));
}
}

// Save Armor
if (player.getHelmet().isPresent())
{
Object object = ItemStackSerializer.serializeItemStack(player.getHelmet().get());
Configs.setValue(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots", "helmet" }, object);
}
else if (Configs.getConfig(inventoryConfig).getNode("inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots").getChildrenMap().containsKey("helmet"))
{
Configs.removeChild(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots" }, "helmet");
}

if (player.getChestplate().isPresent())
{
Object object = ItemStackSerializer.serializeItemStack(player.getChestplate().get());
Configs.setValue(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots", "chestplate" }, object);
}
else if (Configs.getConfig(inventoryConfig).getNode("inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots").getChildrenMap().containsKey("chestplate"))
{
Configs.removeChild(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots" }, "chestplate");
}

if (player.getLeggings().isPresent())
{
Object object = ItemStackSerializer.serializeItemStack(player.getLeggings().get());
Configs.setValue(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots", "leggings" }, object);
}
else if (Configs.getConfig(inventoryConfig).getNode("inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots").getChildrenMap().containsKey("leggings"))
{
Configs.removeChild(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots" }, "leggings");
}

if (stack.isPresent())
if (player.getBoots().isPresent())
{
Object object = ItemStackSerializer.serializeItemStack(stack.get());
Configs.setValue(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots", String.valueOf(i) }, object);
Object object = ItemStackSerializer.serializeItemStack(player.getBoots().get());
Configs.setValue(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots", "boots" }, object);
}
else if (Configs.getConfig(inventoryConfig).getNode("inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots").getChildrenMap().containsKey(String.valueOf(i)))
else if (Configs.getConfig(inventoryConfig).getNode("inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots").getChildrenMap().containsKey("boots"))
{
Configs.removeChild(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots" }, String.valueOf(i));
Configs.removeChild(inventoryConfig, new Object[] { "inventory", player.getUniqueId().toString(), worldUuid.toString(), "slots" }, "boots");
}
}
}
Expand All @@ -1268,20 +1331,42 @@ public static PlayerInventory getPlayerInventory(UUID playerUuid, UUID worldUuid

for (int slot = 0; slot < parentNode.getChildrenMap().keySet().size(); slot++)
{
ConfigurationNode inventoryNode = Configs.getConfig(inventoryConfig).getNode("inventory", playerUuid.toString(), worldUuid.toString(), "slots", String.valueOf(slot));
Optional<ItemStack> optionalStack = ItemStackSerializer.readItemStack(inventoryNode, slot);
if (parentNode.getChildrenMap().keySet().contains(String.valueOf(slot)))
{
ConfigurationNode inventoryNode = Configs.getConfig(inventoryConfig).getNode("inventory", playerUuid.toString(), worldUuid.toString(), "slots", String.valueOf(slot));
Optional<ItemStack> optionalStack = ItemStackSerializer.readItemStack(inventoryNode, slot);
slots.add(optionalStack.orElse(null));
}
else
{
slots.add(null);
}
}

List<ItemStack> armorSlots = Lists.newArrayList();

if (optionalStack.isPresent())
for (String armor : Utils.ARMOR)
{
if (parentNode.getChildrenMap().keySet().contains(armor))
{
ConfigurationNode inventoryNode = Configs.getConfig(inventoryConfig).getNode("inventory", playerUuid.toString(), worldUuid.toString(), "slots", armor);
Optional<ItemStack> optionalStack = ItemStackSerializer.readItemStack(inventoryNode, armor);
armorSlots.add(optionalStack.orElse(null));
}
else
{
slots.add(optionalStack.get());
armorSlots.add(null);
}
}

return new PlayerInventory(playerUuid, worldUuid, slots);
return new PlayerInventory(playerUuid, worldUuid, slots, armorSlots);
}

public static void updatePlayerInventory(Player player, UUID worldUuid)
{
if (!Utils.areSeparateWorldInventoriesEnabled())
return;

PlayerInventory playerInventory = Utils.getPlayerInventory(player.getUniqueId(), worldUuid);
player.getInventory().clear();

Expand All @@ -1291,8 +1376,42 @@ public static void updatePlayerInventory(Player player, UUID worldUuid)

for (int i = 0; i < playerInventory.getSlots().size(); i++)
{
slots.next().set(playerInventory.getSlots().get(i));
if (playerInventory.getSlots().get(i) != null)
slots.next().set(playerInventory.getSlots().get(i));
}

if (playerInventory.getArmorSlots().get(0) != null)
player.setHelmet(playerInventory.getArmorSlots().get(0));

if (playerInventory.getArmorSlots().get(1) != null)
player.setChestplate(playerInventory.getArmorSlots().get(1));

if (playerInventory.getArmorSlots().get(2) != null)
player.setLeggings(playerInventory.getArmorSlots().get(2));

if (playerInventory.getArmorSlots().get(3) != null)
player.setBoots(playerInventory.getArmorSlots().get(3));
}
}

public static boolean areSeparateWorldInventoriesEnabled()
{
return Configs.getConfig(worldConfig).getNode("world", "inventory", "separate").getBoolean();
}

public static boolean doShareInventories(String world1, String world2)
{
CommentedConfigurationNode valueNode = Configs.getConfig(worldConfig).getNode("world", "inventory", "groups");

for (Object group : valueNode.getChildrenMap().keySet())
{
String worldString = Configs.getConfig(worldConfig).getNode("world", "inventory", "groups", String.valueOf(group)).getString();
List<String> worlds = Arrays.asList(worldString.split("\\s*,\\s*"));

if (worlds.contains(world1) && worlds.contains(world2))
return true;
}

return false;
}
}

0 comments on commit 3f23215

Please sign in to comment.