Skip to content

Commit

Permalink
馃awt is gone馃
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuclearfarts authored and Nuclearfarts committed Aug 7, 2020
1 parent b728362 commit 03e7fc2
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.nuclearfarts.cbt.mixin;

import java.nio.channels.WritableByteChannel;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

import net.minecraft.client.texture.NativeImage;

@Mixin(NativeImage.class)
public interface NativeImageAccessor {
@Invoker
boolean invokeWrite(WritableByteChannel channel);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.nuclearfarts.cbt.resource;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -13,16 +13,16 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javax.imageio.ImageIO;

import com.google.common.collect.ImmutableSet;

import net.minecraft.client.texture.NativeImage;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceType;
import net.minecraft.resource.metadata.ResourceMetadataReader;
import net.minecraft.util.Identifier;

import io.github.nuclearfarts.cbt.mixin.NativeImageAccessor;
import io.github.nuclearfarts.cbt.tile.Tile;

public class CBTResourcePack implements ResourcePack {
Expand All @@ -47,17 +47,14 @@ public void alias(String location, Identifier to) {
aliases.put(location, to);
}

public void putImage(String location, BufferedImage image) {
public void putImage(String location, NativeImage image) {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try {
ImageIO.write(image, "png", byteOut);
} catch (IOException e) {
throw new RuntimeException("IOException writing to bytearrayoutputstream!?", e);
}
((NativeImageAccessor) (Object) image).invokeWrite(Channels.newChannel(byteOut));
image.close();
putResource(location, byteOut.toByteArray());
}

public Identifier dynamicallyPutImage(BufferedImage image) {
public Identifier dynamicallyPutImage(NativeImage image) {
String texPath = "gen/" + genCounter++;
putImage("assets/connectedblocktextures/textures/" + texPath + ".png", image);
return new Identifier("connectedblocktextures", texPath);
Expand All @@ -73,13 +70,12 @@ public void putTile(String location, Tile tile) {
if(tile.hasResource()) {
alias(location, tile.getResource());
} else {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
try {
ImageIO.write(tile.getImage(), "png", byteStream);
putImage(location, tile.getImage());
} catch (IOException e) {
throw new RuntimeException("IOException writing to byte array output stream!?", e);
//should be unreachable
e.printStackTrace();
}
resources.put(location, byteStream.toByteArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.nuclearfarts.cbt.tile;

import java.awt.image.BufferedImage;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.util.Identifier;

public class ImageBackedTile implements Tile {
private final BufferedImage image;
private final NativeImage image;

public ImageBackedTile(BufferedImage image) {
public ImageBackedTile(NativeImage image) {
this.image = image;
}

Expand All @@ -21,7 +21,7 @@ public Identifier getResource() {
}

@Override
public BufferedImage getImage() {
public NativeImage getImage() {
return image;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.github.nuclearfarts.cbt.tile;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.minecraft.client.texture.NativeImage;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;

Expand All @@ -28,8 +26,8 @@ public Identifier getResource() {
}

@Override
public BufferedImage getImage() throws IOException {
return ImageIO.read(resourceManager.getResource(resource).getInputStream());
public NativeImage getImage() throws IOException {
return NativeImage.read(resourceManager.getResource(resource).getInputStream());
}

}
4 changes: 2 additions & 2 deletions src/main/java/io/github/nuclearfarts/cbt/tile/Tile.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.nuclearfarts.cbt.tile;

import java.awt.image.BufferedImage;
import java.io.IOException;

import net.minecraft.client.texture.NativeImage;
import net.minecraft.util.Identifier;

public interface Tile {
Expand All @@ -11,5 +11,5 @@ public interface Tile {
* @throws UnsupportedOperationException if this is a dynamically generated tile.
* */
Identifier getResource();
BufferedImage getImage() throws IOException;
NativeImage getImage() throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.github.nuclearfarts.cbt.tile.loader;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;

import net.minecraft.client.texture.NativeImage;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;

Expand All @@ -19,32 +17,32 @@ public class DynamicBookshelfTileLoader implements TileLoader {

public DynamicBookshelfTileLoader(Properties properties, Identifier location, ResourceManager manager) throws IOException {
Identifier textureLocation = new Identifier(properties.getProperty("cbt_special_bookshelf_texture"));
BufferedImage bookshelf = ImageIO.read(manager.getResource(textureLocation).getInputStream());
tiles[3] = new ResourceBackedTile(textureLocation, manager);

BufferedImage work;
work = CBTUtil.copy(bookshelf);
copyVLine(work, 4, 1, 6, 15, 9);
copyVLine(work, 8, 9, 6, 15, 1);
tiles[0] = new ImageBackedTile(work);
work = CBTUtil.copy(work);
copyVLine(work, 5, 1, 6, 0, 9);
copyVLine(work, 9, 9, 6, 0, 1);
tiles[1] = new ImageBackedTile(work);
work = CBTUtil.copy(bookshelf);
copyVLine(work, 5, 1, 6, 0, 9);
copyVLine(work, 9, 9, 6, 0, 1);
tiles[2] = new ImageBackedTile(work);
try(NativeImage bookshelf = NativeImage.read(manager.getResource(textureLocation).getInputStream())) {
tiles[3] = new ResourceBackedTile(textureLocation, manager);
NativeImage work;
work = CBTUtil.copy(bookshelf);
copyVLine(work, 4, 1, 6, 15, 9);
copyVLine(work, 8, 9, 6, 15, 1);
tiles[0] = new ImageBackedTile(work);
work = CBTUtil.copy(work);
copyVLine(work, 5, 1, 6, 0, 9);
copyVLine(work, 9, 9, 6, 0, 1);
tiles[1] = new ImageBackedTile(work);
work = CBTUtil.copy(bookshelf);
copyVLine(work, 5, 1, 6, 0, 9);
copyVLine(work, 9, 9, 6, 0, 1);
tiles[2] = new ImageBackedTile(work);
}
}

@Override
public Tile[] getTiles() {
return tiles;
}

private void copyVLine(BufferedImage on, int srcX, int srcY, int height, int dstX, int dstY) {
private void copyVLine(NativeImage on, int srcX, int srcY, int height, int dstX, int dstY) {
for(int i = 0; i < height; i++) {
on.setRGB(dstX, dstY + i, on.getRGB(srcX, srcY + i));
on.setPixelColor(dstX, dstY + i, on.getPixelColor(srcX, srcY + i));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.github.nuclearfarts.cbt.tile.loader;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;

import net.minecraft.client.texture.NativeImage;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;

Expand All @@ -17,11 +15,11 @@ public class DynamicGlassTileLoader implements TileLoader {
private final Tile[] tiles = new Tile[5];

public DynamicGlassTileLoader(Properties properties, Identifier location, ResourceManager manager) throws IOException {
BufferedImage glass = ImageIO.read(manager.getResource(new Identifier(properties.getProperty("cbt_special_glass_texture"))).getInputStream());
int background = Integer.parseUnsignedInt(properties.getProperty("cbt_special_glass_background"), 16);
NativeImage glass = NativeImage.read(manager.getResource(new Identifier(properties.getProperty("cbt_special_glass_texture"))).getInputStream());
int background = CBTUtil.toABGR(Integer.parseUnsignedInt(properties.getProperty("cbt_special_glass_background"), 16));
tiles[0] = new ImageBackedTile(glass); //all-borders

BufferedImage work;
NativeImage work;
work = CBTUtil.copy(glass);
hLine(work, 0, 0, 16, background);
hLine(work, 0, 15, 16, background);
Expand Down Expand Up @@ -52,15 +50,15 @@ public Tile[] getTiles() {
return tiles;
}

private static void hLine(BufferedImage image, int xStart, int y, int length, int color) {
private static void hLine(NativeImage image, int xStart, int y, int length, int color) {
for(int x = 0; x < length; x++) {
image.setRGB(x + xStart, y, color);
image.setPixelColor(x + xStart, y, color);
}
}

private static void vLine(BufferedImage image, int x, int yStart, int length, int color) {
private static void vLine(NativeImage image, int x, int yStart, int length, int color) {
for(int y = 0; y < length; y++) {
image.setRGB(x, y + yStart, color);
image.setPixelColor(x, y + yStart, color);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.github.nuclearfarts.cbt.tile.loader;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;

import net.minecraft.client.texture.NativeImage;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;

Expand All @@ -16,10 +14,10 @@ public class DynamicSandstoneTileLoader implements TileLoader {
private Tile[] tiles = new Tile[1];

public DynamicSandstoneTileLoader(Properties properties, Identifier location, ResourceManager manager) throws IOException {
BufferedImage sandstone = ImageIO.read(manager.getResource(new Identifier(properties.getProperty("cbt_special_sandstone_texture"))).getInputStream());
NativeImage sandstone = NativeImage.read(manager.getResource(new Identifier(properties.getProperty("cbt_special_sandstone_texture"))).getInputStream());
for(int x = 0; x < 16; x++) {
for(int y = 0; y < 3; y++) {
sandstone.setRGB(15 - x, y, sandstone.getRGB(x, y + 12));
sandstone.setPixelColor(15 - x, y, sandstone.getPixelColor(x, y + 12));
}
}
tiles[0] = new ImageBackedTile(sandstone);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.github.nuclearfarts.cbt.tile.provider;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.client.util.SpriteIdentifier;
Expand All @@ -15,12 +16,12 @@ public class CompactTileProvider implements TileProvider {
private final List<SpriteIdentifier> spriteIds = new ArrayList<>();

public CompactTileProvider(Tile[] origTiles) throws IOException {
BufferedImage[] origImages = new BufferedImage[5];
NativeImage[] origImages = new NativeImage[5];
for(int i = 0; i < 5; i++) {
origImages[i] = origTiles[i].getImage();
}
for(int i = 0; i <= 46; i++) {
BufferedImage tile = new BufferedImage(origImages[0].getWidth(), origImages[0].getHeight(), BufferedImage.TYPE_INT_ARGB);
NativeImage tile = new NativeImage(origImages[0].getWidth(), origImages[0].getHeight(), true);
short bithack = ConnectedBlockTextures.CTM_TO_IDEALIZED_BITHACK[i];
//Up-left. up-left-upleft

Expand Down Expand Up @@ -99,14 +100,14 @@ public CompactTileProvider(Tile[] origTiles) throws IOException {
}
}

private static void blitQuarter(int offsetX, int offsetY, BufferedImage src, BufferedImage dst) {
private static void blitQuarter(int offsetX, int offsetY, NativeImage src, NativeImage dst) {
int w = src.getWidth() / 2;
int h = src.getHeight() / 2;
int x = w * offsetX;
int y = h * offsetY;
for(int i = x; i < w + x; i++) {
for(int j = y; j < h + y; j++) {
dst.setRGB(i, j, src.getRGB(i, j));
dst.setPixelColor(i, j, src.getPixelColor(i, j));
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/io/github/nuclearfarts/cbt/util/CBTUtil.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.nuclearfarts.cbt.util;

import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.function.Function;
import java.util.function.IntPredicate;
import java.util.function.Predicate;

import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.util.Identifier;

Expand Down Expand Up @@ -77,13 +77,9 @@ public static Identifier stripVariants(ModelIdentifier modelId) {
return new Identifier(modelId.getNamespace(), modelId.getPath());
}

public static BufferedImage copy(BufferedImage image) {
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
newImage.setRGB(x, y, image.getRGB(x, y));
}
}
public static NativeImage copy(NativeImage image) {
NativeImage newImage = new NativeImage(image.getFormat(), image.getWidth(), image.getHeight(), true);
newImage.copyFrom(image);
return newImage;
}

Expand All @@ -94,4 +90,11 @@ public static int actualMod(int in, int modulo) {
}
return mod;
}

public static int toABGR(int argb) {
return ((argb >> 24) << 24) | // Alpha
((argb >> 16) & 0xFF) | // Red -> Blue
((argb >> 8) & 0xFF) << 8 | // Green
((argb) & 0xFF) << 16; // Blue -> Red
}
}
3 changes: 2 additions & 1 deletion src/main/resources/connected_block_textures.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"ModelLoaderMixin",
"ReloadableResourceManagerImplMixin",
"ClientWorldMixin",
"IdentifierMixin"
"IdentifierMixin",
"NativeImageAccessor"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 03e7fc2

Please sign in to comment.