Skip to content

Commit

Permalink
TextDisplay fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHell228 committed Feb 19, 2025
1 parent 6f3610c commit 44e3995
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.spongepowered.api.entity.display.BillboardType;
import org.spongepowered.api.entity.display.DisplayEntity;
import org.spongepowered.api.entity.display.ItemDisplayType;
import org.spongepowered.api.entity.display.TextAlignment;
import org.spongepowered.api.util.Color;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.api.util.Transform;
Expand Down Expand Up @@ -119,11 +120,11 @@ public static void register(final DataProviderRegistrator registrator) {
.set((h, v) -> h.invoker$setBlockState((net.minecraft.world.level.block.state.BlockState) v))
.asMutable(Display_ItemDisplayAccessor.class)
.create(Keys.ITEM_STACK_SNAPSHOT)
.get(h -> ItemStackUtil.snapshotOf(((Display_ItemDisplayAccessor)h).invoker$getItemStack()))
.get(h -> ItemStackUtil.snapshotOf(h.invoker$getItemStack()))
.set((h, v) -> h.invoker$setItemStack(ItemStackUtil.fromSnapshotToNative(v)))
.create(Keys.ITEM_DISPLAY_TYPE)
.get(h -> (ItemDisplayType) (Object) h.invoker$getItemTransform())
.set((h, v) -> ((Display_ItemDisplayAccessor) h).invoker$setItemTransform(((ItemDisplayContext) (Object) v)))
.set((h, v) -> h.invoker$setItemTransform(((ItemDisplayContext) (Object) v)))
.asMutable(Display_TextDisplayAccessor.class)
.create(Keys.DISPLAY_NAME)
.get(h -> SpongeAdventure.asAdventure(h.invoker$getText()))
Expand All @@ -143,12 +144,15 @@ public static void register(final DataProviderRegistrator registrator) {
.create(Keys.HAS_DEFAULT_BACKGROUND)
.get(h -> DisplayEntityData.getFlagValue(h, Display.TextDisplay.FLAG_USE_DEFAULT_BACKGROUND))
.set((h, v) -> DisplayEntityData.setFlagValue(h, v, Display.TextDisplay.FLAG_USE_DEFAULT_BACKGROUND))
.create(Keys.HAS_DEFAULT_BACKGROUND)
.get(h -> DisplayEntityData.getFlagValue(h, Display.TextDisplay.FLAG_USE_DEFAULT_BACKGROUND))
.set((h, v) -> DisplayEntityData.setFlagValue(h, v, Display.TextDisplay.FLAG_USE_DEFAULT_BACKGROUND))
.create(Keys.TEXT_ALIGNMENT)
.get(DisplayEntityData::getAlignment)
.set(DisplayEntityData::setAlignment)
.create(Keys.TEXT_BACKGROUND_COLOR)
.get(h -> DisplayEntityData.colorFromInt(h.invoker$getBackgroundColor()))
.set((h, v) -> h.invoker$setBackgroundColor(DisplayEntityData.colorToInt(v)))
.get(h -> DisplayEntityData.argbToColor(h.invoker$getBackgroundColor()))
.set((h, v) -> h.invoker$setBackgroundColor(DisplayEntityData.argbWithColor(h.invoker$getBackgroundColor(), v)))
.create(Keys.TEXT_BACKGROUND_OPACITY)
.get(h -> DisplayEntityData.argbToOpacity(h.invoker$getBackgroundColor()))
.set((h, v) -> h.invoker$setBackgroundColor(DisplayEntityData.argbWithOpacity(h.invoker$getBackgroundColor(), v)))
;
registrator.spongeDataStore(Keys.TELEPORT_DURATION.key(), DisplayEntity.class, Keys.TELEPORT_DURATION);
}
Expand Down Expand Up @@ -200,12 +204,45 @@ private static Integer skyLight(final int original) {
return Brightness.unpack(original).sky();
}

private static Color colorFromInt(final int color) {
return Color.ofRgb(color);
private static Color argbToColor(final int argb) {
return Color.ofRgb(argb & 0x00ffffff);
}

private static byte argbToOpacity(final int argb) {
return (byte) (argb >> 24);
}

private static int argbWithColor(final int argb, final Color color) {
final int alpha = argb & 0xff000000;
final int rgb = color.rgb();
return alpha | rgb;
}

private static int argbWithOpacity(final int argb, final Byte opacity) {
final int alpha = opacity << 24;
final int rgb = argb & 0x00ffffff;
return alpha | rgb;
}

private static int colorToInt(final Color color) {
return color.rgb();
private static TextAlignment getAlignment(final Display_TextDisplayAccessor h) {
return (TextAlignment) (Object) Display.TextDisplay.getAlign(h.invoker$getFlags());
}

private static void setAlignment(final Display_TextDisplayAccessor h, final TextAlignment alignment) {
switch ((Display.TextDisplay.Align) (Object) alignment) {
case LEFT -> {
DisplayEntityData.setFlagValue(h, true, Display.TextDisplay.FLAG_ALIGN_LEFT);
DisplayEntityData.setFlagValue(h, false, Display.TextDisplay.FLAG_ALIGN_RIGHT);
}
case RIGHT -> {
DisplayEntityData.setFlagValue(h, false, Display.TextDisplay.FLAG_ALIGN_LEFT);
DisplayEntityData.setFlagValue(h, true, Display.TextDisplay.FLAG_ALIGN_RIGHT);
}
case CENTER -> {
DisplayEntityData.setFlagValue(h, false, Display.TextDisplay.FLAG_ALIGN_LEFT);
DisplayEntityData.setFlagValue(h, false, Display.TextDisplay.FLAG_ALIGN_RIGHT);
}
}
}

private static Transform getTransform(final Display display) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,21 @@ public void onRegisterCommand(final RegisterCommandEvent<Parameterized> event) {
var textDisplay = spawnEntity(player.world(), EntityTypes.TEXT_DISPLAY, centerPos, forwardDir, -4, -1);
textDisplay.offer(Keys.DISPLAY_NAME, Component.text("DisplayEntityTest").color(NamedTextColor.GOLD));
textDisplay.offer(Keys.SEE_THROUGH_BLOCKS, true);
textDisplay.offer(Keys.TEXT_ALIGNMENT, TextAlignments.LEFT.get());
textDisplay.offer(Keys.TEXT_BACKGROUND_COLOR, Color.GRAY);
textDisplay.offer(Keys.TEXT_BACKGROUND_COLOR, Color.CYAN);
textDisplay.offer(Keys.TEXT_BACKGROUND_OPACITY, (byte) 255);

textDisplay = spawnEntity(player.world(), EntityTypes.TEXT_DISPLAY, centerPos, forwardDir, col0, 0);
textDisplay.offer(Keys.DISPLAY_NAME, Component.text("Fixed"));
textDisplay.offer(Keys.BILLBOARD_TYPE, BillboardTypes.FIXED.get());
textDisplay.offer(Keys.TEXT_BACKGROUND_OPACITY, (byte) 0);

var itemDisplay = spawnEntity(player.world(), EntityTypes.ITEM_DISPLAY, centerPos, forwardDir, col2, 0);
itemDisplay.offer(Keys.ITEM_STACK_SNAPSHOT, ItemStack.of(ItemTypes.NETHERITE_INGOT).asImmutable());
itemDisplay.offer(Keys.BILLBOARD_TYPE, BillboardTypes.FIXED.get());

textDisplay = spawnEntity(player.world(), EntityTypes.TEXT_DISPLAY, centerPos, forwardDir, col1, 0);
textDisplay.offer(Keys.DISPLAY_NAME, Component.text("default\nlight"));
textDisplay.offer(Keys.TEXT_ALIGNMENT, TextAlignments.RIGHT.get());

textDisplay = spawnEntity(player.world(), EntityTypes.TEXT_DISPLAY, centerPos, forwardDir, col0, 1);
textDisplay.offer(Keys.DISPLAY_NAME, Component.text("Center"));
Expand Down

0 comments on commit 44e3995

Please sign in to comment.