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 #47 from Chailotl/sprite-block
Added the sprite block
- Loading branch information
Showing
35 changed files
with
391 additions
and
7 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
84 changes: 84 additions & 0 deletions
84
src/main/java/dev/hephaestus/glowcase/block/SpriteBlock.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,84 @@ | ||
package dev.hephaestus.glowcase.block; | ||
|
||
import dev.hephaestus.glowcase.Glowcase; | ||
import dev.hephaestus.glowcase.block.entity.SpriteBlockEntity; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.BlockEntityProvider; | ||
import net.minecraft.block.BlockState; | ||
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.ItemPlacementContext; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.item.tooltip.TooltipType; | ||
import net.minecraft.state.StateManager; | ||
import net.minecraft.state.property.Properties; | ||
import net.minecraft.text.Text; | ||
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.math.MathHelper; | ||
import net.minecraft.world.World; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
public class SpriteBlock extends GlowcaseBlock implements BlockEntityProvider { | ||
public SpriteBlock() { | ||
super(); | ||
this.setDefaultState(this.getDefaultState().with(Properties.ROTATION, 0)); | ||
} | ||
|
||
@Override | ||
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { | ||
super.appendProperties(builder); | ||
builder.add(Properties.ROTATION); | ||
} | ||
|
||
@Override | ||
public BlockState getPlacementState(ItemPlacementContext ctx) { | ||
return this.getDefaultState().with(Properties.ROTATION, MathHelper.floor((double) ((180.0F + ctx.getPlayerYaw()) * 16.0F / 360.0F) + 0.5D) & 15); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { | ||
return new SpriteBlockEntity(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 SpriteBlockEntity be) { | ||
blockEntityTag.applyToBlockEntity(be, world.getRegistryManager()); | ||
} | ||
|
||
Glowcase.proxy.openSpriteBlockEditScreen(pos); | ||
} | ||
} | ||
|
||
@Override | ||
protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { | ||
if (!(world.getBlockEntity(pos) instanceof SpriteBlockEntity be)) return ItemActionResult.CONSUME; | ||
|
||
if (world.isClient && player.getStackInHand(hand).isIn(Glowcase.ITEM_TAG) && canEditGlowcase(player, pos)) { | ||
Glowcase.proxy.openSpriteBlockEditScreen(pos); | ||
} | ||
|
||
return ItemActionResult.SUCCESS; | ||
} | ||
|
||
@Override | ||
public void appendTooltip(ItemStack itemStack, Item.TooltipContext context, List<Text> tooltip, TooltipType options) { | ||
tooltip.add(Text.translatable("block.glowcase.sprite_block.tooltip.0").formatted(Formatting.GRAY)); | ||
tooltip.add(Text.translatable("block.glowcase.generic.tooltip").formatted(Formatting.DARK_GRAY)); | ||
tooltip.add(Text.translatable("block.glowcase.sprite_block.tooltip.1").formatted(Formatting.DARK_GRAY)); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/dev/hephaestus/glowcase/block/entity/SpriteBlockEntity.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,77 @@ | ||
package dev.hephaestus.glowcase.block.entity; | ||
|
||
import dev.hephaestus.glowcase.Glowcase; | ||
import dev.hephaestus.glowcase.client.render.block.entity.BakedBlockEntityRenderer; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.nbt.NbtCompound; | ||
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.util.math.BlockPos; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class SpriteBlockEntity extends BlockEntity { | ||
public String sprite = "arrow"; | ||
public int rotation = 0; | ||
public TextBlockEntity.ZOffset zOffset = TextBlockEntity.ZOffset.CENTER; | ||
public int color = 0xFFFFFF; | ||
|
||
public SpriteBlockEntity(BlockPos pos, BlockState state) { | ||
super(Glowcase.SPRITE_BLOCK_ENTITY.get(), pos, state); | ||
} | ||
|
||
public void setSprite(String newSprite) { | ||
sprite = newSprite; | ||
markDirty(); | ||
dispatch(); | ||
} | ||
|
||
@Override | ||
public void writeNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) { | ||
super.writeNbt(tag, registryLookup); | ||
|
||
tag.putString("sprite", this.sprite); | ||
tag.putInt("rotation", this.rotation); | ||
tag.putString("z_offset", this.zOffset.name()); | ||
tag.putInt("color", this.color); | ||
} | ||
|
||
@Override | ||
public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) { | ||
super.readNbt(tag, registryLookup); | ||
|
||
this.sprite = tag.getString("sprite"); | ||
this.rotation = tag.getInt("rotation"); | ||
this.zOffset = TextBlockEntity.ZOffset.valueOf(tag.getString("z_offset")); | ||
this.color = tag.getInt("color"); | ||
} | ||
|
||
@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
75 changes: 75 additions & 0 deletions
75
src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/SpriteBlockEditScreen.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,75 @@ | ||
package dev.hephaestus.glowcase.client.gui.screen.ingame; | ||
|
||
import dev.hephaestus.glowcase.block.entity.SpriteBlockEntity; | ||
import dev.hephaestus.glowcase.block.entity.TextBlockEntity; | ||
import dev.hephaestus.glowcase.packet.C2SEditSpriteBlock; | ||
import net.minecraft.client.gui.widget.ButtonWidget; | ||
import net.minecraft.client.gui.widget.TextFieldWidget; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.text.TextColor; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class SpriteBlockEditScreen extends GlowcaseScreen { | ||
private final SpriteBlockEntity spriteBlockEntity; | ||
|
||
private TextFieldWidget spriteWidget; | ||
private ButtonWidget rotationWidget; | ||
private ButtonWidget zOffsetToggle; | ||
private TextFieldWidget colorEntryWidget; | ||
|
||
public SpriteBlockEditScreen(SpriteBlockEntity spriteBlockEntity) { | ||
this.spriteBlockEntity = spriteBlockEntity; | ||
} | ||
|
||
@Override | ||
public void init() { | ||
super.init(); | ||
|
||
if (this.client == null) return; | ||
|
||
this.spriteWidget = new TextFieldWidget(this.client.textRenderer, width / 2 - 75, height / 2 - 55, 150, 20, Text.empty()); | ||
this.spriteWidget.setText(spriteBlockEntity.sprite); | ||
this.spriteWidget.setChangedListener(string -> { | ||
if (Identifier.isPathValid(this.spriteWidget.getText())) { | ||
this.spriteBlockEntity.sprite = this.spriteWidget.getText(); | ||
} | ||
}); | ||
|
||
this.rotationWidget = ButtonWidget.builder(Text.literal("Rotate"), (action) -> { | ||
this.spriteBlockEntity.rotation += 45; | ||
if (this.spriteBlockEntity.rotation >= 360) { | ||
this.spriteBlockEntity.rotation = 0; | ||
} | ||
}).dimensions(width / 2 - 75, height / 2 - 25, 150, 20).build(); | ||
|
||
this.zOffsetToggle = ButtonWidget.builder(Text.literal(this.spriteBlockEntity.zOffset.name()), action -> { | ||
switch (spriteBlockEntity.zOffset) { | ||
case FRONT -> spriteBlockEntity.zOffset = TextBlockEntity.ZOffset.CENTER; | ||
case CENTER -> spriteBlockEntity.zOffset = TextBlockEntity.ZOffset.BACK; | ||
case BACK -> spriteBlockEntity.zOffset = TextBlockEntity.ZOffset.FRONT; | ||
} | ||
|
||
this.zOffsetToggle.setMessage(Text.literal(this.spriteBlockEntity.zOffset.name())); | ||
}).dimensions(width / 2 - 75, height / 2 + 5, 150, 20).build(); | ||
|
||
this.colorEntryWidget = new TextFieldWidget(this.client.textRenderer, width / 2 - 75, height / 2 + 35, 150, 20, Text.empty()); | ||
this.colorEntryWidget.setText("#" + String.format("%1$06X", this.spriteBlockEntity.color & 0x00FFFFFF)); | ||
this.colorEntryWidget.setChangedListener(string -> { | ||
TextColor.parse(this.colorEntryWidget.getText()).ifSuccess(color -> { | ||
this.spriteBlockEntity.color = color == null ? 0xFFFFFFFF : color.getRgb() | 0xFF000000; | ||
}); | ||
}); | ||
|
||
this.addDrawableChild(this.spriteWidget); | ||
this.addDrawableChild(this.rotationWidget); | ||
this.addDrawableChild(this.zOffsetToggle); | ||
this.addDrawableChild(this.colorEntryWidget); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
spriteBlockEntity.setSprite(spriteWidget.getText()); | ||
C2SEditSpriteBlock.of(spriteBlockEntity).send(); | ||
super.close(); | ||
} | ||
} |
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.