Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First try to organize this repo more and implementation of #3 #4

Merged
merged 6 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 8 additions & 24 deletions src/main/java/net/arbee/addola/Addola.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
package net.arbee.addola;

import net.arbee.addola.entity.vehicle.ChestBoatEntity;
import net.arbee.addola.item.ChestBoatItem;
import net.arbee.addola.registries.Commands;
import net.arbee.addola.registries.Gamerules;
import net.arbee.addola.registries.AddolaCommands;
import net.arbee.addola.registries.AddolaEntities;
import net.arbee.addola.registries.AddolaGamerules;
import net.arbee.addola.registries.AddolaItems;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.entity.vehicle.BoatEntity;
import net.minecraft.item.ItemGroup;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class Addola implements ModInitializer {
public static final String MOD_NAME = "Addola";

public static final ChestBoatItem CHESTBOAT_ITEM = new ChestBoatItem(BoatEntity.Type.OAK, new FabricItemSettings().group(ItemGroup.TRANSPORTATION));

public static final EntityType<ChestBoatEntity> CHESTBOAT = Registry.register(
Registry.ENTITY_TYPE,
new Identifier("addola", "chestboat"),
FabricEntityTypeBuilder.<ChestBoatEntity>create(SpawnGroup.MISC, ChestBoatEntity::new).dimensions(EntityDimensions.fixed(1.375f, 0.5625f)).build()
);

@Override
public void onInitialize() {
Registry.register(Registry.ITEM, new Identifier("addola", "chestboat"), CHESTBOAT_ITEM);
Gamerules.setupGamerules();
Commands.setupCommands();
AddolaGamerules.setupGamerules();
AddolaCommands.setupCommands();
AddolaEntities.setupEntities();
AddolaItems.setupItems();
}
}
72 changes: 50 additions & 22 deletions src/main/java/net/arbee/addola/AddolaClient.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package net.arbee.addola;

import net.arbee.addola.entity.renderer.ChestBoatEntityRenderer;
import io.netty.buffer.Unpooled;
import net.arbee.addola.client.render.BoatItemRenderer;
import net.arbee.addola.client.render.ChestBoatEntityRenderer;
import net.arbee.addola.client.render.ChestBoatItemRenderer;
import net.arbee.addola.entity.vehicle.ChestBoatEntity;
import net.arbee.addola.network.SpawnChestBoatEntityPacketSender;
import net.arbee.addola.registries.AddolaEntities;
import net.arbee.addola.registries.AddolaItems;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendereregistry.v1.EntityRendererRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.MinecraftVersion;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.item.BoatItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;

import java.util.UUID;

Expand All @@ -16,27 +36,35 @@ public class AddolaClient implements ClientModInitializer {

@Override
public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(SpawnChestBoatEntityPacketSender.IDENTIFIER, (client, handler, buf, responseSender) -> {
final int entityId = buf.readInt();
final UUID uuid = buf.readUuid();
final double x = buf.readDouble();
final double y = buf.readDouble();
final double z = buf.readDouble();
final double xVelocity = buf.readDouble();
final double yVelocity = buf.readDouble();
final double zVelocity = buf.readDouble();
final float pitch = buf.readFloat();
final float yaw = buf.readFloat();
final ChestBoatEntity boat = new ChestBoatEntity(Addola.CHESTBOAT, client.world);
boat.setEntityId(entityId);
boat.setUuid(uuid);
boat.setPos(x, y, z);
boat.setVelocity(xVelocity, yVelocity, zVelocity);
boat.setYaw(yaw);
boat.pitch = pitch;
client.execute(() -> client.world.addEntity(entityId, boat));
});
AddolaItems.setupItemRenderers();

EntityRendererRegistry.INSTANCE.register(AddolaEntities.CHESTBOAT, (dispatcher, context) -> new ChestBoatEntityRenderer(dispatcher));

EntityRendererRegistry.INSTANCE.register(Addola.CHESTBOAT, (dispatcher, context) -> new ChestBoatEntityRenderer(dispatcher));
receiveEntityPacket();
}

public void receiveEntityPacket() {
ClientSidePacketRegistry.INSTANCE.register(SpawnChestBoatEntityPacketSender.IDENTIFIER, (ctx, byteBuf) -> {
EntityType<?> et = Registry.ENTITY_TYPE.get(byteBuf.readVarInt());
UUID uuid = byteBuf.readUuid();
int entityId = byteBuf.readVarInt();
Vec3d pos = SpawnChestBoatEntityPacketSender.PacketBufUtil.readVec3d(byteBuf);
float pitch = SpawnChestBoatEntityPacketSender.PacketBufUtil.readAngle(byteBuf);
float yaw = SpawnChestBoatEntityPacketSender.PacketBufUtil.readAngle(byteBuf);
ctx.getTaskQueue().execute(() -> {
if (MinecraftClient.getInstance().world == null)
throw new IllegalStateException("Tried to spawn entity in a null world!");
Entity e = et.create(MinecraftClient.getInstance().world);
if (e == null)
throw new IllegalStateException("Failed to create instance of entity \"" + Registry.ENTITY_TYPE.getId(et) + "\"!");
e.updateTrackedPosition(pos);
e.setPos(pos.x, pos.y, pos.z);
e.pitch = pitch;
e.yaw = yaw;
e.setEntityId(entityId);
e.setUuid(uuid);
MinecraftClient.getInstance().world.addEntity(entityId, e);
});
});
}
}
29 changes: 29 additions & 0 deletions src/main/java/net/arbee/addola/client/render/BoatItemRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.arbee.addola.client.render;

