From 51e3b271511005dcc6c4d6db12872637566bdc83 Mon Sep 17 00:00:00 2001 From: Adrian Siekierka Date: Sun, 2 Dec 2018 22:36:51 +0100 Subject: [PATCH] add tick events --- .../fabric/events/PlayerInteractionEvent.java | 8 +-- .../net/fabricmc/fabric/events/TickEvent.java | 57 +++++++++++++++++++ .../fabric/events/client/ClientTickEvent.java | 31 ++++++++++ .../mixin/events/MixinMinecraftClient.java | 41 +++++++++++++ .../mixin/events/MixinMinecraftServer.java | 41 +++++++++++++ .../fabric/mixin/events/MixinWorld.java | 41 +++++++++++++ .../net.fabricmc.fabric.mixins.client.json | 1 + .../net.fabricmc.fabric.mixins.common.json | 2 + 8 files changed, 217 insertions(+), 5 deletions(-) create mode 100644 src/main/java/net/fabricmc/fabric/events/TickEvent.java create mode 100644 src/main/java/net/fabricmc/fabric/events/client/ClientTickEvent.java create mode 100644 src/main/java/net/fabricmc/fabric/mixin/events/MixinMinecraftClient.java create mode 100644 src/main/java/net/fabricmc/fabric/mixin/events/MixinMinecraftServer.java create mode 100644 src/main/java/net/fabricmc/fabric/mixin/events/MixinWorld.java diff --git a/src/main/java/net/fabricmc/fabric/events/PlayerInteractionEvent.java b/src/main/java/net/fabricmc/fabric/events/PlayerInteractionEvent.java index 689fcba0e..1af054161 100644 --- a/src/main/java/net/fabricmc/fabric/events/PlayerInteractionEvent.java +++ b/src/main/java/net/fabricmc/fabric/events/PlayerInteractionEvent.java @@ -18,9 +18,7 @@ package net.fabricmc.fabric.events; import net.fabricmc.fabric.util.HandlerList; import net.fabricmc.fabric.util.HandlerRegistry; -import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.ItemStack; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.math.BlockPos; @@ -52,9 +50,9 @@ public final class PlayerInteractionEvent { ActionResult interact(PlayerEntity player, World world, Hand hand); } - public static HandlerRegistry BREAK_BLOCK = new HandlerList<>(); - public static HandlerRegistry INTERACT_BLOCK = new HandlerList<>(); - public static HandlerRegistry INTERACT_ITEM = new HandlerList<>(); + public static final HandlerRegistry BREAK_BLOCK = new HandlerList<>(); + public static final HandlerRegistry INTERACT_BLOCK = new HandlerList<>(); + public static final HandlerRegistry INTERACT_ITEM = new HandlerList<>(); private PlayerInteractionEvent() { diff --git a/src/main/java/net/fabricmc/fabric/events/TickEvent.java b/src/main/java/net/fabricmc/fabric/events/TickEvent.java new file mode 100644 index 000000000..d409759c3 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/events/TickEvent.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.events; + +import net.fabricmc.fabric.util.HandlerList; +import net.fabricmc.fabric.util.HandlerRegistry; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.Profiler; +import net.minecraft.world.World; + +import java.util.function.Consumer; + +public final class TickEvent { + public static final HandlerRegistry> SERVER = new HandlerList<>(); + public static final HandlerRegistry> WORLD = new HandlerList<>(); + + private TickEvent() { + + } + + public static void tick(HandlerRegistry> registry, T object, Profiler profiler) { + Object[] handlers = ((HandlerList>) registry).getBackingArray(); + if (handlers.length > 0) { + profiler.begin("fabric"); + + int i = 0; + for (Object handler : handlers) { + if ((i++) == 0) { + profiler.begin(handler.getClass().getName()); + } else { + profiler.endBegin(handler.getClass().getName()); + } + //noinspection unchecked + ((Consumer) handler).accept(object); + } + + if (i > 0) { + profiler.end(); + } + profiler.end(); + } + } +} diff --git a/src/main/java/net/fabricmc/fabric/events/client/ClientTickEvent.java b/src/main/java/net/fabricmc/fabric/events/client/ClientTickEvent.java new file mode 100644 index 000000000..02b1b9dcc --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/events/client/ClientTickEvent.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.events.client; + +import net.fabricmc.fabric.util.HandlerList; +import net.fabricmc.fabric.util.HandlerRegistry; +import net.minecraft.client.MinecraftClient; + +import java.util.function.Consumer; + +public final class ClientTickEvent { + public static final HandlerRegistry> CLIENT = new HandlerList<>(); + + private ClientTickEvent() { + + } +} diff --git a/src/main/java/net/fabricmc/fabric/mixin/events/MixinMinecraftClient.java b/src/main/java/net/fabricmc/fabric/mixin/events/MixinMinecraftClient.java new file mode 100644 index 000000000..1206f7209 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/mixin/events/MixinMinecraftClient.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.mixin.events; + +import net.fabricmc.fabric.events.TickEvent; +import net.fabricmc.fabric.events.client.ClientTickEvent; +import net.minecraft.class_3689; +import net.minecraft.client.MinecraftClient; +import net.minecraft.server.MinecraftServer; +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 java.util.function.BooleanSupplier; + +@Mixin(MinecraftClient.class) +public class MixinMinecraftClient { + @Shadow + private class_3689 profiler; + + @Inject(at = @At("RETURN"), method = "tick") + public void tick(CallbackInfo info) { + TickEvent.tick(ClientTickEvent.CLIENT, (MinecraftClient) (Object) this, this.profiler); + } +} diff --git a/src/main/java/net/fabricmc/fabric/mixin/events/MixinMinecraftServer.java b/src/main/java/net/fabricmc/fabric/mixin/events/MixinMinecraftServer.java new file mode 100644 index 000000000..b543a9695 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/mixin/events/MixinMinecraftServer.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.mixin.events; + +import net.fabricmc.fabric.events.TickEvent; +import net.minecraft.class_3689; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.Profiler; +import net.minecraft.world.World; +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 java.util.function.BooleanSupplier; + +@Mixin(MinecraftServer.class) +public class MixinMinecraftServer { + @Shadow + private class_3689 profiler; + + @Inject(at = @At("RETURN"), method = "method_3813") + protected void method_3813(BooleanSupplier var1, CallbackInfo info) { + TickEvent.tick(TickEvent.SERVER, (MinecraftServer) (Object) this, this.profiler); + } +} diff --git a/src/main/java/net/fabricmc/fabric/mixin/events/MixinWorld.java b/src/main/java/net/fabricmc/fabric/mixin/events/MixinWorld.java new file mode 100644 index 000000000..c3c40e657 --- /dev/null +++ b/src/main/java/net/fabricmc/fabric/mixin/events/MixinWorld.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.mixin.events; + +import net.fabricmc.fabric.events.PlayerInteractionEvent; +import net.fabricmc.fabric.events.TickEvent; +import net.fabricmc.fabric.util.HandlerList; +import net.minecraft.util.Profiler; +import net.minecraft.world.World; +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 java.util.function.Consumer; + +@Mixin(World.class) +public class MixinWorld { + @Shadow + private Profiler profiler; + + @Inject(at = @At("RETURN"), method = "updateEntities") + public void updateEntities(CallbackInfo info) { + TickEvent.tick(TickEvent.WORLD, (World) (Object) this, this.profiler); + } +} diff --git a/src/main/resources/net.fabricmc.fabric.mixins.client.json b/src/main/resources/net.fabricmc.fabric.mixins.client.json index 2cee76b4a..628b0607b 100644 --- a/src/main/resources/net.fabricmc.fabric.mixins.client.json +++ b/src/main/resources/net.fabricmc.fabric.mixins.client.json @@ -4,6 +4,7 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "events.MixinClientPlayerInteractionManager", + "events.MixinMinecraftClient", "networking.MixinClientPlayNetworkHandler", "registry.client.MixinBlockColorMap", "registry.client.MixinItemColorMap", diff --git a/src/main/resources/net.fabricmc.fabric.mixins.common.json b/src/main/resources/net.fabricmc.fabric.mixins.common.json index 192a0c70f..68b0e490d 100644 --- a/src/main/resources/net.fabricmc.fabric.mixins.common.json +++ b/src/main/resources/net.fabricmc.fabric.mixins.common.json @@ -4,7 +4,9 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "commands.MixinServerCommandManager", + "events.MixinMinecraftServer", "events.MixinServerPlayerInteractionManager", + "events.MixinWorld", "helpers.MixinBlock", "helpers.MixinBlockBuilder", "helpers.MixinItem",