2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2024-01-01 12:39:45 +01:00
|
|
|
* Copyright (C) 2016-2024 ViaVersion and contributors
|
2021-03-22 23:06:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8;
|
2016-04-12 13:01:37 +02:00
|
|
|
|
2023-02-12 11:44:25 +01:00
|
|
|
import com.viaversion.viaversion.api.Via;
|
|
|
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
|
|
|
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
|
|
|
|
import com.viaversion.viaversion.api.type.Type;
|
|
|
|
import com.viaversion.viaversion.bukkit.listeners.ViaBukkitListener;
|
2021-07-31 12:43:07 +02:00
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9;
|
2023-02-12 11:44:25 +01:00
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
|
2016-04-12 13:01:37 +02:00
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.entity.PlayerDeathEvent;
|
2020-04-13 20:19:55 +02:00
|
|
|
import org.bukkit.plugin.Plugin;
|
2024-03-09 13:57:41 +01:00
|
|
|
import java.util.logging.Level;
|
2016-04-12 13:01:37 +02:00
|
|
|
|
2016-09-26 22:30:59 +02:00
|
|
|
public class DeathListener extends ViaBukkitListener {
|
2020-04-13 20:19:55 +02:00
|
|
|
|
|
|
|
public DeathListener(Plugin plugin) {
|
2019-04-23 16:34:17 +02:00
|
|
|
super(plugin, Protocol1_9To1_8.class);
|
2016-07-11 20:27:44 +02:00
|
|
|
}
|
2016-04-12 13:01:37 +02:00
|
|
|
|
|
|
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
|
|
|
|
public void onDeath(PlayerDeathEvent e) {
|
|
|
|
Player p = e.getEntity();
|
2020-02-10 10:05:56 +01:00
|
|
|
if (isOnPipe(p) && Via.getConfig().isShowNewDeathMessages() && checkGamerule(p.getWorld()) && e.getDeathMessage() != null)
|
2016-04-12 13:01:37 +02:00
|
|
|
sendPacket(p, e.getDeathMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean checkGamerule(World w) {
|
|
|
|
try {
|
2016-05-04 21:03:48 +02:00
|
|
|
return Boolean.parseBoolean(w.getGameRuleValue("showDeathMessages"));
|
2016-04-12 13:01:37 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendPacket(final Player p, final String msg) {
|
2023-10-11 15:14:41 +02:00
|
|
|
Via.getPlatform().runSync(() -> {
|
|
|
|
// If online
|
|
|
|
UserConnection userConnection = getUserConnection(p);
|
|
|
|
if (userConnection != null) {
|
|
|
|
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.COMBAT_EVENT, null, userConnection);
|
|
|
|
try {
|
|
|
|
wrapper.write(Type.VAR_INT, 2); // Event - Entity dead
|
|
|
|
wrapper.write(Type.VAR_INT, p.getEntityId()); // Player ID
|
|
|
|
wrapper.write(Type.INT, p.getEntityId()); // Entity ID
|
|
|
|
Protocol1_9To1_8.FIX_JSON.write(wrapper, msg); // Message
|
2016-07-11 20:27:44 +02:00
|
|
|
|
2023-10-11 15:14:41 +02:00
|
|
|
wrapper.scheduleSend(Protocol1_9To1_8.class);
|
|
|
|
} catch (Exception e) {
|
2024-03-09 13:57:41 +01:00
|
|
|
Via.getPlatform().getLogger().log(Level.WARNING, "Failed to send death message", e);
|
2016-04-12 13:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|