Optimize stonecutter button code

This commit is contained in:
Camotoy 2020-12-29 20:31:48 -05:00
parent 3c1a40c56a
commit c4fc604e0c
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
3 changed files with 74 additions and 49 deletions

View file

@ -1,41 +0,0 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* 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
*
*/
package org.geysermc.connector.inventory;
import lombok.Getter;
import lombok.Setter;
//TODO: Figure out what this is and if we should remove it
@Getter
public class FurnaceInventory extends Inventory {
@Setter
private int test;
public FurnaceInventory(String title, int id, int size) {
super(title, id, size);
}
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* 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
*/
package org.geysermc.connector.inventory;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.geysermc.connector.network.session.GeyserSession;
public class StonecutterContainer extends Container {
/**
* The button that has currently been pressed Java-side
*/
@Getter
@Setter
private int stonecutterButton = -1;
public StonecutterContainer(String title, int id, int size, PlayerInventory playerInventory) {
super(title, id, size, playerInventory);
}
@Override
public void setItem(int slot, @NonNull GeyserItemStack newItem, GeyserSession session) {
if (slot == 0 && newItem.getJavaId() != items[slot].getJavaId()) {
// The pressed stonecutter button output resets whenever the input item changes
this.stonecutterButton = -1;
}
super.setItem(slot, newItem, session);
}
}

View file

@ -26,6 +26,7 @@
package org.geysermc.connector.network.translators.inventory.translators; package org.geysermc.connector.network.translators.inventory.translators;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.data.game.window.WindowType;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientClickWindowButtonPacket; import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientClickWindowButtonPacket;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerSlotType; import com.nukkitx.protocol.bedrock.data.inventory.ContainerSlotType;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType; import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
@ -38,6 +39,8 @@ import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
import it.unimi.dsi.fastutil.ints.IntList; import it.unimi.dsi.fastutil.ints.IntList;
import org.geysermc.connector.inventory.GeyserItemStack; import org.geysermc.connector.inventory.GeyserItemStack;
import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.inventory.PlayerInventory;
import org.geysermc.connector.inventory.StonecutterContainer;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.inventory.BedrockContainerSlot; import org.geysermc.connector.network.translators.inventory.BedrockContainerSlot;
import org.geysermc.connector.network.translators.inventory.SlotType; import org.geysermc.connector.network.translators.inventory.SlotType;
@ -57,12 +60,12 @@ public class StonecutterInventoryTranslator extends AbstractBlockInventoryTransl
@Override @Override
public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequestPacket.Request request) { public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequestPacket.Request request) {
// TODO: Also surely to change in the future // TODO: Also surely to change in the future
// TODO: don't spam the ClickWindowButtonPacket?
StackRequestActionData data = request.getActions()[1]; StackRequestActionData data = request.getActions()[1];
if (!(data instanceof CraftResultsDeprecatedStackRequestActionData)) { if (!(data instanceof CraftResultsDeprecatedStackRequestActionData)) {
return rejectRequest(request); return rejectRequest(request);
} }
CraftResultsDeprecatedStackRequestActionData craftData = (CraftResultsDeprecatedStackRequestActionData) data; CraftResultsDeprecatedStackRequestActionData craftData = (CraftResultsDeprecatedStackRequestActionData) data;
StonecutterContainer container = (StonecutterContainer) inventory;
// Get the ID of the item we are cutting // Get the ID of the item we are cutting
int id = inventory.getItem(0).getJavaId(); int id = inventory.getItem(0).getJavaId();
// Look up all possible options of cutting from this ID // Look up all possible options of cutting from this ID
@ -73,14 +76,19 @@ public class StonecutterInventoryTranslator extends AbstractBlockInventoryTransl
System.out.println(id + " " + results); System.out.println(id + " " + results);
ItemStack javaOutput = ItemTranslator.translateToJava(craftData.getResultItems()[0]); ItemStack javaOutput = ItemTranslator.translateToJava(craftData.getResultItems()[0]);
System.out.println(javaOutput); System.out.println(javaOutput);
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 // Getting the index of the item in the Java stonecutter list
ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), results.indexOf(javaOutput.getId())); ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), button);
System.out.println(packet.toString()); System.out.println(packet.toString());
session.sendDownstreamPacket(packet); session.sendDownstreamPacket(packet);
container.setStonecutterButton(button);
if (inventory.getItem(1).getJavaId() != javaOutput.getId()) { if (inventory.getItem(1).getJavaId() != javaOutput.getId()) {
// We don't know there is an output here, so we tell ourselves that there is // We don't know there is an output here, so we tell ourselves that there is
inventory.setItem(1, GeyserItemStack.from(javaOutput, session.getNextItemNetId()), session); inventory.setItem(1, GeyserItemStack.from(javaOutput, session.getNextItemNetId()), session);
} }
}
return translateRequest(session, inventory, request); return translateRequest(session, inventory, request);
} }
@ -124,4 +132,9 @@ public class StonecutterInventoryTranslator extends AbstractBlockInventoryTransl
} }
return super.getSlotType(javaSlot); return super.getSlotType(javaSlot);
} }
@Override
public Inventory createInventory(String name, int windowId, WindowType windowType, PlayerInventory playerInventory) {
return new StonecutterContainer(name, windowId, this.size, playerInventory);
}
} }