Skip to content

Commit

Permalink
Merge branch '1.16'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bulldog83 committed Jul 7, 2020
2 parents e0cc279 + 20f94ef commit a178791
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import ru.bulldog.justmap.client.JustMapClient;
import ru.bulldog.justmap.config.ConfigKeeper.EnumEntry;
import ru.bulldog.justmap.map.DirectionArrow;
import ru.bulldog.justmap.map.minimap.MapSkin;
import ru.bulldog.justmap.map.minimap.Minimap;
import ru.bulldog.justmap.map.minimap.skin.MapSkin;
import ru.bulldog.justmap.util.ScreenPosition;

public final class ConfigFactory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
package ru.bulldog.justmap.map.icon;
package ru.bulldog.justmap.client.render;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;

import ru.bulldog.justmap.util.RenderUtil;

public abstract class AbstractIcon extends Sprite {
public abstract class Image {

protected static TextureManager textureManager = MinecraftClient.getInstance().getTextureManager();

protected AbstractIcon(SpriteAtlasTexture spriteAtlasTexture, Info info, int i, int j, int k, int l, int m, NativeImage nativeImage) {
super(spriteAtlasTexture, info, i, j, k, l, m, nativeImage);
protected final NativeImage image;
protected Identifier textureId;
protected int width;
protected int height;

protected Image(Identifier id, NativeImage image) {
this.width = image.getWidth();
this.height = image.getHeight();
this.textureId = id;
this.image = image;
}

public abstract void draw(double x, double y, int w, int h);
public abstract void draw(MatrixStack matrix, double x, double y, int w, int h);

public void draw(double x, double y) {
MatrixStack matrix = new MatrixStack();
this.draw(matrix, x, y, this.getWidth(), this.getHeight());
public int getWidth() {
return this.width;
}

public int getHeight() {
return this.height;
}

public void draw(MatrixStack matrix, double x, double y) {
public Identifier getId() {
return this.textureId;
}

public void bindTexture() {
textureManager.bindTexture(textureId);
}

public void draw(double x, double y) {
MatrixStack matrix = new MatrixStack();
this.draw(matrix, x, y, this.getWidth(), this.getHeight());
}

Expand All @@ -38,6 +57,6 @@ public void draw(MatrixStack matrix, double x, double y, int size) {
}

protected void draw(MatrixStack matrix, double x, double y, float w, float h) {
RenderUtil.drawSprite(matrix, this, x, y, w, h);
RenderUtil.drawImage(matrix, this, x, y, w, h);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import ru.bulldog.justmap.map.icon.PlayerIcon;
import ru.bulldog.justmap.map.icon.WaypointIcon;
import ru.bulldog.justmap.map.minimap.MapPlayerManager;
import ru.bulldog.justmap.map.minimap.MapSkin;
import ru.bulldog.justmap.map.minimap.Minimap;
import ru.bulldog.justmap.map.minimap.skin.MapSkin;
import ru.bulldog.justmap.util.RenderUtil.TextAlignment;
import ru.bulldog.justmap.util.Colors;
import ru.bulldog.justmap.util.RenderUtil;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/ru/bulldog/justmap/config/ConfigWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import net.fabricmc.loader.api.FabricLoader;
import ru.bulldog.justmap.JustMap;
import ru.bulldog.justmap.util.JsonFactory;
import ru.bulldog.justmap.util.StorageUtil;

public class ConfigWriter extends JsonFactory {

private final static File CONFIG_DIR = FabricLoader.getInstance().getConfigDirectory();
private final static File CONFIG_FILE = new File(CONFIG_DIR, "/" + JustMap.MODID + ".json");
private final static File CONFIG_FILE = new File(StorageUtil.configDir(), JustMap.MODID + ".json");

private static JsonObject configObject;

Expand Down
26 changes: 12 additions & 14 deletions src/main/java/ru/bulldog/justmap/map/data/MapCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ public static void setLayerLevel(int level) {

public static MapCache get() {
World world = minecraft.world;
if (minecraft.isIntegratedServerRunning() && world != null) {
world = minecraft.getServer().getWorld(minecraft.world.getRegistryKey());
}
if (currentWorld == null || (world != null &&
world != currentWorld)) {

currentWorld = world;
if (world != null) {
if (minecraft.isIntegratedServerRunning()) {
World serverWorld = minecraft.getServer().getWorld(world.getRegistryKey());
if (serverWorld != null) {
world = serverWorld;
}
}
if (!world.equals(currentWorld)) {
currentWorld = world;
}
}

if (currentWorld == null) return null;

Identifier dimId = currentWorld.getDimensionRegistryKey().getValue();
Expand Down Expand Up @@ -207,17 +209,13 @@ private Map<ChunkPos, MapChunk> getChunks() {
return this.chunks;
}

public MapChunk getCurrentChunk(ChunkPos chunkPos) {
return this.getChunk(currentLayer, currentLevel, chunkPos.x, chunkPos.z);
}

public MapChunk getCurrentChunk(int posX, int posZ) {
return this.getChunk(currentLayer, currentLevel, posX, posZ);
}

public MapChunk getChunk(Layer.Type layer, int level, int posX, int posZ) {
ChunkPos chunkPos = new ChunkPos(posX, posZ);
ChunkPos chunkPos = new ChunkPos(posX, posZ);

MapChunk mapChunk;
if (chunks.containsKey(chunkPos)) {
mapChunk = this.chunks.get(chunkPos);
Expand Down
70 changes: 40 additions & 30 deletions src/main/java/ru/bulldog/justmap/map/icon/EntityHeadIcon.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package ru.bulldog.justmap.map.icon;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ru.bulldog.justmap.client.config.ClientParams;
import ru.bulldog.justmap.client.render.Image;
import ru.bulldog.justmap.util.ImageUtil;
import ru.bulldog.justmap.util.SpriteAtlas;
import ru.bulldog.justmap.util.StorageUtil;
import ru.bulldog.justmap.util.ColorUtil;
import ru.bulldog.justmap.util.Colors;
import ru.bulldog.justmap.util.RenderUtil;
import ru.bulldog.justmap.util.math.Line.Point;

import net.minecraft.client.resource.metadata.AnimationResourceMetadata;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.NativeImageBackedTexture;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.mob.HostileEntity;
import net.minecraft.entity.passive.TameableEntity;
import net.minecraft.util.Identifier;

public class EntityHeadIcon extends AbstractIcon {
public class EntityHeadIcon extends Image {

private final static Map<Identifier, EntityHeadIcon> ICONS = new HashMap<>();

Expand All @@ -33,9 +33,15 @@ public static EntityHeadIcon getIcon(Entity entity) {
if (ICONS.containsKey(id)) {
return ICONS.get(id);
} else {
Identifier iconId = iconId(id);
if (ImageUtil.imageExists(iconId)) {
return registerIcon(entity, id, iconId);
File iconsDir = StorageUtil.iconsDir();
File iconPng = new File(iconsDir, String.format("%s/%s.png", id.getNamespace(), id.getPath()));
if (iconPng.exists()) {
return registerIcon(entity, id, iconPng);
} else {
Identifier iconId = iconId(id);
if (ImageUtil.imageExists(iconId)) {
return registerIcon(entity, id, iconId);
}
}
}

Expand All @@ -48,18 +54,16 @@ public static EntityHeadIcon getIcon(Entity entity) {
private boolean solid;

private EntityHeadIcon(Identifier id, Identifier texture, int w, int h) {
super(SpriteAtlas.ENTITY_HEAD_ICONS, new Sprite.Info(texture, w, h, AnimationResourceMetadata.EMPTY), 0, w, h, 0, 0, ImageUtil.loadImage(texture, w, h));
this(id, texture, ImageUtil.loadImage(texture, w, h));
}

private EntityHeadIcon(Identifier id, Identifier texture, NativeImage image) {
super(texture, image);

this.solid = this.isSolid();
this.id = id;
}

@Override
public void draw(double x, double y, int w, int h) {
MatrixStack matrix = new MatrixStack();
this.draw(matrix, x, y, (float) w, (float) h);
}

@Override
public void draw(MatrixStack matrix, double x, double y, int w, int h) {
if (ClientParams.showIconsOutline) {
Expand All @@ -71,7 +75,6 @@ public void draw(MatrixStack matrix, double x, double y, int w, int h) {
RenderUtil.draw(x - thickness / 2, y - thickness / 2, (float) (w + thickness), (float) (h + thickness));
}
}
textureManager.bindTexture(this.getId());
this.draw(matrix, x, y, (float) w, (float) h);
}

Expand All @@ -84,7 +87,7 @@ private void bindOutline() {
}

private boolean isSolid() {
NativeImage icon = this.images[0];
NativeImage icon = this.image;

int width = icon.getWidth();
int height = icon.getHeight();
Expand All @@ -102,11 +105,6 @@ private boolean isSolid() {
}

private NativeImage generateOutline() {
NativeImage icon = this.images[0];

int width = icon.getWidth();
int height = icon.getHeight();

NativeImage outline = new NativeImage(width + 4, height + 4, false);
ImageUtil.fillImage(outline, Colors.TRANSPARENT);

Expand All @@ -120,15 +118,15 @@ private NativeImage generateOutline() {
int left = x - 1;
int right = x + 1;
for (int y = 0; y < height; y++) {
int alpha = (icon.getPixelColor(x, y) >> 24) & 255;
int alpha = (image.getPixelColor(x, y) >> 24) & 255;
if (alpha == 0) continue;

outlinePixels.add(new Point(x + 2, y + 2));

int top = y - 1;
int bottom = y + 1;
if (top >= 0) {
alpha = (icon.getPixelColor(x, top) >> 24) & 255;
alpha = (image.getPixelColor(x, top) >> 24) & 255;
if (alpha == 0) {
Point pixel = new Point(x + 2, y);
if (!outlinePixels.contains(pixel)) {
Expand All @@ -137,7 +135,7 @@ private NativeImage generateOutline() {
}
}
if (left >= 0) {
alpha = (icon.getPixelColor(left, top) >> 24) & 255;
alpha = (image.getPixelColor(left, top) >> 24) & 255;
if (alpha == 0) {
Point pixel = new Point(x, y);
if (!outlinePixels.contains(pixel)) {
Expand All @@ -149,7 +147,7 @@ private NativeImage generateOutline() {
}
}
if (right < width) {
alpha = (icon.getPixelColor(right, top) >> 24) & 255;
alpha = (image.getPixelColor(right, top) >> 24) & 255;
if (alpha == 0) {
Point pixel = new Point(right + 2, y);
if (!outlinePixels.contains(pixel)) {
Expand All @@ -168,7 +166,7 @@ private NativeImage generateOutline() {
}
}
if (bottom < height) {
alpha = (icon.getPixelColor(x, bottom) >> 24) & 255;
alpha = (image.getPixelColor(x, bottom) >> 24) & 255;
if (alpha == 0) {
Point pixel = new Point(x + 2, bottom + 1);
if (!outlinePixels.contains(pixel)) {
Expand All @@ -177,7 +175,7 @@ private NativeImage generateOutline() {
}
}
if (left >= 0) {
alpha = (icon.getPixelColor(left, bottom) >> 24) & 255;
alpha = (image.getPixelColor(left, bottom) >> 24) & 255;
if (alpha == 0) {
Point pixel = new Point(x, bottom + 2);
if (!outlinePixels.contains(pixel)) {
Expand All @@ -189,7 +187,7 @@ private NativeImage generateOutline() {
}
}
if (right < width) {
alpha = (icon.getPixelColor(right, bottom) >> 24) & 255;
alpha = (image.getPixelColor(right, bottom) >> 24) & 255;
if (alpha == 0) {
Point pixel = new Point(right + 2, bottom + 2);
if (!outlinePixels.contains(pixel)) {
Expand All @@ -208,7 +206,7 @@ private NativeImage generateOutline() {
}
}
if (left >= 0) {
alpha = (icon.getPixelColor(left, y) >> 24) & 255;
alpha = (image.getPixelColor(left, y) >> 24) & 255;
if (alpha == 0) {
Point pixel = new Point(x, y + 2);
if (!outlinePixels.contains(pixel)) {
Expand All @@ -224,7 +222,7 @@ private NativeImage generateOutline() {
}
}
if (right < width) {
alpha = (icon.getPixelColor(right, y) >> 24) & 255;
alpha = (image.getPixelColor(right, y) >> 24) & 255;
if (alpha == 0) {
Point pixel = new Point(right + 1, y + 2);
if (!outlinePixels.contains(pixel)) {
Expand Down Expand Up @@ -255,6 +253,18 @@ private static Identifier iconId(Identifier id) {

private static EntityHeadIcon registerIcon(Entity entity, Identifier entityId, Identifier texture) {
EntityHeadIcon icon = new EntityHeadIcon(entityId, texture, 32, 32);
return registerIcon(entity, entityId, icon);
}

private static EntityHeadIcon registerIcon(Entity entity, Identifier entityId, File image) {
NativeImage iconImage = ImageUtil.loadImage(image, 32, 32);
String prefix = String.format("icon_%s_%s", entityId.getNamespace(), entityId.getPath());
Identifier textureId = textureManager.registerDynamicTexture(prefix, new NativeImageBackedTexture(iconImage));
EntityHeadIcon icon = new EntityHeadIcon(entityId, textureId, iconImage);
return registerIcon(entity, entityId, icon);
}

private static EntityHeadIcon registerIcon(Entity entity, Identifier entityId, EntityHeadIcon icon) {
if (entity instanceof HostileEntity) {
icon.color = Colors.DARK_RED;
} else if (entity instanceof TameableEntity) {
Expand Down
Loading

0 comments on commit a178791

Please sign in to comment.