Skip to content

Commit

Permalink
Added an anvil hammering sound for when a piglin crafts an armor piece
Browse files Browse the repository at this point in the history
Added a cooldown to armor crafting checks
Changed finding of nearby piglins from TARGET to NEARBY_VISIBLE
Changed piglins giving out armor to allow giving to Brutes
Changed piglin drops during death/zombification to not drop stored gold
Added two helper utils classes
  • Loading branch information
GStefanowich committed Mar 17, 2021
1 parent a67030b commit 9802a1e
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 110 deletions.
50 changes: 15 additions & 35 deletions src/main/java/me/TheElm/NetherMod/goals/PiglinTradingGoal.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,26 @@

import com.google.common.collect.ImmutableMap;
import me.TheElm.NetherMod.interfaces.EmotionalPiglins;
import net.minecraft.entity.LivingEntity;
import me.TheElm.NetherMod.utils.EntityUtils;
import net.minecraft.entity.ai.brain.Brain;
import net.minecraft.entity.ai.brain.MemoryModuleState;
import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.ai.brain.task.LookTargetUtil;
import net.minecraft.entity.ai.brain.task.Task;
import net.minecraft.entity.mob.AbstractPiglinEntity;
import net.minecraft.entity.mob.PiglinBrain;
import net.minecraft.entity.mob.PiglinEntity;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import org.jetbrains.annotations.NotNull;

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

/**
* Created on Mar 11 2021 at 12:14 AM.
* By greg in PiglinMod
*/
public final class PiglinTradingGoal extends Task<PiglinEntity> {

private static final int TICKS = 60;
private int cooldown = TICKS;

public PiglinTradingGoal() {
this(60);
}
Expand All @@ -59,46 +56,29 @@ public PiglinTradingGoal( int runTime ) {
}

public PiglinTradingGoal( int minRunTime, int maxRunTime ) {
super(ImmutableMap.of(MemoryModuleType.NEARBY_ADULT_PIGLINS, MemoryModuleState.VALUE_PRESENT, MemoryModuleType.INTERACTION_TARGET, MemoryModuleState.VALUE_PRESENT), minRunTime, maxRunTime);
super(ImmutableMap.of(MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLINS, MemoryModuleState.VALUE_PRESENT), minRunTime, maxRunTime);
}

@Override
protected boolean shouldRun( ServerWorld world, PiglinEntity piglin ) {
if ( this.cooldown-- > 0 )
return false;
this.cooldown = PiglinTradingGoal.TICKS;
if ( !((EmotionalPiglins)piglin).hasFullArmorSet() )
return false;
SimpleInventory inventory = ((EmotionalPiglins)piglin).getInventory();
if ( inventory.count(PiglinBrain.BARTERING_ITEM) <= 0 )
return false;
LivingEntity entity;
Brain<PiglinEntity> brain = piglin.getBrain();
Optional<LivingEntity> optional = brain.getOptionalMemory(MemoryModuleType.INTERACTION_TARGET);
return optional.isPresent() && ((entity = optional.get()) instanceof PiglinEntity) && !((EmotionalPiglins)entity).hasFullArmorSet();
return piglin.getBrain()
.getOptionalMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLINS)
.map(list -> list.stream().anyMatch(EntityUtils::missingArmorSet))
.orElse(Boolean.FALSE);
}

@Override
protected void run( ServerWorld world, PiglinEntity piglin, long time ) {
protected void run(ServerWorld world, @NotNull PiglinEntity piglin, long time) {
Brain<PiglinEntity> brain = piglin.getBrain();
brain.getOptionalMemory(MemoryModuleType.INTERACTION_TARGET).ifPresent((target) -> {
SimpleInventory inventory = ((EmotionalPiglins)piglin).getInventory();
if (!( target instanceof PiglinEntity ))
return;
boolean doCheck = false;

for ( int i = 0; i < inventory.size(); i++ ) {
ItemStack stack = inventory.getStack(i);
if (stack.isEmpty() || !PiglinBrain.BARTERING_ITEM.equals(stack.getItem()) )
continue;

// Directly give the piglin gold telepathically
ItemStack remainder = ((EmotionalPiglins)target)
.incrementGold(stack);
inventory.setStack(i, remainder);

doCheck = true;
}

if ( doCheck )
((EmotionalPiglins) target).craftingCheck();
});
brain.getOptionalMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLINS)
.ifPresent((target) -> EntityUtils.transferDesiredItems(target, piglin));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package me.TheElm.NetherMod.interfaces;

import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleEffect;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -66,4 +67,7 @@ default void incrementGold(Iterable<ItemStack> stacks) {
boolean hasFullArmorSet();
void craftingCheck();

boolean canCraftItem(@NotNull Item item);
@NotNull ItemStack tryCraftItem(@NotNull Item item);

}
Loading

0 comments on commit 9802a1e

Please sign in to comment.