SteamWar/BauSystem2.0
Archiviert
12
0
Signed-off-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Chaoscaot 2021-04-18 14:34:59 +02:00
Ursprung 7790b13340
Commit 0038f22194

Datei anzeigen

@ -0,0 +1,97 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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.bausystem.features.world;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.BlockPosition;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
@Linked(LinkageType.LISTENER)
public class SignEdit implements Listener {
@EventHandler
public void editSign(PlayerInteractEvent event) {
if (event.getAction() != Action.LEFT_CLICK_BLOCK ||
!event.getClickedBlock().getType().name().contains("SIGN") ||
!event.getPlayer().isSneaking())
return;
event.setCancelled(true);
Player player = event.getPlayer();
Sign sign = (org.bukkit.block.Sign) event.getClickedBlock().getState();
String[] lines = sign.getLines();
for (int i = 0; i < lines.length; i++) {
sign.setLine(i, lines[i].replace('§', '&'));
}
sign.update();
PacketContainer signOpen = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.OPEN_SIGN_EDITOR);
signOpen.getBlockPositionModifier().write(0, new BlockPosition(event.getClickedBlock().getLocation().toVector()));
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, signOpen);
} catch (InvocationTargetException e) {
Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e);
}
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.UPDATE_SIGN) {
@Override
public void onPacketReceiving(PacketEvent event) {
if (!event.getPlayer().equals(player))
return;
event.setCancelled(true);
Bukkit.getScheduler().runTask(BauSystem.getInstance(), () -> {
PacketContainer packetContainer = event.getPacket();
BlockPosition position = packetContainer.getBlockPositionModifier().read(0);
String[] lines = packetContainer.getStringArrays().read(0);
Block signLoc = position.toLocation(player.getWorld()).getBlock();
if (!signLoc.getType().name().contains("SIGN"))
return;
org.bukkit.block.Sign sign = ((Sign) signLoc.getState());
for (int i = 0; i < lines.length; i++) {
sign.setLine(i, ChatColor.translateAlternateColorCodes('&', lines[i]));
}
sign.update();
ProtocolLibrary.getProtocolManager().removePacketListener(this);
});
}
});
}
}