Skip to content

Commit

Permalink
shrimp spawning and wandering
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Apr 2, 2024
1 parent 41e9706 commit 11daa62
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@
import io.github.tropheusj.its_as_shrimple_as_that.item.FriedRiceItem;
import net.fabricmc.api.ModInitializer;

import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;

import net.minecraft.tags.BiomeTags;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.SpawnPlacementTypes;
import net.minecraft.world.entity.SpawnPlacements;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.ArrowItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Item.Properties;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.level.levelgen.Heightmap.Types;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -47,7 +53,9 @@ public class ItsAsShrimpleAsThat implements ModInitializer {

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

public static final MobEffect KRILLED = new MobEffect(MobEffectCategory.BENEFICIAL, 1){}.addAttributeModifier(
public static final Item SHRIMP_EGG = new SpawnEggItem(SHRIMP_TYPE, 0xFF977C66, 0xFFFFFFFF, new Properties());

public static final MobEffect KRILLED = new MobEffect(MobEffectCategory.BENEFICIAL, 0xFF977C66){}.addAttributeModifier(
Attributes.SCALE, "5876bde6-b02e-42e9-84c4-e3317b37cb26",
-0.66, AttributeModifier.Operation.ADD_VALUE
);
Expand All @@ -58,13 +66,22 @@ public void onInitialize() {
Registry.register(BuiltInRegistries.ENTITY_TYPE, id("shrimp_arrow"), SHRIMP_ARROW_TYPE);
Registry.register(BuiltInRegistries.ITEM, id("shrimp_arrow"), SHRIMP_ARROW);
Registry.register(BuiltInRegistries.ITEM, id("fried_rice"), FRIED_RICE);
Registry.register(BuiltInRegistries.ITEM, id("shrimp_spawn_egg"), SHRIMP_EGG);
Registry.register(BuiltInRegistries.MOB_EFFECT, id("krilled"), KRILLED);

CommandRegistrationCallback.EVENT.register(
(dispatcher, registryAccess, environment) -> dispatcher.register(KrillCommand.build())
);

FabricDefaultAttributeRegistry.register(SHRIMP_TYPE, ShrimpEntity.createAttributes().build());

BiomeModifications.addSpawn(
BiomeSelectors.tag(BiomeTags.IS_OCEAN),
MobCategory.WATER_AMBIENT, SHRIMP_TYPE,
50, 3, 5
);

SpawnPlacements.register(SHRIMP_TYPE, SpawnPlacementTypes.IN_WATER, Types.MOTION_BLOCKING_NO_LEAVES, ShrimpEntity::checkShrimpSpawnRules);
}

public static ResourceLocation id(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Optional;

import io.github.tropheusj.its_as_shrimple_as_that.entity.goal.FollowDreamsGoal;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -11,17 +13,28 @@
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.network.syncher.SynchedEntityData.Builder;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
import net.minecraft.world.entity.ai.goal.PanicGoal;
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.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.pathfinder.PathType;

public class ShrimpEntity extends PathfinderMob {
public static final EntityDataAccessor<Optional<BlockPos>> WORKSTATION = SynchedEntityData.defineId(ShrimpEntity.class, EntityDataSerializers.OPTIONAL_BLOCK_POS);
Expand All @@ -31,6 +44,7 @@ public class ShrimpEntity extends PathfinderMob {

public ShrimpEntity(EntityType<? extends ShrimpEntity> type, Level level) {
super(type, level);
this.setPathfindingMalus(PathType.WATER, 0);
}

@Override
Expand All @@ -41,8 +55,11 @@ protected void defineSynchedData(Builder builder) {

@Override
protected void registerGoals() {
this.goalSelector.addGoal(1, new FollowDreamsGoal(this, 1, 16, 2));
this.goalSelector.addGoal(5, new LookAtPlayerGoal(this, Player.class, 10, 0.1f));
this.goalSelector.addGoal(1, new PanicGoal(this, 1.25));
this.goalSelector.addGoal(2, new FollowDreamsGoal(this, 1, 16, 2));
this.goalSelector.addGoal(5, new RandomStrollGoal(this, 0.45));
this.goalSelector.addGoal(7, new LookAtPlayerGoal(this, Player.class, 10, 0.1f));
this.goalSelector.addGoal(8, new RandomLookAroundGoal(this));
}

@Override
Expand All @@ -65,6 +82,31 @@ protected InteractionResult mobInteract(Player player, InteractionHand interacti
return InteractionResult.PASS;
}

@Override
public int getAirSupply() {
// never drown
return 100;
}

@Override
public float getWalkTargetValue(BlockPos blockPos, LevelReader levelReader) {
BlockState state = levelReader.getBlockState(blockPos);
if (state.is(Blocks.WATER))
return 10;

return levelReader.getPathfindingCostFromLightLevels(blockPos);
}

@Override
public boolean checkSpawnObstruction(LevelReader levelReader) {
return levelReader.isUnobstructed(this);
}

@Override
public boolean isPushedByFluid() {
return false;
}

@Override
public void remove(RemovalReason removalReason) {
super.remove(removalReason);
Expand Down Expand Up @@ -97,4 +139,15 @@ public boolean isChef() {
public static AttributeSupplier.Builder createAttributes() {
return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 8);
}

// from WaterAnimal
public static boolean checkShrimpSpawnRules(EntityType<? extends ShrimpEntity> type, LevelAccessor level,
MobSpawnType spawnType, BlockPos pos, RandomSource random) {
int i = level.getSeaLevel();
int j = i - 13;
return pos.getY() >= j
&& pos.getY() <= i
&& level.getFluidState(pos.below()).is(FluidTags.WATER)
&& level.getBlockState(pos.above()).is(Blocks.WATER);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.github.tropheusj.its_as_shrimple_as_that.entity;
package io.github.tropheusj.its_as_shrimple_as_that.entity.goal;

import io.github.tropheusj.its_as_shrimple_as_that.entity.ShrimpEntity;

import org.jetbrains.annotations.NotNull;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Direction.Plane;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.MoveToBlockGoal;
import net.minecraft.world.level.LevelReader;
Expand Down Expand Up @@ -48,7 +51,7 @@ public void tick() {

@Override
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
if (!level.isEmptyBlock(pos))
if (!level.isEmptyBlock(pos) && !level.getFluidState(pos).is(FluidTags.WATER))
return false;
for (Direction direction : Plane.HORIZONTAL) {
BlockPos adjacent = pos.relative(direction);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"item.its_as_shrimple_as_that.fried_rice": "Fried Rice",
"item.its_as_shrimple_as_that.shrimp_spawn_egg": "Shrimp Spawn Egg",

"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": "minecraft:item/template_spawn_egg"
}

0 comments on commit 11daa62

Please sign in to comment.