Skip to content

Commit

Permalink
Add option to replace PlayerDeathEvent death message (useful for repl…
Browse files Browse the repository at this point in the history
…acing messages shown by DiscordSRV)
  • Loading branch information
EvModder committed Jun 6, 2022
1 parent b25ddad commit 6834551
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
3 changes: 3 additions & 0 deletions config.yml
Expand Up @@ -14,6 +14,9 @@ local-broadcast-cross-dimension: true
# Replaces death messages if player behead announcement is GLOBAL
behead-announcement-replaces-player-death-message: true

# Also replace Bukkit event message; can help with DiscordSRV compatibility
behead-announcement-replaces-player-death-event-message: false

# Replaces death message that is sent to the owner when a pet is beheaded
behead-message-replaces-pet-death-message: true

Expand Down
5 changes: 3 additions & 2 deletions plugin.yml
Expand Up @@ -5,8 +5,9 @@ description: Provides a wide assortment of mob heads
website: https://dev.bukkit.org/projects/dropheads
#3=major rewrite
#7=pet death msgs, API fix, 1.19 mobs, full-cmd-translation, more pre-jappas(tentative), fix shulker rotation, updated spawn-cause-modifiers
#3=fix alwaysbehead not showing in /droprate, change spawn_eggs and /summon to have no affect on droprate, add sub-perm per entity to canbehead, fix discordsrv integration
version: 3.7.3
#3=fix alwaysbehead not showing in /droprate, change spawn_eggs and /summon to have no affect on droprate, add sub-perm per entity to canbehead
#1=fix discordsrv integration showing behead instead of regular death
version: 3.7.3.1
api-version: 1.13
softdepend: [HeadDatabase, Essentials, VanishNoPacket]

Expand Down
18 changes: 13 additions & 5 deletions src/net/evmodder/DropHeads/DropChanceAPI.java
Expand Up @@ -43,6 +43,7 @@
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.Vector;
import net.evmodder.DropHeads.events.EntityBeheadEvent;
import net.evmodder.DropHeads.listeners.DeathMessagePacketIntercepter;
import net.evmodder.EvLib.EvUtils;
import net.evmodder.EvLib.FileIO;
import net.evmodder.EvLib.extras.HeadUtils;
Expand All @@ -63,7 +64,7 @@ private enum AnnounceMode {GLOBAL, LOCAL, DIRECT, OFF};
private enum DropMode {EVENT, SPAWN, PLACE, PLACE_BY_KILLER, PLACE_BY_VICTIM, GIVE};
private final ArrayList<DropMode> DROP_MODES; // TODO: final HashMap<EntityType, DropMode> mobDropModes

private final boolean PLAYER_HEADS_ONLY, REPLACE_PLAYER_DEATH_EVT_MESSAGE, REPLACE_PET_DEATH_MESSAGE;
private final boolean PLAYER_HEADS_ONLY, REPLACE_PLAYER_DEATH_EVT_MESSAGE, REPLACE_PET_DEATH_MESSAGE, REPLACE_PLAYER_DEATH_MESSAGE;
private final boolean VANILLA_WSKELE_HANDLING;
private final double LOOTING_ADD, LOOTING_MULT;
private final boolean DEBUG_MODE, LOG_PLAYER_BEHEAD, LOG_MOB_BEHEAD;
Expand All @@ -80,6 +81,7 @@ private enum DropMode {EVENT, SPAWN, PLACE, PLACE_BY_KILLER, PLACE_BY_VICTIM, GI
};

private final DropHeads pl;
private final DeathMessagePacketIntercepter deathMessageBlocker;
private final Random rand;
private final HashSet<Material> headOverwriteBlocks;
private final Set<Material> mustUseTools;
Expand Down Expand Up @@ -121,16 +123,18 @@ private static String[] parseStringOrStringList(String key, String defaultMsg, C
return new String[]{TextUtils.translateAlternateColorCodes('&', defaultMsg)};
}

