Skip to content

Commit

Permalink
A lot of villager work
Browse files Browse the repository at this point in the history
  • Loading branch information
GirafiStudios committed Mar 5, 2021
1 parent 14fd945 commit b237e43
Show file tree
Hide file tree
Showing 25 changed files with 1,285 additions and 883 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: false
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true
}
}
apply plugin: 'net.minecraftforge.gradle'
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/teammetallurgy/atum/Atum.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.teammetallurgy.atum.integration.IntegrationHandler;
import com.teammetallurgy.atum.misc.AtumConfig;
import com.teammetallurgy.atum.misc.AtumItemGroup;
import com.teammetallurgy.atum.misc.AtumRegistry;
import com.teammetallurgy.atum.network.NetworkHandler;
import com.teammetallurgy.atum.world.SandstormHandler;
import com.teammetallurgy.atum.world.biome.AtumBiomeProvider;
Expand Down Expand Up @@ -54,6 +55,7 @@ public Atum() {
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, AtumConfig.spec);
IntegrationHandler.INSTANCE.addSupport();
AtumAPI.Tags.init();
AtumRegistry.registerDeferredRegistries(modBus);
}

private void setupCommon(FMLCommonSetupEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.teammetallurgy.atum.entity.ai.brain.task;

import com.google.common.collect.ImmutableMap;
import com.teammetallurgy.atum.entity.villager.AtumVillagerEntity;
import com.teammetallurgy.atum.entity.villager.AtumVillagerProfession;
import com.teammetallurgy.atum.misc.AtumRegistry;
import net.minecraft.entity.ai.brain.memory.MemoryModuleStatus;
import net.minecraft.entity.ai.brain.memory.MemoryModuleType;
import net.minecraft.entity.ai.brain.task.Task;
import net.minecraft.entity.merchant.villager.VillagerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.GlobalPos;
import net.minecraft.world.server.ServerWorld;

import javax.annotation.Nonnull;
import java.util.Optional;

public class AtumAssignProfessionTask extends Task<VillagerEntity> {

public AtumAssignProfessionTask() {
super(ImmutableMap.of(MemoryModuleType.POTENTIAL_JOB_SITE, MemoryModuleStatus.VALUE_PRESENT));
}

@Override
protected boolean shouldExecute(@Nonnull ServerWorld world, VillagerEntity owner) {
BlockPos pos = owner.getBrain().getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).get().getPos();
return pos.withinDistance(owner.getPositionVec(), 2.0D) || owner.shouldAssignProfessionOnSpawn();
}

@Override
protected void startExecuting(ServerWorld world, VillagerEntity entity, long gameTimeIn) {
GlobalPos globalpos = entity.getBrain().getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).get();
entity.getBrain().removeMemory(MemoryModuleType.POTENTIAL_JOB_SITE);
entity.getBrain().setMemory(MemoryModuleType.JOB_SITE, globalpos);
world.setEntityState(entity, (byte) 14);
if (entity instanceof AtumVillagerEntity && ((AtumVillagerEntity) entity).getAtumVillagerData().getProfession() == AtumVillagerProfession.NONE.get()) {
MinecraftServer minecraftserver = world.getServer();
Optional.ofNullable(minecraftserver.getWorld(globalpos.getDimension())).flatMap((w) -> {
return w.getPointOfInterestManager().getType(globalpos.getPos());
}).flatMap((poiType) -> {
return AtumRegistry.VILLAGER_PROFESSION.get().getValues().stream().filter((profession) -> {
return profession.getPointOfInterest() == poiType;
}).findFirst();
}).ifPresent((profession) -> {
((AtumVillagerEntity) entity).setAtumVillagerData(((AtumVillagerEntity) entity).getAtumVillagerData().withProfession(profession));
entity.resetBrain(world);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.teammetallurgy.atum.entity.ai.brain.task;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.teammetallurgy.atum.entity.villager.AtumVillagerEntity;
import com.teammetallurgy.atum.entity.villager.AtumVillagerProfession;
import net.minecraft.block.*;
import net.minecraft.entity.ai.brain.memory.MemoryModuleStatus;
import net.minecraft.entity.ai.brain.memory.MemoryModuleType;
import net.minecraft.entity.ai.brain.memory.WalkTarget;
import net.minecraft.entity.ai.brain.task.Task;
import net.minecraft.entity.merchant.villager.VillagerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPosWrapper;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.event.ForgeEventFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class AtumFarmTask extends Task<VillagerEntity> {
@Nullable
private BlockPos pos;
private long taskCooldown;
private int idleTime;
private final List<BlockPos> farmableBlocks = Lists.newArrayList();

public AtumFarmTask() {
super(ImmutableMap.of(MemoryModuleType.LOOK_TARGET, MemoryModuleStatus.VALUE_ABSENT, MemoryModuleType.WALK_TARGET, MemoryModuleStatus.VALUE_ABSENT, MemoryModuleType.SECONDARY_JOB_SITE, MemoryModuleStatus.VALUE_PRESENT));
}

@Override
protected boolean shouldExecute(@Nonnull ServerWorld world, @Nonnull VillagerEntity owner) {
if (!ForgeEventFactory.getMobGriefingEvent(world, owner) && !(owner instanceof AtumVillagerEntity)) {
return false;
} else if (((AtumVillagerEntity) owner).getAtumVillagerData().getProfession() != AtumVillagerProfession.FARMER.get()) {
return false;
} else {
BlockPos.Mutable mutablePos = owner.getPosition().toMutable();
this.farmableBlocks.clear();

for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
for (int k = -1; k <= 1; ++k) {
mutablePos.setPos(owner.getPosX() + (double) i, owner.getPosY() + (double) j, owner.getPosZ() + (double) k);
if (this.isValidPosForFarming(mutablePos, world)) {
this.farmableBlocks.add(new BlockPos(mutablePos));
}
}
}
}
this.pos = this.getNextPosForFarming(world);
return this.pos != null;
}
}

@Nullable
private BlockPos getNextPosForFarming(ServerWorld serverworld) {
return this.farmableBlocks.isEmpty() ? null : this.farmableBlocks.get(serverworld.getRandom().nextInt(this.farmableBlocks.size()));
}

private boolean isValidPosForFarming(BlockPos pos, ServerWorld serverworld) {
BlockState blockstate = serverworld.getBlockState(pos);
Block block = blockstate.getBlock();
Block block1 = serverworld.getBlockState(pos.down()).getBlock();
return block instanceof CropsBlock && ((CropsBlock) block).isMaxAge(blockstate) || blockstate.isAir() && block1 instanceof FarmlandBlock;
}

@Override
protected void startExecuting(ServerWorld world, VillagerEntity entity, long gameTimeIn) {
if (gameTimeIn > this.taskCooldown && this.pos != null) {
entity.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new BlockPosWrapper(this.pos));
entity.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(new BlockPosWrapper(this.pos), 0.5F, 1));
}

}

@Override
protected void resetTask(ServerWorld world, VillagerEntity entity, long gameTimeIn) {
entity.getBrain().removeMemory(MemoryModuleType.LOOK_TARGET);
entity.getBrain().removeMemory(MemoryModuleType.WALK_TARGET);
this.idleTime = 0;
this.taskCooldown = gameTimeIn + 40L;
}

@Override
protected void updateTask(ServerWorld world, VillagerEntity owner, long gameTime) {
if (this.pos == null || this.pos.withinDistance(owner.getPositionVec(), 1.0D)) {
if (this.pos != null && gameTime > this.taskCooldown) {
BlockState blockstate = world.getBlockState(this.pos);
Block block = blockstate.getBlock();
Block block1 = world.getBlockState(this.pos.down()).getBlock();
if (block instanceof CropsBlock && ((CropsBlock) block).isMaxAge(blockstate)) {
world.destroyBlock(this.pos, true, owner);
}

if (blockstate.isAir() && block1 instanceof FarmlandBlock && owner.isFarmItemInInventory()) {
Inventory inventory = owner.getVillagerInventory();

for (int i = 0; i < inventory.getSizeInventory(); ++i) {
ItemStack itemstack = inventory.getStackInSlot(i);
boolean flag = false;
if (!itemstack.isEmpty()) {
if (itemstack.getItem() == Items.WHEAT_SEEDS) {
world.setBlockState(this.pos, Blocks.WHEAT.getDefaultState(), 3);
flag = true;
} else if (itemstack.getItem() == Items.POTATO) {
world.setBlockState(this.pos, Blocks.POTATOES.getDefaultState(), 3);
flag = true;
} else if (itemstack.getItem() == Items.CARROT) {
world.setBlockState(this.pos, Blocks.CARROTS.getDefaultState(), 3);
flag = true;
} else if (itemstack.getItem() == Items.BEETROOT_SEEDS) {
world.setBlockState(this.pos, Blocks.BEETROOTS.getDefaultState(), 3);
flag = true;
} else if (itemstack.getItem() instanceof net.minecraftforge.common.IPlantable) {
if (((net.minecraftforge.common.IPlantable) itemstack.getItem()).getPlantType(world, pos) == net.minecraftforge.common.PlantType.CROP) {
world.setBlockState(pos, ((net.minecraftforge.common.IPlantable) itemstack.getItem()).getPlant(world, pos), 3);
flag = true;
}
}
}

if (flag) {
world.playSound((PlayerEntity) null, (double) this.pos.getX(), (double) this.pos.getY(), (double) this.pos.getZ(), SoundEvents.ITEM_CROP_PLANT, SoundCategory.BLOCKS, 1.0F, 1.0F);
itemstack.shrink(1);
if (itemstack.isEmpty()) {
inventory.setInventorySlotContents(i, ItemStack.EMPTY);
}
break;
}
}
}

if (block instanceof CropsBlock && !((CropsBlock) block).isMaxAge(blockstate)) {
this.farmableBlocks.remove(this.pos);
this.pos = this.getNextPosForFarming(world);
if (this.pos != null) {
this.taskCooldown = gameTime + 20L;
owner.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(new BlockPosWrapper(this.pos), 0.5F, 1));
owner.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new BlockPosWrapper(this.pos));
}
}
}

++this.idleTime;
}
}

