All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.inventory.transaction.action.SlotChangeAction Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.inventory.transaction.action;

import cn.nukkit.Player;
import cn.nukkit.inventory.Inventory;
import cn.nukkit.inventory.transaction.InventoryTransaction;
import cn.nukkit.item.Item;

import java.util.HashSet;
import java.util.Set;

/**
 * @author CreeperFace
 */
public class SlotChangeAction extends InventoryAction {

    protected Inventory inventory;
    private int inventorySlot;

    public SlotChangeAction(Inventory inventory, int inventorySlot, Item sourceItem, Item targetItem) {
        super(sourceItem, targetItem);
        this.inventory = inventory;
        this.inventorySlot = inventorySlot;
    }

    /**
     * Returns the inventory involved in this action.
     *
     * @return inventory
     */
    public Inventory getInventory() {
        return this.inventory;
    }

    /**
     * Returns the inventorySlot in the inventory which this action modified.
     *
     * @return slot
     */
    public int getSlot() {
        return inventorySlot;
    }

    /**
     * Checks if the item in the inventory at the specified inventorySlot is the same as this action's source item.
     *
     * @param source player
     * @return valid
     */
    @Override
    public boolean isValid(Player source) {
        Item check = inventory.getItem(this.inventorySlot);
        return check.equalsExact(this.sourceItem);
    }

    /**
     * Sets the item into the target inventory.
     *
     * @param source player
     * @return successfully executed
     */
    @Override
    public boolean execute(Player source) {
        return this.inventory.setItem(this.inventorySlot, this.targetItem, false);
    }

    /**
     * Sends inventorySlot changes to other viewers of the inventory. This will not send any change back to the source Player.
     *
     * @param source player
     */
    @Override
    public void onExecuteSuccess(Player source) {
        Set viewers = new HashSet<>(this.inventory.getViewers());
        if (source.getLoginChainData().getDeviceOS() == 7) {
            this.inventory.sendSlot(this.inventorySlot, viewers);
        } else {
            viewers.remove(source);
            this.inventory.sendSlot(this.inventorySlot, viewers);
        }
    }

    /**
     * Sends the original inventorySlot contents to the source player to revert the action.
     *
     * @param source player
     */
    @Override
    public void onExecuteFail(Player source) {
        this.inventory.sendSlot(this.inventorySlot, source);
    }

    @Override
    public void onAddToTransaction(InventoryTransaction transaction) {
        transaction.addInventory(this.inventory);
    }

    @Override
    public String toString() {
        return "SlotChangeAction{" +
                "inventory=" + inventory +
                ", inventorySlot=" + inventorySlot +
                ", sourceItem=" + sourceItem +
                ", targetItem=" + targetItem +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy