Implement events required by Simple Discord Link
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package com.hypherionmc.craterlib.mixin.events;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import com.hypherionmc.craterlib.api.event.server.CraterCommandEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import com.mojang.brigadier.ParseResults;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
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.CallbackInfoReturnable;
|
||||
|
||||
@Mixin(Commands.class)
|
||||
public class CommandMixin {
|
||||
|
||||
@Inject(method = "performCommand",
|
||||
at = @At(value = "INVOKE",
|
||||
target = "Lcom/mojang/brigadier/CommandDispatcher;execute(Lcom/mojang/brigadier/ParseResults;)I",
|
||||
shift = At.Shift.BEFORE
|
||||
), cancellable = true
|
||||
)
|
||||
private void injectCommandEvent(ParseResults<CommandSourceStack> stackParseResults, String command, CallbackInfoReturnable<Integer> cir) {
|
||||
CraterCommandEvent commandEvent = new CraterCommandEvent(stackParseResults, command);
|
||||
CraterEventBus.INSTANCE.postEvent(commandEvent);
|
||||
if (commandEvent.wasCancelled()) {
|
||||
cir.setReturnValue(1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (commandEvent.getException() != null) {
|
||||
Throwables.throwIfUnchecked(commandEvent.getException());
|
||||
cir.setReturnValue(1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package com.hypherionmc.craterlib.mixin.events;
|
||||
|
||||
import com.hypherionmc.craterlib.api.event.common.CraterLivingDeathEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import net.minecraft.world.damagesource.DamageSource;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
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;
|
||||
|
||||
@Mixin(LivingEntity.class)
|
||||
public class LivingEntityMixin {
|
||||
|
||||
@Inject(method = "die", at = @At("HEAD"), cancellable = true)
|
||||
private void injectPlayerDeathEvent(DamageSource damageSource, CallbackInfo ci) {
|
||||
CraterLivingDeathEvent event = new CraterLivingDeathEvent(((LivingEntity)(Object) this), damageSource);
|
||||
CraterEventBus.INSTANCE.postEvent(event);
|
||||
if (event.wasCancelled())
|
||||
ci.cancel();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.hypherionmc.craterlib.mixin.events;
|
||||
|
||||
import com.hypherionmc.craterlib.api.event.server.CraterAdvancementEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import net.minecraft.advancements.Advancement;
|
||||
import net.minecraft.server.PlayerAdvancements;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
@Mixin(PlayerAdvancements.class)
|
||||
public class PlayerAdvancementsMixin {
|
||||
|
||||
@Shadow private ServerPlayer player;
|
||||
|
||||
@Inject(method = "award", at = @At(value = "INVOKE", target = "Lnet/minecraft/advancements/AdvancementRewards;grant(Lnet/minecraft/server/level/ServerPlayer;)V", shift = At.Shift.AFTER))
|
||||
private void injectAdvancementEvent(Advancement advancement, String $$1, CallbackInfoReturnable<Boolean> cir) {
|
||||
CraterAdvancementEvent event = new CraterAdvancementEvent(this.player, advancement);
|
||||
|
||||
if (advancement.getDisplay() != null && advancement.getDisplay().shouldAnnounceChat()) {
|
||||
CraterEventBus.INSTANCE.postEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
package com.hypherionmc.craterlib.mixin.events;
|
||||
|
||||
import com.hypherionmc.craterlib.api.event.server.CraterPlayerEvent;
|
||||
import com.hypherionmc.craterlib.api.event.server.PlayerPreLoginEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.players.PlayerList;
|
||||
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
|
||||
@Mixin(PlayerList.class)
|
||||
public class PlayerListMixin {
|
||||
|
||||
@Inject(method = "placeNewPlayer", at = @At("TAIL"))
|
||||
private void injectPlayerLoginEvent(Connection connection, ServerPlayer serverPlayer, CallbackInfo ci) {
|
||||
CraterPlayerEvent.PlayerLoggedIn loggedIn = new CraterPlayerEvent.PlayerLoggedIn(serverPlayer);
|
||||
CraterEventBus.INSTANCE.postEvent(loggedIn);
|
||||
}
|
||||
|
||||
@Inject(method = "remove", at = @At("HEAD"))
|
||||
private void injectPlayerLogoutEvent(ServerPlayer player, CallbackInfo ci) {
|
||||
CraterPlayerEvent.PlayerLoggedOut loggedOut = new CraterPlayerEvent.PlayerLoggedOut(player);
|
||||
CraterEventBus.INSTANCE.postEvent(loggedOut);
|
||||
}
|
||||
|
||||
@Inject(method = "canPlayerLogin", at = @At("HEAD"), cancellable = true)
|
||||
private void injectPreLoginEvent(SocketAddress address, GameProfile gameProfile, CallbackInfoReturnable<Component> cir) {
|
||||
PlayerPreLoginEvent event = new PlayerPreLoginEvent(address, gameProfile);
|
||||
CraterEventBus.INSTANCE.postEvent(event);
|
||||
if (event.getMessage() != null) {
|
||||
cir.setReturnValue(event.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package com.hypherionmc.craterlib.mixin.events;
|
||||
|
||||
import com.hypherionmc.craterlib.api.event.common.CraterLivingDeathEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import net.minecraft.world.damagesource.DamageSource;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
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;
|
||||
|
||||
@Mixin(Player.class)
|
||||
public class PlayerMixin {
|
||||
|
||||
@Inject(method = "die", at = @At("HEAD"), cancellable = true)
|
||||
private void injectPlayerDeathEvent(DamageSource damageSource, CallbackInfo ci) {
|
||||
CraterLivingDeathEvent event = new CraterLivingDeathEvent(((Player)(Object) this), damageSource);
|
||||
CraterEventBus.INSTANCE.postEvent(event);
|
||||
if (event.wasCancelled())
|
||||
ci.cancel();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.hypherionmc.craterlib.mixin.events;
|
||||
|
||||
import com.hypherionmc.craterlib.api.event.server.CraterServerChatEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import net.minecraft.network.chat.PlayerChatMessage;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(ServerGamePacketListenerImpl.class)
|
||||
public class ServerGamePacketListenerImplMixin {
|
||||
|
||||
@Shadow public ServerPlayer player;
|
||||
|
||||
@Inject(method = "broadcastChatMessage", at = @At("HEAD"), cancellable = true)
|
||||
private void injectChatEvent(PlayerChatMessage chatMessage, CallbackInfo ci) {
|
||||
CraterServerChatEvent event = new CraterServerChatEvent(this.player, chatMessage.decoratedContent().getString(), chatMessage.decoratedContent());
|
||||
CraterEventBus.INSTANCE.postEvent(event);
|
||||
if (event.wasCancelled())
|
||||
ci.cancel();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package com.hypherionmc.craterlib.mixin.events;
|
||||
|
||||
import com.hypherionmc.craterlib.api.event.common.CraterLivingDeathEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.damagesource.DamageSource;
|
||||
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;
|
||||
|
||||
@Mixin(ServerPlayer.class)
|
||||
public class ServerPlayerMixin {
|
||||
|
||||
@Inject(method = "die", at = @At("HEAD"), cancellable = true)
|
||||
private void injectPlayerDeathEvent(DamageSource damageSource, CallbackInfo ci) {
|
||||
CraterLivingDeathEvent event = new CraterLivingDeathEvent(((ServerPlayer)(Object) this), damageSource);
|
||||
CraterEventBus.INSTANCE.postEvent(event);
|
||||
if (event.wasCancelled())
|
||||
ci.cancel();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user