Skip to content

Commit

Permalink
feat: ✨ Added support for tinting vanilla dyeable items in the decora…
Browse files Browse the repository at this point in the history
…tion table
  • Loading branch information
P3pp3rF1y committed Jan 14, 2025
1 parent 59dd9e2 commit 1161108
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.daemon=false

mod_id=sophisticatedstorage
mod_group_id=sophisticatedstorage
mod_version=1.2.0
mod_version=1.2.1
sonar_project_key=sophisticatedstorage:SophisticatedStorage
github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedStorage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.DyeableLeatherItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
Expand All @@ -25,6 +26,7 @@

import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class DecorationTableBlockEntity extends BlockEntity {
Expand All @@ -41,10 +43,10 @@ public class DecorationTableBlockEntity extends BlockEntity {
public static final Set<Item> STORAGES_WIHOUT_TOP_INNER_TRIM = Set.of(ModBlocks.BARREL_ITEM.get(), ModBlocks.COPPER_BARREL_ITEM.get(), ModBlocks.IRON_BARREL_ITEM.get(), ModBlocks.GOLD_BARREL_ITEM.get(), ModBlocks.DIAMOND_BARREL_ITEM.get(), ModBlocks.NETHERITE_BARREL_ITEM.get(),
ModBlocks.LIMITED_BARREL_1_ITEM.get(), ModBlocks.LIMITED_COPPER_BARREL_1_ITEM.get(), ModBlocks.LIMITED_IRON_BARREL_1_ITEM.get(), ModBlocks.LIMITED_GOLD_BARREL_1_ITEM.get(), ModBlocks.LIMITED_DIAMOND_BARREL_1_ITEM.get(), ModBlocks.LIMITED_NETHERITE_BARREL_1_ITEM.get());

private static final Map<Class<? extends Item>, IItemDecorator> ITEM_DECORATORS = new LinkedHashMap<>();
private static final Map<Predicate<ItemStack>, IItemDecorator> ITEM_DECORATORS = new LinkedHashMap<>();

public static void registerItemDecorator(Class<? extends Item> itemClass, IItemDecorator itemDecorator) {
ITEM_DECORATORS.put(itemClass, itemDecorator);
public static void registerItemDecorator(Predicate<ItemStack> itemMatcher, IItemDecorator itemDecorator) {
ITEM_DECORATORS.put(itemMatcher, itemDecorator);
}

private final Map<ResourceLocation, Integer> remainingParts = new HashMap<>();
Expand Down Expand Up @@ -102,7 +104,7 @@ protected void onContentsChanged(int slot) {

@Override
public boolean isItemValid(int slot, ItemStack stack) {
return ITEM_DECORATORS.keySet().stream().anyMatch(clazz -> clazz.isInstance(stack.getItem()));
return ITEM_DECORATORS.keySet().stream().anyMatch(predicate -> predicate.test(stack));
}
};

Expand All @@ -123,7 +125,7 @@ private void updateResult() {
}

private static Optional<IItemDecorator> getItemDecorator(ItemStack input) {
return ITEM_DECORATORS.entrySet().stream().filter(e -> e.getKey().isInstance(input.getItem())).findFirst().map(Map.Entry::getValue);
return ITEM_DECORATORS.entrySet().stream().filter(e -> e.getKey().test(input)).findFirst().map(Map.Entry::getValue);
}

private TintDecorationResult decorateItem(IItemDecorator itemDecorator, ItemStack input) {
Expand Down Expand Up @@ -539,8 +541,8 @@ public TintDecorationResult decorateWithTints(ItemStack input, int mainColorToSe
};

static {
ITEM_DECORATORS.put(StorageBlockItem.class, STORAGE_DECORATOR);
ITEM_DECORATORS.put(PaintbrushItem.class, new IItemDecorator() {
ITEM_DECORATORS.put(stack -> stack.getItem() instanceof StorageBlockItem, STORAGE_DECORATOR);
ITEM_DECORATORS.put(stack -> stack.getItem() instanceof PaintbrushItem, new IItemDecorator() {
@Override
public boolean consumesIngredientsOnCraft() {
return false;
Expand Down Expand Up @@ -599,5 +601,38 @@ public List<ItemStack> getPreviewStackInputs(ItemStack input, boolean materialsI
return List.of(new ItemStack(ModBlocks.LIMITED_BARREL_3_ITEM.get()), new ItemStack(ModBlocks.CHEST_ITEM.get()), new ItemStack(ModBlocks.SHULKER_BOX_ITEM.get()));
}
});
ITEM_DECORATORS.put(stack -> stack.getItem() instanceof DyeableLeatherItem, new IItemDecorator() {
@Override
public boolean supportsMaterials(ItemStack input) {
return false;
}

@Override
public boolean supportsTints(ItemStack input) {
return true;
}

@Override
public boolean supportsTopInnerTrim(ItemStack input) {
return false;
}

@Override
public ItemStack decorateWithMaterials(ItemStack input, Map<BarrelMaterial, ResourceLocation> materialsToApply) {
return ItemStack.EMPTY;
}

@Override
public TintDecorationResult decorateWithTints(ItemStack input, int mainColorToSet, int accentColorToSet) {
if (mainColorToSet == -1 || !(input.getItem() instanceof DyeableLeatherItem dyeableLeatherItem) || dyeableLeatherItem.getColor(input) == mainColorToSet) {
return TintDecorationResult.EMPTY;
}
int currentColor = dyeableLeatherItem.getColor(input);
ItemStack result = input.copyWithCount(1);
dyeableLeatherItem.setColor(result, mainColorToSet);

return new TintDecorationResult(result, DecorationHelper.getDyePartsNeeded(mainColorToSet, -1, currentColor, -1, 24, 0));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class SBCompat implements ICompat {
@Override
public void setup() {
DecorationTableBlockEntity.registerItemDecorator(BackpackItem.class, new DecorationTableBlockEntity.IItemDecorator() {
DecorationTableBlockEntity.registerItemDecorator(stack -> stack.getItem() instanceof BackpackItem, new DecorationTableBlockEntity.IItemDecorator() {
@Override
public boolean supportsMaterials(ItemStack input) {
return false;
Expand Down

0 comments on commit 1161108

Please sign in to comment.