2019-07-29 22:20:48 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2019-07-29 22:20:48 +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
|
|
|
|
*/
|
|
|
|
|
2019-07-29 22:28:01 +00:00
|
|
|
package org.geysermc.connector.network.translators.java.window;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-10-21 03:41:46 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket;
|
2019-07-29 22:20:48 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket;
|
2019-10-21 03:41:46 +00:00
|
|
|
import org.geysermc.connector.inventory.Inventory;
|
2019-07-29 22:20:48 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.PacketTranslator;
|
2020-03-24 04:24:17 +00:00
|
|
|
import org.geysermc.connector.network.translators.Translator;
|
2019-10-21 03:41:46 +00:00
|
|
|
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
|
2019-07-29 22:20:48 +00:00
|
|
|
import org.geysermc.connector.utils.InventoryUtils;
|
2020-09-29 17:19:37 +00:00
|
|
|
import org.geysermc.connector.utils.LocaleUtils;
|
2020-11-16 23:57:57 +00:00
|
|
|
import org.geysermc.connector.network.translators.chat.MessageTranslator;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2020-03-24 04:24:17 +00:00
|
|
|
@Translator(packet = ServerOpenWindowPacket.class)
|
2019-07-29 22:20:48 +00:00
|
|
|
public class JavaOpenWindowTranslator extends PacketTranslator<ServerOpenWindowPacket> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void translate(ServerOpenWindowPacket packet, GeyserSession session) {
|
2020-10-16 23:25:05 +00:00
|
|
|
session.addInventoryTask(() -> {
|
|
|
|
if (packet.getWindowId() == 0) {
|
|
|
|
return;
|
2019-10-22 23:31:03 +00:00
|
|
|
}
|
2019-11-01 09:07:13 +00:00
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
InventoryTranslator newTranslator = InventoryTranslator.INVENTORY_TRANSLATORS.get(packet.getType());
|
|
|
|
Inventory openInventory = session.getOpenInventory();
|
|
|
|
//No translator exists for this window type. Close all windows and return.
|
|
|
|
if (newTranslator == null) {
|
|
|
|
if (openInventory != null) {
|
|
|
|
InventoryUtils.closeInventory(session, openInventory.getId());
|
|
|
|
}
|
|
|
|
ClientCloseWindowPacket closeWindowPacket = new ClientCloseWindowPacket(packet.getWindowId());
|
|
|
|
session.sendDownstreamPacket(closeWindowPacket);
|
|
|
|
return;
|
2019-10-22 23:31:03 +00:00
|
|
|
}
|
2019-11-01 09:07:13 +00:00
|
|
|
|
2020-12-09 06:12:02 +00:00
|
|
|
String name = MessageTranslator.convertMessageLenient(packet.getName(), session.getLocale());
|
|
|
|
name = LocaleUtils.getLocaleString(name, session.getLocale());
|
2020-09-29 17:19:37 +00:00
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
Inventory newInventory = newTranslator.createInventory(name, packet.getWindowId(), packet.getType(), session.getPlayerInventory());
|
|
|
|
if (openInventory != null) {
|
2020-12-26 17:20:59 +00:00
|
|
|
InventoryTranslator openTranslator = session.getInventoryTranslator();
|
2020-10-16 23:25:05 +00:00
|
|
|
if (!openTranslator.getClass().equals(newTranslator.getClass())) {
|
|
|
|
InventoryUtils.closeInventory(session, openInventory.getId());
|
|
|
|
}
|
2019-10-21 03:41:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 17:20:59 +00:00
|
|
|
session.setInventoryTranslator(newTranslator);
|
2020-10-16 23:25:05 +00:00
|
|
|
InventoryUtils.openInventory(session, newInventory);
|
|
|
|
});
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
}
|