2020-10-16 23:25:05 +00:00
|
|
|
/*
|
2021-01-01 19:22:26 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-10-16 23:25:05 +00:00
|
|
|
*
|
2021-01-01 19:22:26 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2020-10-16 23:25:05 +00:00
|
|
|
*
|
2021-01-01 19:22:26 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-10-16 23:25:05 +00:00
|
|
|
*
|
2021-01-01 19:22:26 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2020-10-16 23:25:05 +00:00
|
|
|
*
|
2021-01-01 19:22:26 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-10-16 23:25:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.inventory;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
|
|
|
|
import lombok.Data;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
|
|
|
import org.geysermc.connector.network.translators.item.ItemRegistry;
|
|
|
|
import org.geysermc.connector.network.translators.item.ItemTranslator;
|
|
|
|
|
|
|
|
@Data
|
|
|
|
public class GeyserItemStack {
|
|
|
|
public static final GeyserItemStack EMPTY = new GeyserItemStack(0, 0, null);
|
|
|
|
|
2020-12-30 00:59:22 +00:00
|
|
|
private final int javaId;
|
2020-10-16 23:25:05 +00:00
|
|
|
private int amount;
|
|
|
|
private CompoundTag nbt;
|
|
|
|
private int netId;
|
|
|
|
|
2020-12-30 00:59:22 +00:00
|
|
|
public GeyserItemStack(int javaId) {
|
|
|
|
this(javaId, 1);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 00:59:22 +00:00
|
|
|
public GeyserItemStack(int javaId, int amount) {
|
|
|
|
this(javaId, amount, null);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 00:59:22 +00:00
|
|
|
public GeyserItemStack(int javaId, int amount, CompoundTag nbt) {
|
|
|
|
this(javaId, amount, nbt, 1);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 00:59:22 +00:00
|
|
|
public GeyserItemStack(int javaId, int amount, CompoundTag nbt, int netId) {
|
|
|
|
this.javaId = javaId;
|
2020-10-16 23:25:05 +00:00
|
|
|
this.amount = amount;
|
|
|
|
this.nbt = nbt;
|
|
|
|
this.netId = netId;
|
|
|
|
}
|
|
|
|
|
2020-12-30 00:59:22 +00:00
|
|
|
public int getJavaId() {
|
|
|
|
return isEmpty() ? 0 : javaId;
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getAmount() {
|
|
|
|
return isEmpty() ? 0 : amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CompoundTag getNbt() {
|
|
|
|
return isEmpty() ? null : nbt;
|
|
|
|
}
|
|
|
|
|
2020-12-30 00:59:22 +00:00
|
|
|
public void setNetId(int netId) {
|
|
|
|
this.netId = netId;
|
|
|
|
}
|
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
public int getNetId() {
|
|
|
|
return isEmpty() ? 0 : netId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add(int add) {
|
|
|
|
amount += add;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sub(int sub) {
|
|
|
|
amount -= sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static GeyserItemStack from(ItemStack itemStack) {
|
2021-01-04 23:19:43 +00:00
|
|
|
return itemStack == null ? EMPTY : new GeyserItemStack(itemStack.getId(), itemStack.getAmount(), itemStack.getNbt());
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getItemStack() {
|
2020-12-30 00:59:22 +00:00
|
|
|
return isEmpty() ? null : new ItemStack(javaId, amount, nbt);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ItemData getItemData(GeyserSession session) {
|
|
|
|
ItemData itemData = ItemTranslator.translateToBedrock(session, getItemStack());
|
|
|
|
itemData.setNetId(getNetId());
|
|
|
|
return itemData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemEntry getItemEntry() {
|
2020-12-30 00:59:22 +00:00
|
|
|
return ItemRegistry.ITEM_ENTRIES.get(getJavaId());
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEmpty() {
|
2020-12-30 00:59:22 +00:00
|
|
|
return amount <= 0 || javaId == 0;
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public GeyserItemStack copy() {
|
|
|
|
return copy(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public GeyserItemStack copy(int newAmount) {
|
2020-12-30 00:59:22 +00:00
|
|
|
return isEmpty() ? EMPTY : new GeyserItemStack(javaId, newAmount, nbt == null ? null : nbt.clone(), netId);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
}
|