Implement anvil inventory handling through SWAnvilInv and related network packets

This commit is contained in:
2025-07-19 22:34:36 +02:00
parent 03005adcd8
commit eac0390dd3
7 changed files with 202 additions and 1 deletions
@@ -0,0 +1,41 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.network.packets.client;
import de.steamwar.network.packets.NetworkPacket;
import lombok.*;
@EqualsAndHashCode(callSuper = true)
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder(toBuilder = true)
public class AnvilAnswerPacket extends NetworkPacket {
private int playerId;
private Action action;
private String text;
public enum Action {
CLOSE,
ANSWER,
LEFT_CLICK
}
}
@@ -0,0 +1,36 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.network.packets.server;
import de.steamwar.network.packets.NetworkPacket;
import lombok.*;
@EqualsAndHashCode(callSuper = true)
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class AnvilInventoryPacket extends NetworkPacket {
private static final long serialVersionUID = -6004390311854048209L;
private int playerId;
private String title;
private String defaultText;
private String material;
}
@@ -59,6 +59,11 @@ public class CoreNetworkHandler extends PacketHandler {
InventoryHandler.handleInventoryPacket(packet); InventoryHandler.handleInventoryPacket(packet);
} }
@Handler
public void handleAnvilPacket(AnvilInventoryPacket packet) {
InventoryHandler.handleAnvilInventoryPacket(packet);
}
@Handler @Handler
public void handlePingPacket(PingPacket packet) { public void handlePingPacket(PingPacket packet) {
UUID uuid = SteamwarUser.get(packet.getId()).getUUID(); UUID uuid = SteamwarUser.get(packet.getId()).getUUID();
@@ -20,11 +20,15 @@
package de.steamwar.network.handlers; package de.steamwar.network.handlers;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import de.steamwar.inventory.SWAnvilInv;
import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWItem;
import de.steamwar.network.NetworkSender; import de.steamwar.network.NetworkSender;
import de.steamwar.network.packets.NetworkPacket;
import de.steamwar.network.packets.PacketHandler; import de.steamwar.network.packets.PacketHandler;
import de.steamwar.network.packets.client.AnvilAnswerPacket;
import de.steamwar.network.packets.client.InventoryCallbackPacket; import de.steamwar.network.packets.client.InventoryCallbackPacket;
import de.steamwar.network.packets.server.AnvilInventoryPacket;
import de.steamwar.network.packets.server.InventoryPacket; import de.steamwar.network.packets.server.InventoryPacket;
import de.steamwar.sql.SteamwarUser; import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@@ -53,4 +57,20 @@ public class InventoryHandler extends PacketHandler {
}); });
inventory.open(); inventory.open();
} }
@Handler
public static void handleAnvilInventoryPacket(AnvilInventoryPacket packet) {
Player player = Bukkit.getPlayer(SteamwarUser.get(packet.getPlayerId()).getUUID());
SWAnvilInv inv = new SWAnvilInv(player, packet.getTitle(), packet.getDefaultText());
if (packet.getMaterial() != null && !packet.getMaterial().isEmpty()) {
inv.setItem(SWItem.getMaterial(packet.getMaterial()));
}
inv.setCallback(s -> NetworkSender.send(AnvilAnswerPacket.builder().playerId(packet.getPlayerId()).action(AnvilAnswerPacket.Action.ANSWER).text(s).build()));
inv.addLeftCallback(() -> NetworkSender.send(AnvilAnswerPacket.builder().playerId(packet.getPlayerId()).action(AnvilAnswerPacket.Action.LEFT_CLICK).build()));
inv.addCloseCallback(() -> NetworkSender.send(AnvilAnswerPacket.builder().playerId(packet.getPlayerId()).action(AnvilAnswerPacket.Action.CLOSE).build()));
inv.open();
}
} }
@@ -224,7 +224,7 @@ public class VelocityCore implements ReloadablePlugin {
for(PacketHandler handler : new PacketHandler[] { for(PacketHandler handler : new PacketHandler[] {
new EloPlayerHandler(), new EloSchemHandler(), new ExecuteCommandHandler(), new FightInfoHandler(), new EloPlayerHandler(), new EloSchemHandler(), new ExecuteCommandHandler(), new FightInfoHandler(),
new ImALobbyHandler(), new InventoryCallbackHandler(), new PrepareSchemHandler(), new PlayerSkinHandler() new ImALobbyHandler(), new InventoryCallbackHandler(), new PrepareSchemHandler(), new PlayerSkinHandler(), new AnvilAnswerHandler()
}) })
handler.register(); handler.register();
@@ -0,0 +1,67 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.velocitycore.inventory;
import com.velocitypowered.api.proxy.Player;
import de.steamwar.network.packets.client.AnvilAnswerPacket;
import de.steamwar.network.packets.server.AnvilInventoryPacket;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.velocitycore.network.NetworkSender;
import lombok.AllArgsConstructor;
import lombok.Builder;
import java.util.HashMap;
import java.util.function.Consumer;
@AllArgsConstructor
@Builder(toBuilder = true)
public class SWAnvilInv {
private static final HashMap<Integer, SWAnvilInv> openInv = new HashMap<>();
public static void handleClick(AnvilAnswerPacket packet) {
switch (packet.getAction()) {
case LEFT_CLICK:
openInv.get(packet.getPlayerId()).leftClickCallback.run();
break;
case CLOSE:
openInv.get(packet.getPlayerId()).closeCallback.run();
break;
case ANSWER:
openInv.get(packet.getPlayerId()).callback.accept(packet.getText());
break;
}
}
private final Player player;
private final String title;
private final String defaultText;
private final String material;
private final Consumer<String> callback;
private final Runnable closeCallback;
private final Runnable leftClickCallback;
public void open() {
SteamwarUser user = SteamwarUser.get(player.getUniqueId());
openInv.put(user.getId(), this);
NetworkSender.send(player, new AnvilInventoryPacket(user.getId(), title, defaultText, material));
}
}
@@ -0,0 +1,32 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.velocitycore.network.handlers;
import de.steamwar.network.packets.PacketHandler;
import de.steamwar.network.packets.client.AnvilAnswerPacket;
import de.steamwar.velocitycore.inventory.SWAnvilInv;
public class AnvilAnswerHandler extends PacketHandler {
@Handler
public void handle(AnvilAnswerPacket packet) {
SWAnvilInv.handleClick(packet);
}
}