2020-12-24 16:23:47 +00:00
/ *
2022-01-01 19:03:05 +00:00
* Copyright ( c ) 2019 - 2022 GeyserMC . http : //geysermc.org
2020-12-24 16:23:47 +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-24 16:23:47 +00:00
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack ;
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.ServerboundContainerButtonClickPacket ;
2020-12-24 16:23:47 +00:00
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-24 16:23:47 +00:00
import com.nukkitx.protocol.bedrock.data.inventory.StackRequestSlotInfoData ;
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.CraftResultsDeprecatedStackRequestActionData ;
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.StackRequestActionData ;
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.StackRequestActionType ;
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket ;
2020-12-24 23:29:25 +00:00
import it.unimi.dsi.fastutil.ints.IntList ;
2021-11-20 21:34:30 +00:00
import org.geysermc.geyser.inventory.GeyserItemStack ;
import org.geysermc.geyser.inventory.Inventory ;
import org.geysermc.geyser.inventory.PlayerInventory ;
import org.geysermc.geyser.inventory.StonecutterContainer ;
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.SlotType ;
import org.geysermc.geyser.inventory.updater.UIInventoryUpdater ;
import org.geysermc.geyser.translator.inventory.item.ItemTranslator ;
2020-12-24 16:23:47 +00:00
public class StonecutterInventoryTranslator extends AbstractBlockInventoryTranslator {
public StonecutterInventoryTranslator ( ) {
2021-11-14 05:07:24 +00:00
super ( 2 , " minecraft:stonecutter[facing=north] " , com . nukkitx . protocol . bedrock . data . inventory . ContainerType . STONECUTTER , UIInventoryUpdater . INSTANCE ) ;
2020-12-24 16:23:47 +00:00
}
@Override
2022-01-16 19:42:17 +00:00
protected boolean shouldHandleRequestFirst ( StackRequestActionData action , Inventory inventory ) {
2021-12-08 14:23:23 +00:00
// First is pre-1.18. TODO remove after 1.17.40 support is dropped and refactor stonecutter support to use CraftRecipeStackRequestActionData's recipe ID
return action . getType ( ) = = StackRequestActionType . CRAFT_NON_IMPLEMENTED_DEPRECATED | | action . getType ( ) = = StackRequestActionType . CRAFT_RECIPE ;
2020-12-24 16:23:47 +00:00
}
@Override
2021-11-22 19:52:26 +00:00
public ItemStackResponsePacket . Response translateSpecialRequest ( GeyserSession session , Inventory inventory , ItemStackRequest request ) {
2020-12-24 16:23:47 +00:00
// TODO: Also surely to change in the future
StackRequestActionData data = request . getActions ( ) [ 1 ] ;
2021-09-10 18:10:56 +00:00
if ( ! ( data instanceof CraftResultsDeprecatedStackRequestActionData craftData ) ) {
2020-12-24 16:23:47 +00:00
return rejectRequest ( request ) ;
}
2021-03-08 21:57:31 +00:00
2020-12-30 01:31:48 +00:00
StonecutterContainer container = ( StonecutterContainer ) inventory ;
2020-12-24 16:23:47 +00:00
// Get the ID of the item we are cutting
2020-12-30 00:59:22 +00:00
int id = inventory . getItem ( 0 ) . getJavaId ( ) ;
2020-12-24 16:23:47 +00:00
// Look up all possible options of cutting from this ID
2020-12-24 23:29:25 +00:00
IntList results = session . getStonecutterRecipes ( ) . get ( id ) ;
2020-12-24 16:23:47 +00:00
if ( results = = null ) {
return rejectRequest ( request ) ;
}
2021-03-08 21:57:31 +00:00
2021-07-13 01:19:40 +00:00
ItemStack javaOutput = ItemTranslator . translateToJava ( craftData . getResultItems ( ) [ 0 ] , session . getItemMappings ( ) ) ;
2020-12-30 01:31:48 +00:00
int button = results . indexOf ( javaOutput . getId ( ) ) ;
// If we've already pressed the button with this item, no need to press it again!
if ( container . getStonecutterButton ( ) ! = button ) {
// Getting the index of the item in the Java stonecutter list
2021-11-13 03:44:15 +00:00
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket ( inventory . getId ( ) , button ) ;
2020-12-30 01:31:48 +00:00
session . sendDownstreamPacket ( packet ) ;
container . setStonecutterButton ( button ) ;
if ( inventory . getItem ( 1 ) . getJavaId ( ) ! = javaOutput . getId ( ) ) {
// We don't know there is an output here, so we tell ourselves that there is
2021-01-04 23:19:43 +00:00
inventory . setItem ( 1 , GeyserItemStack . from ( javaOutput ) , session ) ;
2020-12-30 01:31:48 +00:00
}
2020-12-24 23:29:25 +00:00
}
2021-03-08 21:57:31 +00:00
2020-12-24 16:23:47 +00:00
return translateRequest ( session , inventory , request ) ;
}
@Override
public int bedrockSlotToJava ( StackRequestSlotInfoData slotInfoData ) {
2021-09-10 18:10:56 +00:00
return switch ( slotInfoData . getContainer ( ) ) {
case STONECUTTER_INPUT - > 0 ;
case STONECUTTER_RESULT , CREATIVE_OUTPUT - > 1 ;
default - > super . bedrockSlotToJava ( slotInfoData ) ;
} ;
2020-12-24 16:23:47 +00:00
}
@Override
public BedrockContainerSlot javaSlotToBedrockContainer ( int slot ) {
if ( slot = = 0 ) {
return new BedrockContainerSlot ( ContainerSlotType . STONECUTTER_INPUT , 3 ) ;
}
if ( slot = = 1 ) {
return new BedrockContainerSlot ( ContainerSlotType . STONECUTTER_RESULT , 50 ) ;
}
return super . javaSlotToBedrockContainer ( slot ) ;
}
@Override
public int javaSlotToBedrock ( int slot ) {
if ( slot = = 0 ) {
return 3 ;
}
if ( slot = = 1 ) {
return 50 ;
}
return super . javaSlotToBedrock ( slot ) ;
}
2020-12-24 23:29:25 +00:00
@Override
public SlotType getSlotType ( int javaSlot ) {
if ( javaSlot = = 1 ) {
return SlotType . OUTPUT ;
}
return super . getSlotType ( javaSlot ) ;
}
2020-12-30 01:31:48 +00:00
@Override
2021-11-14 05:07:24 +00:00
public Inventory createInventory ( String name , int windowId , ContainerType containerType , PlayerInventory playerInventory ) {
return new StonecutterContainer ( name , windowId , this . size , containerType , playerInventory ) ;
2020-12-30 01:31:48 +00:00
}
2020-12-24 16:23:47 +00:00
}