Skip to content

Commit

Permalink
feat: mirror player deaths (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored Jun 7, 2021
1 parent 5616164 commit 33e5d02
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/net/modfest/utilities/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public String getToken() {
return this.data.discord.token;
}

public boolean shouldMirrorDeath() {
return this.data.discord.mirrorDeath;
}

public String getName() {
return this.data.server.name;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/modfest/utilities/data/ConfigData.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public static class Discord {
@Expose public String webhook = "";
@Expose public String channel = "";
@Expose public String token = "";
@Expose public boolean mirrorDeath = false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.modfest.utilities.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.modfest.utilities.config.Config;
import net.modfest.utilities.data.WebHookJson;
import net.modfest.utilities.discord.WebHookUtil;

@Mixin(ServerPlayerEntity.class)
public class ServerPlayerEntityMixin {
@Inject(method = "onDeath", at = @At("TAIL"))
private void sendDeathMessage(DamageSource source, CallbackInfo info) {
if (!Config.getInstance().shouldMirrorDeath()) return;
if (Config.getInstance().getWebhook().isEmpty()) return;

Text displayName = ((PlayerEntity) (Object) this).getDisplayName();
if (source.getAttacker() instanceof PlayerEntity) {
Text attackerDisplayName = ((PlayerEntity) source.getAttacker()).getDisplayName();
WebHookUtil.send(WebHookJson.createSystem("**" + displayName.getString() + "** Died To **" + attackerDisplayName.getString() + "**."));
} else {
WebHookUtil.send(WebHookJson.createSystem("**" + displayName.getString() + "** Died."));
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/modfestutilities.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"PlayerManagerMixin"
],
"server": [
"MinecraftDedicatedServerMixin"
"MinecraftDedicatedServerMixin",
"ServerPlayerEntityMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 33e5d02

Please sign in to comment.