2018-10-23 11:49:35 +02:00
|
|
|
package de.ellpeck.naturesaura.items;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2018-11-11 22:58:58 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2020-01-29 01:34:58 +01:00
|
|
|
import de.ellpeck.naturesaura.data.ItemModelGenerator;
|
2020-01-29 20:22:10 +01:00
|
|
|
import de.ellpeck.naturesaura.misc.ColoredBlockHelper;
|
2018-10-23 11:49:35 +02:00
|
|
|
import de.ellpeck.naturesaura.reg.IColorProvidingItem;
|
2020-01-29 01:34:58 +01:00
|
|
|
import de.ellpeck.naturesaura.reg.ICustomItemModel;
|
2020-01-29 20:22:10 +01:00
|
|
|
import net.minecraft.block.Block;
|
2018-10-23 11:49:35 +02:00
|
|
|
import net.minecraft.client.renderer.color.IItemColor;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.DyeColor;
|
2018-10-23 11:49:35 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.item.ItemUseContext;
|
2018-10-23 11:49:35 +02:00
|
|
|
import net.minecraft.util.*;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2018-10-23 11:49:35 +02:00
|
|
|
|
2020-01-29 20:22:10 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-01-29 01:34:58 +01:00
|
|
|
public class ItemColorChanger extends ItemImpl implements IColorProvidingItem, ICustomItemModel {
|
2018-10-23 11:49:35 +02:00
|
|
|
|
2020-01-26 01:41:49 +01:00
|
|
|
public ItemColorChanger() {
|
2020-02-07 23:50:16 +01:00
|
|
|
super("color_changer", new Properties().maxStackSize(1));
|
2018-10-23 11:49:35 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
private static boolean changeOrCopyColor(PlayerEntity player, ItemStack stack, World world, BlockPos pos, DyeColor firstColor) {
|
2020-01-29 20:22:10 +01:00
|
|
|
Block block = world.getBlockState(pos).getBlock();
|
|
|
|
List<Block> blocks = ColoredBlockHelper.getBlocksContaining(block);
|
|
|
|
if (blocks == null)
|
|
|
|
return false;
|
|
|
|
DyeColor color = DyeColor.byId(blocks.indexOf(block));
|
|
|
|
if (firstColor == null || color == firstColor) {
|
|
|
|
DyeColor stored = getStoredColor(stack);
|
2020-09-22 03:17:02 +02:00
|
|
|
if (player.isSneaking()) {
|
2020-01-29 20:22:10 +01:00
|
|
|
if (stored != color) {
|
|
|
|
world.playSound(player, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
|
|
|
|
SoundEvents.ITEM_BUCKET_FILL, SoundCategory.PLAYERS, 0.65F, 1F);
|
|
|
|
if (!world.isRemote)
|
|
|
|
storeColor(stack, color);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (stored != null && stored != color) {
|
|
|
|
if (NaturesAuraAPI.instance().extractAuraFromPlayer(player, 1000, world.isRemote)) {
|
|
|
|
if (firstColor == null) {
|
2018-10-23 19:23:02 +02:00
|
|
|
world.playSound(player, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
|
2020-01-29 20:22:10 +01:00
|
|
|
SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.PLAYERS, 0.65F, 1F);
|
2018-10-23 11:49:35 +02:00
|
|
|
}
|
2020-01-29 20:22:10 +01:00
|
|
|
if (!world.isRemote) {
|
|
|
|
world.setBlockState(pos, blocks.get(stored.getId()).getDefaultState());
|
2018-10-23 11:49:35 +02:00
|
|
|
|
2020-01-29 20:22:10 +01:00
|
|
|
if (isFillMode(stack)) {
|
|
|
|
for (Direction off : Direction.values()) {
|
|
|
|
changeOrCopyColor(player, stack, world, pos.offset(off), color);
|
2018-10-23 11:49:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 20:22:10 +01:00
|
|
|
return true;
|
2018-10-23 11:49:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-22 15:08:38 +02:00
|
|
|
public static DyeColor getStoredColor(ItemStack stack) {
|
2019-11-04 19:08:49 +01:00
|
|
|
if (!stack.hasTag()) {
|
2018-10-23 11:49:35 +02:00
|
|
|
return null;
|
|
|
|
} else {
|
2019-11-04 19:08:49 +01:00
|
|
|
int color = stack.getTag().getInt("color");
|
|
|
|
return DyeColor.byId(color);
|
2018-10-23 11:49:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
private static void storeColor(ItemStack stack, DyeColor color) {
|
2019-11-04 19:08:49 +01:00
|
|
|
stack.getOrCreateTag().putInt("color", color.getId());
|
2018-10-23 11:49:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-22 15:08:38 +02:00
|
|
|
public static boolean isFillMode(ItemStack stack) {
|
2019-11-04 19:08:49 +01:00
|
|
|
if (!stack.hasTag()) {
|
2018-10-23 11:49:35 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
2019-11-04 19:08:49 +01:00
|
|
|
return stack.getTag().getBoolean("fill");
|
2018-10-23 11:49:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void setFillMode(ItemStack stack, boolean fill) {
|
2019-11-04 19:08:49 +01:00
|
|
|
stack.getOrCreateTag().putBoolean("fill", fill);
|
2018-10-23 11:49:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:22:30 +01:00
|
|
|
@Override
|
|
|
|
public ActionResultType onItemUse(ItemUseContext context) {
|
|
|
|
ItemStack stack = context.getPlayer().getHeldItem(context.getHand());
|
|
|
|
if (changeOrCopyColor(context.getPlayer(), stack, context.getWorld(), context.getPos(), null)) {
|
|
|
|
return ActionResultType.SUCCESS;
|
|
|
|
} else {
|
|
|
|
return ActionResultType.PASS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
|
|
|
|
ItemStack stack = playerIn.getHeldItem(handIn);
|
2020-09-22 03:17:02 +02:00
|
|
|
if (playerIn.isSneaking() && getStoredColor(stack) != null) {
|
2020-02-07 15:22:30 +01:00
|
|
|
worldIn.playSound(playerIn, playerIn.getPosX(), playerIn.getPosY(), playerIn.getPosZ(), SoundEvents.ITEM_BUCKET_FILL_LAVA, SoundCategory.PLAYERS, 0.65F, 1F);
|
|
|
|
if (!worldIn.isRemote) {
|
|
|
|
setFillMode(stack, !isFillMode(stack));
|
|
|
|
}
|
|
|
|
return new ActionResult<>(ActionResultType.SUCCESS, stack);
|
|
|
|
} else {
|
|
|
|
return new ActionResult<>(ActionResultType.PASS, stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:49:35 +02:00
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2018-10-23 11:49:35 +02:00
|
|
|
public IItemColor getItemColor() {
|
|
|
|
return (stack, tintIndex) -> {
|
|
|
|
if (tintIndex > 0) {
|
2019-10-20 22:30:49 +02:00
|
|
|
DyeColor color = getStoredColor(stack);
|
2018-10-23 11:49:35 +02:00
|
|
|
if (color != null) {
|
|
|
|
return color.getColorValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0xFFFFFF;
|
|
|
|
};
|
|
|
|
}
|
2020-01-29 01:34:58 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void generateCustomItemModel(ItemModelGenerator generator) {
|
|
|
|
// noop
|
|
|
|
}
|
2018-10-23 11:49:35 +02:00
|
|
|
}
|