Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ private ServerEntityEvents() {
}
});

/**
* Called before an Entity is added to a ServerLevel.
*
* <p>If return value is {@code false} entity will not be added to a server.</p>
*/
public static final Event<ServerEntityEvents.AllowFreshLoad> ALLOW_FRESH_LOAD = EventFactory.createArrayBacked(ServerEntityEvents.AllowFreshLoad.class, callbacks -> (entity, level) -> {
boolean bl = true;

for (AllowFreshLoad callback : callbacks) {
bl = bl && callback.onFreshLoad(entity, level);
}

return bl;
});

/**
* Called when an Entity is added to a ServerLevel.
*
* <p>When this event is called, the entity is already in the level.
*/
public static final Event<ServerEntityEvents.AfterFreshLoad> AFTER_FRESH_LOAD = EventFactory.createArrayBacked(ServerEntityEvents.AfterFreshLoad.class, callbacks -> (entity, level) -> {
for (AfterFreshLoad callback : callbacks) {
callback.afterFreshLoad(entity, level);
}
});

/**
* Called when an Entity is unloaded from a ServerLevel.
*
Expand Down Expand Up @@ -68,6 +94,16 @@ public interface Load {
void onLoad(Entity entity, ServerLevel level);
}

@FunctionalInterface
public interface AllowFreshLoad {
boolean onFreshLoad(Entity entity, ServerLevel level);
}

@FunctionalInterface
public interface AfterFreshLoad {
void afterFreshLoad(Entity entity, ServerLevel level);
}

@FunctionalInterface
public interface Unload {
void onUnload(Entity entity, ServerLevel level);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@

import java.util.function.BooleanSupplier;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import org.objectweb.asm.Opcodes;
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.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;

@Mixin(ServerLevel.class)
Expand All @@ -40,4 +44,18 @@ private void startLevelTick(BooleanSupplier shouldKeepTicking, CallbackInfo ci)
private void endLevelTick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) {
ServerTickEvents.END_LEVEL_TICK.invoker().onEndTick((ServerLevel) (Object) this);
}

@WrapOperation(method = "addFreshEntity", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerLevel;addEntity(Lnet/minecraft/world/entity/Entity;)Z"))
private boolean afterEntityFreshLoad(ServerLevel instance, Entity entity, Operation<Boolean> original) {
ServerLevel serverLevel = (ServerLevel) (Object) this;
if (!ServerEntityEvents.ALLOW_FRESH_LOAD.invoker().onFreshLoad(entity, serverLevel)) return false;

boolean result = original.call(instance, entity);

if (result) {
ServerEntityEvents.AFTER_FRESH_LOAD.invoker().afterFreshLoad(entity, serverLevel);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public void onInitialize() {
}
});

ServerEntityEvents.ALLOW_FRESH_LOAD.register((entity, level) -> {
if (PRINT_SERVER_ENTITY_MESSAGES) {
logger.info("[SERVER] ALLOW FRESH LOAD {} IN {}", entity, level);
}

return true;
});

ServerEntityEvents.AFTER_FRESH_LOAD.register((entity, level) -> {
if (PRINT_SERVER_ENTITY_MESSAGES) {
logger.info("[SERVER] FRESH LOADED {} IN {}", entity, level);
}
});

ServerEntityEvents.ENTITY_UNLOAD.register((entity, level) -> {
this.serverEntities.remove(entity);

Expand Down
Loading