Skip to content

Commit

Permalink
Add karting renderer.
Browse files Browse the repository at this point in the history
Signed-off-by: 秋雨落 <[email protected]>
  • Loading branch information
qyl27 committed Nov 17, 2024
1 parent a690456 commit cfdc56f
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 25 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ neoForge {
// "SCAN": For mods scan.
// "REGISTRIES": For firing of registry events.
// "REGISTRYDUMP": For getting the contents of all registries.
systemProperty 'forge.logging.markers', 'REGISTRIES'
// systemProperty 'forge.logging.markers', 'REGISTRIES'

// Recommended logging level for the console
// You can set various levels here.
// Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
logLevel = org.slf4j.event.Level.DEBUG
logLevel = org.slf4j.event.Level.INFO
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/teacon/powertool/client/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.teacon.powertool.client.renders.FenceKnotRenderer;
import org.teacon.powertool.client.renders.TempleRenderer;
import org.teacon.powertool.client.renders.entity.MartingEntityRenderer;
import org.teacon.powertool.client.renders.entity.model.MartingEntityModel;
import org.teacon.powertool.client.renders.holo_sign.HolographicSignBlockEntityRenderer;
import org.teacon.powertool.client.renders.ItemDisplayBlockEntityRenderer;
import org.teacon.powertool.client.renders.ItemSupplierBlockEntityRenderer;
Expand Down Expand Up @@ -165,6 +166,12 @@ public static void renderers(EntityRenderersEvent.RegisterRenderers event) {

event.registerEntityRenderer(PowerToolEntities.MARTING.get(), MartingEntityRenderer::new);
}

@SubscribeEvent
public static void on(EntityRenderersEvent.RegisterLayerDefinitions event) {
event.registerLayerDefinition(MartingEntityModel.LAYER, MartingEntityModel::createBodyLayer);
}

@SubscribeEvent
public static void on(RegisterGuiLayersEvent event) {
event.registerAbove(VanillaGuiLayers.CROSSHAIR, ResourceLocation.fromNamespaceAndPath(PowerTool.MODID, "cashier_hud"), (guiGraphics, partialTicks) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.teacon.powertool.client.renders.entity;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
Expand All @@ -12,23 +13,44 @@
import org.teacon.powertool.entity.MartingEntity;

public class MartingEntityRenderer extends EntityRenderer<MartingEntity> {

private MartingEntityModel<MartingEntity> model;
// private final Map<MartingEntity.Variant, Tuple<ResourceLocation, MartingEntityModel<MartingEntity>>> variantToModel;

public MartingEntityRenderer(EntityRendererProvider.Context context) {
super(context);
this.model = new MartingEntityModel<>(context.bakeLayer(MartingEntityModel.LAYER));

// this.variantToModel = Arrays.stream(MartingEntity.Variant.values())
// .collect(
// ImmutableMap.toImmutableMap(
// v -> v,
// v -> new Tuple<>(v.getTexture(), createModel(context, v)))
// );
}

// private MartingEntityModel<MartingEntity> createModel(EntityRendererProvider.Context context, MartingEntity.Variant variant) {
// }

@Override
public @NotNull ResourceLocation getTextureLocation(@NotNull MartingEntity entity) {
return entity.getVariant().getTexture();
}

@Override
public void render(MartingEntity entity, float entityYaw, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight) {
public void render(@NotNull MartingEntity entity, float entityYaw, float partialTick,
@NotNull PoseStack poseStack, @NotNull MultiBufferSource bufferSource, int packedLight) {
super.render(entity, entityYaw, partialTick, poseStack, bufferSource, packedLight);

var buffer = bufferSource.getBuffer(model.renderType(getTextureLocation(entity)));

poseStack.pushPose();
poseStack.translate(0, 1.5, 0);
poseStack.mulPose(Axis.YP.rotationDegrees(180.0F - entityYaw));
poseStack.scale(1, -1, 1);

model.renderToBuffer(poseStack, buffer, packedLight, OverlayTexture.NO_OVERLAY, FastColor.ARGB32.color(255,255,255,255));

poseStack.popPose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import org.teacon.powertool.entity.MartingEntity;

public class MartingEntityModel<T extends MartingEntity> extends EntityModel<T> {
public static final ModelLayerLocation LAYER = new ModelLayerLocation(ResourceLocation.fromNamespaceAndPath(PowerTool.MODID, "marting"), "main");
public static final ModelLayerLocation LAYER = new ModelLayerLocation(ResourceLocation.fromNamespaceAndPath(PowerTool.MODID, "marting_car"), "main");
// public static final ModelLayerLocation LAYER_RED = new ModelLayerLocation(ResourceLocation.fromNamespaceAndPath(PowerTool.MODID, "marting_car_red"), "main");
// public static final ModelLayerLocation LAYER_BLUE = new ModelLayerLocation(ResourceLocation.fromNamespaceAndPath(PowerTool.MODID, "marting_car_green"), "main");
// public static final ModelLayerLocation LAYER_GREEN = new ModelLayerLocation(ResourceLocation.fromNamespaceAndPath(PowerTool.MODID, "marting_car_blue"), "main");

private final ModelPart kart;
private final ModelPart seat;
Expand Down
113 changes: 111 additions & 2 deletions src/main/java/org/teacon/powertool/entity/MartingEntity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.teacon.powertool.entity;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.entity.vehicle.VehicleEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
Expand All @@ -12,18 +17,118 @@

import java.util.function.Supplier;

/**
* MalayP asked me to write a Karting Car.
* @author qyl27
*/
public class MartingEntity extends VehicleEntity {

// Todo: wheel speed should depends on velocity.
public static final double WHEEL_ROTATE_DEGREE_PER_TICK = 18; // 360 / 20

public static final int MAX_REMAINING_LIFE_TIME_TICKS = 120 * 20; // 2 minutes

// <editor-fold desc="Persistent states.">

private Variant variant = Variant.RED;

private int remainingLifeTimeTicks = MAX_REMAINING_LIFE_TIME_TICKS;

// </editor-fold>

// <editor-fold desc="Temporary states.">

// Rotate degrees of the steering wheel, negative for left, positive for right.
// Todo: each wheel speed depends on the steering wheel.
private static final EntityDataAccessor<Float> DATA_ID_STEERING_WHEEL_ROTATE_DEGREE = SynchedEntityData.defineId(MartingEntity.class, EntityDataSerializers.FLOAT);

private double wheelRotateDegree = 0; // Wheels rotate degrees, client only

// </editor-fold>

public MartingEntity(EntityType<?> entityType, Level level) {
super(entityType, level);
}

// <editor-fold desc="Entity staff.">

public void setVariant(Variant variant) {
this.variant = variant;
}

public Variant getVariant() {
return variant;
}

@Override
protected @NotNull Item getDropItem() {
return variant.getItemSupplier().get();
}

@Override
public void tick() {
if (!level().isClientSide) {
if (remainingLifeTimeTicks < 0) {
discard();
}

if (!getPassengers().isEmpty()) {
remainingLifeTimeTicks = MAX_REMAINING_LIFE_TIME_TICKS;
} else {
remainingLifeTimeTicks -= 1;
}

applyGravity();
updateInWaterStateAndDoFluidPushing();

move(MoverType.SELF, getDeltaMovement());
} else {
if (!getPassengers().isEmpty()) {
wheelRotateDegree += WHEEL_ROTATE_DEGREE_PER_TICK;
wheelRotateDegree %= 360;
}
}

super.tick();
}

// </editor-fold>

// <editor-fold desc="Physics.">

@Override
protected double getDefaultGravity() {
return 0.04;
}

@Override
public boolean canBeCollidedWith() {
return true;
}

@Override
public boolean isPushable() {
return true;
}

@Override
public boolean isPickable() {
return true;
}

@Override
public boolean canCollideWith(@NotNull Entity entity) {
return canVehicleCollide(this, entity);
}

public static boolean canVehicleCollide(@NotNull Entity vehicle, @NotNull Entity entity) {
return (entity.canBeCollidedWith() || entity.isPushable()) && !vehicle.isPassengerOfSameVehicle(entity);
}

// </editor-fold>

// <editor-fold desc="Data storage and sync.">

@Override
protected void readAdditionalSaveData(@NotNull CompoundTag compound) {
var variant = compound.getString("variant");
Expand All @@ -35,10 +140,14 @@ protected void addAdditionalSaveData(@NotNull CompoundTag compound) {
compound.putString("variant", variant.getName());
}

public Variant getVariant() {
return variant;
@Override
protected void defineSynchedData(SynchedEntityData.@NotNull Builder builder) {
super.defineSynchedData(builder);
builder.define(DATA_ID_STEERING_WHEEL_ROTATE_DEGREE, 0F);
}

// </editor-fold>

public enum Variant {
RED("marting_red", PowerToolItems.MARTING_RED),
GREEN("marting_green", PowerToolItems.MARTING_GREEN),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public class PowerToolEntities {
.clientTrackingRange(8)
.build("auto_vanish_minecart"));

public static final DeferredHolder<EntityType<?>, EntityType<MartingEntity>> MARTING = ENTITIES.register("marting",
public static final DeferredHolder<EntityType<?>, EntityType<MartingEntity>> MARTING = ENTITIES.register("marting_car",
() -> EntityType.Builder.of(MartingEntity::new, MobCategory.MISC)
.sized(0.98F, 0.7F)
.sized(1.5F, 0.75F)
.passengerAttachments(0.1875F)
.clientTrackingRange(8)
.build("marting"));
.build("marting_car"));

public static final DeferredHolder<EntityDataSerializer<?>, EntityDataSerializer<Set<BlockPos>>> BLOCK_POS_LIST = ENTITY_DATA_SERIALIZER.register(
"block_pos_list", () -> EntityDataSerializer.forValueType(BlockPos.STREAM_CODEC.apply(ByteBufCodecs.collection(HashSet::new))));
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/teacon/powertool/item/MartingItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
public class MartingItem extends Item {
public static final String TOOLTIP = "tooltip.powertool.marting";

public MartingItem(Properties properties) {
private final MartingEntity.Variant variant;

public MartingItem(Properties properties, MartingEntity.Variant variant) {
super(properties);
this.variant = variant;
}

@Override
Expand All @@ -31,8 +34,9 @@ public void appendHoverText(@NotNull ItemStack stack, @NotNull TooltipContext co
public @NotNull InteractionResult useOn(@NotNull UseOnContext context) {
var level = context.getLevel();
if (!level.isClientSide()) {
var pos = context.getClickedPos().relative(context.getClickedFace().getOpposite());
var pos = context.getClickedPos().relative(context.getClickedFace());
var entity = new MartingEntity(PowerToolEntities.MARTING.get(), level);
entity.setVariant(variant);
entity.setPos(pos.getCenter());
level.addFreshEntity(entity);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/teacon/powertool/item/PowerToolItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.teacon.powertool.block.CosmeticBlock;
import org.teacon.powertool.block.PowerToolBlocks;
import org.teacon.powertool.entity.FenceKnotEntity;
import org.teacon.powertool.entity.MartingEntity;
import org.teacon.powertool.utils.VanillaUtils;

import javax.annotation.ParametersAreNonnullByDefault;
Expand Down Expand Up @@ -109,9 +110,9 @@ public class PowerToolItems {
public static DeferredHolder<Item,AutoVanishMinecartItem> AV_MINE_CART;
public static DeferredHolder<Item, DisplayModeToolItem> DISPLAY_MODE_TOOL;

public static Supplier<Item> MARTING_RED = ITEMS.register("marting_red", () -> new MartingItem(new Item.Properties()));
public static Supplier<Item> MARTING_GREEN = ITEMS.register("marting_green", () -> new MartingItem(new Item.Properties()));
public static Supplier<Item> MARTING_BLUE = ITEMS.register("marting_blue", () -> new MartingItem(new Item.Properties()));
public static Supplier<Item> MARTING_RED = ITEMS.register("marting_car_red", () -> new MartingItem(new Item.Properties(), MartingEntity.Variant.RED));
public static Supplier<Item> MARTING_GREEN = ITEMS.register("marting_car_green", () -> new MartingItem(new Item.Properties(), MartingEntity.Variant.GREEN));
public static Supplier<Item> MARTING_BLUE = ITEMS.register("marting_car_blue", () -> new MartingItem(new Item.Properties(), MartingEntity.Variant.BLUE));

public static void register(IEventBus bus) {
ITEMS.register(bus);
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/assets/powertool/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@
"powertool.gui.display_mode_disabled": "Disabled display mode on %s",
"powertool.gui.display_mode_error": "Could not enable display mode on %s because it has no ability to open any screen. Or press shift key and click again to force enable display mode.",

"item.powertool.marting_red": "Marting Car (Red)",
"item.powertool.marting_green": "Marting Car (Green)",
"item.powertool.marting_blue": "Marting Car (Blue)",
"item.powertool.marting_car_red": "Marting Car (Red)",
"item.powertool.marting_car_green": "Marting Car (Green)",
"item.powertool.marting_car_blue": "Marting Car (Blue)",
"tooltip.powertool.marting": "MalayP asked me to write a karting car"
}
8 changes: 4 additions & 4 deletions src/main/resources/assets/powertool/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@
"powertool.gui.display_mode_disabled": "已在%s上禁用展示模式",
"powertool.gui.display_mode_error": "无法在%s上启用展示模式,因为它无法打开任何可交互界面.按下shift键强制启用展示模式.",

"item.powertool.marting_red": "马丁车(红色)",
"item.powertool.marting_green": "马丁车(绿色)",
"item.powertool.marting_blue": "马丁车(蓝色)",
"tooltip.powertool.marting": "马雷的卡丁车,简称马丁车"
"item.powertool.marting_car_red": "马丁车(红色)",
"item.powertool.marting_car_green": "马丁车(绿色)",
"item.powertool.marting_car_blue": "马丁车(蓝色)",
"tooltip.powertool.marting": "马雷让写的卡丁车,简称马丁车"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"credit": "Made with Blockbench",
"texture_size": [128, 128],
"textures": {
"1": "powertool:entity/marting_red"
"1": "powertool:item/marting_red"
},
"elements": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"parent": "powertool:item/marting",
"parent": "powertool:item/marting_car",
"textures": {
"1": "powertool:item/marting_blue"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"parent": "powertool:item/marting",
"parent": "powertool:item/marting_car",
"textures": {
"1": "powertool:item/marting_green"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"parent": "powertool:item/marting",
"parent": "powertool:item/marting_car",
"textures": {
"1": "powertool:item/marting_red"
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit cfdc56f

Please sign in to comment.