@Override
protected boolean shouldContinueExecuting(@Nonnull ServerWorld world, @Nonnull VillagerEntity entity, long gameTimeIn) {
return this.idleTime < 200;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.teammetallurgy.atum.entity.ai.brain.task;

import com.google.common.collect.ImmutableMap;
import com.teammetallurgy.atum.entity.villager.AtumVillagerEntity;
import com.teammetallurgy.atum.entity.villager.AtumVillagerProfession;
import net.minecraft.entity.ai.brain.BrainUtil;
import net.minecraft.entity.ai.brain.memory.MemoryModuleStatus;
import net.minecraft.entity.ai.brain.memory.MemoryModuleType;
import net.minecraft.entity.ai.brain.task.Task;
import net.minecraft.entity.merchant.villager.VillagerEntity;
import net.minecraft.network.DebugPacketSender;
import net.minecraft.pathfinding.Path;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.GlobalPos;
import net.minecraft.village.PointOfInterestType;
import net.minecraft.world.server.ServerWorld;

import javax.annotation.Nonnull;
import java.util.Optional;

public class AtumFindJobTask extends Task<VillagerEntity> {
private final float f;

public AtumFindJobTask(float f) {
super(ImmutableMap.of(MemoryModuleType.POTENTIAL_JOB_SITE, MemoryModuleStatus.VALUE_PRESENT, MemoryModuleType.JOB_SITE, MemoryModuleStatus.VALUE_ABSENT, MemoryModuleType.MOBS, MemoryModuleStatus.VALUE_PRESENT));
this.f = f;
}

@Override
protected boolean shouldExecute(@Nonnull ServerWorld world, VillagerEntity owner) {
if (owner.isChild()) {
return false;
} else {
return owner instanceof AtumVillagerEntity && ((AtumVillagerEntity) owner).getAtumVillagerData().getProfession() == AtumVillagerProfession.NONE.get();
}
}

@Override
protected void startExecuting(ServerWorld worldIn, VillagerEntity entity, long gameTimeIn) {
BlockPos blockpos = entity.getBrain().getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).get().getPos();
Optional<PointOfInterestType> optional = worldIn.getPointOfInterestManager().getType(blockpos);
if (optional.isPresent()) {
BrainUtil.getNearbyVillagers(entity, (p_234021_3_) -> {
return this.func_234018_a_(optional.get(), p_234021_3_, blockpos);
}).findFirst().ifPresent((p_234023_4_) -> {
this.func_234022_a_(worldIn, entity, p_234023_4_, blockpos, p_234023_4_.getBrain().getMemory(MemoryModuleType.JOB_SITE).isPresent());
});
}
}

private boolean func_234018_a_(PointOfInterestType poi, VillagerEntity entity, BlockPos pos) {
boolean flag = entity.getBrain().getMemory(MemoryModuleType.POTENTIAL_JOB_SITE).isPresent();
if (flag || !(entity instanceof AtumVillagerEntity)) {
return false;
} else {
Optional<GlobalPos> optional = entity.getBrain().getMemory(MemoryModuleType.JOB_SITE);
AtumVillagerProfession villagerprofession = ((AtumVillagerEntity) entity).getAtumVillagerData().getProfession();
if (((AtumVillagerEntity) entity).getAtumVillagerData().getProfession() != AtumVillagerProfession.NONE.get() && villagerprofession.getPointOfInterest().getPredicate().test(poi)) {
return !optional.isPresent() ? this.func_234020_a_(entity, pos, poi) : optional.get().getPos().equals(pos);
} else {
return false;
}
}
}

private void func_234022_a_(ServerWorld serverWorld, VillagerEntity p_234022_2_, VillagerEntity p_234022_3_, BlockPos p_234022_4_, boolean p_234022_5_) {
this.func_234019_a_(p_234022_2_);
if (!p_234022_5_) {
BrainUtil.setTargetPosition(p_234022_3_, p_234022_4_, this.f, 1);
p_234022_3_.getBrain().setMemory(MemoryModuleType.POTENTIAL_JOB_SITE, GlobalPos.getPosition(serverWorld.getDimensionKey(), p_234022_4_));
DebugPacketSender.func_218801_c(serverWorld, p_234022_4_);
}

}

private boolean func_234020_a_(VillagerEntity p_234020_1_, BlockPos p_234020_2_, PointOfInterestType p_234020_3_) {
Path path = p_234020_1_.getNavigator().getPathToPos(p_234020_2_, p_234020_3_.getValidRange());
return path != null && path.reachesTarget();
}

private void func_234019_a_(VillagerEntity p_234019_1_) {
p_234019_1_.getBrain().removeMemory(MemoryModuleType.WALK_TARGET);
p_234019_1_.getBrain().removeMemory(MemoryModuleType.LOOK_TARGET);
p_234019_1_.getBrain().removeMemory(MemoryModuleType.POTENTIAL_JOB_SITE);
}
}

0 comments on commit b237e43

Please sign in to comment.