Skip to content

Commit

Permalink
SignHistorian - New dynamic color option derives ESP color from the s…
Browse files Browse the repository at this point in the history
…ign's wood type.
  • Loading branch information
0xTas committed Sep 18, 2024
1 parent c699205 commit 177f4c4
Showing 1 changed file with 107 additions and 61 deletions.
168 changes: 107 additions & 61 deletions src/main/java/dev/stardust/modules/SignHistorian.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public SignHistorian() {
.build()
);

private final Setting<Boolean> dynamicColor = sgESP.add(
new BoolSetting.Builder()
.name("Dynamic Color")
.description("Derive ESP side color from the sign's wood type.")
.defaultValue(true)
.build()
);

private final Setting<ESPBlockData> destroyedSettings = sgESP.add(
new GenericSetting.Builder<ESPBlockData>()
.name("Destroyed/Missing Signs ESP")
Expand All @@ -106,7 +114,7 @@ public SignHistorian() {
new ESPBlockData(
ShapeMode.Both,
new SettingColor(255, 42, 0, 255),
new SettingColor(255, 42, 0, 44),
new SettingColor(255, 42, 0, 69),
true,
new SettingColor(255, 42, 0, 137)
)
Expand Down Expand Up @@ -263,6 +271,7 @@ public SignHistorian() {
private final HashSet<SignBlockEntity> signsToGlowInk = new HashSet<>();
private final HashMap<Integer, Vec3d> trackedGriefers = new HashMap<>();
private final HashSet<HostileEntity> approachingGriefers = new HashSet<>();
private final HashMap<SignBlockEntity, WoodType> woodTypeMap = new HashMap<>();
private final HashMap<SignBlockEntity, DyeColor> signsToColor = new HashMap<>();
private final HashMap<Integer, Pair<Boolean, Long>> grieferHadLineOfSight = new HashMap<>();
private final Map<BlockPos, Pair<SignBlockEntity, BlockState>> serverSigns = new HashMap<>();
Expand Down Expand Up @@ -326,6 +335,9 @@ private void readSignsFromFile(Path signsFile) {

if (be instanceof SignBlockEntity sbeReconstructed) {
if (!serverSigns.containsKey(bPos)) {
if (state.getBlock() instanceof AbstractSignBlock signBlock) {
woodTypeMap.put(sbeReconstructed, signBlock.getWoodType());
}
serverSigns.put(bPos, new Pair<>(sbeReconstructed, sbeReconstructed.getCachedState()));
}
}
Expand Down Expand Up @@ -426,6 +438,34 @@ private Vec3d getTracerOffset(BlockPos pos, BlockState state) {
return new Vec3d(offsetX, offsetY, offsetZ);
}

private SettingColor colorFromWoodType(@Nullable WoodType type) {
if (type == null || !dynamicColor.get()) {
return dangerESP.get().sideColor;
} else if (type == WoodType.OAK) {
return new SettingColor(181, 146, 94, dangerESP.get().sideColor.a);
} else if (type == WoodType.BIRCH) {
return new SettingColor(212, 200, 139, dangerESP.get().sideColor.a);
} else if (type == WoodType.SPRUCE) {
return new SettingColor(126, 93, 53, dangerESP.get().sideColor.a);
} else if (type == WoodType.JUNGLE) {
return new SettingColor(181, 133, 98, dangerESP.get().sideColor.a);
} else if (type == WoodType.ACACIA) {
return new SettingColor(170, 92, 49, dangerESP.get().sideColor.a);
} else if (type == WoodType.BAMBOO) {
return new SettingColor(133, 124, 53, dangerESP.get().sideColor.a);
} else if (type == WoodType.CHERRY) {
return new SettingColor(227, 191, 184, dangerESP.get().sideColor.a);
} else if (type == WoodType.WARPED) {
return new SettingColor(57, 140, 138, dangerESP.get().sideColor.a);
} else if (type == WoodType.CRIMSON) {
return new SettingColor(124, 57, 85, dangerESP.get().sideColor.a);
} else if (type == WoodType.MANGROVE) {
return new SettingColor(109, 41, 44, dangerESP.get().sideColor.a);
} else if (type == WoodType.DARK_OAK) {
return new SettingColor(72, 46, 23, dangerESP.get().sideColor.a);
} else return dangerESP.get().sideColor;
}

@Nullable
private BlockPos getTargetedSign() {
ClientPlayerEntity player = mc.player;
Expand Down Expand Up @@ -532,6 +572,61 @@ private boolean hasNearbySigns() {
return false;
}

private boolean mobHasLineOfSight(HostileEntity mob) {
Vec3d mobEyePos = mob.getEyePos();
Vec3d eyePos = mc.player.getEyePos();
HitResult lineOfSightCheck = mc.world.raycast(
new RaycastContext(
mobEyePos, eyePos,
RaycastContext.ShapeType.COLLIDER,
RaycastContext.FluidHandling.WATER, mob
)
);

return lineOfSightCheck.getType() != HitResult.Type.BLOCK;
}

private boolean isMobAThreat(HostileEntity mob) {
if (!Utils.canUpdate()) return false;

Vec3d newPos = mob.getPos();
Vec3d playerPos = mc.player.getPos();
Vec3d lastPos = trackedGriefers.get(mob.getId());

if (lastPos == null) return false;
double newDistance = playerPos.squaredDistanceTo(newPos);
double oldDistance = playerPos.squaredDistanceTo(lastPos);

long now = System.currentTimeMillis();
boolean mobHasLoS = mobHasLineOfSight(mob);
if (grieferHadLineOfSight.get(mob.getId()) == null) {
grieferHadLineOfSight.put(mob.getId(), new Pair<>(mobHasLoS, now));
} else {
Pair<Boolean, Long> prevLoSCheck = grieferHadLineOfSight.get(mob.getId());
if (mobHasLoS || now - prevLoSCheck.getRight() >= 7000) {
grieferHadLineOfSight.put(mob.getId(), new Pair<>(mobHasLoS, now));
}
}

if (mob instanceof CreeperEntity creeper) {
if (newDistance <= MathHelper.square(10)) {
return mobHasLoS || (newDistance < oldDistance && grieferHadLineOfSight.get(creeper.getId()).getLeft());
} else if (newDistance <= MathHelper.square(20)) {
return (newDistance < oldDistance && mobHasLoS);
}
} else if (mob instanceof WitherEntity wither) {
if (newDistance <= MathHelper.square(16)) {
return true;
} else if (newDistance <= MathHelper.square(32)) {
return mobHasLoS || (newDistance < oldDistance && grieferHadLineOfSight.get(wither.getId()).getLeft());
} else if (newDistance <= MathHelper.square(48)) {
return (newDistance < oldDistance && mobHasLoS);
}
}

return false;
}

private void processSign(@NotNull SignBlockEntity sbe) {
if (!sbe.getFrontText().hasText(mc.player) && !sbe.getBackText().hasText(mc.player)) return;
else if (contentBlacklist.get() && containsBlacklistedText(sbe)) return;
Expand All @@ -545,6 +640,9 @@ private void processSign(@NotNull SignBlockEntity sbe) {
}
destroyedSigns.remove(serverSigns.get(pos).getLeft());
} else {
if (sbe.getCachedState().getBlock() instanceof AbstractSignBlock signBlock) {
woodTypeMap.put(sbe, signBlock.getWoodType());
}
serverSigns.put(pos, new Pair<>(sbe, sbe.getCachedState()));
if (persistenceSetting.get()) {
saveSignToFile(sbe, sbe.getCachedState());
Expand Down Expand Up @@ -593,61 +691,6 @@ private void interactSign(SignBlockEntity sbe, Item dye) {
}
}

private boolean mobHasLineOfSight(HostileEntity mob) {
Vec3d mobEyePos = mob.getEyePos();
Vec3d eyePos = mc.player.getEyePos();
HitResult lineOfSightCheck = mc.world.raycast(
new RaycastContext(
mobEyePos, eyePos,
RaycastContext.ShapeType.COLLIDER,
RaycastContext.FluidHandling.WATER, mob
)
);

return lineOfSightCheck.getType() != HitResult.Type.BLOCK;
}

private boolean isMobAThreat(HostileEntity mob) {
if (!Utils.canUpdate()) return false;

Vec3d newPos = mob.getPos();
Vec3d playerPos = mc.player.getPos();
Vec3d lastPos = trackedGriefers.get(mob.getId());

if (lastPos == null) return false;
double newDistance = playerPos.squaredDistanceTo(newPos);
double oldDistance = playerPos.squaredDistanceTo(lastPos);

long now = System.currentTimeMillis();
boolean mobHasLoS = mobHasLineOfSight(mob);
if (grieferHadLineOfSight.get(mob.getId()) == null) {
grieferHadLineOfSight.put(mob.getId(), new Pair<>(mobHasLoS, now));
} else {
Pair<Boolean, Long> prevLoSCheck = grieferHadLineOfSight.get(mob.getId());
if (mobHasLoS || now - prevLoSCheck.getRight() >= 7000) {
grieferHadLineOfSight.put(mob.getId(), new Pair<>(mobHasLoS, now));
}
}

if (mob instanceof CreeperEntity creeper) {
if (newDistance <= MathHelper.square(10)) {
return mobHasLoS || (newDistance < oldDistance && grieferHadLineOfSight.get(creeper.getId()).getLeft());
} else if (newDistance <= MathHelper.square(20)) {
return (newDistance < oldDistance && mobHasLoS);
}
} else if (mob instanceof WitherEntity wither) {
if (newDistance <= MathHelper.square(16)) {
return true;
} else if (newDistance <= MathHelper.square(32)) {
return mobHasLoS || (newDistance < oldDistance && grieferHadLineOfSight.get(wither.getId()).getLeft());
} else if (newDistance <= MathHelper.square(48)) {
return (newDistance < oldDistance && mobHasLoS);
}
}

return false;
}

@Override
public void onActivate() {
if (!Utils.canUpdate()) {
Expand All @@ -665,6 +708,7 @@ public void onDeactivate() {
gracePeriod = 0;
currentDim = null;
signsToWax.clear();
woodTypeMap.clear();
serverSigns.clear();
signsToColor.clear();
modifiedSigns.clear();
Expand Down Expand Up @@ -886,7 +930,7 @@ else if (targeted == null) {
@EventHandler
private void onRender3D(Render3DEvent event) {
if (!Utils.canUpdate()) return;
// if (mc.getNetworkHandler().getPlayerList().size() <= 1) return; // ignore queue TODO: disabled for testing, re-enable this before committing
if (mc.getNetworkHandler().getPlayerList().size() <= 1) return; // ignore queue

if (espSigns.get()) {
ESPBlockData mESP = modifiedSettings.get();
Expand All @@ -906,10 +950,11 @@ private void onRender3D(Render3DEvent event) {
double z2 = sign.getPos().getZ() + shape.getMax(Direction.Axis.Z);

if (dESP.sideColor.a > 0 || dESP.lineColor.a > 0) {
WoodType woodType = woodTypeMap.get(sign);
event.renderer.box(
x1, y1, z1, x2, y2, z2,
dESP.sideColor, dESP.lineColor,
ShapeMode.Both, 0
colorFromWoodType(woodType), dESP.lineColor,
dESP.shapeMode, 0
);
}

Expand All @@ -936,10 +981,11 @@ private void onRender3D(Render3DEvent event) {
double z2 = sign.getPos().getZ() + shape.getMax(Direction.Axis.Z);

if (mESP.sideColor.a > 0 || mESP.lineColor.a > 0) {
WoodType woodType = woodTypeMap.get(sign);
event.renderer.box(
x1, y1, z1, x2, y2, z2,
mESP.sideColor, mESP.lineColor,
ShapeMode.Both, 0
colorFromWoodType(woodType), mESP.lineColor,
mESP.shapeMode, 0
);
}

Expand Down

0 comments on commit 177f4c4

Please sign in to comment.