add tick events

This commit is contained in:
Adrian Siekierka 2018-12-02 22:36:51 +01:00
parent 8dbce0f433
commit 51e3b27151
8 changed files with 217 additions and 5 deletions

View file

@ -18,9 +18,7 @@ package net.fabricmc.fabric.events;
import net.fabricmc.fabric.util.HandlerList; import net.fabricmc.fabric.util.HandlerList;
import net.fabricmc.fabric.util.HandlerRegistry; import net.fabricmc.fabric.util.HandlerRegistry;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -52,9 +50,9 @@ public final class PlayerInteractionEvent {
ActionResult interact(PlayerEntity player, World world, Hand hand); ActionResult interact(PlayerEntity player, World world, Hand hand);
} }
public static HandlerRegistry<Block> BREAK_BLOCK = new HandlerList<>(); public static final HandlerRegistry<Block> BREAK_BLOCK = new HandlerList<>();
public static HandlerRegistry<BlockPositioned> INTERACT_BLOCK = new HandlerList<>(); public static final HandlerRegistry<BlockPositioned> INTERACT_BLOCK = new HandlerList<>();
public static HandlerRegistry<Item> INTERACT_ITEM = new HandlerList<>(); public static final HandlerRegistry<Item> INTERACT_ITEM = new HandlerList<>();
private PlayerInteractionEvent() { private PlayerInteractionEvent() {

View file

@ -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<Consumer<MinecraftServer>> SERVER = new HandlerList<>();
public static final HandlerRegistry<Consumer<World>> WORLD = new HandlerList<>();
private TickEvent() {
}
public static <T> void tick(HandlerRegistry<Consumer<T>> registry, T object, Profiler profiler) {
Object[] handlers = ((HandlerList<Consumer<T>>) 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<T>) handler).accept(object);
}
if (i > 0) {
profiler.end();
}
profiler.end();
}
}
}

View file

@ -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<Consumer<MinecraftClient>> CLIENT = new HandlerList<>();
private ClientTickEvent() {
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -4,6 +4,7 @@
"compatibilityLevel": "JAVA_8", "compatibilityLevel": "JAVA_8",
"mixins": [ "mixins": [
"events.MixinClientPlayerInteractionManager", "events.MixinClientPlayerInteractionManager",
"events.MixinMinecraftClient",
"networking.MixinClientPlayNetworkHandler", "networking.MixinClientPlayNetworkHandler",
"registry.client.MixinBlockColorMap", "registry.client.MixinBlockColorMap",
"registry.client.MixinItemColorMap", "registry.client.MixinItemColorMap",

View file

@ -4,7 +4,9 @@
"compatibilityLevel": "JAVA_8", "compatibilityLevel": "JAVA_8",
"mixins": [ "mixins": [
"commands.MixinServerCommandManager", "commands.MixinServerCommandManager",
"events.MixinMinecraftServer",
"events.MixinServerPlayerInteractionManager", "events.MixinServerPlayerInteractionManager",
"events.MixinWorld",
"helpers.MixinBlock", "helpers.MixinBlock",
"helpers.MixinBlockBuilder", "helpers.MixinBlockBuilder",
"helpers.MixinItem", "helpers.MixinItem",