forked from Haven-King/glowcase
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Chailotl/popup-block
Add the popup block
- Loading branch information
Showing
17 changed files
with
809 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/dev/hephaestus/glowcase/block/PopupBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package dev.hephaestus.glowcase.block; | ||
|
||
import dev.hephaestus.glowcase.Glowcase; | ||
import dev.hephaestus.glowcase.block.entity.PopupBlockEntity; | ||
import net.minecraft.block.BlockEntityProvider; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.ShapeContext; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.component.DataComponentTypes; | ||
import net.minecraft.component.type.NbtComponent; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.item.tooltip.TooltipType; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.Formatting; | ||
import net.minecraft.util.Hand; | ||
import net.minecraft.util.ItemActionResult; | ||
import net.minecraft.util.hit.BlockHitResult; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.shape.VoxelShape; | ||
import net.minecraft.util.shape.VoxelShapes; | ||
import net.minecraft.world.BlockView; | ||
import net.minecraft.world.World; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
public class PopupBlock extends GlowcaseBlock implements BlockEntityProvider { | ||
private static final VoxelShape OUTLINE = VoxelShapes.cuboid(0.25, 0.25, 0.25, 0.75, 0.75, 0.75); | ||
|
||
@Override | ||
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { | ||
return OUTLINE; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { | ||
return new PopupBlockEntity(pos, state); | ||
} | ||
|
||
@Override | ||
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) { | ||
if (world.isClient && placer instanceof PlayerEntity player && canEditGlowcase(player, pos)) { | ||
//load any ctrl-picked NBT clientside | ||
NbtComponent blockEntityTag = stack.get(DataComponentTypes.BLOCK_ENTITY_DATA); | ||
if (blockEntityTag != null && world.getBlockEntity(pos) instanceof PopupBlockEntity be) { | ||
blockEntityTag.applyToBlockEntity(be, world.getRegistryManager()); | ||
} | ||
|
||
Glowcase.proxy.openPopupBlockEditScreen(pos); | ||
} | ||
} | ||
|
||
@Override | ||
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { | ||
if (!(world.getBlockEntity(pos) instanceof PopupBlockEntity be)) return ActionResult.CONSUME; | ||
if (world.isClient) { | ||
Glowcase.proxy.openPopupBlockViewScreen(pos); | ||
} | ||
return ActionResult.SUCCESS; | ||
} | ||
|
||
@Override | ||
protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { | ||
if (!(world.getBlockEntity(pos) instanceof PopupBlockEntity)) return ItemActionResult.CONSUME; | ||
if (player.getStackInHand(hand).isIn(Glowcase.ITEM_TAG) && canEditGlowcase(player, pos)) { | ||
if (world.isClient) { | ||
Glowcase.proxy.openPopupBlockEditScreen(pos); | ||
} | ||
return ItemActionResult.SUCCESS; | ||
} | ||
return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; | ||
} | ||
|
||
@Override | ||
public void appendTooltip(ItemStack itemStack, Item.TooltipContext context, List<Text> tooltip, TooltipType options) { | ||
tooltip.add(Text.translatable("block.glowcase.popup_block.tooltip.0").formatted(Formatting.GRAY)); | ||
tooltip.add(Text.translatable("block.glowcase.generic.tooltip").formatted(Formatting.DARK_GRAY)); | ||
tooltip.add(Text.translatable("block.glowcase.popup_block.tooltip.1").formatted(Formatting.DARK_GRAY)); | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
src/main/java/dev/hephaestus/glowcase/block/entity/PopupBlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package dev.hephaestus.glowcase.block.entity; | ||
|
||
import dev.hephaestus.glowcase.Glowcase; | ||
import dev.hephaestus.glowcase.client.render.block.entity.BakedBlockEntityRenderer; | ||
import eu.pb4.placeholders.api.ParserContext; | ||
import eu.pb4.placeholders.api.parsers.NodeParser; | ||
import eu.pb4.placeholders.api.parsers.TagParser; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.nbt.NbtElement; | ||
import net.minecraft.nbt.NbtList; | ||
import net.minecraft.nbt.NbtString; | ||
import net.minecraft.network.listener.ClientPlayPacketListener; | ||
import net.minecraft.network.packet.Packet; | ||
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; | ||
import net.minecraft.registry.RegistryWrapper; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.text.Style; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.math.BlockPos; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PopupBlockEntity extends BlockEntity { | ||
public static final NodeParser PARSER = TagParser.DEFAULT; | ||
public String title = ""; | ||
public List<Text> lines = new ArrayList<>(); | ||
public TextBlockEntity.TextAlignment textAlignment = TextBlockEntity.TextAlignment.CENTER; | ||
public int color = 0xFFFFFF; | ||
public boolean renderDirty = true; | ||
|
||
public PopupBlockEntity(BlockPos pos, BlockState state) { | ||
super(Glowcase.POPUP_BLOCK_ENTITY.get(), pos, state); | ||
lines.add(Text.empty()); | ||
} | ||
|
||
@Override | ||
protected void writeNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) { | ||
super.writeNbt(tag, registryLookup); | ||
|
||
tag.putString("title", this.title); | ||
tag.putInt("color", this.color); | ||
|
||
tag.putString("text_alignment", this.textAlignment.name()); | ||
|
||
NbtList lines = tag.getList("lines", 8); | ||
for (var text : this.lines) { | ||
lines.add(NbtString.of(Text.Serialization.toJsonString(text, registryLookup))); | ||
} | ||
|
||
tag.put("lines", lines); | ||
} | ||
|
||
@Override | ||
protected void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) { | ||
super.readNbt(tag, registryLookup); | ||
|
||
this.title = tag.getString("title"); | ||
this.lines = new ArrayList<>(); | ||
this.color = tag.getInt("color"); | ||
|
||
this.textAlignment = TextBlockEntity.TextAlignment.valueOf(tag.getString("text_alignment")); | ||
|
||
NbtList lines = tag.getList("lines", 8); | ||
|
||
for (NbtElement line : lines) { | ||
if (line.getType() == NbtElement.END_TYPE) break; | ||
this.lines.add(Text.Serialization.fromJson(line.asString(), registryLookup)); | ||
} | ||
|
||
this.renderDirty = true; | ||
} | ||
|
||
public String getRawLine(int i) { | ||
var line = this.lines.get(i); | ||
|
||
if (line.getStyle() == null) { | ||
return line.getString(); | ||
} | ||
|
||
var insert = line.getStyle().getInsertion(); | ||
|
||
if (insert == null) { | ||
return line.getString(); | ||
} | ||
return insert; | ||
} | ||
|
||
public void addRawLine(int i, String string) { | ||
var parsed = PARSER.parseText(string, ParserContext.of()); | ||
|
||
if (parsed.getString().equals(string)) { | ||
this.lines.add(i, Text.literal(string)); | ||
} else { | ||
this.lines.add(i, Text.empty().append(parsed).setStyle(Style.EMPTY.withInsertion(string))); | ||
} | ||
} | ||
|
||
public void setRawLine(int i, String string) { | ||
var parsed = PARSER.parseText(string, ParserContext.of()); | ||
|
||
if (parsed.getString().equals(string)) { | ||
this.lines.set(i, Text.literal(string)); | ||
} else { | ||
this.lines.set(i, Text.empty().append(parsed).setStyle(Style.EMPTY.withInsertion(string))); | ||
} | ||
} | ||
|
||
@SuppressWarnings({"MethodCallSideOnly", "VariableUseSideOnly"}) | ||
@Override | ||
public void markRemoved() { | ||
if (world != null && world.isClient) { | ||
BakedBlockEntityRenderer.Manager.markForRebuild(getPos()); | ||
} | ||
super.markRemoved(); | ||
} | ||
|
||
// standard blockentity boilerplate | ||
|
||
public void dispatch() { | ||
if (world instanceof ServerWorld sworld) sworld.getChunkManager().markForUpdate(pos); | ||
} | ||
|
||
@Override | ||
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup) { | ||
return createNbt(registryLookup); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Packet<ClientPlayPacketListener> toUpdatePacket() { | ||
return BlockEntityUpdateS2CPacket.create(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.