Skip to content

Commit

Permalink
stuffs
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed Jul 11, 2024
1 parent 403620c commit 753da61
Show file tree
Hide file tree
Showing 23 changed files with 188 additions and 252 deletions.
3 changes: 0 additions & 3 deletions winterly-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ dependencies {
// Do NOT use other classes from fabric loader
modImplementation("net.fabricmc:fabric-loader:${rootProject.property("fabric_loader_version")}")
modCompileOnly("me.shedaniel.cloth:cloth-config-fabric:${rootProject.property("cloth_config")}") { exclude("net.fabricmc.fabric-api") }

// Remove the next line if you don't want to depend on the API
//modApi("dev.architectury:architectury:${rootProject.property("architectury_version")}")
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package ru.pinkgoosik.winterly.block;

import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.Registries;
import net.minecraft.locale.Language;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.TagKey;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
Expand All @@ -25,6 +36,7 @@
import ru.pinkgoosik.winterly.Winterly;
import ru.pinkgoosik.winterly.block.entity.GiftBoxBlockEntity;
import ru.pinkgoosik.winterly.block.entity.GiftBoxBlockEntityData;
import ru.pinkgoosik.winterly.data.GiftBoxData;

import java.util.List;

Expand All @@ -47,43 +59,49 @@ public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, Co
return GiftBoxBlock.SHAPE;
}

// @Override
// public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
// super.setPlacedBy(world, pos, state, placer, stack);
//
// if(world.getBlockEntity(pos) instanceof GiftBoxBlockEntityData entity) {
// entity.load(stack.getOrCreateTag());
// }
// }

// @Override
// public BlockState playerWillDestroy(Level world, BlockPos pos, BlockState state, Player player) {
// super.playerWillDestroy(world, pos, state, player);
//
// if(!world.isClientSide && world.getBlockEntity(pos) instanceof GiftBoxBlockEntityData entity) {
// ItemStack stack = player.getItemInHand(InteractionHand.MAIN_HAND);
// if(!EnchantmentHelper.getEnchantments(stack).containsKey(Enchantments.SILK_TOUCH)) {
// if(entity.getStacks().isEmpty()) {
// popResource(world, pos, new ItemStack(this.asItem()));
// }
// else {
// entity.getStacks().forEach(st -> popResource(world, pos, st));
// }
// }
// else {
// ItemStack box = new ItemStack(this);
//
// if(!entity.getStacks().isEmpty()) {
// var nbt = new CompoundTag();
// entity.saveAdditional(nbt);
// box.setTag(nbt);
// }
// popResource(world, pos, box);
// }
// }
// return state;
// }
@Override
public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
super.setPlacedBy(world, pos, state, placer, stack);

if(world.getBlockEntity(pos) instanceof GiftBoxBlockEntityData entity && placer != null) {
if(stack.getComponents().has(DataComponents.CUSTOM_DATA)) {
entity.loadAdditional(stack.getComponents().get(DataComponents.CUSTOM_DATA).copyTag(), placer.registryAccess());
}

}
}

@Override
public BlockState playerWillDestroy(Level world, BlockPos pos, BlockState state, Player player) {
super.playerWillDestroy(world, pos, state, player);

if(!world.isClientSide && world.getBlockEntity(pos) instanceof GiftBoxBlockEntityData entity) {
ItemStack stack = player.getItemInHand(InteractionHand.MAIN_HAND);


if(!EnchantmentHelper.hasTag(stack, TagKey.create(Registries.ENCHANTMENT, Winterly.id("gift_box_pickup")))) {
if(entity.getStacks().isEmpty()) {
popResource(world, pos, new ItemStack(this.asItem()));
}
else {
entity.getStacks().forEach(st -> popResource(world, pos, st));
}
}
else {
ItemStack box = new ItemStack(this);

if(!entity.getStacks().isEmpty()) {
var nbt = new CompoundTag();
entity.saveAdditional(nbt, player.registryAccess());
box.applyComponents(DataComponentMap.builder().set(DataComponents.CUSTOM_DATA, CustomData.of(nbt)).build());
}
popResource(world, pos, box);
}


}
return state;
}

