Improve ctrl-pick behavior on glowing templates (rel: #1)

This commit is contained in:
quat1024 2023-07-31 00:21:20 -04:00
parent b20efb8479
commit 552ee09e95
19 changed files with 129 additions and 2 deletions

View File

@ -55,6 +55,10 @@ public class TemplateInteractionUtil {
return in;
}
public static @Nullable BlockState modifyPlacementState(@Nullable BlockState in, ItemPlacementContext ctx) {
return TemplateEntity.weirdNbtLightLevelStuff(in, ctx.getStack());
}
public static ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if(!(world.getBlockEntity(pos) instanceof TemplateEntity be)) return ActionResult.PASS;
if(!player.canModifyBlocks() || !world.canPlayerModifyAt(player, pos)) return ActionResult.PASS;

View File

@ -10,6 +10,7 @@ import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -38,6 +39,12 @@ public class TemplateBlock extends Block implements BlockEntityProvider {
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -12,6 +12,7 @@ import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -45,6 +46,12 @@ public class TemplateButtonBlock extends ButtonBlock implements BlockEntityProvi
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -12,6 +12,7 @@ import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -44,6 +45,12 @@ public class TemplateCandleBlock extends CandleBlock implements BlockEntityProvi
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -11,6 +11,7 @@ import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -40,6 +41,12 @@ public class TemplateCarpetBlock extends CarpetBlock implements BlockEntityProvi
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -7,6 +7,7 @@ import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -36,6 +37,12 @@ public class TemplateDoorBlock extends DoorBlock implements BlockEntityProvider
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -1,6 +1,7 @@
package io.github.cottonmc.templates.block;
import io.github.cottonmc.templates.Templates;
import io.github.cottonmc.templates.api.TemplateInteractionUtil;
import io.github.cottonmc.templates.api.ThemeableBlockEntity;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
@ -33,8 +34,8 @@ public class TemplateEntity extends BlockEntity implements ThemeableBlockEntity
protected static final int IS_SOLID_MASK = 0b00010000;
protected static final byte DEFAULT_BITFIELD = IS_SOLID_MASK; //brand-new templates shall be solid
//Using one-character names is a little brash, like, what if there's a mod that adds crap to the NBT of ever
//block entity, and uses short names for the same reason I am (there are lots and lots of block entities).
//Using one-character names is a little brash, like, what if there's a mod that adds crap to the NBT of every
//block entity, and uses short names for the same reason I am (because there are lots and lots of block entities)?
//Kinda doubt it?
protected static final String BLOCKSTATE_KEY = "s";
protected static final String BITFIELD_KEY = "b";
@ -92,6 +93,27 @@ public class TemplateEntity extends BlockEntity implements ThemeableBlockEntity
else return NbtHelper.toBlockState(Registries.BLOCK.getReadOnlyWrapper(), subCompound);
}
//Awkward: usually the BlockState is the source of truth for things like the "emits light" blockstate, but if you
//ctrl-pick a glowing block and place it, it should still be glowing. This is some hacky shit that guesses the value of
//the LIGHT blockstate based off information in the NBT tag, and also prevents bugginess like "the blockstate is not
//glowing but the copied NBT thinks glowstone dust was already added, so it refuses to accept more dust"
public static @Nullable BlockState weirdNbtLightLevelStuff(@Nullable BlockState state, ItemStack stack) {
if(state == null || stack == null) return state;
NbtCompound blockEntityTag = BlockItem.getBlockEntityNbt(stack);
if(blockEntityTag == null) return state;
if(state.contains(TemplateInteractionUtil.LIGHT)) {
state = state.with(TemplateInteractionUtil.LIGHT,
blockEntityTag.getBoolean("spentglow") || //2.0.4
((blockEntityTag.contains(BITFIELD_KEY) ? blockEntityTag.getByte(BITFIELD_KEY) : DEFAULT_BITFIELD) & SPENT_GLOWSTONE_DUST_MASK) != 0 || //2.0.5
readStateFromItem(stack).getLuminance() != 0 //glowstone dust wasn't manually added, the block just emits light
);
}
return state;
}
//RenderAttachmentBlockEntity impl. Note that ThemeableBlockEntity depends on this returning a BlockState object.
@Override
public BlockState getRenderAttachmentData() {

View File

@ -11,6 +11,7 @@ import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -40,6 +41,12 @@ public class TemplateFenceBlock extends FenceBlock implements BlockEntityProvide
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -13,6 +13,7 @@ import net.minecraft.block.WoodType;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -41,6 +42,12 @@ public class TemplateFenceGateBlock extends FenceGateBlock implements BlockEntit
return Templates.TEMPLATE_BLOCK_ENTITY.instantiate(pos, state);
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));

View File

@ -11,6 +11,7 @@ import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -35,6 +36,12 @@ public class TemplateLeverBlock extends LeverBlock implements BlockEntityProvide
return Templates.TEMPLATE_BLOCK_ENTITY.instantiate(pos, state);
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));

View File

@ -11,6 +11,7 @@ import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -34,6 +35,12 @@ public class TemplatePaneBlock extends PaneBlock implements BlockEntityProvider
return Templates.TEMPLATE_BLOCK_ENTITY.instantiate(pos, state);
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));

View File

@ -28,6 +28,7 @@ public class TemplatePostBlock extends WaterloggableTemplateBlock {
@Override
public @Nullable BlockState getPlacementState(ItemPlacementContext ctx) {
//not calling TemplateInteractionUtil.modifyPlacementState because we extend WaterloggableTemplateBlock which already does
BlockState sup = super.getPlacementState(ctx);
if(sup != null) sup = sup.with(Properties.AXIS, ctx.getSide().getAxis());
return sup;

View File

@ -12,6 +12,7 @@ import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -44,6 +45,12 @@ public class TemplatePressurePlateBlock extends PressurePlateBlock implements Bl
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -11,6 +11,7 @@ import net.minecraft.block.SlabBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -40,6 +41,12 @@ public class TemplateSlabBlock extends SlabBlock implements BlockEntityProvider
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -39,6 +39,7 @@ public class TemplateSlopeBlock extends WaterloggableTemplateBlock {
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
//not calling TemplateInteractionUtil.modifyPlacementState because we extend TemplateBlock which already does
BlockState sup = super.getPlacementState(ctx);
if(sup != null) sup = sup.with(EDGE, Edge.stairslikePlacement(ctx));

View File

@ -12,6 +12,7 @@ import net.minecraft.block.StairsBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -45,6 +46,12 @@ public class TemplateStairsBlock extends StairsBlock implements BlockEntityProvi
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -13,6 +13,7 @@ import net.minecraft.block.TrapdoorBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -45,6 +46,12 @@ public class TemplateTrapdoorBlock extends TrapdoorBlock implements BlockEntityP
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -13,6 +13,7 @@ import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.enums.WallShape;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
@ -48,6 +49,12 @@ public class TemplateWallBlock extends WallBlock implements BlockEntityProvider
super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return TemplateInteractionUtil.modifyPlacementState(super.getPlacementState(ctx), ctx);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ActionResult r = TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);

View File

@ -28,6 +28,7 @@ public class WaterloggableTemplateBlock extends TemplateBlock implements Waterlo
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
//not calling TemplateInteractionUtil.modifyPlacementState because we extend TemplateBlock which already does
BlockState sup = super.getPlacementState(ctx);
if(sup != null) sup = sup.with(Properties.WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).isOf(Fluids.WATER));
return sup;