forked from trueagi-io/Vereya
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/io/singularitynet/events/ServerEntityEventsVereya.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.singularitynet.events; | ||
|
||
import net.fabricmc.fabric.api.event.Event; | ||
import net.fabricmc.fabric.api.event.EventFactory; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.profiler.Profiler; | ||
|
||
public interface ServerEntityEventsVereya { | ||
|
||
/** | ||
* Called when an Entity is about to be loaded into a ServerWorld. | ||
* | ||
* <p>Can be used to cancel entity loading. | ||
*/ | ||
public static final Event<ServerEntityEventsVereya> BEFORE_ENTITY_LOAD = EventFactory.createArrayBacked(ServerEntityEventsVereya.class, callbacks -> (entity, world) -> { | ||
if (EventFactory.isProfilingEnabled()) { | ||
final Profiler profiler = world.getProfiler(); | ||
profiler.push("fabricServerEntityLoadBefore"); | ||
|
||
for (ServerEntityEventsVereya callback : callbacks) { | ||
profiler.push(EventFactory.getHandlerName(callback)); | ||
ActionResult result = callback.interact(entity, world); | ||
profiler.pop(); | ||
if (result != ActionResult.PASS) { | ||
return result; | ||
} | ||
} | ||
profiler.pop(); | ||
} else { | ||
for (ServerEntityEventsVereya callback : callbacks) { | ||
ActionResult result = callback.interact(entity, world); | ||
if (result != ActionResult.PASS) { | ||
return result; | ||
} | ||
} | ||
} | ||
return ActionResult.PASS; | ||
}); | ||
|
||
public static final Event<ServerEntityEventsVereya> BEFORE_ENTITY_ADD = EventFactory.createArrayBacked(ServerEntityEventsVereya.class, callbacks -> (entity, world) -> { | ||
if (EventFactory.isProfilingEnabled()) { | ||
final Profiler profiler = world.getProfiler(); | ||
profiler.push("fabricServerEntityAddBefore"); | ||
|
||
for (ServerEntityEventsVereya callback : callbacks) { | ||
profiler.push(EventFactory.getHandlerName(callback)); | ||
ActionResult result = callback.interact(entity, world); | ||
profiler.pop(); | ||
if (result != ActionResult.PASS) { | ||
return result; | ||
} | ||
} | ||
profiler.pop(); | ||
} else { | ||
for (ServerEntityEventsVereya callback : callbacks) { | ||
ActionResult result = callback.interact(entity, world); | ||
if (result != ActionResult.PASS) { | ||
return result; | ||
} | ||
} | ||
} | ||
return ActionResult.PASS; | ||
}); | ||
|
||
ActionResult interact( Entity entity, ServerWorld world); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/io/singularitynet/mixin/ServerEntityManagerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.singularitynet.mixin; | ||
|
||
import net.minecraft.entity.Entity; | ||
import net.minecraft.server.world.ServerEntityManager; | ||
import net.minecraft.world.entity.EntityLike; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Invoker; | ||
|
||
@Mixin(ServerEntityManager.class) | ||
public interface ServerEntityManagerMixin<T extends EntityLike> { | ||
|
||
@Invoker("stopTracking") | ||
public void stopTracking(T entity); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/io/singularitynet/mixin/ServerWorldEntityLoaderMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.singularitynet.mixin; | ||
|
||
import io.singularitynet.events.ServerEntityEventsVereya; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.ActionResult; | ||
import org.spongepowered.asm.mixin.Final; | ||
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(targets = "net/minecraft/server/world/ServerWorld$ServerEntityHandler") | ||
abstract class ServerWorldEntityLoaderMixin { | ||
// final synthetic Lnet/minecraft/server/world/ServerWorld; field_26936 | ||
@SuppressWarnings("ShadowTarget") | ||
@Shadow | ||
@Final | ||
private ServerWorld field_26936; | ||
|
||
@Inject(method = "startTracking(Lnet/minecraft/entity/Entity;)V", at = @At("HEAD"), cancellable = true) | ||
private void invokeEntityLoadEvent(Entity entity, CallbackInfo ci) { | ||
ActionResult result = ServerEntityEventsVereya.BEFORE_ENTITY_LOAD.invoker().interact(entity, this.field_26936); | ||
if (result == ActionResult.FAIL) { | ||
ci.cancel(); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/io/singularitynet/mixin/ServerWorldMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.singularitynet.mixin; | ||
|
||
import io.singularitynet.events.ServerEntityEventsVereya; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.ActionResult; | ||
import org.spongepowered.asm.mixin.Final; | ||
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; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
@Mixin(ServerWorld.class) | ||
class ServerWorldMixin { | ||
|
||
@Inject(method = "Lnet/minecraft/server/world/ServerWorld;addEntity(Lnet/minecraft/entity/Entity;)Z", at = @At("HEAD"), cancellable = true) | ||
void invokeEntityAddEvent(Entity entity, CallbackInfoReturnable<Boolean> cir) { | ||
ActionResult result = ServerEntityEventsVereya.BEFORE_ENTITY_ADD.invoker().interact(entity, (ServerWorld)(Object)this); | ||
if (result == ActionResult.FAIL) { | ||
cir.cancel(); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters