Skip to content

Commit

Permalink
Entity module for fabric.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Dec 15, 2024
1 parent a736d1e commit 70eef29
Show file tree
Hide file tree
Showing 28 changed files with 555 additions and 58 deletions.
22 changes: 22 additions & 0 deletions api/fabric-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
dependencies {
minecraft("com.mojang:minecraft:${properties["minecraft_version"]}")
mappings(loom.layered {
officialMojangMappings()
parchment("org.parchmentmc.data:${properties["parchment"]}")
})
//Kyori
modCompileOnly("net.fabricmc:fabric-loader:${properties["loader_version"]}")
modCompileOnly("net.fabricmc.fabric-api:fabric-api:${properties["fabric_version"]}")
modCompileOnly("net.kyori:adventure-platform-mod-shared-fabric-repack:${properties["kyori_mod_implementation"]}")
modCompileOnly("net.kyori:adventure-platform-fabric:${properties["kyori_mod_implementation"]}")
}

loom {
decompilerOptions.named("vineflower") {
options.put("win", "0")
}
}

tasks.runServer {
enabled = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

public interface FabricBootstrap extends BetterHudBootstrap {

EventRegistry<EventRegistry.Unit> PRE_RELOAD_EVENT = new EventRegistry<>();
EventRegistry<EventRegistry.Unit> PRE_RELOAD_EVENT = new EventRegistry<EventRegistry.Unit>()
.register(u -> EventRegistry.clearAll());
EventRegistry<ReloadState> POST_RELOAD_EVENT = new EventRegistry<>();

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.toxicity.hud.api.fabric.event;

import net.minecraft.world.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;

/**
* Entity event
* @param <T> registry type
*/
public interface EntityEvent<T extends FabricEvent<?>> extends FabricEvent<T> {
@NotNull LivingEntity entity();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,61 @@

public final class EventRegistry<T> {

/**
* All registries
*/
private static final List<EventRegistry<?>> REGISTRIES = new ArrayList<>();

/**
* Unit instance
*/
public static final Unit UNIT = new Unit();

public static final class Unit {
private Unit() {
}
}

{
REGISTRIES.add(this);
}

private final List<Consumer<? super T>> registry = new ArrayList<>();
private final List<Consumer<? super T>> tempRegistry = new ArrayList<>();

/**
* Registers listener
* @param consumer listener
* @return self
*/
public @NotNull EventRegistry<T> register(@NotNull Consumer<? super T> consumer) {
registry.add(consumer);
return this;
}

@ApiStatus.Internal
public @NotNull EventRegistry<T> registerTemp(@NotNull Consumer<? super T> consumer) {
tempRegistry.add(consumer);
return this;
}

@ApiStatus.Internal
public void clear() {
tempRegistry.clear();
}

@ApiStatus.Internal
public static void clearAll() {
REGISTRIES.forEach(EventRegistry::clear);
}

@ApiStatus.Internal
public void call(@NotNull T t) {
for (Consumer<? super T> consumer : registry) {
consumer.accept(t);
}
for (Consumer<? super T> consumer : tempRegistry) {
consumer.accept(t);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.toxicity.hud.api.fabric.event;

import org.jetbrains.annotations.NotNull;

public interface FabricEvent<T extends FabricEvent<?>> {
@NotNull EventRegistry<T> getRegistry();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.toxicity.hud.api.fabric.event;

import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.NotNull;

/**
* Player event
* @param <T> registry type
*/
public interface PlayerEvent<T extends FabricEvent<?>> extends FabricEvent<T> {
@NotNull ServerPlayer player();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kr.toxicity.hud.api.fabric.event.entity;

import kr.toxicity.hud.api.fabric.event.EntityEvent;
import kr.toxicity.hud.api.fabric.event.EventRegistry;
import kr.toxicity.hud.api.fabric.event.PlayerEvent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;

/**
* Player attacks entity.
* @param player player
* @param entity entity
*/
public record PlayerAttackEntityEvent(@NotNull ServerPlayer player, @NotNull LivingEntity entity) implements PlayerEvent<PlayerAttackEntityEvent>, EntityEvent<PlayerAttackEntityEvent> {
/**
* Event registry
*/
public static final EventRegistry<PlayerAttackEntityEvent> REGISTRY = new EventRegistry<>();

@Override
public @NotNull EventRegistry<PlayerAttackEntityEvent> getRegistry() {
return REGISTRY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kr.toxicity.hud.api.fabric.event.entity;

import kr.toxicity.hud.api.fabric.event.EntityEvent;
import kr.toxicity.hud.api.fabric.event.EventRegistry;
import kr.toxicity.hud.api.fabric.event.PlayerEvent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;

/**
* Player damaged by entity.
* @param player player
* @param entity entity
*/
public record PlayerDamageByEntityEvent(@NotNull ServerPlayer player, @NotNull LivingEntity entity) implements PlayerEvent<PlayerDamageByEntityEvent>, EntityEvent<PlayerDamageByEntityEvent> {
/**
* Event registry
*/
public static final EventRegistry<PlayerDamageByEntityEvent> REGISTRY = new EventRegistry<>();

@Override
public @NotNull EventRegistry<PlayerDamageByEntityEvent> getRegistry() {
return REGISTRY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kr.toxicity.hud.api.fabric.event.entity;

import kr.toxicity.hud.api.fabric.event.EventRegistry;
import kr.toxicity.hud.api.fabric.event.PlayerEvent;
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.NotNull;

/**
* Player is dead.
* @param player player
*/
public record PlayerDeathEvent(@NotNull ServerPlayer player) implements PlayerEvent<PlayerDeathEvent> {
/**
* Event registry
*/
public static final EventRegistry<PlayerDeathEvent> REGISTRY = new EventRegistry<>();

@Override
public @NotNull EventRegistry<PlayerDeathEvent> getRegistry() {
return REGISTRY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kr.toxicity.hud.api.fabric.event.entity;

import kr.toxicity.hud.api.fabric.event.EntityEvent;
import kr.toxicity.hud.api.fabric.event.EventRegistry;
import kr.toxicity.hud.api.fabric.event.FabricEvent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;

/**
* Player kills entity
* @param player player
* @param entity entity
*/
public record PlayerKillEntityEvent(@NotNull ServerPlayer player, @NotNull LivingEntity entity) implements FabricEvent<PlayerKillEntityEvent>, EntityEvent<PlayerKillEntityEvent> {
/**
* Event registry
*/
public static final EventRegistry<PlayerKillEntityEvent> REGISTRY = new EventRegistry<>();

@Override
public @NotNull EventRegistry<PlayerKillEntityEvent> getRegistry() {
return REGISTRY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kr.toxicity.hud.api.fabric.trigger;

import kr.toxicity.hud.api.fabric.event.EventRegistry;
import kr.toxicity.hud.api.fabric.event.FabricEvent;
import kr.toxicity.hud.api.trigger.HudTrigger;
import org.jetbrains.annotations.NotNull;

/**
* Represents Fabric platform trigger.
* @param <T> registry type
*/
public interface HudFabricEventTrigger<T extends FabricEvent<?>> extends HudTrigger<T> {
/**
* Gets event registry
* @return registry
*/
@NotNull EventRegistry<T> registry();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kr.toxicity.hud.api.fabric.update;

import kr.toxicity.hud.api.fabric.event.FabricEvent;
import kr.toxicity.hud.api.update.UpdateEvent;
import kr.toxicity.hud.api.update.UpdateReason;
import org.jetbrains.annotations.NotNull;

/**
* Wrapped event of Fabric.
* @param event fabric event
* @see FabricEvent
* @see kr.toxicity.hud.api.fabric.event.EventRegistry
* @param key unique key
*/
public record FabricUpdateEvent(@NotNull FabricEvent<?> event, @NotNull Object key) implements UpdateEvent {

@Override
public @NotNull UpdateReason getType() {
return UpdateReason.FABRIC_EVENT;
}

@Override
public @NotNull Object getKey() {
return key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private BetterHudDependency(
"adventure-text-serializer-legacy",
BetterHud.ADVENTURE_VERSION,
false,
List.of(BetterHudPlatform.BUKKIT)
List.of(BetterHudPlatform.BUKKIT, BetterHudPlatform.FABRIC)
);
public static final BetterHudDependency ADVENTURE_TEXT_SERIALIZER_JSON = new BetterHudDependency(
"net{}kyori",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public enum UpdateReason {
EMPTY,
BUKKIT_EVENT,
VELOCITY_EVENT,
FABRIC_EVENT,
OTHER
}
15 changes: 9 additions & 6 deletions bootstrap/fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ repositories {
}

dependencies {
minecraft("com.mojang:minecraft:${project.properties["minecraft_version"]}")
minecraft("com.mojang:minecraft:${properties["minecraft_version"]}")
mappings(loom.layered {
officialMojangMappings()
parchment("org.parchmentmc.data:parchment-1.21:2024.07.28")
parchment("org.parchmentmc.data:${properties["parchment"]}")
})
//Other mod dependency
modCompileOnly("eu.pb4:polymer-resource-pack:0.11.1+1.21.4")
Expand All @@ -27,10 +27,10 @@ dependencies {
modCompileOnly("net.luckperms:api:5.4")

//Kyori
modCompileOnly("net.fabricmc:fabric-loader:${project.properties["loader_version"]}")
modCompileOnly("net.fabricmc.fabric-api:fabric-api:${project.properties["fabric_version"]}")
modCompileOnly("net.kyori:adventure-platform-mod-shared-fabric-repack:6.1.0")
modImplementation(include("net.kyori:adventure-platform-fabric:6.1.0")!!)
modCompileOnly("net.fabricmc:fabric-loader:${properties["loader_version"]}")
modCompileOnly("net.fabricmc.fabric-api:fabric-api:${properties["fabric_version"]}")
modCompileOnly("net.kyori:adventure-platform-mod-shared-fabric-repack:${properties["kyori_mod_implementation"]}")
modImplementation(include("net.kyori:adventure-platform-fabric:${properties["kyori_mod_implementation"]}")!!)
}

loom {
Expand Down Expand Up @@ -65,6 +65,9 @@ fabricModJson {
"polymer-autohost" to listOf("*"),
"placeholder-api" to listOf("*")
)
mixins = listOf(
mixin("betterhud.mixins.json")
)
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kr.toxicity.hud.bootstrap.fabric.mixin.entity;

import kr.toxicity.hud.api.fabric.event.entity.PlayerDeathEvent;
import net.minecraft.core.Holder;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.gameevent.GameEvent;
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(Entity.class)
public class EntityMixin {
@Inject(method = "gameEvent(Lnet/minecraft/core/Holder;)V", at = @At("HEAD"))
private void gameEvent(Holder<GameEvent> event, CallbackInfo info) {
if (GameEvent.ENTITY_DIE.is(event)) {
if (((Object) this) instanceof ServerPlayer serverPlayer) {
PlayerDeathEvent.REGISTRY.call(new PlayerDeathEvent(serverPlayer));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kr.toxicity.hud.bootstrap.fabric.mixin.entity;

import kr.toxicity.hud.api.fabric.event.entity.PlayerDamageByEntityEvent;
import kr.toxicity.hud.api.fabric.event.entity.PlayerAttackEntityEvent;
import kr.toxicity.hud.api.fabric.event.entity.PlayerKillEntityEvent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
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.CallbackInfoReturnable;

@Mixin(LivingEntity.class)
public class LivingEntityMixin {

@Inject(method = "hurtServer", at = @At("TAIL"))
private void hurtServer(ServerLevel serverLevel, DamageSource damageSource, float f, CallbackInfoReturnable<Boolean> cir) {
var entity = (LivingEntity) (Object) this;
if (damageSource.getEntity() instanceof ServerPlayer player) {
PlayerAttackEntityEvent.REGISTRY.call(new PlayerAttackEntityEvent(player, entity));
if (entity.isDeadOrDying()) {
PlayerKillEntityEvent.REGISTRY.call(new PlayerKillEntityEvent(player, entity));
}
}
if (((Object) this) instanceof ServerPlayer player) {
PlayerDamageByEntityEvent.REGISTRY.call(new PlayerDamageByEntityEvent(player, entity));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.toxicity.hud.bootstrap.fabric.module

import kr.toxicity.hud.api.fabric.trigger.HudFabricEventTrigger
import kr.toxicity.hud.api.yaml.YamlObject

interface FabricModule : Module {
override val triggers: Map<String, (YamlObject) -> HudFabricEventTrigger<*>>
}
Loading

0 comments on commit 70eef29

Please sign in to comment.