DropChanceAPI(final boolean replacePlayerDeathMsg, final boolean replacePetDeathMsg){
DropChanceAPI(final boolean replacePlayerDeathMsg, final boolean replacePetDeathMsg, DeathMessagePacketIntercepter deathMessageBlocker){
pl = DropHeads.getPlugin();
this.deathMessageBlocker = deathMessageBlocker;
rand = new Random();
PLAYER_HEADS_ONLY = pl.getConfig().getBoolean("player-heads-only", false);
VANILLA_WSKELE_HANDLING = pl.getConfig().getBoolean("vanilla-wither-skeleton-skulls", false);
LOOTING_ADD = pl.getConfig().getDouble("looting-addition", 0D);
LOOTING_MULT = pl.getConfig().getDouble("looting-multiplier", pl.getConfig().getDouble("looting-mutliplier", 1.01D));
if(LOOTING_ADD >= 1) pl.getLogger().warning("looting-addition is set to 1.0 or greater, this means heads will ALWAYS drop when looting is used!");
if(LOOTING_MULT < 1) pl.getLogger().warning("looting-multiplier is set below 1.0, this means looting will DECREASE the chance of head drops!");
REPLACE_PLAYER_DEATH_EVT_MESSAGE = replacePlayerDeathMsg;
REPLACE_PLAYER_DEATH_MESSAGE = replacePlayerDeathMsg;
REPLACE_PLAYER_DEATH_EVT_MESSAGE = pl.getConfig().getBoolean("behead-announcement-replaces-player-death-event-message", false);
REPLACE_PET_DEATH_MESSAGE = replacePetDeathMsg;
DEBUG_MODE = pl.getConfig().getBoolean("debug-messages", true);
final boolean ENABLE_LOG = pl.getConfig().getBoolean("log.enable", false);
Expand Down Expand Up @@ -562,10 +566,14 @@ public void announceHeadDrop(Component message, Entity entity, Entity killer, Ev
switch(mode){
case GLOBAL:
sendTellraw("@a", message.toString());
if(entity instanceof Player && REPLACE_PLAYER_DEATH_EVT_MESSAGE && evt != null){
if(entity instanceof Player && REPLACE_PLAYER_DEATH_MESSAGE && evt != null){
// Set the behead death message so that other plugins will see it (it will still get cleared by the intercepter)
//TODO: in order to do this, we will need to cancel this extra plaintext message in DeathMessagePacketIntercepter
//((PlayerDeathEvent)evt).setDeathMessage(message.toPlainText());
if(REPLACE_PLAYER_DEATH_EVT_MESSAGE){
final String plainDeathMsg = message.toPlainText();
((PlayerDeathEvent)evt).setDeathMessage(plainDeathMsg);
deathMessageBlocker.blockSpeficicMessage(plainDeathMsg, /*ticksBlockedFor=*/5);
}
}
break;
case LOCAL:
Expand Down
2 changes: 1 addition & 1 deletion src/net/evmodder/DropHeads/DropHeads.java
Expand Up @@ -76,7 +76,7 @@ public final class DropHeads extends EvPlugin{
if(REPLACE_PLAYER_DEATH_MSG || REPLACE_PET_DEATH_MSG){
deathMessageBlocker = new DeathMessagePacketIntercepter(REPLACE_PLAYER_DEATH_MSG, REPLACE_PET_DEATH_MSG);
}
dropChanceAPI = new DropChanceAPI(REPLACE_PLAYER_DEATH_MSG, REPLACE_PET_DEATH_MSG);
dropChanceAPI = new DropChanceAPI(REPLACE_PLAYER_DEATH_MSG, REPLACE_PET_DEATH_MSG, deathMessageBlocker);
new EntityDeathListener(deathMessageBlocker);

if(config.getBoolean("track-mob-spawns", true)){
Expand Down
Expand Up @@ -29,6 +29,7 @@ public class DeathMessagePacketIntercepter{
final boolean REPLACE_PLAYER_DEATH_MSG, REPLACE_PET_DEATH_MSG;
final HashSet<UUID> unblockedDeathBroadcasts;
final HashSet<String> unblockedSpecificDeathMsgs;
final HashSet<String> blockedSpecificMsgs;

final RefClass packetPlayOutChatClazz = ReflectionUtils.getRefClass(
"{nms}.PacketPlayOutChat", "{nm}.network.protocol.game.PacketPlayOutChat");
Expand All @@ -55,6 +56,9 @@ private void injectPlayer(Player player){
final Object chatBaseComp = chatBaseCompField.of(packet).get();
if(chatBaseComp != null){
final String jsonMsg = (String)toJsonMethod.call(chatBaseComp);
if(blockedSpecificMsgs.contains(jsonMsg)){
return;
}
// TODO: Possibly make death-message-translate-detection less hacky?
if(jsonMsg.startsWith("{\"translate\":\"death.") && !unblockedSpecificDeathMsgs.remove(jsonMsg)){
// pl.getLogger().info("detected death msg:\n"+jsonMsg);
Expand Down Expand Up @@ -104,6 +108,7 @@ public DeathMessagePacketIntercepter(boolean replacePlayerDeathMsg, boolean repl
REPLACE_PET_DEATH_MSG = replacePetDeathMsg;
unblockedDeathBroadcasts = new HashSet<>();
unblockedSpecificDeathMsgs = new HashSet<>();
blockedSpecificMsgs = new HashSet<>();

pl.getServer().getPluginManager().registerEvents(new Listener(){
@EventHandler public void onJoin(PlayerJoinEvent evt){injectPlayer(evt.getPlayer());}
Expand All @@ -119,6 +124,11 @@ public void unblockDeathMessage(Entity entity){
}
}

public void blockSpeficicMessage(String message, long ticksBlockedFor){
blockedSpecificMsgs.add(message);
new BukkitRunnable(){@Override public void run(){blockedSpecificMsgs.remove(message);}}.runTaskLater(pl, ticksBlockedFor);
}

public void unregisterAll(){
for(Player player : pl.getServer().getOnlinePlayers()) removePlayer(player);
}
Expand Down

0 comments on commit 6834551

Please sign in to comment.