Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions XercaPaintMod/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.8-SNAPSHOT'
id 'fabric-loom' version '1.12-SNAPSHOT'
id 'maven-publish'
}

Expand Down Expand Up @@ -33,10 +33,7 @@ dependencies {
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings loom.layered() {
officialMojangMappings()
parchment("org.parchmentmc.data:parchment-1.21.3:2024.12.07@zip")
}
mappings loom.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
Expand Down
8 changes: 4 additions & 4 deletions XercaPaintMod/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.21.3
loader_version=0.16.10
minecraft_version=1.21.10
loader_version=0.17.3

# Fabric API
fabric_version=0.114.0+1.21.3
fabric_version=0.137.0+1.21.10

# Mod Properties
mod_version=1.21.3-1.0.0
mod_version=1.21.10-smponline-1.0.0
maven_group=xerca
archives_base_name=xercapaint
2 changes: 1 addition & 1 deletion XercaPaintMod/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
86 changes: 38 additions & 48 deletions XercaPaintMod/src/main/java/xerca/xercapaint/CommandExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,11 @@
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import xerca.xercapaint.item.ItemCanvas;
import xerca.xercapaint.item.Items;
import xerca.xercapaint.packets.ExportPaintingPacket;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

public class CommandExport {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Expand Down Expand Up @@ -47,44 +37,44 @@ private static int paintExport(CommandSourceStack stack, String name){
}

public static boolean doExport(Player player, String name){
String dir = "paintings";
String filename = name + ".paint";
String filepath = dir + "/" + filename;
File directory = new File(dir);
if (!directory.exists()){
directory.mkdir();
}

for(ItemStack s : player.getHandSlots()){
if(s.getItem() instanceof ItemCanvas){
List<Integer> pixels = s.get(Items.CANVAS_PIXELS);
String canvasId = s.get(Items.CANVAS_ID);
if(pixels != null && canvasId != null){
try {
int version = s.getOrDefault(Items.CANVAS_VERSION, 1);
int generation = s.getOrDefault(Items.CANVAS_GENERATION, 0);
String title = s.get(Items.CANVAS_TITLE);
String author = s.get(Items.CANVAS_AUTHOR);

CompoundTag tag = new CompoundTag();

tag.putIntArray("pixels", pixels);
tag.putString("name", canvasId);
tag.putInt("v", version);
tag.putInt("generation", generation);
tag.putByte("ct", (byte)((ItemCanvas) s.getItem()).getCanvasType().ordinal());
if (title != null && author != null) {
tag.putString("title", title);
tag.putString("author", author);
}
NbtIo.write(tag, Path.of(filepath));
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// String dir = "paintings";
// String filename = name + ".paint";
// String filepath = dir + "/" + filename;
// File directory = new File(dir);
// if (!directory.exists()){
// directory.mkdir();
// }
//
// for(ItemStack s : player.getHandSlots()){
// if(s.getItem() instanceof ItemCanvas){
// List<Integer> pixels = s.get(Items.CANVAS_PIXELS);
// String canvasId = s.get(Items.CANVAS_ID);
// if(pixels != null && canvasId != null){
// try {
// int version = s.getOrDefault(Items.CANVAS_VERSION, 1);
// int generation = s.getOrDefault(Items.CANVAS_GENERATION, 0);
// String title = s.get(Items.CANVAS_TITLE);
// String author = s.get(Items.CANVAS_AUTHOR);
//
// CompoundTag tag = new CompoundTag();
//
// tag.putIntArray("pixels", pixels);
// tag.putString("name", canvasId);
// tag.putInt("v", version);
// tag.putInt("generation", generation);
// tag.putByte("ct", (byte)((ItemCanvas) s.getItem()).getCanvasType().ordinal());
// if (title != null && author != null) {
// tag.putString("title", title);
// tag.putString("author", author);
// }
// NbtIo.write(tag, Path.of(filepath));
// return true;
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// }
return false;
}
}
197 changes: 96 additions & 101 deletions XercaPaintMod/src/main/java/xerca/xercapaint/CommandImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import xerca.xercapaint.item.ItemCanvas;
import xerca.xercapaint.item.ItemPalette;
import xerca.xercapaint.item.Items;
import xerca.xercapaint.packets.ImportPaintingPacket;

import java.util.Arrays;

public class CommandImport {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Expand All @@ -44,101 +38,102 @@ private static int paintImport(CommandSourceStack stack, String name){
}

public static void doImport(CompoundTag tag, ServerPlayer player){
player.sendSystemMessage(Component.literal("not supported").withStyle(ChatFormatting.GREEN));
// Sanitizing
if (!tag.contains("name", 8)) {
player.sendSystemMessage(Component.translatable("xercapaint.import.fail.5").withStyle(ChatFormatting.RED));
Mod.LOGGER.warn("Broken paint file");
return;
}
String canvasId = tag.getString("name");
if (!canvasId.matches("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}_[0-9]+$")) {
player.sendSystemMessage(Component.translatable("xercapaint.import.fail.5").withStyle(ChatFormatting.RED));
Mod.LOGGER.warn("Broken paint file");
return;
}
if ((tag.contains("author", 8) && !tag.contains("title", 8)) ||
(!tag.contains("author", 8) && tag.contains("title", 8))) {
player.sendSystemMessage(Component.translatable("xercapaint.import.fail.5").withStyle(ChatFormatting.RED));
Mod.LOGGER.warn("Broken paint file");
return;
}
if (tag.contains("title", 8) && tag.getString("title").length() > 16) {
tag.putString("title", tag.getString("title").substring(0, 16));
}
if (tag.contains("author", 8) && tag.getString("author").length() > 16) {
tag.putString("author", tag.getString("author").substring(0, 16));
}
if (!tag.contains("v", 3)) {
tag.putInt("v", 1);
}

byte canvasType = tag.getByte("ct");
tag.remove("ct");
if(tag.getInt("generation") > 0){
tag.putInt("generation", tag.getInt("generation") + 1);
}

ItemStack itemStack;
boolean doAddItem = false;
if(player.isCreative()){
CanvasType type = CanvasType.fromByte(canvasType);
if (type == null) {
Mod.LOGGER.error("Invalid canvas type");
return;
}
switch (type){
case SMALL -> itemStack = new ItemStack(Items.ITEM_CANVAS);
case LONG -> itemStack = new ItemStack(Items.ITEM_CANVAS_LONG);
case TALL -> itemStack = new ItemStack(Items.ITEM_CANVAS_TALL);
case LARGE -> itemStack = new ItemStack(Items.ITEM_CANVAS_LARGE);
default -> {
Mod.LOGGER.error("Unknown canvas type");
return;
}
}
doAddItem = true;
}
else {
ItemStack mainhand = player.getMainHandItem();
ItemStack offhand = player.getOffhandItem();

if(!(mainhand.getItem() instanceof ItemCanvas) || (mainhand.get(Items.CANVAS_PIXELS) != null || mainhand.get(Items.CANVAS_ID) != null)){
player.sendSystemMessage(Component.translatable("xercapaint.import.fail.1").withStyle(ChatFormatting.RED));
return;
}
if(((ItemCanvas)mainhand.getItem()).getCanvasType() != CanvasType.fromByte(canvasType)){
Component typeName = Items.ITEM_CANVAS.getName(ItemStack.EMPTY);
CanvasType type = CanvasType.fromByte(canvasType);
if (type == null) {
return;
}
switch (type){
case LONG -> typeName = Items.ITEM_CANVAS_LONG.getName(ItemStack.EMPTY);
case TALL -> typeName = Items.ITEM_CANVAS_TALL.getName(ItemStack.EMPTY);
case LARGE -> typeName = Items.ITEM_CANVAS_LARGE.getName(ItemStack.EMPTY);
}
player.sendSystemMessage(Component.translatable("xercapaint.import.fail.2", typeName).withStyle(ChatFormatting.RED));
return;
}
if(!ItemPalette.isFull(offhand)){
player.sendSystemMessage(Component.translatable("xercapaint.import.fail.3").withStyle(ChatFormatting.RED));
return;
}
itemStack = mainhand;
}

itemStack.set(Items.CANVAS_VERSION, tag.getInt("v"));
itemStack.set(Items.CANVAS_ID, canvasId);
itemStack.set(Items.CANVAS_PIXELS, Arrays.stream(tag.getIntArray("pixels")).boxed().toList());
itemStack.set(Items.CANVAS_GENERATION, tag.getInt("generation"));
if (tag.contains("title", 8) && tag.contains("author", 8)) {
itemStack.set(Items.CANVAS_TITLE, tag.getString("title"));
itemStack.set(Items.CANVAS_AUTHOR, tag.getString("author"));
}
if (doAddItem) {
player.addItem(itemStack);
}

player.sendSystemMessage(Component.translatable("xercapaint.import.success").withStyle(ChatFormatting.GREEN));
// if (!tag.contains("name", 8)) {
// player.sendSystemMessage(Component.translatable("xercapaint.import.fail.5").withStyle(ChatFormatting.RED));
// Mod.LOGGER.warn("Broken paint file");
// return;
// }
// String canvasId = tag.getString("name");
// if (!canvasId.matches("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}_[0-9]+$")) {
// player.sendSystemMessage(Component.translatable("xercapaint.import.fail.5").withStyle(ChatFormatting.RED));
// Mod.LOGGER.warn("Broken paint file");
// return;
// }
// if ((tag.contains("author", 8) && !tag.contains("title", 8)) ||
// (!tag.contains("author", 8) && tag.contains("title", 8))) {
// player.sendSystemMessage(Component.translatable("xercapaint.import.fail.5").withStyle(ChatFormatting.RED));
// Mod.LOGGER.warn("Broken paint file");
// return;
// }
// if (tag.contains("title", 8) && tag.getString("title").length() > 16) {
// tag.putString("title", tag.getString("title").substring(0, 16));
// }
// if (tag.contains("author", 8) && tag.getString("author").length() > 16) {
// tag.putString("author", tag.getString("author").substring(0, 16));
// }
// if (!tag.contains("v", 3)) {
// tag.putInt("v", 1);
// }
//
// byte canvasType = tag.getByte("ct");
// tag.remove("ct");
// if(tag.getInt("generation") > 0){
// tag.putInt("generation", tag.getInt("generation") + 1);
// }
//
// ItemStack itemStack;
// boolean doAddItem = false;
// if(player.isCreative()){
// CanvasType type = CanvasType.fromByte(canvasType);
// if (type == null) {
// Mod.LOGGER.error("Invalid canvas type");
// return;
// }
// switch (type){
// case SMALL -> itemStack = new ItemStack(Items.ITEM_CANVAS);
// case LONG -> itemStack = new ItemStack(Items.ITEM_CANVAS_LONG);
// case TALL -> itemStack = new ItemStack(Items.ITEM_CANVAS_TALL);
// case LARGE -> itemStack = new ItemStack(Items.ITEM_CANVAS_LARGE);
// default -> {
// Mod.LOGGER.error("Unknown canvas type");
// return;
// }
// }
// doAddItem = true;
// }
// else {
// ItemStack mainhand = player.getMainHandItem();
// ItemStack offhand = player.getOffhandItem();
//
// if(!(mainhand.getItem() instanceof ItemCanvas) || (mainhand.get(Items.CANVAS_PIXELS) != null || mainhand.get(Items.CANVAS_ID) != null)){
// player.sendSystemMessage(Component.translatable("xercapaint.import.fail.1").withStyle(ChatFormatting.RED));
// return;
// }
// if(((ItemCanvas)mainhand.getItem()).getCanvasType() != CanvasType.fromByte(canvasType)){
// Component typeName = Items.ITEM_CANVAS.getName(ItemStack.EMPTY);
// CanvasType type = CanvasType.fromByte(canvasType);
// if (type == null) {
// return;
// }
// switch (type){
// case LONG -> typeName = Items.ITEM_CANVAS_LONG.getName(ItemStack.EMPTY);
// case TALL -> typeName = Items.ITEM_CANVAS_TALL.getName(ItemStack.EMPTY);
// case LARGE -> typeName = Items.ITEM_CANVAS_LARGE.getName(ItemStack.EMPTY);
// }
// player.sendSystemMessage(Component.translatable("xercapaint.import.fail.2", typeName).withStyle(ChatFormatting.RED));
// return;
// }
// if(!ItemPalette.isFull(offhand)){
// player.sendSystemMessage(Component.translatable("xercapaint.import.fail.3").withStyle(ChatFormatting.RED));
// return;
// }
// itemStack = mainhand;
// }
//
// itemStack.set(Items.CANVAS_VERSION, tag.getInt("v"));
// itemStack.set(Items.CANVAS_ID, canvasId);
// itemStack.set(Items.CANVAS_PIXELS, Arrays.stream(tag.getIntArray("pixels")).boxed().toList());
// itemStack.set(Items.CANVAS_GENERATION, tag.getInt("generation"));
// if (tag.contains("title", 8) && tag.contains("author", 8)) {
// itemStack.set(Items.CANVAS_TITLE, tag.getString("title"));
// itemStack.set(Items.CANVAS_AUTHOR, tag.getString("author"));
// }
// if (doAddItem) {
// player.addItem(itemStack);
// }
//
// player.sendSystemMessage(Component.translatable("xercapaint.import.success").withStyle(ChatFormatting.GREEN));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package xerca.xercapaint;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.network.FriendlyByteBuf;

public class PaletteUtil {
Expand Down Expand Up @@ -46,7 +45,7 @@ else if(ratio == 0.f){
int averageMaximum = (int)(Math.max(Math.max(a.r, a.g), a.b)*ratio) + (int)(Math.max(Math.max(b.r, b.g), b.b)*(1-ratio));

int maximumOfAverage = Math.max(Math.max(res.r, res.g), res.b);
int gainFactor = averageMaximum / maximumOfAverage;
int gainFactor = maximumOfAverage == 0 ? 0 : averageMaximum / maximumOfAverage;

res.r *= gainFactor;
res.g *= gainFactor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package xerca.xercapaint;

import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;

import static net.minecraft.core.registries.BuiltInRegistries.SOUND_EVENT;
Expand Down
Loading