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
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
package org.geysermc.geyser.inventory;
|
2020-10-16 23:25:05 +00:00
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
|
2020-10-16 23:25:05 +00:00
|
|
|
import lombok.Getter;
|
2023-12-05 23:54:42 +00:00
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
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.InventoryTranslator;
|
2022-01-16 19:42:17 +00:00
|
|
|
import org.jetbrains.annotations.Range;
|
2020-10-16 23:25:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Combination of {@link Inventory} and {@link PlayerInventory}
|
|
|
|
*/
|
|
|
|
@Getter
|
|
|
|
public class Container extends Inventory {
|
|
|
|
private final PlayerInventory playerInventory;
|
|
|
|
private final int containerSize;
|
|
|
|
|
2021-02-12 19:39:41 +00:00
|
|
|
/**
|
|
|
|
* Whether we are using a real block when opening this inventory.
|
|
|
|
*/
|
|
|
|
private boolean isUsingRealBlock = false;
|
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
public Container(String title, int id, int size, ContainerType containerType, PlayerInventory playerInventory) {
|
|
|
|
super(title, id, size, containerType);
|
2020-10-16 23:25:05 +00:00
|
|
|
this.playerInventory = playerInventory;
|
|
|
|
this.containerSize = this.size + InventoryTranslator.PLAYER_INVENTORY_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GeyserItemStack getItem(int slot) {
|
|
|
|
if (slot < this.size) {
|
|
|
|
return super.getItem(slot);
|
|
|
|
} else {
|
|
|
|
return playerInventory.getItem(slot - this.size + InventoryTranslator.PLAYER_INVENTORY_OFFSET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 19:42:17 +00:00
|
|
|
@Override
|
|
|
|
public int getOffsetForHotbar(@Range(from = 0, to = 8) int slot) {
|
|
|
|
return playerInventory.getOffsetForHotbar(slot) - InventoryTranslator.PLAYER_INVENTORY_OFFSET + this.size;
|
|
|
|
}
|
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
@Override
|
2023-12-05 23:54:42 +00:00
|
|
|
public void setItem(int slot, @NonNull GeyserItemStack newItem, GeyserSession session) {
|
2020-10-16 23:25:05 +00:00
|
|
|
if (slot < this.size) {
|
2020-12-30 00:59:22 +00:00
|
|
|
super.setItem(slot, newItem, session);
|
2020-10-16 23:25:05 +00:00
|
|
|
} else {
|
2020-12-30 00:59:22 +00:00
|
|
|
playerInventory.setItem(slot - this.size + InventoryTranslator.PLAYER_INVENTORY_OFFSET, newItem, session);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSize() {
|
|
|
|
return this.containerSize;
|
|
|
|
}
|
2021-02-12 19:39:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will be overwritten for droppers.
|
|
|
|
*
|
|
|
|
* @param usingRealBlock whether this container is using a real container or not
|
|
|
|
* @param javaBlockId the Java block string of the block, if real
|
|
|
|
*/
|
|
|
|
public void setUsingRealBlock(boolean usingRealBlock, String javaBlockId) {
|
|
|
|
isUsingRealBlock = usingRealBlock;
|
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|