Fix backpack resize on MC 1.20 with viaversion

This commit is contained in:
GeorgH93 2023-07-13 22:32:29 +02:00
parent 7bb0e2f79b
commit 5e24997f93
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
3 changed files with 24 additions and 27 deletions

View File

@ -27,6 +27,7 @@
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Map; import java.util.Map;
import java.util.UUID;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public interface Backpack extends InventoryHolder public interface Backpack extends InventoryHolder
@ -36,8 +37,16 @@ public interface Backpack extends InventoryHolder
* *
* @return The owner of the backpack; * @return The owner of the backpack;
*/ */
@Deprecated
@NotNull OfflinePlayer getOwner(); @NotNull OfflinePlayer getOwner();
/**
* Gets the id of the player owning the backpack.
*
* @return The uuid of the owning player.
*/
UUID getOwnerId();
/** /**
* Let a given player open this backpack. * Let a given player open this backpack.
* *

View File

@ -22,6 +22,7 @@
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.InventoryCompressor; import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.InventoryCompressor;
import at.pcgamingfreaks.Util.StringUtils; import at.pcgamingfreaks.Util.StringUtils;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -35,6 +36,7 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class Backpack implements at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack public class Backpack implements at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack
@ -43,10 +45,11 @@ public class Backpack implements at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack
private static Object titleOwn; private static Object titleOwn;
private static String titleOtherFormat; private static String titleOtherFormat;
private final String titleOther; private final String titleOther;
private final OfflinePlayer owner; @Getter private final UUID ownerId;
private final Map<Player, Boolean> opened = new ConcurrentHashMap<>(); //Thanks Minecraft 1.14 private final Map<Player, Boolean> opened = new ConcurrentHashMap<>(); //Thanks Minecraft 1.14
private Inventory bp; private Inventory bp;
private int size, ownerID; @Getter private int size;
@Getter @Setter private int ownerDatabaseId;
private boolean hasChanged; private boolean hasChanged;
public static void setTitle(final @NotNull String title, final @NotNull String titleOther) public static void setTitle(final @NotNull String title, final @NotNull String titleOther)
@ -72,11 +75,11 @@ public Backpack(OfflinePlayer owner, int size, int ID)
size = 54; size = 54;
Minepacks.getInstance().getLogger().warning("Backpacks with more than 6 rows are no longer supported on Minecraft 1.14 and up!"); Minepacks.getInstance().getLogger().warning("Backpacks with more than 6 rows are no longer supported on Minecraft 1.14 and up!");
} }
this.owner = owner; this.ownerId = owner.getUniqueId();
titleOther = StringUtils.limitLength(String.format(titleOtherFormat, owner.getName()), 32); titleOther = StringUtils.limitLength(String.format(titleOtherFormat, owner.getName()), 32);
bp = Bukkit.createInventory(this, size, titleOther); bp = Bukkit.createInventory(this, size, titleOther);
this.size = size; this.size = size;
ownerID = ID; ownerDatabaseId = ID;
} }
public Backpack(final OfflinePlayer owner, ItemStack[] backpack, final int ID) public Backpack(final OfflinePlayer owner, ItemStack[] backpack, final int ID)
@ -108,29 +111,20 @@ public Backpack(final OfflinePlayer owner, ItemStack[] backpack, final int ID)
} }
bp.setContents(backpack); bp.setContents(backpack);
} }
public int getOwnerID()
{
return ownerID;
}
public void setOwnerID(int id)
{
ownerID = id;
}
@Override @Override
@Deprecated
public @NotNull OfflinePlayer getOwner() public @NotNull OfflinePlayer getOwner()
{ {
return owner; return Bukkit.getServer().getOfflinePlayer(ownerId);
} }
private void checkResize() private void checkResize()
{ {
if(owner.isOnline()) Player owner = Bukkit.getServer().getPlayer(this.ownerId);
if(owner != null)
{ {
Player owner = this.owner.getPlayer(); if(owner.hasPermission(Permissions.USE))
if(owner != null && owner.hasPermission(Permissions.USE))
{ {
int size = Minepacks.getInstance().getBackpackPermSize(owner); int size = Minepacks.getInstance().getBackpackPermSize(owner);
if(size != bp.getSize()) if(size != bp.getSize())
@ -153,7 +147,7 @@ public void open(final @NotNull Player player, final boolean editable)
{ {
checkResize(); checkResize();
opened.put(player, editable); opened.put(player, editable);
if(owner.equals(player)) InventoryUtils.openInventoryWithCustomTitlePrepared(player, bp, titleOwn); if(ownerId.equals(player.getUniqueId())) InventoryUtils.openInventoryWithCustomTitlePrepared(player, bp, titleOwn);
else player.openInventory(bp); else player.openInventory(bp);
} }
@ -194,12 +188,6 @@ public boolean canEdit(@NotNull Player player)
return opened.containsKey(player) && opened.get(player); return opened.containsKey(player) && opened.get(player);
} }
@Override
public int getSize()
{
return size;
}
public @NotNull List<ItemStack> setSize(int newSize) public @NotNull List<ItemStack> setSize(int newSize)
{ {
opened.forEach((key, value) -> key.closeInventory()); // Close all open views of the inventory opened.forEach((key, value) -> key.closeInventory()); // Close all open views of the inventory

View File

@ -202,7 +202,7 @@ public void updatePlayer(final Player player)
public void saveBackpack(final Backpack backpack) public void saveBackpack(final Backpack backpack)
{ {
final byte[] data = itsSerializer.serialize(backpack.getInventory()); final byte[] data = itsSerializer.serialize(backpack.getInventory());
final int id = backpack.getOwnerID(), usedSerializer = itsSerializer.getUsedSerializer(); final int id = backpack.getOwnerDatabaseId(), usedSerializer = itsSerializer.getUsedSerializer();
final String nameOrUUID = getPlayerFormattedUUID(backpack.getOwner()), name = backpack.getOwner().getName(); final String nameOrUUID = getPlayerFormattedUUID(backpack.getOwner()), name = backpack.getOwner().getName();
Runnable runnable = () -> { Runnable runnable = () -> {
@ -219,7 +219,7 @@ public void saveBackpack(final Backpack backpack)
{ {
final int newID = rs.getInt(fieldPlayerID); final int newID = rs.getInt(fieldPlayerID);
DBTools.runStatement(connection, queryInsertBp, newID, data, usedSerializer); DBTools.runStatement(connection, queryInsertBp, newID, data, usedSerializer);
plugin.getServer().getScheduler().runTask(plugin, () -> backpack.setOwnerID(newID)); plugin.getServer().getScheduler().runTask(plugin, () -> backpack.setOwnerDatabaseId(newID));
} }
else else
{ {