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
|
|
|
*/
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
package org.geysermc.geyser.inventory;
|
2020-10-16 23:25:05 +00:00
|
|
|
|
|
|
|
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;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.translator.inventory.item.ItemTranslator;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.registry.type.ItemMapping;
|
2020-10-16 23:25:05 +00:00
|
|
|
|
2021-08-31 23:49:55 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
@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;
|
|
|
|
|
2021-08-31 23:49:55 +00:00
|
|
|
private GeyserItemStack(int javaId, int amount, CompoundTag nbt) {
|
2020-12-30 00:59:22 +00:00
|
|
|
this(javaId, amount, nbt, 1);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 23:49:55 +00:00
|
|
|
private GeyserItemStack(int javaId, int amount, CompoundTag nbt, int netId) {
|
2020-12-30 00:59:22 +00:00
|
|
|
this.javaId = javaId;
|
2020-10-16 23:25:05 +00:00
|
|
|
this.amount = amount;
|
|
|
|
this.nbt = nbt;
|
|
|
|
this.netId = netId;
|
|
|
|
}
|
|
|
|
|
2021-08-31 23:49:55 +00:00
|
|
|
public static @Nonnull GeyserItemStack from(ItemStack itemStack) {
|
2021-03-13 23:07:43 +00:00
|
|
|
return itemStack == null ? EMPTY : new GeyserItemStack(itemStack.getId(), itemStack.getAmount(), itemStack.getNbt());
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getNetId() {
|
|
|
|
return isEmpty() ? 0 : netId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add(int add) {
|
|
|
|
amount += add;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sub(int sub) {
|
|
|
|
amount -= sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getItemStack() {
|
2021-01-09 13:10:27 +00:00
|
|
|
return getItemStack(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getItemStack(int newAmount) {
|
|
|
|
return isEmpty() ? null : new ItemStack(javaId, newAmount, nbt);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public ItemData getItemData(GeyserSession session) {
|
2020-10-16 23:25:05 +00:00
|
|
|
ItemData itemData = ItemTranslator.translateToBedrock(session, getItemStack());
|
|
|
|
itemData.setNetId(getNetId());
|
2021-04-06 04:14:06 +00:00
|
|
|
itemData.setUsingNetId(true); // Seems silly - this should probably be on the protocol level
|
2020-10-16 23:25:05 +00:00
|
|
|
return itemData;
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public ItemMapping getMapping(GeyserSession session) {
|
2021-07-13 01:19:40 +00:00
|
|
|
return session.getItemMappings().getMapping(this.javaId);
|
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
|
|
|
}
|
|
|
|
}
|