Skip to content

Commit

Permalink
lock and load
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Apr 2, 2024
1 parent 365f0d7 commit 773b290
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.tropheusj.its_as_shrimple_as_that;

import io.github.tropheusj.its_as_shrimple_as_that.arrow.ShrimpArrowEntity;
import io.github.tropheusj.its_as_shrimple_as_that.arrow.ShrimpArrowItem;
import io.github.tropheusj.its_as_shrimple_as_that.command.KrillCommand;
import io.github.tropheusj.its_as_shrimple_as_that.criteria.KrillSelfTrigger;
import io.github.tropheusj.its_as_shrimple_as_that.criteria.ShrimpAccomplishDreamsTrigger;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class ItsAsShrimpleAsThat implements ModInitializer {

public static final Item FRIED_RICE = new FriedRiceItem(FriedRiceItem.makeProperties());

public static final Item SHRIMP_ARROW = new ArrowItem(new Item.Properties());
public static final Item SHRIMP_ARROW = new ShrimpArrowItem(new Item.Properties());

public static final Item SHRIMP_EGG = new SpawnEggItem(SHRIMP_TYPE, 0xFF977C66, 0xFFFFFFFF, new Properties());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.github.tropheusj.its_as_shrimple_as_that;

import io.github.tropheusj.its_as_shrimple_as_that.arrow.ShrimpArrowRenderer;
import io.github.tropheusj.its_as_shrimple_as_that.entity.render.ShrimpModel;
import io.github.tropheusj.its_as_shrimple_as_that.entity.render.ShrimpRenderer;
import io.github.tropheusj.its_as_shrimple_as_that.item.ShrimpCrossbowRenderer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.EntityModelLayerRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry;

import net.minecraft.client.renderer.entity.NoopRenderer;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.Items;
Expand All @@ -15,14 +17,16 @@
public class ItsAsShrimpleAsThatClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ItemProperties.register(Items.CROSSBOW, ItsAsShrimpleAsThat.id("krill"), (stack, level, entity, i) -> {
ItemProperties.register(Items.CROSSBOW, ItsAsShrimpleAsThat.id("shrimp"), (stack, level, entity, i) -> {
ChargedProjectiles projectiles = stack.get(DataComponents.CHARGED_PROJECTILES);
return projectiles != null && projectiles.contains(Items.FIREWORK_ROCKET) ? 1 : 0;
return projectiles != null && projectiles.contains(ItsAsShrimpleAsThat.SHRIMP_ARROW) ? 1 : 0;
});

EntityModelLayerRegistry.registerModelLayer(ShrimpModel.LAYER_LOCATION, ShrimpModel::createBodyLayer);

EntityRendererRegistry.register(ItsAsShrimpleAsThat.SHRIMP_TYPE, ShrimpRenderer::new);
EntityRendererRegistry.register(ItsAsShrimpleAsThat.SHRIMP_ARROW_TYPE, NoopRenderer::new);
EntityRendererRegistry.register(ItsAsShrimpleAsThat.SHRIMP_ARROW_TYPE, ShrimpArrowRenderer::new);

BuiltinItemRendererRegistry.INSTANCE.register(Items.CROSSBOW, new ShrimpCrossbowRenderer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.tropheusj.its_as_shrimple_as_that.entity.ShrimpEntity;
import io.github.tropheusj.its_as_shrimple_as_that.entity.Krillification;
import net.minecraft.core.BlockPos;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.AbstractArrow;
Expand All @@ -14,33 +15,42 @@
import net.minecraft.world.phys.BlockHitResult;

import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.Vec3;

import org.jetbrains.annotations.NotNull;

public class ShrimpArrowEntity extends AbstractArrow {
public ShrimpArrowEntity(EntityType<? extends ShrimpArrowEntity> entityType, Level level) {
super(entityType, level);
this.setSilent(true);
}

@Override
protected void onHitBlock(BlockHitResult hit) {
super.onHitBlock(hit);
if (this.level().isClientSide)
return;
// free him
BlockPos offset = hit.getBlockPos().relative(hit.getDirection());
ShrimpEntity krill = ItsAsShrimpleAsThat.SHRIMP_TYPE.create(this.level());
if (krill != null) {
krill.moveTo(offset, 0, 0);
this.level().addFreshEntity(krill);
}
this.spawnShrimp(Vec3.atBottomCenterOf(offset));
}

@Override
protected void onHitEntity(EntityHitResult hit) {
super.onHitEntity(hit);
if (this.level().isClientSide)
return;
if (hit.getEntity() instanceof LivingEntity living)
Krillification.transform(living, this.getOwner());
this.spawnShrimp(hit.getLocation());
this.discard();
}

private void spawnShrimp(Vec3 pos) {
ShrimpEntity shrimp = ItsAsShrimpleAsThat.SHRIMP_TYPE.create(this.level());
if (shrimp != null) {
shrimp.moveTo(pos);
this.level().addFreshEntity(shrimp);
this.discard();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.tropheusj.its_as_shrimple_as_that.arrow;

import io.github.tropheusj.its_as_shrimple_as_that.ItsAsShrimpleAsThat;

import org.jetbrains.annotations.NotNull;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.item.ArrowItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public class ShrimpArrowItem extends ArrowItem {
public ShrimpArrowItem(Properties properties) {
super(properties);
}

@Override
@NotNull
public AbstractArrow createArrow(Level level, ItemStack itemStack, LivingEntity shooter) {
ShrimpArrowEntity arrow = new ShrimpArrowEntity(ItsAsShrimpleAsThat.SHRIMP_ARROW_TYPE, level);
arrow.setPos(shooter.getX(), shooter.getEyeY() - 0.1, shooter.getZ());
arrow.setOwner(shooter);
return arrow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.github.tropheusj.its_as_shrimple_as_that.arrow;

import com.mojang.math.Axis;

import io.github.tropheusj.its_as_shrimple_as_that.entity.render.ShrimpModel;
import io.github.tropheusj.its_as_shrimple_as_that.entity.render.ShrimpRenderer;

import org.jetbrains.annotations.NotNull;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;

import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider.Context;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;

public class ShrimpArrowRenderer extends EntityRenderer<ShrimpArrowEntity> {
private final ShrimpModel<ShrimpArrowEntity> model;

public ShrimpArrowRenderer(Context context) {
super(context);
this.model = new ShrimpModel<>(context);
}

@Override
public void render(ShrimpArrowEntity shrimp, float f, float g, PoseStack matrices, MultiBufferSource buffers, int light) {
matrices.pushPose();
matrices.mulPose(Axis.YP.rotationDegrees(Mth.lerp(g, shrimp.yRotO, shrimp.getYRot()) - 90.0F));
matrices.mulPose(Axis.ZP.rotationDegrees(Mth.lerp(g, shrimp.xRotO, shrimp.getXRot())));

matrices.mulPose(Axis.ZP.rotationDegrees(90));
matrices.mulPose(Axis.YP.rotationDegrees(90));
matrices.mulPose(Axis.XN.rotationDegrees(90));
matrices.translate(0, -1.4, 0);

VertexConsumer vertexConsumer = buffers.getBuffer(RenderType.entityCutout(this.getTextureLocation(shrimp)));

this.model.renderToBuffer(matrices, vertexConsumer, light, OverlayTexture.NO_OVERLAY, 1, 1, 1, 1);
matrices.popPose();
}

@Override
@NotNull
public ResourceLocation getTextureLocation(ShrimpArrowEntity entity) {
return ShrimpRenderer.TEXTURE;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.tropheusj.its_as_shrimple_as_that.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -10,12 +11,14 @@
import org.jetbrains.annotations.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.network.syncher.SynchedEntityData.Builder;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
Expand All @@ -32,6 +35,9 @@
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
import net.minecraft.world.entity.ai.goal.RandomStrollGoal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.ChargedProjectiles;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
Expand Down Expand Up @@ -78,6 +84,20 @@ public void onSyncedDataUpdated(EntityDataAccessor<?> data) {
@Override
@NotNull
protected InteractionResult mobInteract(Player player, InteractionHand interactionHand) {
ItemStack held = player.getItemInHand(interactionHand);
if (held.is(Items.CROSSBOW)) {
ChargedProjectiles ammo = held.get(DataComponents.CHARGED_PROJECTILES);
if (ammo != null && ammo.isEmpty()) {
// lock and load
if (!this.level().isClientSide) {
ChargedProjectiles newAmmo = ChargedProjectiles.of(new ItemStack(ItsAsShrimpleAsThat.SHRIMP_ARROW));
held.set(DataComponents.CHARGED_PROJECTILES, newAmmo);
this.makeSound(SoundEvents.ITEM_FRAME_REMOVE_ITEM);
this.discard();
}
return InteractionResult.sidedSuccess(this.level().isClientSide);
}
}
if (!this.level().isClientSide && this.merchant != null && !this.merchant.isTrading()) {
this.merchant.setTradingPlayer(player);
this.merchant.openTradingScreen(player, this.getDisplayName(), 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.github.tropheusj.its_as_shrimple_as_that.item;

import java.util.Objects;
import java.util.function.Supplier;

import com.google.common.base.Suppliers;

import com.mojang.math.Axis;

import io.github.tropheusj.its_as_shrimple_as_that.ItsAsShrimpleAsThat;
import io.github.tropheusj.its_as_shrimple_as_that.entity.ShrimpEntity;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;

import com.mojang.blaze3d.vertex.PoseStack;

import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.ChargedProjectiles;

public class ShrimpCrossbowRenderer implements BuiltinItemRendererRegistry.DynamicItemRenderer {
private static final ItemStack normalCrossbow = Util.make(
new ItemStack(Items.CROSSBOW),
stack -> stack.set(DataComponents.CHARGED_PROJECTILES, ChargedProjectiles.of(new ItemStack(Items.ARROW)))
);

private static final Supplier<ShrimpEntity> shrimp = Suppliers.memoize(() -> {
ClientLevel level = Minecraft.getInstance().level;
Objects.requireNonNull(level);
return new ShrimpEntity(ItsAsShrimpleAsThat.SHRIMP_TYPE, level);
});

@Override
public void render(ItemStack stack, ItemDisplayContext mode, PoseStack matrices, MultiBufferSource buffers, int light, int overlay) {
Minecraft mc = Minecraft.getInstance();
EntityRenderDispatcher entityRenderer = mc.getEntityRenderDispatcher();
ItemRenderer itemRenderer = mc.getItemRenderer();

matrices.pushPose();
matrices.translate(0.5, 0.5, 0.5);

matrices.pushPose();
itemRenderer.renderStatic(normalCrossbow, mode, light, overlay, matrices, buffers, null, 0);
matrices.popPose();

matrices.scale(0.5f, 0.5f, 0.5f);
matrices.mulPose(Axis.YP.rotationDegrees(170));
matrices.translate(-0.17, 0.5, -0.3);

entityRenderer.render(shrimp.get(), 0, 0, 0, 0, 1, matrices, buffers, 15728880);
matrices.popPose();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"item.its_as_shrimple_as_that.fried_rice": "Fried Rice",
"item.its_as_shrimple_as_that.shrimp_spawn_egg": "Shrimp Spawn Egg",
"item.its_as_shrimple_as_that.shrimp_arrow": "Shrimp",

"entity.its_as_shrimple_as_that.shrimp": "Shrimp",

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"parent": "builtin/entity"
}
70 changes: 70 additions & 0 deletions src/main/resources/assets/minecraft/models/item/crossbow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"parent": "item/generated",
"textures": {
"layer0": "item/crossbow_standby"
},
"display": {
"thirdperson_righthand": {
"rotation": [ -90, 0, -60 ],
"translation": [ 2, 0.1, -3 ],
"scale": [ 0.9, 0.9, 0.9 ]
},
"thirdperson_lefthand": {
"rotation": [ -90, 0, 30 ],
"translation": [ 2, 0.1, -3 ],
"scale": [ 0.9, 0.9, 0.9 ]
},
"firstperson_righthand": {
"rotation": [ -90, 0, -55 ],
"translation": [ 1.13, 3.2, 1.13],
"scale": [ 0.68, 0.68, 0.68 ]
},
"firstperson_lefthand": {
"rotation": [ -90, 0, 35 ],
"translation": [ 1.13, 3.2, 1.13],
"scale": [ 0.68, 0.68, 0.68 ]
}
},
"overrides": [
{
"predicate": {
"pulling": 1
},
"model": "item/crossbow_pulling_0"
},
{
"predicate": {
"pulling": 1,
"pull": 0.58
},
"model": "item/crossbow_pulling_1"
},
{
"predicate": {
"pulling": 1,
"pull": 1.0
},
"model": "item/crossbow_pulling_2"
},
{
"predicate": {
"charged": 1
},
"model": "item/crossbow_arrow"
},
{
"predicate": {
"charged": 1,
"firework": 1
},
"model": "item/crossbow_firework"
},
{
"predicate": {
"charged": 1,
"its_as_shrimple_as_that:shrimp": 1
},
"model": "its_as_shrimple_as_that:item/crossbow_shrimp"
}
]
}

0 comments on commit 773b290

Please sign in to comment.