Merge pull request #13 from kashike/rf/hl

Be more specific with return type
This commit is contained in:
Adrian Siekierka 2018-12-11 17:42:37 +01:00 committed by GitHub
commit 7eb62310c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 37 additions and 66 deletions

View file

@ -37,19 +37,18 @@ public final class TickEvent {
}
public static <T> void tick(HandlerRegistry<Consumer<T>> registry, T object, Profiler profiler) {
Object[] handlers = ((HandlerList<Consumer<T>>) registry).getBackingArray();
Consumer<T>[] handlers = ((HandlerList<Consumer<T>>) registry).getBackingArray();
if (handlers.length > 0) {
profiler.begin("fabric");
int i = 0;
for (Object handler : handlers) {
for (Consumer<T> handler : handlers) {
if ((i++) == 0) {
profiler.begin(handler.getClass().getName());
} else {
profiler.endBegin(handler.getClass().getName());
}
//noinspection unchecked
((Consumer<T>) handler).accept(object);
handler.accept(object);
}
if (i > 0) {

View file

@ -85,8 +85,8 @@ public abstract class MixinSpriteAtlasTexture {
//noinspection RedundantCast,ConstantConditions
if ((SpriteAtlasTexture) (Object) this == MinecraftClient.getInstance().getSpriteAtlas()) {
SpriteRegistry registry = new SpriteRegistry(sprites, (id) -> addSpriteToLoad(manager, id));
for (Object provider : ((HandlerList<SpriteEvent.Provider>) SpriteEvent.PROVIDE).getBackingArray()) {
((SpriteEvent.Provider) provider).registerSprites(registry);
for (SpriteEvent.Provider provider : ((HandlerList<SpriteEvent.Provider>) SpriteEvent.PROVIDE).getBackingArray()) {
provider.registerSprites(registry);
}
}

View file

@ -30,8 +30,8 @@ import java.util.function.BiConsumer;
public class MixinBlock {
@Inject(method = "<init>(Lnet/minecraft/block/Block$Settings;)V", at = @At("RETURN"))
public void init(Block.Settings builder, CallbackInfo info) {
for (Object o : ((HandlerList<BiConsumer<Block.Settings, Block>>) ObjectBuilderEvent.BLOCK).getBackingArray()) {
((BiConsumer<Block.Settings, Block>) o).accept(builder, (Block) (Object) this);
for (BiConsumer<Block.Settings, Block> consumer : ((HandlerList<BiConsumer<Block.Settings, Block>>) ObjectBuilderEvent.BLOCK).getBackingArray()) {
consumer.accept(builder, (Block) (Object) this);
}
}
}

View file

@ -30,8 +30,8 @@ import java.util.function.BiConsumer;
public class MixinItem {
@Inject(method = "<init>(Lnet/minecraft/item/Item$Settings;)V", at = @At("RETURN"))
public void init(Item.Settings builder, CallbackInfo info) {
for (Object o : ((HandlerList<BiConsumer<Item.Settings, Item>>) ObjectBuilderEvent.ITEM).getBackingArray()) {
((BiConsumer<Item.Settings, Item>) o).accept(builder, (Item) (Object) this);
for (BiConsumer<Item.Settings, Item> consumer : ((HandlerList<BiConsumer<Item.Settings, Item>>) ObjectBuilderEvent.ITEM).getBackingArray()) {
consumer.accept(builder, (Item) (Object) this);
}
}
}

View file

@ -52,9 +52,8 @@ public class MixinClientPlayerInteractionManager {
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/GameMode;isCreative()Z", ordinal = 0), method = "attackBlock", cancellable = true)
public void attackBlock(BlockPos pos, Facing facing, CallbackInfoReturnable<Boolean> info) {
for (Object handler : ((HandlerList<PlayerInteractionEvent.Block>) PlayerInteractionEvent.ATTACK_BLOCK).getBackingArray()) {
PlayerInteractionEvent.Block event = (PlayerInteractionEvent.Block) handler;
ActionResult result = event.interact(client.player, client.world, Hand.MAIN, pos, facing);
for (PlayerInteractionEvent.Block handler : ((HandlerList<PlayerInteractionEvent.Block>) PlayerInteractionEvent.ATTACK_BLOCK).getBackingArray()) {
ActionResult result = handler.interact(client.player, client.world, Hand.MAIN, pos, facing);
if (result != ActionResult.PASS) {
info.setReturnValue(result == ActionResult.SUCCESS);
info.cancel();
@ -69,9 +68,8 @@ public class MixinClientPlayerInteractionManager {
return;
}
for (Object handler : ((HandlerList<PlayerInteractionEvent.Block>) PlayerInteractionEvent.ATTACK_BLOCK).getBackingArray()) {
PlayerInteractionEvent.Block event = (PlayerInteractionEvent.Block) handler;
ActionResult result = event.interact(client.player, client.world, Hand.MAIN, pos, facing);
for (PlayerInteractionEvent.Block handler : ((HandlerList<PlayerInteractionEvent.Block>) PlayerInteractionEvent.ATTACK_BLOCK).getBackingArray()) {
ActionResult result = handler.interact(client.player, client.world, Hand.MAIN, pos, facing);
if (result != ActionResult.PASS) {
info.setReturnValue(result == ActionResult.SUCCESS);
info.cancel();
@ -82,15 +80,14 @@ public class MixinClientPlayerInteractionManager {
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;getStackInHand(Lnet/minecraft/util/Hand;)Lnet/minecraft/item/ItemStack;", ordinal = 0), method = "interactBlock", cancellable = true)
public void interactBlock(ClientPlayerEntity player, ClientWorld world, BlockPos pos, Facing facing, Vec3d vec, Hand hand, CallbackInfoReturnable<ActionResult> info) {
Object[] backingArray = ((HandlerList<PlayerInteractionEvent.BlockPositioned>) PlayerInteractionEvent.INTERACT_BLOCK).getBackingArray();
PlayerInteractionEvent.BlockPositioned[] backingArray = ((HandlerList<PlayerInteractionEvent.BlockPositioned>) PlayerInteractionEvent.INTERACT_BLOCK).getBackingArray();
if (backingArray.length > 0) {
float hitX = (float) (vec.x - pos.getX());
float hitY = (float) (vec.y - pos.getY());
float hitZ = (float) (vec.z - pos.getZ());
for (Object handler : backingArray) {
PlayerInteractionEvent.BlockPositioned event = (PlayerInteractionEvent.BlockPositioned) handler;
ActionResult result = event.interact(player, world, hand, pos, facing, hitX, hitY, hitZ);
for (PlayerInteractionEvent.BlockPositioned handler : backingArray) {
ActionResult result = handler.interact(player, world, hand, pos, facing, hitX, hitY, hitZ);
if (result != ActionResult.PASS) {
if (result == ActionResult.SUCCESS) {
this.networkHandler.sendPacket(new PlayerInteractBlockServerPacket(pos, facing, hand, hitX, hitY, hitZ));
@ -105,9 +102,8 @@ public class MixinClientPlayerInteractionManager {
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;getStackInHand(Lnet/minecraft/util/Hand;)Lnet/minecraft/item/ItemStack;", ordinal = 0), method = "interactItem", cancellable = true)
public void interactItem(PlayerEntity player, World world, Hand hand, CallbackInfoReturnable<ActionResult> info) {
for (Object handler : ((HandlerList<PlayerInteractionEvent.Item>) PlayerInteractionEvent.INTERACT_ITEM).getBackingArray()) {
PlayerInteractionEvent.Item event = (PlayerInteractionEvent.Item) handler;
ActionResult result = event.interact(player, world, hand);
for (PlayerInteractionEvent.Item handler : ((HandlerList<PlayerInteractionEvent.Item>) PlayerInteractionEvent.INTERACT_ITEM).getBackingArray()) {
ActionResult result = handler.interact(player, world, hand);
if (result != ActionResult.PASS) {
info.setReturnValue(result);
info.cancel();
@ -118,9 +114,8 @@ public class MixinClientPlayerInteractionManager {
@Inject(at = @At("HEAD"), method = "attackEntity", cancellable = true)
public void attackEntity(PlayerEntity player, Entity entity, CallbackInfo info) {
for (Object handler : ((HandlerList<PlayerInteractionEvent.Entity>) PlayerInteractionEvent.ATTACK_ENTITY).getBackingArray()) {
PlayerInteractionEvent.Entity event = (PlayerInteractionEvent.Entity) handler;
ActionResult result = event.interact(player, player.getEntityWorld(), Hand.MAIN /* TODO */, entity);
for (PlayerInteractionEvent.Entity handler : ((HandlerList<PlayerInteractionEvent.Entity>) PlayerInteractionEvent.ATTACK_ENTITY).getBackingArray()) {
ActionResult result = handler.interact(player, player.getEntityWorld(), Hand.MAIN /* TODO */, entity);
if (result != ActionResult.PASS) {
info.cancel();
return;
@ -133,9 +128,8 @@ public class MixinClientPlayerInteractionManager {
// TODO: Remove double Vec3d creation?
Vec3d hitVec = new Vec3d(hitResult.pos.x - entity.x, hitResult.pos.y - entity.y, hitResult.pos.z - entity.z);
for (Object handler : ((HandlerList<PlayerInteractionEvent.EntityPositioned>) PlayerInteractionEvent.INTERACT_ENTITY_POSITIONED).getBackingArray()) {
PlayerInteractionEvent.EntityPositioned event = (PlayerInteractionEvent.EntityPositioned) handler;
ActionResult result = event.interact(player, player.getEntityWorld(), hand, entity, hitVec);
for (PlayerInteractionEvent.EntityPositioned handler : ((HandlerList<PlayerInteractionEvent.EntityPositioned>) PlayerInteractionEvent.INTERACT_ENTITY_POSITIONED).getBackingArray()) {
ActionResult result = handler.interact(player, player.getEntityWorld(), hand, entity, hitVec);
if (result != ActionResult.PASS) {
info.setReturnValue(result);
info.cancel();

View file

@ -18,24 +18,15 @@ package net.fabricmc.fabric.mixin.events.playerinteraction;
import net.fabricmc.fabric.events.PlayerInteractionEvent;
import net.fabricmc.fabric.util.HandlerList;
import net.minecraft.client.network.packet.BlockUpdateClientPacket;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.ServerPlayerInteractionManager;
import net.minecraft.server.network.packet.PlayerInteractEntityServerPacket;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Facing;
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(ServerPlayNetworkHandler.class)
public class MixinServerPlayNetworkHandler {
@ -44,9 +35,8 @@ public class MixinServerPlayNetworkHandler {
@Inject(method = "onPlayerInteractEntity", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;interactAt(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/math/Vec3d;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/ActionResult;"), cancellable = true)
public void onPlayerInteractEntity(PlayerInteractEntityServerPacket packet, CallbackInfo info) {
for (Object handler : ((HandlerList<PlayerInteractionEvent.EntityPositioned>) PlayerInteractionEvent.INTERACT_ENTITY_POSITIONED).getBackingArray()) {
PlayerInteractionEvent.EntityPositioned event = (PlayerInteractionEvent.EntityPositioned) handler;
ActionResult result = event.interact(player, player.getEntityWorld(), packet.getHand(), packet.getEntity(player.world), packet.getHitPosition());
for (PlayerInteractionEvent.EntityPositioned handler : ((HandlerList<PlayerInteractionEvent.EntityPositioned>) PlayerInteractionEvent.INTERACT_ENTITY_POSITIONED).getBackingArray()) {
ActionResult result = handler.interact(player, player.getEntityWorld(), packet.getHand(), packet.getEntity(player.world), packet.getHitPosition());
if (result != ActionResult.PASS) {
info.cancel();
return;

View file

@ -19,13 +19,10 @@ package net.fabricmc.fabric.mixin.events.playerinteraction;
import net.fabricmc.fabric.events.PlayerInteractionEvent;
import net.fabricmc.fabric.util.HandlerList;
import net.minecraft.entity.Entity;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.packet.PlayerInteractEntityServerPacket;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
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;
@ -36,9 +33,8 @@ public class MixinServerPlayerEntity {
public void onPlayerInteractEntity(Entity target, CallbackInfo info) {
ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;
for (Object handler : ((HandlerList<PlayerInteractionEvent.Entity>) PlayerInteractionEvent.ATTACK_ENTITY).getBackingArray()) {
PlayerInteractionEvent.Entity event = (PlayerInteractionEvent.Entity) handler;
ActionResult result = event.interact(player, player.getEntityWorld(), Hand.MAIN, target);
for (PlayerInteractionEvent.Entity handler : ((HandlerList<PlayerInteractionEvent.Entity>) PlayerInteractionEvent.ATTACK_ENTITY).getBackingArray()) {
ActionResult result = handler.interact(player, player.getEntityWorld(), Hand.MAIN, target);
if (result != ActionResult.PASS) {
info.cancel();
return;

View file

@ -44,9 +44,8 @@ public class MixinServerPlayerInteractionManager {
@Inject(at = @At("HEAD"), method = "method_14263", cancellable = true)
public void startBlockBreak(BlockPos pos, Facing facing, CallbackInfo info) {
for (Object handler : ((HandlerList<PlayerInteractionEvent.Block>) PlayerInteractionEvent.ATTACK_BLOCK).getBackingArray()) {
PlayerInteractionEvent.Block event = (PlayerInteractionEvent.Block) handler;
ActionResult result = event.interact(player, world, Hand.MAIN, pos, facing);
for (PlayerInteractionEvent.Block handler : ((HandlerList<PlayerInteractionEvent.Block>) PlayerInteractionEvent.ATTACK_BLOCK).getBackingArray()) {
ActionResult result = handler.interact(player, world, Hand.MAIN, pos, facing);
if (result != ActionResult.PASS) {
// The client might have broken the block on its side, so make sure to let it know.
this.player.networkHandler.sendPacket(new BlockUpdateClientPacket(world, pos));
@ -58,9 +57,8 @@ public class MixinServerPlayerInteractionManager {
@Inject(at = @At("HEAD"), method = "interactBlock", cancellable = true)
public void interactBlock(PlayerEntity player, World world, ItemStack stack, Hand hand, BlockPos pos, Facing facing, float hitX, float hitY, float hitZ, CallbackInfoReturnable<ActionResult> info) {
for (Object handler : ((HandlerList<PlayerInteractionEvent.BlockPositioned>) PlayerInteractionEvent.INTERACT_BLOCK).getBackingArray()) {
PlayerInteractionEvent.BlockPositioned event = (PlayerInteractionEvent.BlockPositioned) handler;
ActionResult result = event.interact(player, world, hand, pos, facing, hitX, hitY, hitZ);
for (PlayerInteractionEvent.BlockPositioned handler : ((HandlerList<PlayerInteractionEvent.BlockPositioned>) PlayerInteractionEvent.INTERACT_BLOCK).getBackingArray()) {
ActionResult result = handler.interact(player, world, hand, pos, facing, hitX, hitY, hitZ);
if (result != ActionResult.PASS) {
info.setReturnValue(result);
info.cancel();
@ -71,9 +69,8 @@ public class MixinServerPlayerInteractionManager {
@Inject(at = @At("HEAD"), method = "interactItem", cancellable = true)
public void interactItem(PlayerEntity player, World world, ItemStack stack, Hand hand, CallbackInfoReturnable<ActionResult> info) {
for (Object handler : ((HandlerList<PlayerInteractionEvent.Item>) PlayerInteractionEvent.INTERACT_ITEM).getBackingArray()) {
PlayerInteractionEvent.Item event = (PlayerInteractionEvent.Item) handler;
ActionResult result = event.interact(player, world, hand);
for (PlayerInteractionEvent.Item handler : ((HandlerList<PlayerInteractionEvent.Item>) PlayerInteractionEvent.INTERACT_ITEM).getBackingArray()) {
ActionResult result = handler.interact(player, world, hand);
if (result != ActionResult.PASS) {
info.setReturnValue(result);
info.cancel();

View file

@ -17,26 +17,21 @@
package net.fabricmc.fabric.mixin.events.server;
import net.fabricmc.fabric.events.ServerEvent;
import net.fabricmc.fabric.events.TickEvent;
import net.fabricmc.fabric.util.HandlerList;
import net.minecraft.class_3689;
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;
import java.util.function.Consumer;
@Mixin(MinecraftServer.class)
public class MixinMinecraftServer {
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;method_3791(Lnet/minecraft/server/ServerMetadata;)V", ordinal = 0), method = "run")
public void afterSetupServer(CallbackInfo info) {
for (Object handler : ((HandlerList<Consumer<MinecraftServer>>) ServerEvent.START).getBackingArray()) {
//noinspection unchecked
((Consumer) handler).accept((Object) this);
for (Consumer<MinecraftServer> handler : ((HandlerList<Consumer<MinecraftServer>>) ServerEvent.START).getBackingArray()) {
handler.accept((MinecraftServer) (Object) this);
}
}
}

View file

@ -18,11 +18,11 @@ package net.fabricmc.fabric.util;
public class HandlerList<T> implements HandlerRegistry<T> {
private static final Object[] EMPTY = new Object[0];
private Object[] array;
private T[] array;
@SuppressWarnings("unchecked")
public HandlerList() {
this.array = EMPTY;
this.array = (T[]) EMPTY;
}
@Override
@ -40,7 +40,7 @@ public class HandlerList<T> implements HandlerRegistry<T> {
array = newArray;
}
public Object[] getBackingArray() {
public T[] getBackingArray() {
return array;
}
}