Implement migration for backpacks with more than 6 rows

Minecraft 1.14 no longer allows more than 6 rows. The plugin will try to put all the items within empty space in the backpack, if there are still more items than can fit within the 6 rows and the owner is online the extra items will drop to the ground.
Start work on #64
This commit is contained in:
GeorgH93 2020-01-26 00:17:20 +01:00
parent 018588ef53
commit 46672c44f1
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
3 changed files with 33 additions and 2 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>Minepacks</artifactId>
<version>2.1.3</version>
<version>2.1.4-SNAPSHOT</version>
<scm>
<connection>scm:git:git@github.com:GeorgH93/Minepacks.git</connection>

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 GeorgH93
* Copyright (C) 2020 GeorgH93
*
* 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
@ -23,6 +23,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
@ -90,6 +91,11 @@ public Backpack(OfflinePlayer owner, int size)
public Backpack(OfflinePlayer owner, int size, int ID)
{
if(MCVersion.isNewerOrEqualThan(MCVersion.MC_1_14))
{
size = Math.min(size, 54);
Minepacks.getInstance().getLogger().warning("Backpacks with more than 6 rows are no longer supported on Minecraft 1.14 and up!");
}
this.owner = owner;
titleOther = StringUtils.limitLength(String.format(titleOtherFormat, owner.getName()), 32);
bp = Bukkit.createInventory(this, size, titleOther);
@ -110,6 +116,30 @@ public Backpack(OfflinePlayer owner, int size, int ID)
public Backpack(OfflinePlayer owner, ItemStack[] backpack, int ID)
{
this(owner, backpack.length, ID);
if(MCVersion.isNewerOrEqualThan(MCVersion.MC_1_14) && backpack.length > 54)
{ // Try to optimize space usage to compress items into only 6 rows
ItemStack[] dbStack = backpack;
backpack = new ItemStack[54];
int filled = 0;
for(ItemStack stack : dbStack)
{
if(stack == null || stack.getType() == Material.AIR) continue;
if(filled == 54)
{
Minepacks.getInstance().getLogger().warning(owner.getName() + "'s backpack has to many items.");
if(owner.isOnline())
{
owner.getPlayer().getWorld().dropItemNaturally(owner.getPlayer().getLocation(), stack);
//Owner is online!
}
else throw new RuntimeException("Backpack to big for MC 1.14 and up!");
}
else
{
backpack[filled++] = stack;
}
}
}
bp.setContents(backpack);
}

View File

@ -167,6 +167,7 @@ public int getBackpackMaxSize()
{
int size = getConfigE().getInt("MaxSize", 6);
if(MCVersion.isNewerOrEqualThan(MCVersion.MC_1_14)) size = Math.min(6, size);
if(size > 6) logger.warning("Backpacks with more than 6 rows will not work on MC 1.14");
return Math.max(1, size);
}