@Override
protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
Expand All @@ -96,6 +114,7 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev
}
}
entity.getStacks().add(stack.copy());
((BlockEntity)entity).setChanged();
player.setItemInHand(hand, ItemStack.EMPTY);
level.playSound(null, pos, SoundEvents.BUNDLE_INSERT, SoundSource.BLOCKS, 1, 1);
return ItemInteractionResult.SUCCESS;
Expand All @@ -105,29 +124,36 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev
}

@Override
public void appendHoverText(ItemStack stack, Item.TooltipContext context, List<Component> tooltipComponents, TooltipFlag tooltipFlag) {
super.appendHoverText(stack, context, tooltipComponents, tooltipFlag);

// if(stack.getOrCreateTag().contains("giftBoxData")) {
// var gift = GiftBoxData.fromNbt(stack.getOrCreateTag().getCompound("giftBoxData"));
//
// gift.stacks.forEach(st -> {
// String name = Language.getInstance().getOrDefault(st.getDescriptionId());
// tooltip.add(Component.nullToEmpty("- " + name + " x" + st.getCount()).toFlatList(Style.EMPTY.withColor(ChatFormatting.GRAY)).get(0));
// });
// }
// else {
// Language lang = Language.getInstance();
// String key = "description.winterly.gift_box.";
//
// for(int i = 0; i <= 32; i++) {
// if(lang.has(key + i)) {
// tooltip.add(Component.translatable(key + i).toFlatList(Style.EMPTY.withColor(ChatFormatting.GRAY)).get(0));
// }
// if(!lang.has(key + (i + 1))) {
// break;
// }
// }
// }
public void appendHoverText(ItemStack stack, Item.TooltipContext context, List<Component> tooltip, TooltipFlag tooltipFlag) {
super.appendHoverText(stack, context, tooltip, tooltipFlag);


if(stack.getComponents().has(DataComponents.CUSTOM_DATA)) {
var tag = stack.getComponents().get(DataComponents.CUSTOM_DATA).copyTag();

if(tag.contains("giftBoxData")) {
var gift = GiftBoxData.fromNbt(tag.getCompound("giftBoxData"), context.registries());

gift.stacks.forEach(st -> {
String name = Language.getInstance().getOrDefault(st.getDescriptionId());
tooltip.add(Component.nullToEmpty("- " + name + " x" + st.getCount()).toFlatList(Style.EMPTY.withColor(ChatFormatting.GRAY)).get(0));
});
return;
}

}

Language lang = Language.getInstance();
String key = "description.winterly.gift_box.";

for(int i = 0; i <= 32; i++) {
if(lang.has(key + i)) {
tooltip.add(Component.translatable(key + i).toFlatList(Style.EMPTY.withColor(ChatFormatting.GRAY)).get(0));
}
if(!lang.has(key + (i + 1))) {
break;
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public ArrayList<ItemStack> getStacks() {
@Override
public void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.put("giftBoxData", GiftBoxData.toNbt(this));
tag.put("giftBoxData", GiftBoxData.toNbt(this, registries));
}

@Override
public void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
var data = GiftBoxData.fromNbt(tag.getCompound("giftBoxData"));
var data = GiftBoxData.fromNbt(tag.getCompound("giftBoxData"), registries);
this.stacks = data.stacks;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,38 @@
package ru.pinkgoosik.winterly.data;

import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.core.HolderLookup;

import java.util.ArrayList;
import net.minecraft.core.registries.BuiltInRegistries;
import java.util.Optional;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import ru.pinkgoosik.winterly.block.entity.GiftBoxBlockEntityData;

public class GiftBoxData {
public ArrayList<ItemStack> stacks = new ArrayList<>();

public static GiftBoxData fromNbt(CompoundTag nbt) {
public static GiftBoxData fromNbt(CompoundTag nbt, HolderLookup.Provider registries) {
GiftBoxData gift = new GiftBoxData();

int size = nbt.getInt("size");
for(int i = 0; i < size; i++) {
CompoundTag entryNbt = nbt.getCompound(String.valueOf(i));

var item = BuiltInRegistries.ITEM.getOptional(ResourceLocation.parse(entryNbt.getString("item")));
if(item.isPresent()) {
ItemStack temp = new ItemStack(item.get(), entryNbt.getInt("count"));

if(entryNbt.contains("nbt")) {
temp.applyComponents(DataComponentMap.builder().set(DataComponents.CUSTOM_DATA, CustomData.of((CompoundTag) entryNbt.get("nbt"))).build());
}

gift.stacks.add(temp);
}
Optional<ItemStack> stack = ItemStack.parse(registries, entryNbt.getCompound("itemstack"));
stack.ifPresent(itemStack -> gift.stacks.add(itemStack));
}

return gift;
}

public static CompoundTag toNbt(GiftBoxBlockEntityData entity) {
public static CompoundTag toNbt(GiftBoxBlockEntityData entity, HolderLookup.Provider registries) {
CompoundTag nbt = new CompoundTag();
nbt.putInt("size", entity.getStacks().size());

for(int i = 0; i < entity.getStacks().size(); i++) {
ItemStack stack = entity.getStacks().get(i);
CompoundTag entryNbt = new CompoundTag();
entryNbt.putString("item", BuiltInRegistries.ITEM.getKey(stack.getItem()).toString());

if(stack.getComponents().has(DataComponents.CUSTOM_DATA)) {
entryNbt.put("nbt", stack.getComponents().get(DataComponents.CUSTOM_DATA).copyTag());
}

entryNbt.putInt("count", stack.getCount());
entryNbt.put("itemstack", stack.save(registries));

nbt.put(Integer.toString(i), entryNbt);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,26 @@
package ru.pinkgoosik.winterly.registry;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import ru.pinkgoosik.winterly.Winterly;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.material.PushReaction;
import ru.pinkgoosik.winterly.Winterly;
import ru.pinkgoosik.winterly.block.*;
import ru.pinkgoosik.winterly.block.base.BasePaneBlock;
import ru.pinkgoosik.winterly.block.base.BaseStairsBlock;
import ru.pinkgoosik.winterly.block.base.BaseTransparentBlock;

import static net.minecraft.world.level.block.Blocks.*;
import static ru.pinkgoosik.winterly.registry.CommonWinterlyBlocks.*;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

@SuppressWarnings("unused")
public abstract class CommonWinterlyBlocks {
// public static final Map<ResourceLocation, Supplier<BlockItem>> ITEMS = new LinkedHashMap<>();
public static final Map<ResourceLocation, Supplier<Block>> BLOCKS = new LinkedHashMap<>();

// public static final Block RED_SOCK = add("red_sock", new SockBlock(copyOf(CANDLE).sounds(BlockSoundGroup.WOOL)));
// public static final Block GREEN_SOCK = add("green_sock", new SockBlock(copyOf(CANDLE).sounds(BlockSoundGroup.WOOL)));
// public static final Block BLUE_SOCK = add("blue_sock", new SockBlock(copyOf(CANDLE).sounds(BlockSoundGroup.WOOL)));
//
// public static Block ICICLE = add("icicle", new IcicleBlock(copyOf(ICE).pushReaction(PushReaction.DESTROY)));
// public static Block ICICLE_BLOCK = add("icicle_block", new BaseTransparentBlock(copyOf(PACKED_ICE).noOcclusion()));
// public static Block PACKED_ICICLE_BLOCK = add("packed_icicle_block", new BaseTransparentBlock(copyOf(PACKED_ICE).noOcclusion()));
// public static Block ICICLE_PANE = add("icicle_pane", new BasePaneBlock(copyOf(PACKED_ICE).noOcclusion()));
// public static Block ICICLE_BARS = add("icicle_bars", new BasePaneBlock(copyOf(ICE).noOcclusion()));
// public static Block CRYOMARBLE_BLOCK = add("cryomarble_block", new Block(copyOf(DIAMOND_BLOCK)));
// public static Block SNOWGUY = add("snowguy", new SnowguyBlock(copyOf(WHITE_WOOL).sound(SoundType.SNOW)));
// public static Block SNOWBALL_WALL = add("snowball_wall", new SnowballWallBlock(copyOf(WHITE_WOOL).sound(SoundType.SNOW).noOcclusion()));
// public static Block DENSE_SNOW = add("dense_snow", new Block(copyOf(WHITE_WOOL).sound(SoundType.SNOW)));
// public static Block DENSE_SNOW_STAIRS = add("dense_snow_stairs", new BaseStairsBlock(SNOW_BLOCK.defaultBlockState(), copyOf(WHITE_WOOL).sound(SoundType.SNOW)));
// public static Block DENSE_SNOW_SLAB = add("dense_snow_slab", new SlabBlock(copyOf(WHITE_WOOL).sound(SoundType.SNOW)));
// public static Block SNOW_BRICKS = add("snow_bricks", new Block(copyOf(WHITE_WOOL).sound(SoundType.SNOW)));
// public static Block SNOW_BRICK_STAIRS = add("snow_brick_stairs", new BaseStairsBlock(SNOW_BLOCK.defaultBlockState(), copyOf(WHITE_WOOL).sound(SoundType.SNOW)));
// public static Block SNOW_BRICK_SLAB = add("snow_brick_slab", new SlabBlock(copyOf(WHITE_WOOL).sound(SoundType.SNOW)));
// public static Block FROZEN_GRASS;
// public static Block FROZEN_FLOWER;
// public static Block RAW_CRYOMARBLE_SHARD = add("raw_cryomarble_shard", new IcicleBlock(copyOf(WHITE_WOOL).sound(SoundType.GLASS).lightLevel(state -> 12)));
// public static Block RED_GIFT_BOX = add("red_gift_box", new GiftBoxBlock(copyOf(RED_WOOL).pushReaction(PushReaction.DESTROY)));
// public static Block ORANGE_GIFT_BOX = add("orange_gift_box", new GiftBoxBlock(copyOf(RED_GIFT_BOX)));
// public static Block YELLOW_GIFT_BOX = add("yellow_gift_box", new GiftBoxBlock(copyOf(RED_GIFT_BOX)));
// public static Block GREEN_GIFT_BOX = add("green_gift_box", new GiftBoxBlock(copyOf(RED_GIFT_BOX)));
// public static Block CYAN_GIFT_BOX = add("cyan_gift_box", new GiftBoxBlock(copyOf(RED_GIFT_BOX)));
// public static Block BLUE_GIFT_BOX = add("blue_gift_box", new GiftBoxBlock(copyOf(RED_GIFT_BOX)));
// public static Block PURPLE_GIFT_BOX = add("purple_gift_box", new GiftBoxBlock(copyOf(RED_GIFT_BOX)));
// public static Block BLACK_GIFT_BOX = add("black_gift_box", new GiftBoxBlock(copyOf(RED_GIFT_BOX)));
// public static Block WHITE_GIFT_BOX = add("white_gift_box", new GiftBoxBlock(copyOf(RED_GIFT_BOX)));
// public static Block GARLAND_LIGHTS = add("garland_lights", new GarlandLightsBlock(copyOf(GREEN_WOOL).pushReaction(PushReaction.DESTROY).noCollission().sound(SoundType.CANDLE)));
// public static Block RAINY_GARLAND_LIGHTS = add("rainy_garland_lights", new GarlandLightsBlock(copyOf(WHITE_WOOL).pushReaction(PushReaction.DESTROY).noCollission().sound(SoundType.CANDLE)));

public static void init() {
add("icicle", () -> new IcicleBlock(copyOf(ICE).pushReaction(PushReaction.DESTROY)));
add("icicle_block", () -> new BaseTransparentBlock(copyOf(PACKED_ICE).noOcclusion()));
Expand Down Expand Up @@ -100,20 +54,6 @@ public static void add(String name, Supplier<Block> block) {
BLOCKS.put(Winterly.id(name), block);
}

// public static <T extends Block> T addBlockItem(String name, T block, BlockItem item) {
// addBlock(name, block);
// if (item != null) {
// item.registerBlocks(Item.BY_BLOCK, item);
// ITEMS.put(Winterly.id(name), item);
// }
// return block;
// }
//
// public static <T extends Block> T addBlock(String name, T block) {
// BLOCKS.put(Winterly.id(name), block);
// return block;
// }

public static BlockBehaviour.Properties copyOf(Block block) {
return BlockBehaviour.Properties.ofFullCopy(block);
}
Expand Down
Loading

0 comments on commit 753da61

Please sign in to comment.