Skip to content

Commit

Permalink
Instead of using fabric's events, a kind of hook interface is better.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pugsworth committed Sep 25, 2020
1 parent b3cab7f commit 8701318
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 50 deletions.
32 changes: 14 additions & 18 deletions src/main/java/net/pugsworth/manythings/ManyThingsClientMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.render.EntityRendererRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.entity.ArrowEntityRenderer;
import net.minecraft.client.render.entity.FlyingItemEntityRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.util.ActionResult;
import net.pugsworth.manythings.client.EntityCreationStruct;
import net.minecraft.entity.EntityType;
import net.pugsworth.manythings.client.event.ClientEntitySpawnCallback;
import net.pugsworth.manythings.entity.ModEntities;
import net.pugsworth.manythings.entity.projectile.ArrowExplodingEntity;
import net.pugsworth.manythings.entity.thrown.ThrownDynamiteStickEntity;
import net.pugsworth.manythings.mixin.client.ClientPlayNetworkHandlerMixin;

@Environment(EnvType.CLIENT)
public class ManyThingsClientMod implements ClientModInitializer
Expand All @@ -25,21 +21,21 @@ public void onInitializeClient()
EntityRendererRegistry.INSTANCE.register(ArrowExplodingEntity.class, (dispatcher, context) -> new ArrowEntityRenderer(dispatcher));
EntityRendererRegistry.INSTANCE.register(ThrownDynamiteStickEntity.class, (dispatcher, context) -> new FlyingItemEntityRenderer<ThrownDynamiteStickEntity>(dispatcher, context.getItemRenderer(), 1.0f));

ClientEntitySpawnCallback.HOOKS.add((packet, player, world) -> {


ClientEntitySpawnCallback.EVENT.register(
(world, player, packet) -> {

if (packet.getEntityTypeId() == ModEntities.THROWN_DYNAMITE_STICK_ENTITY)
{
Entity entity = new ThrownDynamiteStickEntity(world, player);
EntityCreationStruct struct = new EntityCreationStruct(entity, packet);
struct.createEntity(world);
}

return ActionResult.PASS;
EntityType<?> id = packet.getEntityTypeId();

if (id == ModEntities.THROWN_DYNAMITE_STICK_ENTITY)
{
return new ThrownDynamiteStickEntity(world, player);
}
);
else if (id == ModEntities.ARROW_EXPLODING_ENTITY)
{
return new ArrowExplodingEntity(world, player);
}

return null;
});
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package net.pugsworth.manythings.client.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import java.util.ArrayList;

import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.network.packet.EntitySpawnS2CPacket;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.ActionResult;

public interface ClientEntitySpawnCallback {
Event<ClientEntitySpawnCallback> EVENT = EventFactory.createArrayBacked(ClientEntitySpawnCallback.class,
(listeners) -> (world, client, packet) -> {
for (ClientEntitySpawnCallback event : listeners) {
ActionResult result = event.onEntitySpawn(world, client, packet);

if (result != ActionResult.PASS) {
return result;
}
}

return ActionResult.PASS;
}
);
import net.minecraft.entity.Entity;

ActionResult onEntitySpawn(ClientWorld world, ClientPlayerEntity client, EntitySpawnS2CPacket packet);
public class ClientEntitySpawnCallback {
public static final ArrayList<ClientEntitySpawnHook> HOOKS = new ArrayList<>();
public interface ClientEntitySpawnHook {
public Entity getEntity(EntitySpawnS2CPacket packet, ClientPlayerEntity player, ClientWorld world);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package net.pugsworth.manythings.mixin.client;

import java.sql.Struct;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.Inject;
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.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.packet.EntitySpawnS2CPacket;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.pugsworth.manythings.client.event.ClientEntitySpawnCallback;
import net.pugsworth.manythings.client.event.ClientEntitySpawnCallback.ClientEntitySpawnHook;

@Mixin(ClientPlayNetworkHandler.class)
public class ClientPlayNetworkHandlerMixin {
Expand All @@ -29,12 +24,17 @@ public class ClientPlayNetworkHandlerMixin {

@Inject(method="onEntitySpawn",at=@At("TAIL"))
public void onEntitySpawn(EntitySpawnS2CPacket packet, CallbackInfo info) {
EntityType<?> etypeid = packet.getEntityTypeId();

ActionResult result = ClientEntitySpawnCallback.EVENT.invoker().onEntitySpawn(world, client.player, packet);

if (result == ActionResult.FAIL) {
info.cancel();
for (ClientEntitySpawnHook hook : ClientEntitySpawnCallback.HOOKS) {
Entity entity = hook.getEntity(packet, client.player, world);

if (entity != null) {
entity.updateTrackedPosition(packet.getX(), packet.getY(), packet.getZ());
entity.pitch = (float)(packet.getPitch() * 360) / 256.0F;
entity.yaw = (float)(packet.getYaw() * 360) / 256.0F;
entity.setEntityId(packet.getId());
entity.setUuid(packet.getUuid());
world.addEntity(packet.getId(), entity);
}
}
}
}
Binary file removed src/main/resources/assets/manythings/icon.png
Binary file not shown.

0 comments on commit 8701318

Please sign in to comment.