[Idea]: Folia support for OpenInv #196
@@ -99,6 +99,9 @@ public class PlayerDataManager implements IPlayerDataManager {
|
|||||||
|
|
||||||
ServerPlayer entity = new ServerPlayer(server, worldServer, profile);
|
ServerPlayer entity = new ServerPlayer(server, worldServer, profile);
|
||||||
|
|
||||||
|
// Stop listening for advancement progression - if this is not cleaned up, loading causes a memory leak.
|
||||||
|
entity.getAdvancements().stopListening();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
injectPlayer(entity);
|
injectPlayer(entity);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
|
@@ -67,18 +67,29 @@ public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPlayerOnline(@NotNull final org.bukkit.entity.Player player) {
|
public void setPlayerOnline(@NotNull final org.bukkit.entity.Player player) {
|
||||||
if (!this.playerOnline) {
|
if (this.playerOnline) {
|
||||||
try {
|
return;
|
||||||
this.owner = PlayerDataManager.getHandle(player);
|
|
||||||
PlayerEnderChestContainer enderChest = owner.getEnderChestInventory();
|
|
||||||
for (int i = 0; i < enderChest.getContainerSize(); ++i) {
|
|
||||||
enderChest.setItem(i, this.items.get(i));
|
|
||||||
}
|
|
||||||
this.items = enderChest.items;
|
|
||||||
enderChest.transaction.addAll(this.transaction);
|
|
||||||
} catch (Exception ignored) {}
|
|
||||||
this.playerOnline = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServerPlayer offlinePlayer = this.owner;
|
||||||
|
ServerPlayer onlinePlayer = PlayerDataManager.getHandle(player);
|
||||||
|
|
||||||
|
// Set owner to new player.
|
||||||
|
this.owner = onlinePlayer;
|
||||||
|
|
||||||
|
// Set player's ender chest contents to our modified contents.
|
||||||
|
PlayerEnderChestContainer onlineEnderChest = onlinePlayer.getEnderChestInventory();
|
||||||
|
for (int i = 0; i < onlineEnderChest.getContainerSize(); ++i) {
|
||||||
|
onlineEnderChest.setItem(i, this.items.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set our item array to the new inventory's array.
|
||||||
|
this.items = onlineEnderChest.items;
|
||||||
|
|
||||||
|
// Add viewers to new inventory.
|
||||||
|
onlineEnderChest.transaction.addAll(offlinePlayer.getEnderChestInventory().transaction);
|
||||||
|
|
||||||
|
this.playerOnline = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -317,7 +328,7 @@ public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.items.stream().filter((itemStack) -> !itemStack.isEmpty()).collect(Collectors.toList()).toString();
|
return this.items.stream().filter((itemStack) -> !itemStack.isEmpty()).toList().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -77,20 +77,37 @@ public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerI
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPlayerOnline(@NotNull org.bukkit.entity.Player player) {
|
public void setPlayerOnline(@NotNull org.bukkit.entity.Player player) {
|
||||||
if (!this.playerOnline) {
|
if (this.playerOnline) {
|
||||||
Player entityPlayer = PlayerDataManager.getHandle(player);
|
return;
|
||||||
entityPlayer.getInventory().transaction.addAll(this.transaction);
|
|
||||||
this.player = entityPlayer;
|
|
||||||
for (int i = 0; i < getContainerSize(); ++i) {
|
|
||||||
this.player.getInventory().setItem(i, getRawItem(i));
|
|
||||||
}
|
|
||||||
this.player.getInventory().selected = this.selected;
|
|
||||||
this.items = this.player.getInventory().items;
|
|
||||||
this.armor = this.player.getInventory().armor;
|
|
||||||
this.offhand = this.player.getInventory().offhand;
|
|
||||||
this.compartments = ImmutableList.of(this.items, this.armor, this.offhand);
|
|
||||||
this.playerOnline = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Player offlinePlayer = this.player;
|
||||||
|
Player onlinePlayer = PlayerDataManager.getHandle(player);
|
||||||
|
onlinePlayer.getInventory().transaction.addAll(this.transaction);
|
||||||
|
|
||||||
|
// Set owner to new player.
|
||||||
|
this.player = onlinePlayer;
|
||||||
|
|
||||||
|
// Set player's inventory contents to our modified contents.
|
||||||
|
Inventory onlineInventory = onlinePlayer.getInventory();
|
||||||
|
for (int i = 0; i < getContainerSize(); ++i) {
|
||||||
|
onlineInventory.setItem(i, getRawItem(i));
|
||||||
|
}
|
||||||
|
onlineInventory.selected = this.selected;
|
||||||
|
|
||||||
|
// Set our item arrays to the new inventory's arrays.
|
||||||
|
this.items = onlineInventory.items;
|
||||||
|
this.armor = onlineInventory.armor;
|
||||||
|
this.offhand = onlineInventory.offhand;
|
||||||
|
this.compartments = ImmutableList.of(this.items, this.armor, this.offhand);
|
||||||
|
|
||||||
|
// Add existing viewers to new viewer list.
|
||||||
|
Inventory offlineInventory = offlinePlayer.getInventory();
|
||||||
|
// Remove self from listing - player is always a viewer of their own inventory, prevent duplicates.
|
||||||
|
offlineInventory.transaction.remove(offlinePlayer.getBukkitEntity());
|
||||||
|
onlineInventory.transaction.addAll(offlineInventory.transaction);
|
||||||
|
|
||||||
|
this.playerOnline = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -138,7 +155,7 @@ public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static record IndexedCompartment(@Nullable NonNullList<ItemStack> compartment, int index) {}
|
private record IndexedCompartment(@Nullable NonNullList<ItemStack> compartment, int index) {}
|
||||||
|
|
||||||
private @NotNull SpecialPlayerInventory.IndexedCompartment getIndexedContent(int index) {
|
private @NotNull SpecialPlayerInventory.IndexedCompartment getIndexedContent(int index) {
|
||||||
if (index < items.size()) {
|
if (index < items.size()) {
|
||||||
|
@@ -100,6 +100,9 @@ public class PlayerDataManager implements IPlayerDataManager {
|
|||||||
|
|
||||||
ServerPlayer entity = new ServerPlayer(server, worldServer, profile, null);
|
ServerPlayer entity = new ServerPlayer(server, worldServer, profile, null);
|
||||||
|
|
||||||
|
// Stop listening for advancement progression - if this is not cleaned up, loading causes a memory leak.
|
||||||
|
entity.getAdvancements().stopListening();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
injectPlayer(entity);
|
injectPlayer(entity);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
|
@@ -67,18 +67,29 @@ public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPlayerOnline(@NotNull final org.bukkit.entity.Player player) {
|
public void setPlayerOnline(@NotNull final org.bukkit.entity.Player player) {
|
||||||
if (!this.playerOnline) {
|
if (this.playerOnline) {
|
||||||
try {
|
return;
|
||||||
this.owner = PlayerDataManager.getHandle(player);
|
|
||||||
PlayerEnderChestContainer enderChest = owner.getEnderChestInventory();
|
|
||||||
for (int i = 0; i < enderChest.getContainerSize(); ++i) {
|
|
||||||
enderChest.setItem(i, this.items.get(i));
|
|
||||||
}
|
|
||||||
this.items = enderChest.items;
|
|
||||||
enderChest.transaction.addAll(this.transaction);
|
|
||||||
} catch (Exception ignored) {}
|
|
||||||
this.playerOnline = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServerPlayer offlinePlayer = this.owner;
|
||||||
|
ServerPlayer onlinePlayer = PlayerDataManager.getHandle(player);
|
||||||
|
|
||||||
|
// Set owner to new player.
|
||||||
|
this.owner = onlinePlayer;
|
||||||
|
|
||||||
|
// Set player's ender chest contents to our modified contents.
|
||||||
|
PlayerEnderChestContainer onlineEnderChest = onlinePlayer.getEnderChestInventory();
|
||||||
|
for (int i = 0; i < onlineEnderChest.getContainerSize(); ++i) {
|
||||||
|
onlineEnderChest.setItem(i, this.items.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set our item array to the new inventory's array.
|
||||||
|
this.items = onlineEnderChest.items;
|
||||||
|
|
||||||
|
// Add viewers to new inventory.
|
||||||
|
onlineEnderChest.transaction.addAll(offlinePlayer.getEnderChestInventory().transaction);
|
||||||
|
|
||||||
|
this.playerOnline = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -77,20 +77,37 @@ public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerI
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPlayerOnline(@NotNull org.bukkit.entity.Player player) {
|
public void setPlayerOnline(@NotNull org.bukkit.entity.Player player) {
|
||||||
if (!this.playerOnline) {
|
if (this.playerOnline) {
|
||||||
Player entityPlayer = PlayerDataManager.getHandle(player);
|
return;
|
||||||
entityPlayer.getInventory().transaction.addAll(this.transaction);
|
|
||||||
this.player = entityPlayer;
|
|
||||||
for (int i = 0; i < getContainerSize(); ++i) {
|
|
||||||
this.player.getInventory().setItem(i, getRawItem(i));
|
|
||||||
}
|
|
||||||
this.player.getInventory().selected = this.selected;
|
|
||||||
this.items = this.player.getInventory().items;
|
|
||||||
this.armor = this.player.getInventory().armor;
|
|
||||||
this.offhand = this.player.getInventory().offhand;
|
|
||||||
this.compartments = ImmutableList.of(this.items, this.armor, this.offhand);
|
|
||||||
this.playerOnline = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Player offlinePlayer = this.player;
|
||||||
|
Player onlinePlayer = PlayerDataManager.getHandle(player);
|
||||||
|
onlinePlayer.getInventory().transaction.addAll(this.transaction);
|
||||||
|
|
||||||
|
// Set owner to new player.
|
||||||
|
this.player = onlinePlayer;
|
||||||
|
|
||||||
|
// Set player's inventory contents to our modified contents.
|
||||||
|
Inventory onlineInventory = onlinePlayer.getInventory();
|
||||||
|
for (int i = 0; i < getContainerSize(); ++i) {
|
||||||
|
onlineInventory.setItem(i, getRawItem(i));
|
||||||
|
}
|
||||||
|
onlineInventory.selected = this.selected;
|
||||||
|
|
||||||
|
// Set our item arrays to the new inventory's arrays.
|
||||||
|
this.items = onlineInventory.items;
|
||||||
|
this.armor = onlineInventory.armor;
|
||||||
|
this.offhand = onlineInventory.offhand;
|
||||||
|
this.compartments = ImmutableList.of(this.items, this.armor, this.offhand);
|
||||||
|
|
||||||
|
// Add existing viewers to new viewer list.
|
||||||
|
Inventory offlineInventory = offlinePlayer.getInventory();
|
||||||
|
// Remove self from listing - player is always a viewer of their own inventory, prevent duplicates.
|
||||||
|
offlineInventory.transaction.remove(offlinePlayer.getBukkitEntity());
|
||||||
|
onlineInventory.transaction.addAll(offlineInventory.transaction);
|
||||||
|
|
||||||
|
this.playerOnline = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -476,8 +476,9 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
|
|||||||
|
|
||||||
if (!disableSaving()
|
if (!disableSaving()
|
||||||
&& current != null
|
&& current != null
|
||||||
&& current.getPlayer() instanceof Player player && !player.isOnline()) {
|
&& current.getPlayer() instanceof Player player
|
||||||
this.accessor.getPlayerDataManager().inject(player).saveData();
|
&& !player.isOnline()) {
|
||||||
|
this.accessor.getPlayerDataManager().inject(player).saveData();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2011-2021 lishid. All rights reserved.
|
* Copyright (C) 2011-2022 lishid. All rights reserved.
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@@ -27,13 +27,17 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
public class OpenInventoryView extends InventoryView {
|
public class OpenInventoryView extends InventoryView {
|
||||||
|
|
||||||
private final Player player;
|
private final @NotNull Player player;
|
||||||
private final ISpecialInventory inventory;
|
private final @NotNull ISpecialInventory inventory;
|
||||||
private final String titleKey;
|
private final @NotNull String titleKey;
|
||||||
private final String titleDefaultSuffix;
|
private final @NotNull String titleDefaultSuffix;
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
public OpenInventoryView(Player player, ISpecialInventory inventory, String titleKey, String titleDefaultSuffix) {
|
public OpenInventoryView(
|
||||||
|
@NotNull Player player,
|
||||||
|
@NotNull ISpecialInventory inventory,
|
||||||
|
@NotNull String titleKey,
|
||||||
|
@NotNull String titleDefaultSuffix) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.inventory = inventory;
|
this.inventory = inventory;
|
||||||
this.titleKey = titleKey;
|
this.titleKey = titleKey;
|
||||||
|
Reference in New Issue
Block a user