added breeding tracking

This commit is contained in:
gnembon committed 2019-07-28 09:01:30 -03:00
1 parent aac4563268
commit e021ca39b2
4 files changed
+158 -62

No files matched your search

@@ -0,0 +1,73 @@
package carpet.helpers;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.arguments.ParticleArgumentType;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d;
import java.util.HashMap;
import java.util.Map;
public class ParticleDisplay
{
private static Map<String, ParticleEffect> particleCache = new HashMap<>();
private static ParticleEffect parseParticle(String name)
{
try
{
return ParticleArgumentType.readParameters(new StringReader(name));
}
catch (CommandSyntaxException e)
{
throw new RuntimeException("No such particle: "+name);
}
}
private static ParticleEffect getEffect(String name)
{
if (name == null) return null;
ParticleEffect res = particleCache.get(name);
if (res != null) return res;
particleCache.put(name, parseParticle(name));
return particleCache.get(name);
}
public static void drawParticleLine(ServerPlayerEntity player, Vec3d from, Vec3d to,String main)
{
drawParticleLine(player, from, to, main, null, 1, 0.0);
}
public static void drawParticleLine(ServerPlayerEntity player, Vec3d from, Vec3d to, String main, String accent, int count, double spread)
{
ParticleEffect accentParticle = getEffect(accent);
ParticleEffect mainParticle = getEffect(main);
if (accentParticle != null) ((ServerWorld)player.world).spawnParticles(
player,
accentParticle,
true,
to.x, to.y, to.z, count,
spread, spread, spread, 0.0);
double lineLengthSq = from.squaredDistanceTo(to);
if (lineLengthSq == 0) return;
Vec3d incvec = to.subtract(from).normalize();// multiply(50/sqrt(lineLengthSq));
int pcount = 0;
for (Vec3d delta = new Vec3d(0.0,0.0,0.0);
delta.lengthSquared()<lineLengthSq;
delta = delta.add(incvec.multiply(player.world.random.nextFloat())))
{
((ServerWorld)player.world).spawnParticles(
player,
mainParticle,
true,
delta.x+from.x, delta.y+from.y, delta.z+from.z, 1,
0.0, 0.0, 0.0, 0.0);
}
}
}
@@ -1,5 +1,6 @@
package carpet.logging.logHelpers; package carpet.logging.logHelpers;
import carpet.helpers.ParticleDisplay;
import carpet.logging.LoggerRegistry; import carpet.logging.LoggerRegistry;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
@@ -12,64 +13,6 @@ import net.minecraft.util.math.Vec3d;
public class PathfindingVisualizer public class PathfindingVisualizer
{ {
private static ParticleEffect failedPath;
private static ParticleEffect successfulPath;
private static ParticleEffect lvl1;
private static ParticleEffect lvl2;
private static ParticleEffect lvl3;
static
{
failedPath = parseParticle("angry_villager");
successfulPath = parseParticle("happy_villager");
lvl1 = parseParticle("dust 1 1 0 1");
lvl2 = parseParticle("dust 1 0.5 0 1");
lvl3 = parseParticle("dust 1 0 0 1");
}
private static void drawParticleLine(ServerPlayerEntity player, Vec3d from, Vec3d to, float ratio, boolean successful)
{
ParticleEffect accent = successful ? successfulPath : failedPath;
ParticleEffect color = (ratio < 2)? lvl1 : ((ratio < 4)?lvl2:lvl3);
((ServerWorld)player.world).spawnParticles(
player,
accent,
true,
from.x, from.y, from.z, 5,
0.5, 0.5, 0.5, 0.0);
double lineLengthSq = from.squaredDistanceTo(to);
if (lineLengthSq == 0) return;
Vec3d incvec = to.subtract(from).normalize();// multiply(50/sqrt(lineLengthSq));
int pcount = 0;
for (Vec3d delta = new Vec3d(0.0,0.0,0.0);
delta.lengthSquared()<lineLengthSq;
delta = delta.add(incvec.multiply(player.world.random.nextFloat())))
{
((ServerWorld)player.world).spawnParticles(
player,
color,
true,
delta.x+from.x, delta.y+from.y, delta.z+from.z, 1,
0.0, 0.0, 0.0, 0.0);
}
}
private static ParticleEffect parseParticle(String name)
{
try
{
return ParticleArgumentType.readParameters(new StringReader(name));
}
catch (CommandSyntaxException e)
{
throw new RuntimeException("No such particle: "+name);
}
}
public static void slowPath(Entity entity, Vec3d target, float miliseconds, boolean successful) public static void slowPath(Entity entity, Vec3d target, float miliseconds, boolean successful)
{ {
if (!LoggerRegistry.__pathfinding) return; if (!LoggerRegistry.__pathfinding) return;
@@ -84,7 +27,10 @@ public class PathfindingVisualizer
return null; return null;
if (minDuration < 1) if (minDuration < 1)
minDuration = 1; minDuration = 1;
drawParticleLine((ServerPlayerEntity) player, entity.getPos(), target, miliseconds/minDuration, successful );
String accent = successful ? "happy_villager" : "angry_villager";
String color = (miliseconds/minDuration < 2)? "dust 1 1 0 1" : ((miliseconds/minDuration < 4)?"dust 1 0.5 0 1":"dust 1 0 0 1");
ParticleDisplay.drawParticleLine((ServerPlayerEntity) player, entity.getPos(), target, color, accent, 5, 0.5);
return null; return null;
}); });
} }
@@ -1,21 +1,32 @@
package carpet.mixins; package carpet.mixins;
import carpet.helpers.ParticleDisplay;
import carpet.utils.Messenger; import carpet.utils.Messenger;
import carpet.utils.MobAI; import carpet.utils.MobAI;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityInteraction;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
import net.minecraft.entity.ai.brain.Activity; import net.minecraft.entity.ai.brain.Activity;
import net.minecraft.entity.ai.brain.MemoryModuleType; import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.passive.AbstractTraderEntity; import net.minecraft.entity.passive.AbstractTraderEntity;
import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particle.ParticleTypes; import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.GlobalPos;
import net.minecraft.util.Hand;
import net.minecraft.util.Timestamp; import net.minecraft.util.Timestamp;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.Optional; import java.util.Optional;
@@ -24,10 +35,24 @@ public abstract class VillagerEntity_aiMixin extends AbstractTraderEntity
{ {
@Shadow protected abstract boolean hasSeenGolemRecently(long long_1); @Shadow protected abstract boolean hasSeenGolemRecently(long long_1);
@Shadow protected abstract void sayNo();
@Shadow protected abstract int getAvailableFood();
@Shadow protected abstract void depleteFood(int int_1);
@Shadow protected abstract boolean lacksFood();
@Shadow public abstract void eatForBreeding();
int totalFood;
boolean hasBed;
int displayAge;
@Inject(method = "tick", at = @At("HEAD")) @Inject(method = "tick", at = @At("HEAD"))
private void ontick(CallbackInfo ci) private void ontick(CallbackInfo ci)
{ {
if (MobAI.isTracking(this, MobAI.TrackingType.IRON_GOLEM_SPAWNING) && !getEntityWorld().isClient()) if (MobAI.isTracking(this, MobAI.TrackingType.IRON_GOLEM_SPAWNING))
{ {
long time; long time;
Optional<Long> optional_1 = this.brain.getOptionalMemory(MemoryModuleType.GOLEM_LAST_SEEN_TIME); Optional<Long> optional_1 = this.brain.getOptionalMemory(MemoryModuleType.GOLEM_LAST_SEEN_TIME);
@@ -58,8 +83,57 @@ public abstract class VillagerEntity_aiMixin extends AbstractTraderEntity
(recentlySeen?"rb ":"lb ")+time )); (recentlySeen?"rb ":"lb ")+time ));
this.setCustomNameVisible(true); this.setCustomNameVisible(true);
} }
else if (MobAI.isTracking(this, MobAI.TrackingType.VILLAGER_BREEDING))
{
if (age % 50 == 0 || age < 20)
{
totalFood = getAvailableFood() / 12;
hasBed = this.brain.getOptionalMemory(MemoryModuleType.HOME).isPresent();
displayAge = getBreedingAge();
}
if (Math.abs(displayAge) < 100 && displayAge !=0) displayAge = getBreedingAge();
this.setCustomName(Messenger.c(
(hasBed?"eb ":"fb ")+"\u263d ",
(totalFood>0?"eb ":"fb ")+"\u2668",(totalFood>0?"e ":"f ")+totalFood+" ",
(displayAge==0?"eb ":"fb ")+"\u2661",(displayAge==0?"e ":"f ")+displayAge+""
));
this.setCustomNameVisible(true);
}
} }
@Inject(method = "interactMob", at = @At("HEAD"), cancellable = true)
private void onInteract(PlayerEntity playerEntity_1, Hand hand_1, CallbackInfoReturnable<Boolean> cir)
{
if (MobAI.isTracking(this, MobAI.TrackingType.VILLAGER_BREEDING))
{
ItemStack itemStack_1 = playerEntity_1.getStackInHand(hand_1);
if (itemStack_1.getItem() == Items.EMERALD)
{
GlobalPos bedPos = this.brain.getOptionalMemory(MemoryModuleType.HOME).orElse(null);
if (bedPos == null || bedPos.getDimension() != dimension)
{
sayNo();
((ServerWorld) getEntityWorld()).spawnParticles(ParticleTypes.BARRIER, x, y + getStandingEyeHeight() + 1, z, 1, 0.1, 0.1, 0.1, 0.0);
}
else
{
ParticleDisplay.drawParticleLine((ServerPlayerEntity) playerEntity_1, getPos(), new Vec3d(bedPos.getPos()).add(0.5, 0.5, 0.5), "dust 0 0 0 1", "happy_villager", 100, 0.2);
}
}
else if (itemStack_1.getItem() == Items.ROTTEN_FLESH)
{
while(getAvailableFood() >= 12) eatForBreeding();
}
cir.setReturnValue(false);
cir.cancel();
}
}
@Inject(method = "summonGolem", at = @At( @Inject(method = "summonGolem", at = @At(
value = "INVOKE", value = "INVOKE",
target = "Lnet/minecraft/util/math/Box;expand(DDD)Lnet/minecraft/util/math/Box;", target = "Lnet/minecraft/util/math/Box;expand(DDD)Lnet/minecraft/util/math/Box;",
@@ -67,7 +141,7 @@ public abstract class VillagerEntity_aiMixin extends AbstractTraderEntity
)) ))
private void particleIt(long long_1, int int_1, CallbackInfo ci) private void particleIt(long long_1, int int_1, CallbackInfo ci)
{ {
if (MobAI.isTracking(this, MobAI.TrackingType.IRON_GOLEM_SPAWNING) && !getEntityWorld().isClient()) if (MobAI.isTracking(this, MobAI.TrackingType.IRON_GOLEM_SPAWNING))
{ {
((ServerWorld) getEntityWorld()).spawnParticles(ParticleTypes.BARRIER, x, y+3, z, 1, 0.1, 0.1, 0.1, 0.0); ((ServerWorld) getEntityWorld()).spawnParticles(ParticleTypes.BARRIER, x, y+3, z, 1, 0.1, 0.1, 0.1, 0.0);
} }
+4 -1
View File
@@ -25,6 +25,8 @@ public class MobAI
public static boolean isTracking(Entity e, TrackingType type) public static boolean isTracking(Entity e, TrackingType type)
{ {
if (e.getEntityWorld().isClient())
return false;
Set<TrackingType> currentTrackers = aiTrackers.get(e.getType()); Set<TrackingType> currentTrackers = aiTrackers.get(e.getType());
if (currentTrackers == null) if (currentTrackers == null)
return false; return false;
@@ -72,7 +74,8 @@ public class MobAI
public enum TrackingType public enum TrackingType
{ {
IRON_GOLEM_SPAWNING("iron_golem_spawning", Sets.newHashSet(EntityType.VILLAGER)); IRON_GOLEM_SPAWNING("iron_golem_spawning", Sets.newHashSet(EntityType.VILLAGER)),
VILLAGER_BREEDING("villager_breeding", Sets.newHashSet(EntityType.VILLAGER));
public Set<EntityType> types; public Set<EntityType> types;
public String name; public String name;
TrackingType(String name, Set<EntityType> applicableTypes) TrackingType(String name, Set<EntityType> applicableTypes)