Merge pull request #60 from gnembon/villager_observer

Villager observer & gbfix
This commit is contained in:
gnembon authored and GitHub committed 2019-07-28 12:33:32 -03:00
commit b1e52d59cf
10 files changed
+237 -72

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.4.8+build.155 loader_version=0.4.8+build.155
# Mod Properties # Mod Properties
mod_version = 1.0.1+v190726 mod_version = 1.0.2+v190728
maven_group = carpet maven_group = carpet
archives_base_name = fabric-carpet archives_base_name = fabric-carpet
@@ -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;
}); });
} }
@@ -0,0 +1,48 @@
package carpet.mixins;
import carpet.settings.CarpetSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerInteractionManager;
import net.minecraft.server.network.packet.PlayerInteractBlockC2SPacket;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.IWorld;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(ClientPlayerInteractionManager.class)
public abstract class ClientPlayerInteractionManager_ghostBlocksMixin
{
@Shadow @Final private MinecraftClient client;
@Redirect(method = "breakBlock", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/block/Block;onBroken(Lnet/minecraft/world/IWorld;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;)V")
)
private void requestServerStatusForBlock(Block block, IWorld iWorld_1, BlockPos blockPos_1, BlockState blockState_1) {
if ((CarpetSettings.miningGhostBlockFix || (
!client.isInSingleplayer() &&
!client.isIntegratedServerRunning() &&
client.getNetworkHandler().getCommandDispatcher().getRoot().getChild("carpet") == null)) &&
blockState_1.getHardness(client.world, blockPos_1) > 0.0 &&
blockState_1.getCollisionShape(iWorld_1, blockPos_1) != VoxelShapes.empty()
)
{
client.getNetworkHandler().sendPacket(new PlayerInteractBlockC2SPacket(
Hand.MAIN_HAND,
new BlockHitResult(new Vec3d(blockPos_1), Direction.DOWN, blockPos_1, false)
));
}
}
}
@@ -3,21 +3,23 @@ package carpet.mixins;
import carpet.settings.CarpetSettings; import carpet.settings.CarpetSettings;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.ServerPlayerInteractionManager; import net.minecraft.server.network.ServerPlayerInteractionManager;
import net.minecraft.server.network.packet.PlayerActionC2SPacket; import net.minecraft.server.network.packet.PlayerActionC2SPacket;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView; import net.minecraft.world.BlockView;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
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.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.Redirect;
//needs tests, WIP //needs tests, WIP
@Mixin(ServerPlayerInteractionManager.class) @Mixin(ServerPlayerInteractionManager.class)
public class ServerPlayerInteractionManager_ghostBlocksMixin public class ServerPlayerInteractionManager_ghostBlocksMixin
{ {
@Shadow public ServerPlayerEntity player;
@Redirect(method = "method_14263", at = @At( @Redirect(method = "method_14263", at = @At(
value = "INVOKE", value = "INVOKE",
target = "Lnet/minecraft/block/BlockState;calcBlockBreakingDelta(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/world/BlockView;Lnet/minecraft/util/math/BlockPos;)F") target = "Lnet/minecraft/block/BlockState;calcBlockBreakingDelta(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/world/BlockView;Lnet/minecraft/util/math/BlockPos;)F")
@@ -27,12 +29,21 @@ public class ServerPlayerInteractionManager_ghostBlocksMixin
// main args // main args
BlockPos blockPos_1a, PlayerActionC2SPacket.Action playerActionC2SPacket$Action_1, Direction direction_1, int int_1) BlockPos blockPos_1a, PlayerActionC2SPacket.Action playerActionC2SPacket$Action_1, Direction direction_1, int int_1)
{ {
if (CarpetSettings.miningGhostBlockFix && playerActionC2SPacket$Action_1 == PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK) float progress = blockState.calcBlockBreakingDelta(playerEntity_1, blockView_1, blockPos_1);
if (CarpetSettings.miningGhostBlockFix &&
(playerActionC2SPacket$Action_1 == PlayerActionC2SPacket.Action.START_DESTROY_BLOCK) &&
!playerEntity_1.onGround &&
progress >= 0.2
)
return progress*5;
if (CarpetSettings.miningGhostBlockFix &&
(playerActionC2SPacket$Action_1 == PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK)
)
return 1.0f; return 1.0f;
return blockState.calcBlockBreakingDelta(playerEntity_1, blockView_1, blockPos_1); return progress;
} }
/*
@ModifyConstant(method = "method_14263", @ModifyConstant(method = "method_14263",
constant = @Constant(doubleValue = 36D)) constant = @Constant(doubleValue = 36D))
private double addDistance(double original) { private double addDistance(double original) {
@@ -40,4 +51,10 @@ public class ServerPlayerInteractionManager_ghostBlocksMixin
return 1024D; return 1024D;
return original; return original;
} }
@Inject(method = "method_14263", at = @At("HEAD"))
private void logAction(BlockPos blockPos_1, PlayerActionC2SPacket.Action playerActionC2SPacket$Action_1, Direction direction_1, int int_1, CallbackInfo ci)
{
CarpetSettings.LOG.error("Got action: "+playerActionC2SPacket$Action_1+"("+int_1+")");
}
*/
} }
@@ -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);
} }
@@ -26,7 +26,7 @@ import static carpet.settings.RuleCategory.SURVIVAL;
public class CarpetSettings public class CarpetSettings
{ {
public static final String carpetVersion = "1.0.1-v190726"; public static final String carpetVersion = "1.0.2-v190728";
public static final Logger LOG = LogManager.getLogger(); public static final Logger LOG = LogManager.getLogger();
public static boolean skipGenerationChecks = false; public static boolean skipGenerationChecks = false;
public static boolean impendingFillSkipUpdates = false; public static boolean impendingFillSkipUpdates = false;
@@ -229,7 +229,7 @@ public class CarpetSettings
@Rule(desc = "smooth client animations with low tps settings", extra = "works only in SP, and will slow down players", category = CREATIVE) @Rule(desc = "smooth client animations with low tps settings", extra = "works only in SP, and will slow down players", category = CREATIVE)
public static boolean smoothClientAnimations; public static boolean smoothClientAnimations;
//@Rule(desc="Fixes mining ghost blocks by allowing ANY mining speed from clients", category = SURVIVAL) @Rule(desc="Fixes mining ghost blocks by allowing ANY mining speed from clients", category = SURVIVAL)
public static boolean miningGhostBlockFix; public static boolean miningGhostBlockFix;
private static class PushLimitLimits extends Validator<Integer> { private static class PushLimitLimits extends Validator<Integer> {
+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("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)
+4
View File
@@ -87,6 +87,8 @@
"PlayerEntity_horseFixMixin", "PlayerEntity_horseFixMixin",
"ServerPlayerInteractionManager_ghostBlocksMixin",
"HopperBlockEntityMixin", "HopperBlockEntityMixin",
"ContainerMixin", "ContainerMixin",
"ClientConnectionMixin", "ClientConnectionMixin",
@@ -128,6 +130,8 @@
"InGameHudMixin", "InGameHudMixin",
"PlayerListHudMixin", "PlayerListHudMixin",
"ClientPlayerInteractionManager_ghostBlocksMixin",
"BlockEntityRenderDispatcher_movableTEMixin", "BlockEntityRenderDispatcher_movableTEMixin",
"PistonBlockEntityRenderer_movableTEMixin" "PistonBlockEntityRenderer_movableTEMixin"
], ],
+2 -2
View File
@@ -1,7 +1,7 @@
{ {
"schemaVersion": 1, "schemaVersion": 1,
"id": "carpet", "id": "carpet",
"version": "1.0.1", "version": "1.0.2",
"name": "Carpet Mod in Fabric", "name": "Carpet Mod in Fabric",
"description": "Carpet made out of fabric", "description": "Carpet made out of fabric",
@@ -25,7 +25,7 @@
"carpet.mixins.json" "carpet.mixins.json"
], ],
"requires": { "depends": {
"fabricloader": ">=0.4.0" "fabricloader": ">=0.4.0"
}, },
"suggests": { "suggests": {