import net.arbee.addola.mixins.BoatItemAccess;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.render.model.json.ModelTransformation.Mode;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.vehicle.BoatEntity;
import net.minecraft.item.ItemStack;


public class BoatItemRenderer implements BuiltinItemRendererRegistry.DynamicItemRenderer {
public static BoatEntity DUMMY;

@Override
public void render(ItemStack stack, Mode mode, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
if ((DUMMY == null && MinecraftClient.getInstance().world != null) || MinecraftClient.getInstance().world != DUMMY.world) {
DUMMY = new BoatEntity(EntityType.BOAT, MinecraftClient.getInstance().world);
}
DUMMY.setBoatType(((BoatItemAccess)stack.getItem()).getType());
EntityRenderer<? super BoatEntity> renderer = MinecraftClient.getInstance().getEntityRenderDispatcher().getRenderer(DUMMY);
matrices.translate(0.5, 0, 0.5);
renderer.render(DUMMY, DUMMY.getYaw(10), 0, matrices, vertexConsumers, light);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package net.arbee.addola.entity.renderer;
package net.arbee.addola.client.render;

import net.arbee.addola.entity.vehicle.ChestBoatEntity;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.RenderLayer;
Expand All @@ -13,13 +12,10 @@
import net.minecraft.client.render.entity.model.BoatEntityModel;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.entity.vehicle.BoatEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Quaternion;

import java.rmi.registry.Registry;
import net.minecraft.util.registry.Registry;

public class ChestBoatEntityRenderer extends EntityRenderer<ChestBoatEntity> {
protected final BoatEntityModel model = new BoatEntityModel();
Expand Down Expand Up @@ -58,10 +54,12 @@ public void render(ChestBoatEntity chestBoatEntity, float f, float g, MatrixStac
VertexConsumer vertexConsumer2 = vertexConsumerProvider.getBuffer(RenderLayer.getWaterMask());
this.model.getBottom().render(matrixStack, vertexConsumer2, i, OverlayTexture.DEFAULT_UV);
}
matrixStack.scale(-0.9F, -0.9F, 0.9F);
matrixStack.translate(-0.1D, -0.15D, -0.5D);
matrixStack.scale(-0.8F, -0.8F, 0.8F);
matrixStack.translate(-0.0D, -0.05D, -0.5D);

BlockState blockEntity = Registry.BLOCK.get(new Identifier(chestBoatEntity.getBlockEntity())).getDefaultState();

MinecraftClient.getInstance().getBlockRenderManager().renderBlockAsEntity(Blocks.CHEST.getDefaultState(), matrixStack, vertexConsumerProvider, i, 0);
MinecraftClient.getInstance().getBlockRenderManager().renderBlockAsEntity(blockEntity, matrixStack, vertexConsumerProvider, i, OverlayTexture.DEFAULT_UV);

matrixStack.pop();
super.render(chestBoatEntity, f, g, matrixStack, vertexConsumerProvider, i);
Expand All @@ -71,13 +69,4 @@ public void render(ChestBoatEntity chestBoatEntity, float f, float g, MatrixStac
public Identifier getTexture(ChestBoatEntity chestBoatEntity) {
return TEXTURES[chestBoatEntity.getBoatType().ordinal()];
}

protected Block readCustomDataFromTag(CompoundTag tag) {
if (tag.contains("Block", 8)) {
tag.getString("Block");
return Blocks.CHEST;
} else {
return Blocks.CHEST;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.arbee.addola.client.render;

import net.arbee.addola.Addola;
import net.arbee.addola.entity.vehicle.ChestBoatEntity;
import net.arbee.addola.item.ChestBoatItem;
import net.arbee.addola.registries.AddolaEntities;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.render.model.json.ModelTransformation.Mode;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;

public class ChestBoatItemRenderer implements BuiltinItemRendererRegistry.DynamicItemRenderer {
public static ChestBoatEntity DUMMY;

@Override
public void render(ItemStack stack, Mode mode, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
if ((DUMMY == null && MinecraftClient.getInstance().world != null) || MinecraftClient.getInstance().world != DUMMY.world) {
DUMMY = new ChestBoatEntity(AddolaEntities.CHESTBOAT, MinecraftClient.getInstance().world);
}
DUMMY.setBoatType(((ChestBoatItem)stack.getItem()).getBoatType());
EntityRenderer<? super ChestBoatEntity> renderer = MinecraftClient.getInstance().getEntityRenderDispatcher().getRenderer(DUMMY);
matrices.translate(0.5, 0, 0.5);
renderer.render(DUMMY, DUMMY.getYaw(10), 0, matrices, vertexConsumers, light);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.arbee.addola.entity.ai.goal;

import net.arbee.addola.registries.Gamerules;
import net.arbee.addola.registries.AddolaGamerules;
import net.minecraft.entity.ai.TargetPredicate;
import net.minecraft.entity.ai.goal.TemptGoal;
import net.minecraft.entity.mob.PathAwareEntity;
Expand All @@ -16,7 +16,7 @@ public EmeraldGoal(PathAwareEntity mob, double speed, Ingredient food, boolean c

@Override
public boolean canStart() {
if (this.mob.world.getGameRules().getBoolean(Gamerules.VILLAGERS_FOLLOW)) {
if (this.mob.world.getGameRules().getBoolean(AddolaGamerules.VILLAGERS_FOLLOW)) {
if (this.cooldown > 0) {
--this.cooldown;
return false;
Expand Down
Loading