Skip to content

Commit f4508df

Browse files
committed
feat: remove migrate command and migrate all on startup
1 parent 52ddf1a commit f4508df

File tree

26 files changed

+450
-978
lines changed

26 files changed

+450
-978
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.imprex.zip.common;
2+
3+
public class BPConstants {
4+
5+
public static final int VERSION = 2;
6+
public static final int INVENTORY_VERSION = 2;
7+
8+
public static final String KEY_VERSION = "version";
9+
10+
public static final String KEY_ID = "id";
11+
public static final String KEY_TYPE_RAW = "typeRaw";
12+
public static final String KEY_INVENTORY = "inventory";
13+
14+
public static final String KEY_INVENTORY_VERSION = "version";
15+
public static final String KEY_INVENTORY_DATA_VERSION = "dataVersion";
16+
public static final String KEY_INVENTORY_ITEMS = "items";
17+
public static final String KEY_INVENTORY_ITEMS_SIZE = "itemsSize";
18+
public static final String KEY_INVENTORY_SLOT = "ZIPslot";
19+
}

zip-common/src/main/java/net/imprex/zip/common/BPKey.java

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.imprex.zip.nms.api;
2+
3+
import java.util.List;
4+
5+
public record ItemStackContainerResult(int containerSize, List<ItemStackWithSlot> items) {
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.imprex.zip.nms.api;
2+
3+
import org.bukkit.inventory.ItemStack;
4+
5+
public record ItemStackWithSlot(int slot, ItemStack item) {
6+
}

zip-nms/zip-nms-api/src/main/java/net/imprex/zip/nms/api/NmsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface NmsManager {
1010

1111
JsonObject itemstackToJsonElement(ItemStack[] items);
1212

13-
ItemStack[] jsonElementToItemStack(JsonObject jsonElement);
13+
ItemStackContainerResult jsonElementToItemStack(JsonObject jsonElement);
1414

1515
JsonObject migrateToJsonElement(byte[] binary);
1616

zip-nms/zip-nms-v1_19_R1/src/main/java/net/imprex/zip/nms/v1_19_R1/ZipNmsManager.java

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import java.io.IOException;
55
import java.lang.reflect.Method;
66
import java.util.ArrayList;
7-
import java.util.Arrays;
8-
import java.util.Iterator;
97
import java.util.List;
108
import java.util.UUID;
119

@@ -24,9 +22,10 @@
2422
import com.mojang.serialization.DynamicOps;
2523
import com.mojang.serialization.JsonOps;
2624

27-
import net.imprex.zip.common.BPKey;
25+
import net.imprex.zip.common.BPConstants;
2826
import net.imprex.zip.common.ReflectionUtil;
29-
import net.imprex.zip.common.ZIPLogger;
27+
import net.imprex.zip.nms.api.ItemStackContainerResult;
28+
import net.imprex.zip.nms.api.ItemStackWithSlot;
3029
import net.imprex.zip.nms.api.NmsManager;
3130
import net.minecraft.SharedConstants;
3231
import net.minecraft.core.RegistryAccess;
@@ -67,35 +66,31 @@ public JsonObject itemstackToJsonElement(ItemStack[] items) {
6766
DataResult<JsonElement> result = net.minecraft.world.item.ItemStack.CODEC.encodeStart(DYNAMIC_OPS_JSON, minecraftItem);
6867
JsonObject resultJson = result.getOrThrow(false, error -> {}).getAsJsonObject();
6968

70-
resultJson.addProperty(BPKey.INVENTORY_SLOT, slot);
69+
resultJson.addProperty(BPConstants.KEY_INVENTORY_SLOT, slot);
7170
jsonItems.add(resultJson);
7271
}
7372

7473
JsonObject outputJson = new JsonObject();
75-
outputJson.addProperty(BPKey.INVENTORY_VERSION, 2);
76-
outputJson.addProperty(BPKey.INVENTORY_DATA_VERSION, DATA_VERSION);
77-
outputJson.addProperty(BPKey.INVENTORY_ITEMS_SIZE, items.length);
78-
outputJson.add(BPKey.INVENTORY_ITEMS, jsonItems);
74+
outputJson.addProperty(BPConstants.KEY_INVENTORY_VERSION, BPConstants.INVENTORY_VERSION);
75+
outputJson.addProperty(BPConstants.KEY_INVENTORY_DATA_VERSION, DATA_VERSION);
76+
outputJson.addProperty(BPConstants.KEY_INVENTORY_ITEMS_SIZE, items.length);
77+
outputJson.add(BPConstants.KEY_INVENTORY_ITEMS, jsonItems);
7978
return outputJson;
8079
}
8180

8281
@Override
83-
public ItemStack[] jsonElementToItemStack(JsonObject json) {
82+
public ItemStackContainerResult jsonElementToItemStack(JsonObject json) {
8483
// check if current version the same
85-
if (json.get(BPKey.INVENTORY_VERSION).getAsInt() != 2) {
84+
if (json.get(BPConstants.KEY_INVENTORY_VERSION).getAsInt() != BPConstants.INVENTORY_VERSION) {
8685
throw new IllegalStateException("Unable to convert binary to itemstack because zip version is missmatching");
8786
}
8887

89-
int dataVersion = json.get(BPKey.INVENTORY_DATA_VERSION).getAsInt();
90-
int itemsSize = json.get(BPKey.INVENTORY_ITEMS_SIZE).getAsInt();
88+
int dataVersion = json.get(BPConstants.KEY_INVENTORY_DATA_VERSION).getAsInt();
89+
int itemsSize = json.get(BPConstants.KEY_INVENTORY_ITEMS_SIZE).getAsInt();
9190

92-
// convert json into bukkit item
93-
ItemStack[] items = new ItemStack[itemsSize];
94-
Arrays.fill(items, new ItemStack(Material.AIR));
91+
List<ItemStackWithSlot> items = new ArrayList<>();
9592

96-
List<ItemStack> duplicateSlot = null;
97-
98-
JsonArray jsonItems = json.get(BPKey.INVENTORY_ITEMS).getAsJsonArray();
93+
JsonArray jsonItems = json.get(BPConstants.KEY_INVENTORY_ITEMS).getAsJsonArray();
9994
for (JsonElement item : jsonItems) {
10095
Dynamic<JsonElement> dynamicItem = new Dynamic<>(JsonOps.INSTANCE, item);
10196
Dynamic<JsonElement> dynamicItemFixed = DataFixers.getDataFixer()
@@ -106,56 +101,12 @@ public ItemStack[] jsonElementToItemStack(JsonObject json) {
106101
.getOrThrow(false, error -> {});
107102

108103
ItemStack bukkitItem = CraftItemStack.asCraftMirror(minecraftItem);
109-
int slot = item.getAsJsonObject().get(BPKey.INVENTORY_SLOT).getAsInt();
110-
111-
if (itemsSize <= slot) {
112-
// something went wrong !? maybe user modified it him self
113-
ZIPLogger.warn("Slot size was extended from " + itemsSize + " to " + slot + " this should not happen. Do not change the slot number inside the config manually!?");
114-
115-
ItemStack[] newItems = new ItemStack[slot + 1];
116-
System.arraycopy(items, 0, newItems, 0, items.length);
117-
Arrays.fill(newItems, items.length, newItems.length, new ItemStack(Material.AIR));
118-
items = newItems;
119-
}
104+
int slot = item.getAsJsonObject().get(BPConstants.KEY_INVENTORY_SLOT).getAsInt();
120105

121-
if (items[slot].getType() != Material.AIR) {
122-
if (duplicateSlot == null) {
123-
duplicateSlot = new ArrayList<>();
124-
}
125-
duplicateSlot.add(bukkitItem);
126-
ZIPLogger.warn("Duplicate item found on slot " + slot + " this should not happen. Do not change the slot number inside the config manually!?");
127-
} else {
128-
items[slot] = bukkitItem;
129-
}
130-
}
131-
132-
// fill existing empty slots with duplicate item
133-
while (duplicateSlot != null && !duplicateSlot.isEmpty()) {
134-
outher: for (Iterator<ItemStack> iterator = duplicateSlot.iterator(); iterator.hasNext();) {
135-
ItemStack itemStack = (ItemStack) iterator.next();
136-
137-
for (int i = 0; i < items.length; i++) {
138-
if (items[i].getType() == Material.AIR) {
139-
items[i] = itemStack;
140-
iterator.remove();
141-
break;
142-
} else if (i == items.length - 1) {
143-
break outher;
144-
}
145-
}
146-
}
147-
148-
// extend slot limit and try again
149-
if (!duplicateSlot.isEmpty()) {
150-
int extendedSlots = items.length + duplicateSlot.size();
151-
ItemStack[] newItems = new ItemStack[extendedSlots];
152-
System.arraycopy(items, 0, newItems, 0, items.length);
153-
Arrays.fill(newItems, items.length, newItems.length, new ItemStack(Material.AIR));
154-
items = newItems;
155-
}
106+
items.add(new ItemStackWithSlot(slot, bukkitItem));
156107
}
157108

158-
return items;
109+
return new ItemStackContainerResult(itemsSize, items);
159110
}
160111

161112
@Override
@@ -188,18 +139,18 @@ public JsonObject migrateToJsonElement(byte[] binary) {
188139
DataResult<JsonElement> result = net.minecraft.world.item.ItemStack.CODEC.encodeStart(DYNAMIC_OPS_JSON, minecraftItem);
189140
JsonObject resultJson = result.getOrThrow(false, error -> {}).getAsJsonObject();
190141

191-
resultJson.addProperty(BPKey.INVENTORY_SLOT, currentSlot);
142+
resultJson.addProperty(BPConstants.KEY_INVENTORY_SLOT, currentSlot);
192143
jsonItems.add(resultJson);
193144

194145
currentSlot++;
195146
}
196147
}
197148

198149
JsonObject json = new JsonObject();
199-
json.addProperty(BPKey.INVENTORY_VERSION, 2);
200-
json.addProperty(BPKey.INVENTORY_DATA_VERSION, DATA_VERSION);
201-
json.addProperty(BPKey.INVENTORY_ITEMS_SIZE, list.size());
202-
json.add(BPKey.INVENTORY_ITEMS, jsonItems);
150+
json.addProperty(BPConstants.KEY_INVENTORY_VERSION, BPConstants.INVENTORY_VERSION);
151+
json.addProperty(BPConstants.KEY_INVENTORY_DATA_VERSION, DATA_VERSION);
152+
json.addProperty(BPConstants.KEY_INVENTORY_ITEMS_SIZE, list.size());
153+
json.add(BPConstants.KEY_INVENTORY_ITEMS, jsonItems);
203154
return json;
204155
}
205156

zip-nms/zip-nms-v1_19_R2/src/main/java/net/imprex/zip/nms/v1_19_R2/ZipNmsManager.java

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import java.io.IOException;
55
import java.lang.reflect.Method;
66
import java.util.ArrayList;
7-
import java.util.Arrays;
8-
import java.util.Iterator;
97
import java.util.List;
108
import java.util.UUID;
119

@@ -24,9 +22,10 @@
2422
import com.mojang.serialization.DynamicOps;
2523
import com.mojang.serialization.JsonOps;
2624

27-
import net.imprex.zip.common.BPKey;
25+
import net.imprex.zip.common.BPConstants;
2826
import net.imprex.zip.common.ReflectionUtil;
29-
import net.imprex.zip.common.ZIPLogger;
27+
import net.imprex.zip.nms.api.ItemStackContainerResult;
28+
import net.imprex.zip.nms.api.ItemStackWithSlot;
3029
import net.imprex.zip.nms.api.NmsManager;
3130
import net.minecraft.SharedConstants;
3231
import net.minecraft.core.RegistryAccess;
@@ -67,35 +66,31 @@ public JsonObject itemstackToJsonElement(ItemStack[] items) {
6766
DataResult<JsonElement> result = net.minecraft.world.item.ItemStack.CODEC.encodeStart(DYNAMIC_OPS_JSON, minecraftItem);
6867
JsonObject resultJson = result.getOrThrow(false, error -> {}).getAsJsonObject();
6968

70-
resultJson.addProperty(BPKey.INVENTORY_SLOT, slot);
69+
resultJson.addProperty(BPConstants.KEY_INVENTORY_SLOT, slot);
7170
jsonItems.add(resultJson);
7271
}
7372

7473
JsonObject outputJson = new JsonObject();
75-
outputJson.addProperty(BPKey.INVENTORY_VERSION, 2);
76-
outputJson.addProperty(BPKey.INVENTORY_DATA_VERSION, DATA_VERSION);
77-
outputJson.addProperty(BPKey.INVENTORY_ITEMS_SIZE, items.length);
78-
outputJson.add(BPKey.INVENTORY_ITEMS, jsonItems);
74+
outputJson.addProperty(BPConstants.KEY_INVENTORY_VERSION, BPConstants.INVENTORY_VERSION);
75+
outputJson.addProperty(BPConstants.KEY_INVENTORY_DATA_VERSION, DATA_VERSION);
76+
outputJson.addProperty(BPConstants.KEY_INVENTORY_ITEMS_SIZE, items.length);
77+
outputJson.add(BPConstants.KEY_INVENTORY_ITEMS, jsonItems);
7978
return outputJson;
8079
}
8180

8281
@Override
83-
public ItemStack[] jsonElementToItemStack(JsonObject json) {
82+
public ItemStackContainerResult jsonElementToItemStack(JsonObject json) {
8483
// check if current version the same
85-
if (json.get(BPKey.INVENTORY_VERSION).getAsInt() != 2) {
84+
if (json.get(BPConstants.KEY_INVENTORY_VERSION).getAsInt() != BPConstants.INVENTORY_VERSION) {
8685
throw new IllegalStateException("Unable to convert binary to itemstack because zip version is missmatching");
8786
}
8887

89-
int dataVersion = json.get(BPKey.INVENTORY_DATA_VERSION).getAsInt();
90-
int itemsSize = json.get(BPKey.INVENTORY_ITEMS_SIZE).getAsInt();
88+
int dataVersion = json.get(BPConstants.KEY_INVENTORY_DATA_VERSION).getAsInt();
89+
int itemsSize = json.get(BPConstants.KEY_INVENTORY_ITEMS_SIZE).getAsInt();
9190

92-
// convert json into bukkit item
93-
ItemStack[] items = new ItemStack[itemsSize];
94-
Arrays.fill(items, new ItemStack(Material.AIR));
91+
List<ItemStackWithSlot> items = new ArrayList<>();
9592

96-
List<ItemStack> duplicateSlot = null;
97-
98-
JsonArray jsonItems = json.get(BPKey.INVENTORY_ITEMS).getAsJsonArray();
93+
JsonArray jsonItems = json.get(BPConstants.KEY_INVENTORY_ITEMS).getAsJsonArray();
9994
for (JsonElement item : jsonItems) {
10095
Dynamic<JsonElement> dynamicItem = new Dynamic<>(JsonOps.INSTANCE, item);
10196
Dynamic<JsonElement> dynamicItemFixed = DataFixers.getDataFixer()
@@ -106,56 +101,12 @@ public ItemStack[] jsonElementToItemStack(JsonObject json) {
106101
.getOrThrow(false, error -> {});
107102

108103
ItemStack bukkitItem = CraftItemStack.asCraftMirror(minecraftItem);
109-
int slot = item.getAsJsonObject().get(BPKey.INVENTORY_SLOT).getAsInt();
110-
111-
if (itemsSize <= slot) {
112-
// something went wrong !? maybe user modified it him self
113-
ZIPLogger.warn("Slot size was extended from " + itemsSize + " to " + slot + " this should not happen. Do not change the slot number inside the config manually!?");
114-
115-
ItemStack[] newItems = new ItemStack[slot + 1];
116-
System.arraycopy(items, 0, newItems, 0, items.length);
117-
Arrays.fill(newItems, items.length, newItems.length, new ItemStack(Material.AIR));
118-
items = newItems;
119-
}
104+
int slot = item.getAsJsonObject().get(BPConstants.KEY_INVENTORY_SLOT).getAsInt();
120105

121-
if (items[slot].getType() != Material.AIR) {
122-
if (duplicateSlot == null) {
123-
duplicateSlot = new ArrayList<>();
124-
}
125-
duplicateSlot.add(bukkitItem);
126-
ZIPLogger.warn("Duplicate item found on slot " + slot + " this should not happen. Do not change the slot number inside the config manually!?");
127-
} else {
128-
items[slot] = bukkitItem;
129-
}
130-
}
131-
132-
// fill existing empty slots with duplicate item
133-
while (duplicateSlot != null && !duplicateSlot.isEmpty()) {
134-
outher: for (Iterator<ItemStack> iterator = duplicateSlot.iterator(); iterator.hasNext();) {
135-
ItemStack itemStack = (ItemStack) iterator.next();
136-
137-
for (int i = 0; i < items.length; i++) {
138-
if (items[i].getType() == Material.AIR) {
139-
items[i] = itemStack;
140-
iterator.remove();
141-
break;
142-
} else if (i == items.length - 1) {
143-
break outher;
144-
}
145-
}
146-
}
147-
148-
// extend slot limit and try again
149-
if (!duplicateSlot.isEmpty()) {
150-
int extendedSlots = items.length + duplicateSlot.size();
151-
ItemStack[] newItems = new ItemStack[extendedSlots];
152-
System.arraycopy(items, 0, newItems, 0, items.length);
153-
Arrays.fill(newItems, items.length, newItems.length, new ItemStack(Material.AIR));
154-
items = newItems;
155-
}
106+
items.add(new ItemStackWithSlot(slot, bukkitItem));
156107
}
157108

158-
return items;
109+
return new ItemStackContainerResult(itemsSize, items);
159110
}
160111

161112
@Override
@@ -188,18 +139,18 @@ public JsonObject migrateToJsonElement(byte[] binary) {
188139
DataResult<JsonElement> result = net.minecraft.world.item.ItemStack.CODEC.encodeStart(DYNAMIC_OPS_JSON, minecraftItem);
189140
JsonObject resultJson = result.getOrThrow(false, error -> {}).getAsJsonObject();
190141

191-
resultJson.addProperty(BPKey.INVENTORY_SLOT, currentSlot);
142+
resultJson.addProperty(BPConstants.KEY_INVENTORY_SLOT, currentSlot);
192143
jsonItems.add(resultJson);
193144

194145
currentSlot++;
195146
}
196147
}
197148

198149
JsonObject json = new JsonObject();
199-
json.addProperty(BPKey.INVENTORY_VERSION, 2);
200-
json.addProperty(BPKey.INVENTORY_DATA_VERSION, DATA_VERSION);
201-
json.addProperty(BPKey.INVENTORY_ITEMS_SIZE, list.size());
202-
json.add(BPKey.INVENTORY_ITEMS, jsonItems);
150+
json.addProperty(BPConstants.KEY_INVENTORY_VERSION, BPConstants.INVENTORY_VERSION);
151+
json.addProperty(BPConstants.KEY_INVENTORY_DATA_VERSION, DATA_VERSION);
152+
json.addProperty(BPConstants.KEY_INVENTORY_ITEMS_SIZE, list.size());
153+
json.add(BPConstants.KEY_INVENTORY_ITEMS, jsonItems);
203154
return json;
204155
}
205156

0 commit comments

Comments
 (0)