Skip to content

Commit

Permalink
Fix colony map (#10171)
Browse files Browse the repository at this point in the history
fixes nbt checking for filled maps (1.21 only)
reworks how colony map works internally regarding mapData (background map texture) - fixes various weird issues with zooming (no more jumpy) or items outside zoomdrag component
actual changes: allow map of any scale (not just "normal" zero), center at player on open
(will port after merging)
  • Loading branch information
Nightenom authored Oct 23, 2024
1 parent b130e0f commit 1d437d3
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,14 @@
{
"item": "minecraft:fern"
},
{
"checkednbtkeys": [
"minecraft:map_color",
"minecraft:map_decorations",
"minecraft:map_id"
],
"item": "minecraft:filled_map"
},
{
"item": "minecraft:fire_charge"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import com.minecolonies.api.colony.buildings.views.IBuildingView;
import com.minecolonies.api.colony.colonyEvents.descriptions.IColonyEventDescription;
import com.minecolonies.api.colony.permissions.PermissionEvent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.level.saveddata.maps.MapId;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;

import java.util.List;

Expand All @@ -28,4 +33,20 @@ public interface ITownHallView extends IBuildingView
* @return true if so.
*/
boolean canPlayerUseTP();

/**
* Getter for the mapdata.
* @return the original list.
*/
List<MapEntry> getMapDataList();

public record MapEntry(MapId mapId, MapItemSavedData mapData)
{
public static final StreamCodec<RegistryFriendlyByteBuf, MapEntry> STREAM_CODEC =
StreamCodec.composite(MapId.STREAM_CODEC,
MapEntry::mapId,
StreamCodec.of((buf, data) -> buf.writeNbt(data.save(new CompoundTag(), buf.registryAccess())), buf -> MapItemSavedData.load(buf.readNbt(), buf.registryAccess())),
MapEntry::mapData,
MapEntry::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import com.ldtteam.blockui.BOGuiGraphics;
import com.ldtteam.blockui.Pane;
import com.ldtteam.blockui.PaneParams;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.client.Minecraft;
import net.minecraft.world.level.saveddata.maps.MapId;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;

/**
* Simple minecraft map element.
*/
public class MinecraftMap extends Pane implements AutoCloseable
public class MinecraftMap extends Pane
{
private DynamicTexture texture;
private ResourceLocation textureResLoc;
public static final int MAP_SIZE = 128;
public static final int MAP_CENTER = 64;

private MapItemSavedData mapData;
private MapId mapId;

/**
* Default Constructor.
Expand All @@ -38,25 +40,10 @@ public MinecraftMap(final PaneParams params)
* Set the fitting map data.
* @param mapData the mapData to set.
*/
public void setMapData(final MapItemSavedData mapData)
public void setMapData(final MapId mapId, final MapItemSavedData mapData)
{
if (texture != null)
{
freeTexture();
}

texture = new DynamicTexture(128, 128, false);

for (int y = 0; y < 128; ++y)
{
for (int x = 0; x < 128; ++x)
{
texture.getPixels().setPixelRGBA(x, y, MapColor.getColorFromPackedId(mapData.colors[x + y * 128]));
}
}

texture.upload();
textureResLoc = mc.getTextureManager().register("minecolonies_map/" + id, texture);
this.mapId = mapId;
this.mapData = mapData;
}

/**
Expand All @@ -68,27 +55,17 @@ public void setMapData(final MapItemSavedData mapData)
@Override
public void drawSelf(final BOGuiGraphics ms, final double mx, final double my)
{
if (textureResLoc != null)
if (mapData != null)
{
blit(ms.pose(), textureResLoc, x, y, width, height);
}
}
ms.pose().pushPose();
ms.pose().translate(x, y, 0.01f);
ms.pose().scale(getWidth() / MAP_SIZE, getHeight() / MAP_SIZE, 1);

private void freeTexture()
{
if (textureResLoc != null)
{
texture.close();
mc.getTextureManager().release(textureResLoc);
// if fifth bool == false => enable all map decos
Minecraft.getInstance().gameRenderer.getMapRenderer().render(ms.pose(), ms.bufferSource(), mapId, mapData, true, 15728880);

texture = null;
textureResLoc = null;
ms.flush();
ms.pose().popPose();
}
}

@Override
public void close()
{
freeTexture();
}
}
Loading

0 comments on commit 1d437d3

Please sign in to comment.