Skip to content

Commit

Permalink
Make farther decorations smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
Electro593 committed Feb 14, 2025
1 parent 175c07e commit 5ae96b2
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.dafuqs.spectrum.injectors;

public interface MapDecorationInjector {

default void spectrum$setScale(byte scale) {
}

default byte spectrum$getScale() {
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static void createAndSetState(ItemStack stack, ServerWorld world, int ce
if (targetId != null) {
state.startLocator(world);
if (target != null) {
state.addTarget(world, target.getPos());
state.addTarget(world, target.getBoundingBox().getCenter());
}
} else {
state.cancelLocator();
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/de/dafuqs/spectrum/items/map/ArtisansAtlasState.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.dafuqs.spectrum.items.map;

import com.mojang.datafixers.util.Pair;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.mixin.accessors.*;
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
Expand All @@ -22,7 +23,7 @@
public class ArtisansAtlasState extends MapState {

private final MapStateAccessor accessor;
private final Set<ChunkPos> targets;
private Set<BlockPos> targets;
private BlockPos displayedCenter;
private Identifier targetId;
@Nullable
Expand Down Expand Up @@ -58,6 +59,8 @@ public ArtisansAtlasState(double centerX, double centerZ, byte scale, boolean sh
int xDisplay = nbt.contains("displayX", NbtElement.NUMBER_TYPE) ? nbt.getInt("displayX") : this.displayedCenter.getX();
int zDisplay = nbt.contains("displayZ", NbtElement.NUMBER_TYPE) ? nbt.getInt("displayZ") : this.displayedCenter.getZ();
this.displayedCenter = new BlockPos(xDisplay, 0, zDisplay);

this.targets = new HashSet<>(CodecHelper.fromNbt(BlockPos.CODEC.listOf(), nbt.get("targets"), List.of()));
}

public static @Nullable Pair<Identifier, StructureStart> locateAnyStructureAtBlock(ServerWorld world, BlockPos pos) {
Expand Down Expand Up @@ -86,6 +89,8 @@ public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup looku
if (this.targetId != null)
nbt.putString("targetId", targetId.toString());

CodecHelper.writeNbt(nbt, "targets", BlockPos.CODEC.listOf(), targets.stream().toList());

return nbt;
}

Expand All @@ -109,7 +114,7 @@ public void update(PlayerEntity player, ItemStack stack) {

super.update(player, stack);

for (ChunkPos target : this.targets)
for (BlockPos target : this.targets)
addTargetIcon(player.getWorld(), target);
}

Expand All @@ -121,6 +126,9 @@ public void addDecoration(RegistryEntry<MapDecorationType> type, @Nullable World
float scaledX = (float) (x - this.displayedCenter.getX()) / scale;
float scaledZ = (float) (z - this.displayedCenter.getZ()) / scale;

float squaredDistance = scaledX * scaledX + scaledZ * scaledZ;
byte scaleByte = (byte) Math.clamp(-16f * ((0.5f * Math.log(squaredDistance) / Math.log(2)) - 7), -100, 0);

byte pixelX = (byte) (scaledX * 2.0F + 0.5F);
byte pixelZ = (byte) (scaledZ * 2.0F + 0.5F);

Expand Down Expand Up @@ -173,6 +181,8 @@ public void addDecoration(RegistryEntry<MapDecorationType> type, @Nullable World
}

MapDecoration icon = new MapDecoration(type, pixelX, pixelZ, rotationByte, Optional.ofNullable(text));
icon.spectrum$setScale(scaleByte);

MapDecoration previousIcon = accessor.getDecorations().put(key, icon);
if (!icon.equals(previousIcon)) {
if (previousIcon != null && previousIcon.type().value().trackCount())
Expand Down Expand Up @@ -217,14 +227,14 @@ public boolean addBanner(WorldAccess world, BlockPos pos) {
return false;
}

private void addTargetIcon(WorldAccess world, ChunkPos target) {
private void addTargetIcon(WorldAccess world, BlockPos target) {
if (target != null) {
addDecoration(MapDecorationTypes.TARGET_POINT, world, getTargetKey(target), target.getCenterX(), target.getCenterZ(), 180, null);
addDecoration(MapDecorationTypes.TARGET_POINT, world, getTargetKey(target), target.getX(), target.getZ(), 180, null);
}
}

private String getTargetKey(ChunkPos start) {
return String.format("target-%d-%d", start.x, start.z);
private String getTargetKey(BlockPos start) {
return String.format("target-%d-%d-%d", start.getX(), start.getY(), start.getZ());
}

public void startLocator(ServerWorld world) {
Expand All @@ -240,9 +250,9 @@ public BlockPos getDisplayedCenter() {
return this.displayedCenter;
}

public void addTarget(WorldAccess world, ChunkPos chunkPos) {
this.targets.add(chunkPos);
addTargetIcon(world, chunkPos);
public void addTarget(WorldAccess world, BlockPos pos) {
this.targets.add(pos);
addTargetIcon(world, pos);
}

public void setTargetId(@Nullable Identifier targetId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public StructureLocatorAsync(ServerWorld world, Identifier targetId, long delayM
this.delayMillis = delayMillis;
}

public void ping(BlockPos pos, BiConsumer<WorldAccess, ChunkPos> acceptor) {
public void ping(BlockPos pos, BiConsumer<WorldAccess, BlockPos> acceptor) {
if (structures.isEmpty() || pinging.get())
return;

Expand All @@ -43,8 +43,9 @@ public void ping(BlockPos pos, BiConsumer<WorldAccess, ChunkPos> acceptor) {
pinging.set(true);
ChunkGenerator generator = world.getChunkManager().getChunkGenerator();
Pair<BlockPos, RegistryEntry<Structure>> pair = generator.locateStructure(world, structures.get(), pos, 100, false);
// TODO Get the centerpoint of the structure region
if (pair != null) {
acceptor.accept(world, new ChunkPos(pair.getFirst()));
acceptor.accept(world, pair.getFirst());
}
pinging.set(false);
}, Util.getMainWorkerExecutor());
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/de/dafuqs/spectrum/mixin/MapDecorationMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package de.dafuqs.spectrum.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.*;
import com.mojang.datafixers.util.*;
import de.dafuqs.spectrum.injectors.*;
import net.minecraft.item.map.*;
import net.minecraft.network.*;
import net.minecraft.network.codec.*;
import net.minecraft.registry.entry.*;
import net.minecraft.text.*;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;

import java.util.*;
import java.util.function.*;

@Mixin(MapDecoration.class)
public class MapDecorationMixin implements MapDecorationInjector {

@Unique
private byte scale = 0;

@WrapOperation(method = "<clinit>", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/codec/PacketCodec;tuple(Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function5;)Lnet/minecraft/network/codec/PacketCodec;"))
private static PacketCodec<RegistryByteBuf, MapDecoration> wrapCodec(
PacketCodec<? super RegistryByteBuf, RegistryEntry<MapDecorationType>> codec1, Function<MapDecoration, RegistryEntry<MapDecorationType>> from1,
PacketCodec<? super RegistryByteBuf, Byte> codec2, Function<MapDecoration, Byte> from2,
PacketCodec<? super RegistryByteBuf, Byte> codec3, Function<MapDecoration, Byte> from3,
PacketCodec<? super RegistryByteBuf, Byte> codec4, Function<MapDecoration, Byte> from4,
PacketCodec<? super RegistryByteBuf, Optional<Text>> codec5, Function<MapDecoration, Optional<Text>> from5,
Function5<RegistryEntry<MapDecorationType>, Byte, Byte, Byte, Optional<Text>, MapDecoration> _to,
Operation<PacketCodec<RegistryByteBuf, MapDecoration>> original
) {
PacketCodec<RegistryByteBuf, MapDecoration> codec = original.call(codec1, from1, codec2, from2, codec3, from3, codec4, from4, codec5, from5, _to);
return new PacketCodec<>() {
@Override
public void encode(RegistryByteBuf buf, MapDecoration value) {
codec.encode(buf, value);
buf.writeByte(value.spectrum$getScale());
}

@Override
public MapDecoration decode(RegistryByteBuf buf) {
MapDecoration decoration = codec.decode(buf);
decoration.spectrum$setScale(buf.readByte());
return decoration;
}
};
}

@Override
public void spectrum$setScale(byte scale) {
this.scale = scale;
}

@Override
public byte spectrum$getScale() {
return this.scale;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.dafuqs.spectrum.mixin.client;

import com.llamalad7.mixinextras.injector.wrapoperation.*;
import com.llamalad7.mixinextras.sugar.*;
import net.minecraft.client.render.*;
import net.minecraft.client.util.math.*;
import net.minecraft.item.map.*;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;

@Mixin(MapRenderer.MapTexture.class)
public class MapRendererMapTextureMixin {

@WrapOperation(method = "draw(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;ZI)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/math/MatrixStack;scale(FFF)V"))
public void scaleDecorations(MatrixStack instance, float x, float y, float z, Operation<Void> original, @Local MapDecoration decoration) {
float scale = (float) ((int) decoration.spectrum$getScale() + 128) / 128;
original.call(instance, 4f * scale, 4f * scale, 3f * scale);
}

}
5 changes: 4 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
],
"custom": {
"loom:injected_interfaces": {
"net/minecraft/class_1293": [
"net/minecraft/class_20": [
"de/dafuqs/spectrum/injectors/MapDecorationInjector"
],
"net/minecraft/class_1293": [
"de/dafuqs/spectrum/injectors/StatusEffectInstanceInjector"
],
"net/minecraft/class_2783": [
Expand Down
15 changes: 8 additions & 7 deletions src/main/resources/spectrum.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ accessible class net/minecraft/structure/pool/StructurePoolBasedGenerator$Struct
accessible class net/minecraft/registry/RegistryOps$CachedRegistryInfoGetter
accessible class net/minecraft/world/biome/Biome$Weather
accessible class net/minecraft/world/SpawnDensityCapper$DensityCap
accessible field net/minecraft/loot/LootTable pools Ljava/util/List;
accessible field net/minecraft/block/FluidBlock fluid Lnet/minecraft/fluid/FlowableFluid;

extendable method net/minecraft/component/type/BundleContentsComponent$Builder addInternal (Lnet/minecraft/item/ItemStack;)I
extendable method net/minecraft/component/type/BundleContentsComponent$Builder getMaxAllowed (Lnet/minecraft/item/ItemStack;)I
accessible class net/minecraft/client/render/MapRenderer$MapTexture

accessible method net/minecraft/client/particle/BlockLeakParticle$Dripping <init> (Lnet/minecraft/client/world/ClientWorld;DDDLnet/minecraft/fluid/Fluid;Lnet/minecraft/particle/ParticleEffect;)V
accessible method net/minecraft/client/particle/BlockLeakParticle$ContinuousFalling <init> (Lnet/minecraft/client/world/ClientWorld;DDDLnet/minecraft/fluid/Fluid;Lnet/minecraft/particle/ParticleEffect;)V
Expand All @@ -23,13 +19,18 @@ accessible method net/minecraft/client/particle/VibrationParticle <init> (Lnet/m
accessible method net/minecraft/block/FluidBlock getFluidState (Lnet/minecraft/block/BlockState;)Lnet/minecraft/fluid/FluidState;
accessible method net/minecraft/client/render/entity/ItemFrameEntityRenderer getLight (Lnet/minecraft/entity/decoration/ItemFrameEntity;II)I
accessible method net/minecraft/entity/LivingEntity applyDamage (Lnet/minecraft/entity/damage/DamageSource;F)V
extendable method net/minecraft/screen/ForgingScreenHandler addPlayerInventorySlots (Lnet/minecraft/entity/player/PlayerInventory;)V
extendable method net/minecraft/entity/projectile/ProjectileEntity <init> (Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;)V
accessible method net/minecraft/entity/projectile/FishingBobberEntity <init> (Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;II)V
accessible method net/minecraft/registry/entry/RegistryEntryList$Direct <init> (Ljava/util/List;)V
accessible method net/minecraft/advancement/criterion/AbstractCriterion trigger (Lnet/minecraft/server/network/ServerPlayerEntity;Ljava/util/function/Predicate;)V
accessible method net/minecraft/screen/ForgingScreenHandler addPlayerInventorySlots (Lnet/minecraft/entity/player/PlayerInventory;)V

extendable method net/minecraft/component/type/BundleContentsComponent$Builder addInternal (Lnet/minecraft/item/ItemStack;)I
extendable method net/minecraft/component/type/BundleContentsComponent$Builder getMaxAllowed (Lnet/minecraft/item/ItemStack;)I
extendable method net/minecraft/screen/ForgingScreenHandler addPlayerInventorySlots (Lnet/minecraft/entity/player/PlayerInventory;)V
extendable method net/minecraft/entity/projectile/ProjectileEntity <init> (Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;)V
extendable method net/minecraft/item/map/MapState <init> (IIBZZZLnet/minecraft/registry/RegistryKey;)V
extendable method net/minecraft/item/map/MapState addDecoration (Lnet/minecraft/registry/entry/RegistryEntry;Lnet/minecraft/world/WorldAccess;Ljava/lang/String;DDDLnet/minecraft/text/Text;)V
accessible field net/minecraft/loot/LootTable pools Ljava/util/List;
accessible field net/minecraft/block/FluidBlock fluid Lnet/minecraft/fluid/FlowableFluid;

mutable field net/minecraft/item/map/MapState dimension Lnet/minecraft/registry/RegistryKey;
2 changes: 2 additions & 0 deletions src/main/resources/spectrum.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"LivingEntityPreventStatusClearMixin",
"LoomContainerPatternSlotMixin",
"LoomScreenHandlerMixin",
"MapDecorationMixin",
"MapStateMixin",
"MapStatePlayerUpdateTrackerMixin",
"MC252934Mixin",
Expand Down Expand Up @@ -131,6 +132,7 @@
"client.InGameOverlayRendererMixin",
"client.ItemRendererMixin",
"client.LightmapTextureManagerMixin",
"client.MapRendererMapTextureMixin",
"client.MixinParticleManager",
"client.MouseMixin",
"client.PlayerEntityRendererMixin",
Expand Down

0 comments on commit 5ae96b2

Please sign in to comment.