2020-10-16 23:25:05 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 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;
|
2023-04-08 16:45:13 +00:00
|
|
|
import lombok.AccessLevel;
|
|
|
|
import lombok.Getter;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
|
2020-10-16 23:25:05 +00:00
|
|
|
import lombok.Data;
|
2022-12-29 20:10:40 +00:00
|
|
|
import org.geysermc.geyser.item.type.Item;
|
|
|
|
import org.geysermc.geyser.registry.Registries;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.registry.type.ItemMapping;
|
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;
|
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) {
|
2022-12-24 00:40:42 +00:00
|
|
|
if (isEmpty()) {
|
|
|
|
return ItemData.AIR;
|
|
|
|
}
|
2022-12-23 21:18:48 +00:00
|
|
|
ItemData.Builder itemData = ItemTranslator.translateToBedrock(session, javaId, amount, nbt);
|
|
|
|
itemData.netId(getNetId());
|
|
|
|
itemData.usingNetId(true);
|
|
|
|
return itemData.build();
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-08 16:45:13 +00:00
|
|
|
@Getter(AccessLevel.NONE)
|
2022-12-29 20:10:40 +00:00
|
|
|
private Item item; //TODO
|
|
|
|
|
|
|
|
public Item asItem() {
|
|
|
|
if (item == null) {
|
|
|
|
return (item = Registries.JAVA_ITEMS.get().get(javaId));
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|