2020-12-23 17:30:36 +00:00
|
|
|
/*
|
2021-01-01 19:22:26 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-12-23 17:30:36 +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 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.inventory;
|
2020-12-23 17:30:36 +00:00
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetBeaconPacket;
|
2020-12-23 17:30:36 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
|
|
|
import com.nukkitx.nbt.NbtMap;
|
|
|
|
import com.nukkitx.nbt.NbtMapBuilder;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ContainerSlotType;
|
2021-02-18 00:39:10 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemStackRequest;
|
2020-12-23 17:30:36 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.StackRequestSlotInfoData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.BeaconPaymentStackRequestActionData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.StackRequestActionData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.StackRequestActionType;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.inventory.BeaconContainer;
|
|
|
|
import org.geysermc.geyser.inventory.Inventory;
|
|
|
|
import org.geysermc.geyser.inventory.PlayerInventory;
|
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.inventory.BedrockContainerSlot;
|
|
|
|
import org.geysermc.geyser.inventory.holder.BlockInventoryHolder;
|
|
|
|
import org.geysermc.geyser.inventory.updater.UIInventoryUpdater;
|
|
|
|
import org.geysermc.geyser.util.InventoryUtils;
|
2020-12-23 17:30:36 +00:00
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
public class BeaconInventoryTranslator extends AbstractBlockInventoryTranslator {
|
|
|
|
public BeaconInventoryTranslator() {
|
2021-11-14 05:07:24 +00:00
|
|
|
super(1, new BlockInventoryHolder("minecraft:beacon", com.nukkitx.protocol.bedrock.data.inventory.ContainerType.BEACON) {
|
2021-03-12 22:07:29 +00:00
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
protected boolean checkInteractionPosition(GeyserSession session) {
|
2021-03-12 22:07:29 +00:00
|
|
|
// Since we can't fall back to a virtual inventory, let's make opening one easier
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void openInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
|
2021-06-01 19:36:33 +00:00
|
|
|
if (!((BeaconContainer) inventory).isUsingRealBlock()) {
|
2021-03-12 22:07:29 +00:00
|
|
|
InventoryUtils.closeInventory(session, inventory.getId(), false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.openInventory(translator, session, inventory);
|
|
|
|
}
|
|
|
|
}, UIInventoryUpdater.INSTANCE);
|
2020-12-23 17:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
|
2020-12-23 17:30:36 +00:00
|
|
|
//FIXME?: Beacon graphics look weird after inputting an item. This might be a Bedrock bug, since it resets to nothing
|
|
|
|
// on BDS
|
|
|
|
BeaconContainer beaconContainer = (BeaconContainer) inventory;
|
|
|
|
switch (key) {
|
|
|
|
case 0:
|
|
|
|
// Power - beacon doesn't use this, and uses the block position instead
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
beaconContainer.setPrimaryId(value == -1 ? 0 : value);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
beaconContainer.setSecondaryId(value == -1 ? 0 : value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a block entity data packet update to the fake beacon inventory
|
|
|
|
Vector3i position = inventory.getHolderPosition();
|
|
|
|
NbtMapBuilder builder = NbtMap.builder()
|
|
|
|
.putInt("x", position.getX())
|
|
|
|
.putInt("y", position.getY())
|
|
|
|
.putInt("z", position.getZ())
|
|
|
|
.putString("CustomName", inventory.getTitle())
|
|
|
|
.putString("id", "Beacon")
|
|
|
|
.putInt("primary", beaconContainer.getPrimaryId())
|
|
|
|
.putInt("secondary", beaconContainer.getSecondaryId());
|
|
|
|
|
|
|
|
BlockEntityDataPacket packet = new BlockEntityDataPacket();
|
|
|
|
packet.setBlockPosition(position);
|
|
|
|
packet.setData(builder.build());
|
|
|
|
session.sendUpstreamPacket(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-12-24 03:53:54 +00:00
|
|
|
public boolean shouldHandleRequestFirst(StackRequestActionData action, Inventory inventory) {
|
2020-12-23 17:30:36 +00:00
|
|
|
return action.getType() == StackRequestActionType.BEACON_PAYMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2020-12-23 17:30:36 +00:00
|
|
|
// Input a beacon payment
|
|
|
|
BeaconPaymentStackRequestActionData beaconPayment = (BeaconPaymentStackRequestActionData) request.getActions()[0];
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundSetBeaconPacket packet = new ServerboundSetBeaconPacket(beaconPayment.getPrimaryEffect(), beaconPayment.getSecondaryEffect());
|
2020-12-23 17:30:36 +00:00
|
|
|
session.sendDownstreamPacket(packet);
|
|
|
|
return acceptRequest(request, makeContainerEntries(session, inventory, Collections.emptySet()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
|
|
|
|
if (slotInfoData.getContainer() == ContainerSlotType.BEACON_PAYMENT) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return super.bedrockSlotToJava(slotInfoData);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BedrockContainerSlot javaSlotToBedrockContainer(int slot) {
|
|
|
|
if (slot == 0) {
|
|
|
|
return new BedrockContainerSlot(ContainerSlotType.BEACON_PAYMENT, 27);
|
|
|
|
}
|
|
|
|
return super.javaSlotToBedrockContainer(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int javaSlotToBedrock(int slot) {
|
|
|
|
if (slot == 0) {
|
|
|
|
return 27;
|
|
|
|
}
|
|
|
|
return super.javaSlotToBedrock(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-14 05:07:24 +00:00
|
|
|
public Inventory createInventory(String name, int windowId, ContainerType containerType, PlayerInventory playerInventory) {
|
|
|
|
return new BeaconContainer(name, windowId, this.size, containerType, playerInventory);
|
2020-12-23 17:30:36 +00:00
|
|
|
}
|
|
|
|
}
|