This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0289da
commit c1fbd88
Showing
20 changed files
with
563 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/github/kisaragieffective/signpictureported/Box.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.github.kisaragieffective.signpictureported; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
public class Box<T> implements Closeable { | ||
public final Supplier<? extends T> value; | ||
private boolean needsInit = true; | ||
// late-bind | ||
private T cache = null; | ||
public Box(Supplier<? extends T> value) { | ||
this.value = value; | ||
} | ||
|
||
public T getValue() { | ||
if (needsInit) { | ||
cache = value.get(); | ||
needsInit = false; | ||
} | ||
return cache; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Box<?> box = (Box<?>) o; | ||
return Objects.equals(cache, box.cache); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(cache); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (cache instanceof Closeable) { | ||
((Closeable) cache).close(); | ||
needsInit = true; | ||
cache = null; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/github/kisaragieffective/signpictureported/ImageWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.kisaragieffective.signpictureported; | ||
|
||
import net.minecraft.client.texture.NativeImageBackedTexture; | ||
|
||
import java.io.Closeable; | ||
import java.lang.ref.SoftReference; | ||
import java.util.Objects; | ||
|
||
public class ImageWrapper implements Closeable { | ||
public final SoftReference<NativeImageBackedTexture> nibt; | ||
|
||
public ImageWrapper(NativeImageBackedTexture nibt) { | ||
this.nibt = new SoftReference<>(nibt); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
ImageWrapper imageWrapper = (ImageWrapper) o; | ||
return Objects.equals(nibt, imageWrapper.nibt); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(nibt); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ImageWrapper{" + | ||
"nibt=" + nibt.get() + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
NativeImageBackedTexture nibt = this.nibt.get(); | ||
if (nibt != null) nibt.close(); | ||
this.nibt.clear(); | ||
} | ||
} |
5 changes: 0 additions & 5 deletions
5
src/main/java/com/github/kisaragieffective/signpictureported/MayThrowFunction.java
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
src/main/java/com/github/kisaragieffective/signpictureported/NativeImageFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.kisaragieffective.signpictureported; | ||
|
||
import net.minecraft.client.texture.NativeImage; | ||
import net.minecraft.client.texture.NativeImageBackedTexture; | ||
|
||
public class NativeImageFactory { | ||
public static final Box<NativeImageBackedTexture> errorImage = new Box<>(NativeImageFactory::getErrorImage); | ||
public static NativeImageBackedTexture getErrorImage() { | ||
final NativeImage nini = new NativeImage(128, 128, true); | ||
for (int xv = 0; xv < 128; xv++) { | ||
nini.setPixelColor(xv, 0, 0xFF_00_00_FF); | ||
nini.setPixelColor(xv, 127, 0xFF_00_00_FF); | ||
nini.setPixelColor(0, xv, 0xFF_00_00_FF); | ||
nini.setPixelColor(127, xv, 0xFF_00_00_FF); | ||
} | ||
return new NativeImageBackedTexture(nini); | ||
} | ||
|
||
public static final Box<NativeImageBackedTexture> loadingImage = new Box<>(NativeImageFactory::getLoadingImage); | ||
|
||
public static NativeImageBackedTexture getLoadingImage() { | ||
final NativeImage nini = new NativeImage(128, 128, true); | ||
final int gray = 0x80808080; | ||
for (int xv = 0; xv < 128; xv++) { | ||
nini.setPixelColor(xv, 0, gray); | ||
nini.setPixelColor(xv, 127, gray); | ||
nini.setPixelColor(0, xv, gray); | ||
nini.setPixelColor(127, xv, gray); | ||
} | ||
return new NativeImageBackedTexture(nini); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/main/java/com/github/kisaragieffective/signpictureported/OutsideCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.github.kisaragieffective.signpictureported; | ||
|
||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.texture.NativeImageBackedTexture; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.math.BlockPos; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
|
||
public class OutsideCache { | ||
private OutsideCache() { | ||
|
||
} | ||
|
||
public static final Map<BlockPos, ImageWrapper> cache = new ConcurrentHashMap<>(); | ||
public static final Map<BlockPos, ImageWrapper> flippedCache = new ConcurrentHashMap<>(); | ||
public static final Set<String> invalidURL = new HashSet<>(); | ||
|
||
private static final Map<BlockPos, Identifier> identifierMap = new ConcurrentHashMap<>(); | ||
private static final Map<BlockPos, Identifier> flippedIdentifierMap = new ConcurrentHashMap<>(); | ||
public static final Set<BlockPos> sbp = new HashSet<>(); | ||
|
||
public static ImageWrapper putOrCached(BlockPos pos, Supplier<? extends NativeImageBackedTexture> nibt) { | ||
return putImage(cache, pos, nibt); | ||
} | ||
|
||
public static ImageWrapper putFlippedOrCached(BlockPos pos, Supplier<? extends NativeImageBackedTexture> flipped) { | ||
return putImage(flippedCache, pos, flipped); | ||
} | ||
|
||
public static void put(BlockPos pos, NativeImageBackedTexture nibt, boolean flipped) { | ||
putNewIdentifier(pos, nibt); | ||
if (flipped) { | ||
flippedCache.put(pos, new ImageWrapper(nibt)); | ||
} else { | ||
cache.put(pos, new ImageWrapper(nibt)); | ||
} | ||
} | ||
|
||
public static Optional<ImageWrapper> get(BlockPos pos, boolean flipped) { | ||
if (flipped) { | ||
return flippedCache.containsKey(pos) ? Optional.of(flippedCache.get(pos)) : Optional.empty(); | ||
} else { | ||
return cache.containsKey(pos) ? Optional.of(cache.get(pos)) : Optional.empty(); | ||
} | ||
} | ||
|
||
public static Set<? extends BlockPos> locations() { | ||
return identifierMap.keySet(); | ||
} | ||
|
||
public static void drop(BlockPos pos) { | ||
if (flippedCache.containsKey(pos)) { | ||
final ImageWrapper nibt1 = flippedCache.get(pos); | ||
nibt1.close(); | ||
} | ||
if (cache.containsKey(pos)) { | ||
final ImageWrapper nibt = flippedCache.get(pos); | ||
nibt.close(); | ||
} | ||
identifierMap.remove(pos); | ||
flippedIdentifierMap.remove(pos); | ||
} | ||
|
||
private static ImageWrapper putImage( | ||
Map<BlockPos, ImageWrapper> m, | ||
BlockPos pos, | ||
Supplier<? extends NativeImageBackedTexture> v | ||
) { | ||
return m.computeIfAbsent(pos, ig -> { | ||
SignPicturePorted.LOGGER.info("cache(" + pos + "): no hit, creating"); | ||
return new ImageWrapper(v.get()); | ||
}); | ||
} | ||
|
||
public static Identifier putNewIdentifierOrCached(BlockPos pos, NativeImageBackedTexture nibt) { | ||
return identifierMap.computeIfAbsent(pos, p -> computeNewIdentifier(p, nibt)); | ||
} | ||
|
||
public static Identifier putNewIdentifier(BlockPos pos, NativeImageBackedTexture nibt) { | ||
return identifierMap.put(pos, computeNewIdentifier(pos, nibt)); | ||
} | ||
|
||
public static Identifier computeNewIdentifier(BlockPos pos, NativeImageBackedTexture nibt) { | ||
Identifier newIdentifier = MinecraftClient.getInstance().getTextureManager().registerDynamicTexture("sgpc_reloaded", nibt); | ||
SignPicturePorted.LOGGER.info("New identifier payed out (for " + pos + "): " + newIdentifier); | ||
return newIdentifier; | ||
} | ||
|
||
public static Identifier putFlippedNewIdentifierOrCached(BlockPos pos, NativeImageBackedTexture nibt) { | ||
return flippedIdentifierMap.computeIfAbsent(pos, p -> computeNewIdentifier(p, nibt)); | ||
} | ||
|
||
public static Identifier putFlippedNewIdentifier(BlockPos pos, NativeImageBackedTexture nibt) { | ||
return flippedIdentifierMap.put(pos, computeNewIdentifier(pos, nibt)); | ||
} | ||
|
||
public static void invalidate() { | ||
OutsideCache.cache.values() | ||
.forEach(x -> x.close()); | ||
OutsideCache.cache.clear(); | ||
OutsideCache.flippedCache.values() | ||
.forEach(x -> x.close()); | ||
OutsideCache.identifierMap.clear(); | ||
OutsideCache.invalidURL.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.