added advanced model registry
This commit is contained in:
@@ -3,9 +3,13 @@ package eu.mhsl.minecraft.pixelpic;
|
||||
import eu.mhsl.minecraft.pixelpic.commands.PixelPicCommand;
|
||||
import eu.mhsl.minecraft.pixelpic.render.render.DefaultScreenRenderer;
|
||||
import eu.mhsl.minecraft.pixelpic.render.render.Renderer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class Main extends JavaPlugin {
|
||||
private static Main instance;
|
||||
private Renderer screenRenderer;
|
||||
@@ -14,61 +18,11 @@ public final class Main extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
Bukkit.getPluginCommand("pixelPic").setExecutor(new PixelPicCommand());
|
||||
//
|
||||
// Bukkit.getPluginCommand("test2").setExecutor((sender, command, label, args) -> {
|
||||
// if(!(sender instanceof Player player)) return false;
|
||||
// Bukkit.broadcast(Component.text("HI"));
|
||||
//
|
||||
// Resolution.Pixels pixels = Resolution.Pixels._256P;
|
||||
// Resolution.AspectRatio aspectRatio = Resolution.AspectRatio._1_1;
|
||||
// Resolution resolution = new Resolution(pixels, aspectRatio);
|
||||
// BufferedImage image = screenRenderer.render((Player) sender, resolution);
|
||||
// Bukkit.broadcast(Component.text(image.toString()));
|
||||
//
|
||||
// File file = new File(getDataFolder(), "Bild" + ".png");
|
||||
// try {
|
||||
// getDataFolder().mkdir();
|
||||
// ImageIO.write(image, "png", file);
|
||||
// } catch (Exception e) {
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// ItemStack map = new ItemStack(Material.FILLED_MAP, 1);
|
||||
// MapMeta meta = (MapMeta) map.getItemMeta();
|
||||
// MapView mapView = Bukkit.createMap(Bukkit.getWorlds().getFirst());
|
||||
// mapView.addRenderer(new ImageMapRenderer(image, 0, 0));
|
||||
// meta.setMapView(mapView);
|
||||
// map.setItemMeta(meta);
|
||||
// player.getInventory().addItem(map);
|
||||
//
|
||||
// ItemStack map2 = new ItemStack(Material.FILLED_MAP, 1);
|
||||
// MapMeta meta1 = (MapMeta) map2.getItemMeta();
|
||||
// MapView mapView4 = Bukkit.createMap(Bukkit.getWorlds().getFirst());
|
||||
// mapView4.addRenderer(new ImageMapRenderer(image, 1, 0));
|
||||
// meta1.setMapView(mapView4);
|
||||
// map2.setItemMeta(meta1);
|
||||
// player.getInventory().addItem(map2);
|
||||
//
|
||||
// ItemStack map3 = new ItemStack(Material.FILLED_MAP, 1);
|
||||
// MapMeta meta2 = (MapMeta) map3.getItemMeta();
|
||||
// MapView mapView3 = Bukkit.createMap(Bukkit.getWorlds().getFirst());
|
||||
// mapView3.addRenderer(new ImageMapRenderer(image, 0, 1));
|
||||
// meta2.setMapView(mapView3);
|
||||
// map3.setItemMeta(meta2);
|
||||
// player.getInventory().addItem(map3);
|
||||
//
|
||||
// ItemStack map4 = new ItemStack(Material.FILLED_MAP, 1);
|
||||
// MapMeta meta3 = (MapMeta) map4.getItemMeta();
|
||||
// MapView mapView2 = Bukkit.createMap(Bukkit.getWorlds().getFirst());
|
||||
// mapView2.addRenderer(new ImageMapRenderer(image, 1, 1));
|
||||
// meta3.setMapView(mapView2);
|
||||
// map4.setItemMeta(meta3);
|
||||
// player.getInventory().addItem(map4);
|
||||
//
|
||||
// player.updateInventory();
|
||||
//
|
||||
// return true;
|
||||
// });
|
||||
|
||||
Bukkit.getPluginCommand("test").setExecutor((sender, command, label, args) -> {
|
||||
Bukkit.broadcast(Component.text(Material.STONE.getBlockTranslationKey().replace("block.minecraft.", "")));
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package eu.mhsl.minecraft.pixelpic.render.raytrace;
|
||||
|
||||
import eu.mhsl.minecraft.pixelpic.render.model.Model;
|
||||
import eu.mhsl.minecraft.pixelpic.render.registry.DefaultModelRegistry;
|
||||
import eu.mhsl.minecraft.pixelpic.render.registry.AdvancedModelRegistry;
|
||||
import eu.mhsl.minecraft.pixelpic.render.registry.ModelRegistry;
|
||||
import eu.mhsl.minecraft.pixelpic.render.util.BlockRaytracer;
|
||||
import eu.mhsl.minecraft.pixelpic.render.util.Intersection;
|
||||
@@ -29,7 +29,7 @@ public class DefaultRaytracer implements Raytracer {
|
||||
this.maxDistance = maxDistance;
|
||||
this.reflectionDepth = reflectionDepth;
|
||||
|
||||
this.textureRegistry = new DefaultModelRegistry();
|
||||
this.textureRegistry = new AdvancedModelRegistry();
|
||||
this.textureRegistry.initialize();
|
||||
|
||||
this.reflectedBlock = null;
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package eu.mhsl.minecraft.pixelpic.render.registry;
|
||||
|
||||
import eu.mhsl.minecraft.pixelpic.render.model.AbstractModel;
|
||||
import eu.mhsl.minecraft.pixelpic.render.model.Model;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
|
||||
import static eu.mhsl.minecraft.pixelpic.render.registry.DefaultModelRegistry.TEXTURE_SIZE;
|
||||
|
||||
public class AdvancedModelRegistry implements ModelRegistry {
|
||||
private final Map<Material, Map<BlockData, Model>> modelMap = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
// registerModel(Material.STONE, AbstractModel.Builder.createSimple(getTextureArray(Material.STONE.getBlockTranslationKey().replace("block.minecraft.", ""))).build());
|
||||
for (Material currentMaterial : Material.values()) {
|
||||
if(!currentMaterial.isBlock()) continue;
|
||||
List<String> blockTextures = getBlockTextures(currentMaterial);
|
||||
for (String blockTexture : blockTextures) {
|
||||
try {
|
||||
registerModel(currentMaterial, AbstractModel.Builder.createSimple(getTextureArray(blockTexture)).build());
|
||||
} catch (Exception ignored) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Model getModel(Block block) {
|
||||
return ModelRegistry.super.getModel(block);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Model getModel(Material material, BlockData blockData) {
|
||||
return modelMap.computeIfAbsent(material, key -> new HashMap<>()).getOrDefault(blockData,
|
||||
blockData == null ? getDefaultModel()
|
||||
: modelMap.get(material).getOrDefault(null, getDefaultModel()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Model getDefaultModel() {
|
||||
return AbstractModel.Builder.createStatic(Color.PURPLE.asRGB()).build();
|
||||
}
|
||||
|
||||
private void registerModel(Material material, Model blockModel) {
|
||||
modelMap.computeIfAbsent(material, key -> new HashMap<>())
|
||||
.put(null, blockModel);
|
||||
}
|
||||
|
||||
private List<String> getBlockTextures(Material material) {
|
||||
if(!material.isBlock()) return new ArrayList<>();
|
||||
String blockName = material.name().toLowerCase();
|
||||
try {
|
||||
if(!Arrays.deepEquals(getTextureArray(blockName), new int[0][0])) {
|
||||
return List.of(blockName);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String[] possibleVariants = {
|
||||
blockName + "_top",
|
||||
blockName + "_side",
|
||||
blockName + "_bottom",
|
||||
blockName + "_front",
|
||||
blockName + "_back",
|
||||
blockName + "_left",
|
||||
blockName + "_right"
|
||||
};
|
||||
List<String> results = new ArrayList<>();
|
||||
for (String variant : possibleVariants) {
|
||||
try {
|
||||
if(!Arrays.deepEquals(getTextureArray(variant), new int[0][0])) {
|
||||
results.add(variant);
|
||||
}
|
||||
} catch (Exception ignored) { }
|
||||
}
|
||||
return results;
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
private int[][] getTextureArray(String textureName) {
|
||||
int[][] texture = new int[TEXTURE_SIZE][TEXTURE_SIZE];
|
||||
BufferedImage img = null;
|
||||
URL url = this.getClass().getClassLoader().getResource(String.format("textures/block/%s.png", textureName));
|
||||
if (url == null) {
|
||||
throw new RuntimeException("Block Texture Resource not found.");
|
||||
}
|
||||
try (InputStream input = url.openConnection().getInputStream()) {
|
||||
img = ImageIO.read(input);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
for (int pixelY = 0; pixelY < TEXTURE_SIZE; pixelY++) {
|
||||
for (int pixelX = 0; pixelX < TEXTURE_SIZE; pixelX++) {
|
||||
texture[pixelY][pixelX] = img.getRGB(pixelX, pixelY);
|
||||
}
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user