Implement Color Saving and restoring on Dyable Items
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package me.hypherionmc.craterlib.api.rendering;
|
package me.hypherionmc.craterlib.api.rendering;
|
||||||
|
|
||||||
import net.minecraft.world.item.DyeColor;
|
import net.minecraft.world.item.DyeColor;
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper Interface for Dyable Items
|
* Helper Interface for Dyable Items
|
||||||
@@ -12,6 +13,6 @@ public interface ItemDyable {
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public DyeColor getColor();
|
public DyeColor getColor(ItemStack stack);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -28,7 +28,7 @@ public class ItemColorHandler implements ItemColor {
|
|||||||
*/
|
*/
|
||||||
private int getColorFromStack(ItemStack stack) {
|
private int getColorFromStack(ItemStack stack) {
|
||||||
if (stack.getItem() instanceof ItemDyable itemDyable) {
|
if (stack.getItem() instanceof ItemDyable itemDyable) {
|
||||||
return itemDyable.getColor().getMaterialColor().col;
|
return itemDyable.getColor(stack).getMaterialColor().col;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@@ -2,9 +2,21 @@ package me.hypherionmc.craterlib.common.item;
|
|||||||
|
|
||||||
import me.hypherionmc.craterlib.api.rendering.DyableBlock;
|
import me.hypherionmc.craterlib.api.rendering.DyableBlock;
|
||||||
import me.hypherionmc.craterlib.api.rendering.ItemDyable;
|
import me.hypherionmc.craterlib.api.rendering.ItemDyable;
|
||||||
|
import net.minecraft.nbt.CompoundTag;
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
import net.minecraft.util.StringUtil;
|
||||||
import net.minecraft.world.item.BlockItem;
|
import net.minecraft.world.item.BlockItem;
|
||||||
import net.minecraft.world.item.DyeColor;
|
import net.minecraft.world.item.DyeColor;
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.item.context.BlockPlaceContext;
|
||||||
import net.minecraft.world.level.block.Block;
|
import net.minecraft.world.level.block.Block;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
import net.minecraft.world.level.block.state.properties.EnumProperty;
|
||||||
|
import net.minecraft.world.level.block.state.properties.Property;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.commons.lang3.text.WordUtils;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base Item for Blocks that implement @link {DyableBlock}.
|
* Base Item for Blocks that implement @link {DyableBlock}.
|
||||||
@@ -21,10 +33,42 @@ public class BlockItemDyable extends BlockItem implements ItemDyable {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public DyeColor getColor() {
|
public DyeColor getColor(ItemStack stack) {
|
||||||
|
return this.getColorFromNBT(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Component getName(ItemStack stack) {
|
||||||
|
return Component.translatable(
|
||||||
|
this.getDescriptionId(),
|
||||||
|
WordUtils.capitalizeFully(getColorFromNBT(
|
||||||
|
stack).getName().replace("_", " ")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DyeColor getColorFromNBT(ItemStack stack) {
|
||||||
|
CompoundTag tag = stack.getOrCreateTag();
|
||||||
|
if (tag.contains("color")) {
|
||||||
|
return DyeColor.byName(tag.getString("color"), DyeColor.BLACK);
|
||||||
|
} else {
|
||||||
if (this.getBlock() instanceof DyableBlock dyableBlock) {
|
if (this.getBlock() instanceof DyableBlock dyableBlock) {
|
||||||
return dyableBlock.defaultDyeColor();
|
return dyableBlock.defaultDyeColor();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return DyeColor.BLACK;
|
return DyeColor.BLACK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
protected BlockState getPlacementState(BlockPlaceContext ctx) {
|
||||||
|
BlockState state = this.getBlock().getStateForPlacement(ctx);
|
||||||
|
if (state != null && state.getBlock() instanceof DyableBlock) {
|
||||||
|
Property property = state.getBlock().getStateDefinition().getProperty("color");
|
||||||
|
if (property != null) {
|
||||||
|
state = state.setValue(property, getColorFromNBT(ctx.getItemInHand()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return state != null && this.canPlace(ctx, state) ? state : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -48,7 +48,7 @@ public class DyableWaterBottle extends DyeItem implements ItemDyable {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public DyeColor getColor() {
|
public DyeColor getColor(ItemStack stack) {
|
||||||
return this.color;
|
return this.color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -32,7 +32,7 @@ public class DyableWaterBucket extends BucketItem implements ItemDyable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DyeColor getColor() {
|
public DyeColor getColor(ItemStack stack) {
|
||||||
return this.color;
|
return this.color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
# Project
|
# Project
|
||||||
version_major=1
|
version_major=1
|
||||||
version_minor=0
|
version_minor=0
|
||||||
version_patch=6d
|
version_patch=7d
|
||||||
group=me.hypherionmc.craterlib
|
group=me.hypherionmc.craterlib
|
||||||
|
|
||||||
# Common
|
# Common
|
||||||
|
Reference in New Issue
Block a user