Skip to content

Commit

Permalink
Warn player when attempting to place a painting in an invalid position (
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew6rant authored Jun 22, 2023
1 parent 749694a commit b483a7d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/main/java/tk/estecka/invarpaint/mixin/DecorationItemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.entity.decoration.AbstractDecorationEntity;
import net.minecraft.entity.decoration.painting.PaintingEntity;
import net.minecraft.entity.decoration.painting.PaintingVariant;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.DecorationItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -19,6 +20,7 @@
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -46,6 +48,7 @@ public class DecorationItemMixin extends Item {
private final EntityType<? extends AbstractDecorationEntity> entityType;

private ItemStack itemStack;
private PlayerEntity player;

public DecorationItemMixin(Settings settings, EntityType<? extends AbstractDecorationEntity> entityType) {
super(settings);
Expand All @@ -61,8 +64,9 @@ public DecorationItemMixin(Settings settings, EntityType<? extends AbstractDecor
shift = At.Shift.BEFORE
)
)
private void captureItemStack(ItemUsageContext context, CallbackInfoReturnable<ActionResult> cir) {
private void captureContext(ItemUsageContext context, CallbackInfoReturnable<ActionResult> cir) {
itemStack = context.getStack();
player = context.getPlayer();
}

@Redirect(
Expand All @@ -76,8 +80,18 @@ private Optional<PaintingEntity> filterPlacedPainting(World world, BlockPos pos,
String variantId = PaintStackCreator.GetVariantId(this.itemStack);
PaintingVariant itemVariant = (variantId==null) ? null : Registries.PAINTING_VARIANT.get(new Identifier(variantId));

if (itemVariant != null)
return PaintEntityPlacer.PlaceLockedPainting(world, pos, facing, itemVariant);
if (itemVariant != null) {
Optional<PaintingEntity> entity = PaintEntityPlacer.PlaceLockedPainting(world, pos, facing, itemVariant);
if (entity.isEmpty() && player != null) {
player.sendMessage(
Text.translatable("painting.invalid_space",
Text.translatable("painting."+variantId.replace(":",".")+".title").formatted(Formatting.YELLOW),
Text.translatable("painting.dimensions", MathHelper.ceilDiv(itemVariant.getWidth(), 16), MathHelper.ceilDiv(itemVariant.getHeight(), 16)).formatted(Formatting.WHITE)),
true
);
}
return entity;
}
else {
if (variantId != null)
InvariablePaintings.LOGGER.warn("Unknown painting id: {}", variantId);
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/invarpaint/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"painting.invalid_space": "%s needs a %s space!"
}

0 comments on commit b483a7d

Please sign in to comment.