Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@
"op": "replace"
}
]
},
{
"function": "minecraft:set_contents",
"entries": [
{
"type": "minecraft:dynamic",
"name": "minecraft:contents"
}
]
}
],
"name": "tutorial:demo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@
"op": "replace"
}
]
},
{
"function": "minecraft:set_contents",
"entries": [
{
"type": "minecraft:dynamic",
"name": "minecraft:contents"
}
]
}
],
"name": "tutorial:generator"
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/mcjty/blocks/DemoBlock.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.mcjty.blocks;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
Expand Down Expand Up @@ -34,6 +36,20 @@ public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new DemoBE(pos, state);
}

@Override
public void setPlacedBy(Level pLevel, BlockPos pPos, BlockState pState, @Nullable LivingEntity pPlacer, ItemStack pStack) {
super.setPlacedBy(pLevel, pPos, pState, pPlacer, pStack);
if (pStack.hasTag()) {
CompoundTag blockEntityTag = pStack.getTag().getCompound("BlockEntityTag");

if (!pLevel.isClientSide && !blockEntityTag.isEmpty()) {
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
if (blockEntity instanceof DemoBE demoBE) {
demoBE.load(blockEntityTag);
}
}
}
}
@Override
public void appendHoverText(ItemStack stack, @Nullable BlockGetter reader, List<Component> list, TooltipFlag flags) {
list.add(new TranslatableComponent("message.demo.tooltip"));
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/mcjty/blocks/GeneratorBE.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ private void sendOutPower() {
}
}

@Override
public void handleUpdateTag(CompoundTag tag) {
//No Op
// We do not want to call load with a tag that doesn't have the information to be loaded
}

@Override
public void load(CompoundTag tag) {
itemHandler.deserializeNBT(tag.getCompound("inv"));
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/com/mcjty/blocks/GeneratorBlock.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.mcjty.blocks;

import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
Expand Down Expand Up @@ -60,6 +64,20 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, Block
}
}

@Override
public void setPlacedBy(Level pLevel, BlockPos pPos, BlockState pState, @Nullable LivingEntity pPlacer, ItemStack pStack) {
super.setPlacedBy(pLevel, pPos, pState, pPlacer, pStack);
if (pStack.hasTag()) {
CompoundTag blockEntityTag = pStack.getTag().getCompound("BlockEntityTag");

if (!pLevel.isClientSide && !blockEntityTag.isEmpty()) {
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
if (blockEntity instanceof GeneratorBE generatorBE) {
generatorBE.load(blockEntityTag);
}
}
}
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(BlockStateProperties.FACING, BlockStateProperties.POWERED);
Expand All @@ -68,7 +86,9 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
return defaultBlockState().setValue(BlockStateProperties.FACING, context.getNearestLookingDirection().getOpposite());
return defaultBlockState()
.setValue(BlockStateProperties.FACING, context.getNearestLookingDirection().getOpposite())
.setValue(BlockStateProperties.POWERED,false);
}

@Override
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/mcjty/datagen/LootTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ protected LootTable.Builder createStandardTable(String name, Block block) {
.apply(CopyNbtFunction.copyData(ContextNbtProvider.BLOCK_ENTITY)
.copy("inv", "BlockEntityTag.inv", CopyNbtFunction.MergeStrategy.REPLACE)
.copy("energy", "BlockEntityTag.energy", CopyNbtFunction.MergeStrategy.REPLACE))
.apply(SetContainerContents.setContents()
.withEntry(DynamicLoot.dynamicEntry(new ResourceLocation("minecraft", "contents"))))
);
return LootTable.lootTable().withPool(builder);
}
Expand Down