From 16233f9a096415d700195f41fe64c069d4682990 Mon Sep 17 00:00:00 2001 From: Sparky983 <87631423+Sparky983@users.noreply.github.com> Date: Thu, 2 May 2024 19:09:23 +1000 Subject: [PATCH] Switch to Google Java Format --- .../kotlin/vision.java-conventions.gradle.kts | 2 +- .../paper/ExampleGuiCommandExecutor.java | 15 +- .../java/me/sparky983/vision/ButtonImpl.java | 13 +- .../main/java/me/sparky983/vision/Chest.java | 9 +- .../java/me/sparky983/vision/ChestImpl.java | 3 +- .../java/me/sparky983/vision/Container.java | 22 +-- .../java/me/sparky983/vision/Dropper.java | 6 +- .../main/java/me/sparky983/vision/Hopper.java | 6 +- .../java/me/sparky983/vision/ItemType.java | 1 + .../java/me/sparky983/vision/Subscribers.java | 29 ++-- .../java/me/sparky983/vision/ButtonTests.java | 13 +- .../java/me/sparky983/vision/ChestTests.java | 148 +++++++++--------- .../java/me/sparky983/vision/DropperTest.java | 38 ++--- .../vision/paper/InventoryListener.java | 44 +++--- .../vision/paper/PaperComponentFixerImpl.java | 9 +- .../paper/SubscribingPaperButtonMirror.java | 59 +++---- .../SubscribingPaperInventoryMirror.java | 84 +++++----- .../vision/paper/PaperVisionTests.java | 4 +- 18 files changed, 263 insertions(+), 242 deletions(-) diff --git a/buildSrc/src/main/kotlin/vision.java-conventions.gradle.kts b/buildSrc/src/main/kotlin/vision.java-conventions.gradle.kts index 2a661858..1b61f1de 100644 --- a/buildSrc/src/main/kotlin/vision.java-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/vision.java-conventions.gradle.kts @@ -23,7 +23,7 @@ java { spotless { java { - palantirJavaFormat().style("GOOGLE") + googleJavaFormat().formatJavadoc(false) formatAnnotations() } } diff --git a/examples/paper/src/main/java/me/sparky983/vision/examples/paper/ExampleGuiCommandExecutor.java b/examples/paper/src/main/java/me/sparky983/vision/examples/paper/ExampleGuiCommandExecutor.java index 896705bc..fed4d978 100644 --- a/examples/paper/src/main/java/me/sparky983/vision/examples/paper/ExampleGuiCommandExecutor.java +++ b/examples/paper/src/main/java/me/sparky983/vision/examples/paper/ExampleGuiCommandExecutor.java @@ -36,8 +36,8 @@ public boolean onCommand( final AtomicInteger count = new AtomicInteger(0); - final Button counter = Button.of(ItemType.DIAMOND) - .onClick((click) -> sender.sendMessage(click.button().name())); + final Button counter = + Button.of(ItemType.DIAMOND).onClick((click) -> sender.sendMessage(click.button().name())); scheduler.runTaskTimer( plugin, @@ -47,11 +47,12 @@ public boolean onCommand( 20, 10); - final Gui gui = Gui.chest() - .title(Component.text("Paper Example")) - .rows(3) - .slot(Slot.of(1, 4), counter) - .build(); + final Gui gui = + Gui.chest() + .title(Component.text("Paper Example")) + .rows(3) + .slot(Slot.of(1, 4), counter) + .build(); vision.open(player, gui); diff --git a/vision-api/src/main/java/me/sparky983/vision/ButtonImpl.java b/vision-api/src/main/java/me/sparky983/vision/ButtonImpl.java index 6b499217..2a4fe13d 100644 --- a/vision-api/src/main/java/me/sparky983/vision/ButtonImpl.java +++ b/vision-api/src/main/java/me/sparky983/vision/ButtonImpl.java @@ -131,12 +131,13 @@ public Button glow(final boolean glow) { public Button onClick(final Consumer handler) { Objects.requireNonNull(handler, "handler cannot be null"); - subscribe(new Subscriber() { - @Override - public void click(final Click click) { - handler.accept(click); - } - }); + subscribe( + new Subscriber() { + @Override + public void click(final Click click) { + handler.accept(click); + } + }); return this; } diff --git a/vision-api/src/main/java/me/sparky983/vision/Chest.java b/vision-api/src/main/java/me/sparky983/vision/Chest.java index f3e1c54a..0d7c7266 100644 --- a/vision-api/src/main/java/me/sparky983/vision/Chest.java +++ b/vision-api/src/main/java/me/sparky983/vision/Chest.java @@ -30,8 +30,7 @@ public non-sealed interface Chest extends Gui { * @since 1.0 * @vision.experimental because this may be deleted, renamed or moved. */ - @ApiStatus.Experimental - int MIN_ROWS = 1; + @ApiStatus.Experimental int MIN_ROWS = 1; /** * The maximum number of rows that a {@code Chest} can have. @@ -40,8 +39,7 @@ public non-sealed interface Chest extends Gui { * @since 1.0 * @vision.experimental because this may be deleted, renamed or moved. */ - @ApiStatus.Experimental - int MAX_ROWS = 6; + @ApiStatus.Experimental int MAX_ROWS = 6; /** * The number of columns {@code Chest}s have. @@ -50,8 +48,7 @@ public non-sealed interface Chest extends Gui { * @since 1.0 * @vision.experimental because this may be deleted, renamed or moved. */ - @ApiStatus.Experimental - int COLUMNS = 9; + @ApiStatus.Experimental int COLUMNS = 9; /** * {@inheritDoc} diff --git a/vision-api/src/main/java/me/sparky983/vision/ChestImpl.java b/vision-api/src/main/java/me/sparky983/vision/ChestImpl.java index c8facc8c..3cdf4de3 100644 --- a/vision-api/src/main/java/me/sparky983/vision/ChestImpl.java +++ b/vision-api/src/main/java/me/sparky983/vision/ChestImpl.java @@ -19,8 +19,7 @@ final class ChestImpl implements Chest { @VisibleForTesting static final Component DEFAULT_TITLE = Component.translatable("container.chest"); - @VisibleForTesting - static final int DEFAULT_ROWS = 1; + @VisibleForTesting static final int DEFAULT_ROWS = 1; private final Container container; diff --git a/vision-api/src/main/java/me/sparky983/vision/Container.java b/vision-api/src/main/java/me/sparky983/vision/Container.java index 15bc5907..abd2f091 100644 --- a/vision-api/src/main/java/me/sparky983/vision/Container.java +++ b/vision-api/src/main/java/me/sparky983/vision/Container.java @@ -133,10 +133,11 @@ void slot(final Slot slot, final @Nullable Button button) { } else { buttons.put(slot, button); } - subscribers.notify((subscriber) -> { - subscriber.button(slot, button); // Backwards compatibility - subscriber.slot(slot, button); - }); + subscribers.notify( + (subscriber) -> { + subscriber.button(slot, button); // Backwards compatibility + subscriber.slot(slot, button); + }); } List slots() { @@ -146,12 +147,13 @@ List slots() { void onClose(final Consumer handler) { Objects.requireNonNull(handler, "handler cannot be null"); - subscribers.subscribe(new Gui.Subscriber() { - @Override - public void close(final Close close) { - handler.accept(close); - } - }); + subscribers.subscribe( + new Gui.Subscriber() { + @Override + public void close(final Close close) { + handler.accept(close); + } + }); } Gui.Publisher publisher() { diff --git a/vision-api/src/main/java/me/sparky983/vision/Dropper.java b/vision-api/src/main/java/me/sparky983/vision/Dropper.java index bfa102dd..4e1f5b83 100644 --- a/vision-api/src/main/java/me/sparky983/vision/Dropper.java +++ b/vision-api/src/main/java/me/sparky983/vision/Dropper.java @@ -29,8 +29,7 @@ public non-sealed interface Dropper extends Gui { * @since 1.0 * @vision.experimental because this may be deleted, renamed or moved. */ - @ApiStatus.Experimental - int ROWS = 3; + @ApiStatus.Experimental int ROWS = 3; /** * The number of columns {@code Dropper}s have. @@ -39,8 +38,7 @@ public non-sealed interface Dropper extends Gui { * @since 1.0 * @vision.experimental because this may be deleted, renamed or moved. */ - @ApiStatus.Experimental - int COLUMNS = 3; + @ApiStatus.Experimental int COLUMNS = 3; /** * {@inheritDoc} diff --git a/vision-api/src/main/java/me/sparky983/vision/Hopper.java b/vision-api/src/main/java/me/sparky983/vision/Hopper.java index 0a191132..8a0f4613 100644 --- a/vision-api/src/main/java/me/sparky983/vision/Hopper.java +++ b/vision-api/src/main/java/me/sparky983/vision/Hopper.java @@ -29,8 +29,7 @@ public non-sealed interface Hopper extends Gui { * @since 1.0 * @vision.experimental because this may be deleted, renamed or moved. */ - @ApiStatus.Experimental - int ROWS = 1; + @ApiStatus.Experimental int ROWS = 1; /** * The number of columns {@code Hopper}s have. @@ -39,8 +38,7 @@ public non-sealed interface Hopper extends Gui { * @since 1.0 * @vision.experimental because this may be deleted, renamed or moved. */ - @ApiStatus.Experimental - int COLUMNS = 5; + @ApiStatus.Experimental int COLUMNS = 5; /** * {@inheritDoc} diff --git a/vision-api/src/main/java/me/sparky983/vision/ItemType.java b/vision-api/src/main/java/me/sparky983/vision/ItemType.java index a75b0cdf..9ff92312 100644 --- a/vision-api/src/main/java/me/sparky983/vision/ItemType.java +++ b/vision-api/src/main/java/me/sparky983/vision/ItemType.java @@ -8803,6 +8803,7 @@ public sealed interface ItemType extends Keyed, Translatable permits ItemTypeImp * @since 0.1 */ ItemType ZOMBIFIED_PIGLIN_SPAWN_EGG = item(Key.key("zombified_piglin_spawn_egg")); + // @formatter:on // diff --git a/vision-api/src/main/java/me/sparky983/vision/Subscribers.java b/vision-api/src/main/java/me/sparky983/vision/Subscribers.java index e5109a03..4a960f58 100644 --- a/vision-api/src/main/java/me/sparky983/vision/Subscribers.java +++ b/vision-api/src/main/java/me/sparky983/vision/Subscribers.java @@ -18,21 +18,22 @@ final class Subscribers implements Subscribab public Subscription subscribe(final T subscriber) { Objects.requireNonNull(subscriber, "subscriber cannot be null"); - final Subscription subscription = new Subscription() { - @Override - public void cancel() { - final Map copy = new HashMap<>(subscribers); - if (copy.remove(this) == null) { - throw new IllegalStateException("Subscription has already been cancelled"); - } - subscribers = copy; - } + final Subscription subscription = + new Subscription() { + @Override + public void cancel() { + final Map copy = new HashMap<>(subscribers); + if (copy.remove(this) == null) { + throw new IllegalStateException("Subscription has already been cancelled"); + } + subscribers = copy; + } - @Override - public boolean isCanceled() { - return !subscribers.containsKey(this); - } - }; + @Override + public boolean isCanceled() { + return !subscribers.containsKey(this); + } + }; final Map copy = new HashMap<>(subscribers); copy.put(subscription, subscriber); diff --git a/vision-api/src/test/java/me/sparky983/vision/ButtonTests.java b/vision-api/src/test/java/me/sparky983/vision/ButtonTests.java index 1cc52fc1..6c586258 100644 --- a/vision-api/src/test/java/me/sparky983/vision/ButtonTests.java +++ b/vision-api/src/test/java/me/sparky983/vision/ButtonTests.java @@ -160,8 +160,9 @@ void testVarargsLoreWhenLoreIsNull() { void testVarargsLoreWhenLoreContainsNull() { final Button button = Button.of(ItemType.STONE); - final Exception e = assertThrows( - NullPointerException.class, () -> button.lore(Arrays.asList(LORE_LINE_1, null))); + final Exception e = + assertThrows( + NullPointerException.class, () -> button.lore(Arrays.asList(LORE_LINE_1, null))); assertEquals("lore[1] cannot be null", e.getMessage()); assertNotEquals(Arrays.asList(LORE_LINE_1, null), button.lore()); @@ -191,8 +192,9 @@ void testListLoreWhenLoreIsNull() { void testListLoreWhenLoreContainsNull() { final Button button = Button.of(ItemType.STONE); - final Exception e = assertThrows( - NullPointerException.class, () -> button.lore(Arrays.asList(LORE_LINE_1, null))); + final Exception e = + assertThrows( + NullPointerException.class, () -> button.lore(Arrays.asList(LORE_LINE_1, null))); assertEquals("lore[1] cannot be null", e.getMessage()); } @@ -367,8 +369,7 @@ void testCancelSubscription() { @Test void testToString() { - final Button button = - Button.of(ItemType.STONE).name(NAME).lore(LORE_LIST).amount(2).glow(true); + final Button button = Button.of(ItemType.STONE).name(NAME).lore(LORE_LIST).amount(2).glow(true); assertEquals( String.format( diff --git a/vision-api/src/test/java/me/sparky983/vision/ChestTests.java b/vision-api/src/test/java/me/sparky983/vision/ChestTests.java index 1ca85151..ec941bdd 100644 --- a/vision-api/src/test/java/me/sparky983/vision/ChestTests.java +++ b/vision-api/src/test/java/me/sparky983/vision/ChestTests.java @@ -283,19 +283,20 @@ void testBuilderBorderSet() { final Gui gui = builder.build(); - final Set slots = Set.of( - Slot.of(0, 0), - Slot.of(0, 1), - Slot.of(0, 2), - Slot.of(0, 3), - Slot.of(0, 5), - Slot.of(0, 6), - Slot.of(0, 7), - Slot.of(0, 8), - - // Slot.of(0, 0) - corner - Slot.of(1, 0), - Slot.of(2, 0)); + final Set slots = + Set.of( + Slot.of(0, 0), + Slot.of(0, 1), + Slot.of(0, 2), + Slot.of(0, 3), + Slot.of(0, 5), + Slot.of(0, 6), + Slot.of(0, 7), + Slot.of(0, 8), + + // Slot.of(0, 0) - corner + Slot.of(1, 0), + Slot.of(2, 0)); for (final Slot slot : gui.slots()) { if (slots.contains(slot)) { @@ -354,19 +355,20 @@ void testBuilderBorderVarargs() { final Gui gui = builder.build(); - final Set slots = Set.of( - Slot.of(0, 0), - Slot.of(0, 1), - Slot.of(0, 2), - Slot.of(0, 3), - Slot.of(0, 5), - Slot.of(0, 6), - Slot.of(0, 7), - Slot.of(0, 8), - - // Slot.of(0, 0) - corner - Slot.of(1, 0), - Slot.of(2, 0)); + final Set slots = + Set.of( + Slot.of(0, 0), + Slot.of(0, 1), + Slot.of(0, 2), + Slot.of(0, 3), + Slot.of(0, 5), + Slot.of(0, 6), + Slot.of(0, 7), + Slot.of(0, 8), + + // Slot.of(0, 0) - corner + Slot.of(1, 0), + Slot.of(2, 0)); for (final Slot slot : gui.slots()) { if (slots.contains(slot)) { @@ -399,33 +401,34 @@ void testBuilderBorder() { final Gui gui = builder.build(); - final Set slots = Set.of( - Slot.of(0, 0), - Slot.of(0, 1), - Slot.of(0, 2), - Slot.of(0, 3), - Slot.of(0, 5), - Slot.of(0, 6), - Slot.of(0, 7), - Slot.of(0, 8), - Slot.of(2, 0), - Slot.of(2, 1), - Slot.of(2, 2), - Slot.of(2, 3), - Slot.of(2, 4), - Slot.of(2, 5), - Slot.of(2, 6), - Slot.of(2, 7), - Slot.of(2, 8), - - // Slot.of(0, 0) - corner - Slot.of(1, 0), - // Slot.of(2, 0), - corner - - // Slot.of(0, 8) - corner - Slot.of(1, 8) - // Slot.of(2, 8) - corner - ); + final Set slots = + Set.of( + Slot.of(0, 0), + Slot.of(0, 1), + Slot.of(0, 2), + Slot.of(0, 3), + Slot.of(0, 5), + Slot.of(0, 6), + Slot.of(0, 7), + Slot.of(0, 8), + Slot.of(2, 0), + Slot.of(2, 1), + Slot.of(2, 2), + Slot.of(2, 3), + Slot.of(2, 4), + Slot.of(2, 5), + Slot.of(2, 6), + Slot.of(2, 7), + Slot.of(2, 8), + + // Slot.of(0, 0) - corner + Slot.of(1, 0), + // Slot.of(2, 0), - corner + + // Slot.of(0, 8) - corner + Slot.of(1, 8) + // Slot.of(2, 8) - corner + ); for (final Slot slot : gui.slots()) { if (slots.contains(slot)) { @@ -442,25 +445,26 @@ void testBuilderBorder() { void testSlots() { final Gui gui = Gui.chest().rows(2).build(); - final List slots = List.of( - Slot.of(0, 0), - Slot.of(0, 1), - Slot.of(0, 2), - Slot.of(0, 3), - Slot.of(0, 4), - Slot.of(0, 5), - Slot.of(0, 6), - Slot.of(0, 7), - Slot.of(0, 8), - Slot.of(1, 0), - Slot.of(1, 1), - Slot.of(1, 2), - Slot.of(1, 3), - Slot.of(1, 4), - Slot.of(1, 5), - Slot.of(1, 6), - Slot.of(1, 7), - Slot.of(1, 8)); + final List slots = + List.of( + Slot.of(0, 0), + Slot.of(0, 1), + Slot.of(0, 2), + Slot.of(0, 3), + Slot.of(0, 4), + Slot.of(0, 5), + Slot.of(0, 6), + Slot.of(0, 7), + Slot.of(0, 8), + Slot.of(1, 0), + Slot.of(1, 1), + Slot.of(1, 2), + Slot.of(1, 3), + Slot.of(1, 4), + Slot.of(1, 5), + Slot.of(1, 6), + Slot.of(1, 7), + Slot.of(1, 8)); assertEquals(slots, gui.slots()); } diff --git a/vision-api/src/test/java/me/sparky983/vision/DropperTest.java b/vision-api/src/test/java/me/sparky983/vision/DropperTest.java index 1b77420e..2315154b 100644 --- a/vision-api/src/test/java/me/sparky983/vision/DropperTest.java +++ b/vision-api/src/test/java/me/sparky983/vision/DropperTest.java @@ -345,14 +345,15 @@ void testBuilderBorder() { final Gui gui = builder.build(); // @formatter:off - final Set slots = Set.of( - Slot.of(0, 0), /*Slot.of(0, 1),*/ - Slot.of(0, 2), - Slot.of(1, 0), - Slot.of(1, 2), - Slot.of(2, 0), - Slot.of(2, 1), - Slot.of(2, 2)); + final Set slots = + Set.of( + Slot.of(0, 0), /*Slot.of(0, 1),*/ + Slot.of(0, 2), + Slot.of(1, 0), + Slot.of(1, 2), + Slot.of(2, 0), + Slot.of(2, 1), + Slot.of(2, 2)); // @formatter:on for (final Slot slot : gui.slots()) { @@ -370,16 +371,17 @@ void testBuilderBorder() { void testSlots() { final Gui gui = Gui.dropper().build(); - final List slots = List.of( - Slot.of(0, 0), - Slot.of(0, 1), - Slot.of(0, 2), - Slot.of(1, 0), - Slot.of(1, 1), - Slot.of(1, 2), - Slot.of(2, 0), - Slot.of(2, 1), - Slot.of(2, 2)); + final List slots = + List.of( + Slot.of(0, 0), + Slot.of(0, 1), + Slot.of(0, 2), + Slot.of(1, 0), + Slot.of(1, 1), + Slot.of(1, 2), + Slot.of(2, 0), + Slot.of(2, 1), + Slot.of(2, 2)); assertEquals(slots, gui.slots()); } diff --git a/vision-paper/src/main/java/me/sparky983/vision/paper/InventoryListener.java b/vision-paper/src/main/java/me/sparky983/vision/paper/InventoryListener.java index d34db2b6..b3cf4095 100644 --- a/vision-paper/src/main/java/me/sparky983/vision/paper/InventoryListener.java +++ b/vision-paper/src/main/java/me/sparky983/vision/paper/InventoryListener.java @@ -43,27 +43,29 @@ void onInventoryClick(final InventoryClickEvent event) { final Gui gui = guiInventoryHolder.gui(); final Slot slot = gui.slots().get(event.getSlot()); - gui.slot(slot).ifPresent((button) -> { - final ClickType type = - switch (event.getClick()) { - case LEFT -> ClickType.LEFT; - case SHIFT_LEFT -> ClickType.SHIFT_LEFT; - case RIGHT -> ClickType.RIGHT; - case SHIFT_RIGHT -> ClickType.SHIFT_RIGHT; - case MIDDLE -> ClickType.MIDDLE; - case DROP -> ClickType.DROP; - case CONTROL_DROP -> ClickType.CONTROL_DROP; - case DOUBLE_CLICK -> ClickType.DOUBLE_CLICK; - case NUMBER_KEY -> ClickType.NUMBER_KEY; - default -> null; - }; - - if (type == null) { - return; - } - - button.publisher().click(new PaperClick(player, button, slot, type, vision)); - }); + gui.slot(slot) + .ifPresent( + (button) -> { + final ClickType type = + switch (event.getClick()) { + case LEFT -> ClickType.LEFT; + case SHIFT_LEFT -> ClickType.SHIFT_LEFT; + case RIGHT -> ClickType.RIGHT; + case SHIFT_RIGHT -> ClickType.SHIFT_RIGHT; + case MIDDLE -> ClickType.MIDDLE; + case DROP -> ClickType.DROP; + case CONTROL_DROP -> ClickType.CONTROL_DROP; + case DOUBLE_CLICK -> ClickType.DOUBLE_CLICK; + case NUMBER_KEY -> ClickType.NUMBER_KEY; + default -> null; + }; + + if (type == null) { + return; + } + + button.publisher().click(new PaperClick(player, button, slot, type, vision)); + }); } @EventHandler diff --git a/vision-paper/src/main/java/me/sparky983/vision/paper/PaperComponentFixerImpl.java b/vision-paper/src/main/java/me/sparky983/vision/paper/PaperComponentFixerImpl.java index cc6a2e1f..de607222 100644 --- a/vision-paper/src/main/java/me/sparky983/vision/paper/PaperComponentFixerImpl.java +++ b/vision-paper/src/main/java/me/sparky983/vision/paper/PaperComponentFixerImpl.java @@ -16,10 +16,11 @@ final class PaperComponentFixerImpl implements PaperComponentFixer { * The style Vision uses for all items. */ @VisibleForTesting - static final Style VISION_STYLE = Style.style() - .color(NamedTextColor.WHITE) - .decorations(TextDecoration.NAMES.values(), false) - .build(); + static final Style VISION_STYLE = + Style.style() + .color(NamedTextColor.WHITE) + .decorations(TextDecoration.NAMES.values(), false) + .build(); @Override public Component convert(final Component component, final Locale locale) { diff --git a/vision-paper/src/main/java/me/sparky983/vision/paper/SubscribingPaperButtonMirror.java b/vision-paper/src/main/java/me/sparky983/vision/paper/SubscribingPaperButtonMirror.java index 508b16c5..7184921e 100644 --- a/vision-paper/src/main/java/me/sparky983/vision/paper/SubscribingPaperButtonMirror.java +++ b/vision-paper/src/main/java/me/sparky983/vision/paper/SubscribingPaperButtonMirror.java @@ -36,37 +36,42 @@ public Subscription mirror(final Button button, final ItemStack item, final Loca item.addItemFlags(ITEM_FLAGS); - final Button.Subscriber subscriber = new Button.Subscriber() { - @Override - public void type(final ItemType type) { - itemTypeConverter.convert(type).ifPresent(item::setType); - } + final Button.Subscriber subscriber = + new Button.Subscriber() { + @Override + public void type(final ItemType type) { + itemTypeConverter.convert(type).ifPresent(item::setType); + } - @Override - public void name(final Component name) { - item.editMeta((meta) -> meta.displayName(componentFixer.convert(name, locale))); - } + @Override + public void name(final Component name) { + item.editMeta((meta) -> meta.displayName(componentFixer.convert(name, locale))); + } - @Override - public void lore(final List lore) { - item.editMeta((meta) -> meta.lore( - lore.stream().map((line) -> componentFixer.convert(line, locale)).toList())); - } + @Override + public void lore(final List lore) { + item.editMeta( + (meta) -> + meta.lore( + lore.stream() + .map((line) -> componentFixer.convert(line, locale)) + .toList())); + } - @Override - public void amount(final int amount) { - item.setAmount(amount); - } + @Override + public void amount(final int amount) { + item.setAmount(amount); + } - @Override - public void glow(final boolean glow) { - if (glow) { - item.addUnsafeEnchantment(Enchantment.DURABILITY, 1); - } else { - item.removeEnchantment(Enchantment.DURABILITY); - } - } - }; + @Override + public void glow(final boolean glow) { + if (glow) { + item.addUnsafeEnchantment(Enchantment.DURABILITY, 1); + } else { + item.removeEnchantment(Enchantment.DURABILITY); + } + } + }; // Essentially replay the button's state to the subscriber // Ensures that the ItemStack and Button are consistent diff --git a/vision-paper/src/main/java/me/sparky983/vision/paper/SubscribingPaperInventoryMirror.java b/vision-paper/src/main/java/me/sparky983/vision/paper/SubscribingPaperInventoryMirror.java index ac58ccf7..a56cae4e 100644 --- a/vision-paper/src/main/java/me/sparky983/vision/paper/SubscribingPaperInventoryMirror.java +++ b/vision-paper/src/main/java/me/sparky983/vision/paper/SubscribingPaperInventoryMirror.java @@ -53,49 +53,57 @@ public Inventory mirror(final Gui gui, final Locale locale) { final Component title = GlobalTranslator.render(gui.title(), locale); final Function inventoryFactory = - (inventoryHolder) -> switch (gui.type()) { - case CHEST -> server.createInventory(inventoryHolder, gui.rows() * gui.columns(), title); - case HOPPER -> server.createInventory(inventoryHolder, InventoryType.HOPPER, title); - case DROPPER -> server.createInventory(inventoryHolder, InventoryType.DROPPER, title); - }; + (inventoryHolder) -> + switch (gui.type()) { + case CHEST -> + server.createInventory(inventoryHolder, gui.rows() * gui.columns(), title); + case HOPPER -> server.createInventory(inventoryHolder, InventoryType.HOPPER, title); + case DROPPER -> server.createInventory(inventoryHolder, InventoryType.DROPPER, title); + }; final Inventory inventory = new GuiInventoryHolder(gui, inventoryFactory).getInventory(); - final Gui.Subscriber subscriber = new Gui.Subscriber() { - private final Map subscriptions = new HashMap<>(); - - @Override - public void slot(final Slot slot, final @Nullable Button button) { - final Subscription subscription = subscriptions.get(slot); - if (subscription != null) { - subscription.cancel(); - } - - final int rawSlot = slot.column() + (slot.row() * gui.columns()); - if (button == null) { - inventory.setItem(rawSlot, null); - subscriptions.remove(slot); - return; - } - final ItemStack item = itemTypeConverter - .convert(button.type()) - .map(ItemStack::new) - .orElseThrow(() -> - new IllegalStateException(String.format(UNABLE_TO_MIRROR_MESSAGE, button.type()))); - inventory.setItem(rawSlot, item); - - final ItemStack craftItem = inventory.getItem(rawSlot); - // We need to get the item from the inventory because the item is cloned - assert craftItem != null; - subscriptions.put(slot, buttonMirror.mirror(button, craftItem, locale)); - } - }; + final Gui.Subscriber subscriber = + new Gui.Subscriber() { + private final Map subscriptions = new HashMap<>(); + + @Override + public void slot(final Slot slot, final @Nullable Button button) { + final Subscription subscription = subscriptions.get(slot); + if (subscription != null) { + subscription.cancel(); + } + + final int rawSlot = slot.column() + (slot.row() * gui.columns()); + if (button == null) { + inventory.setItem(rawSlot, null); + subscriptions.remove(slot); + return; + } + final ItemStack item = + itemTypeConverter + .convert(button.type()) + .map(ItemStack::new) + .orElseThrow( + () -> + new IllegalStateException( + String.format(UNABLE_TO_MIRROR_MESSAGE, button.type()))); + inventory.setItem(rawSlot, item); + + final ItemStack craftItem = inventory.getItem(rawSlot); + // We need to get the item from the inventory because the item is cloned + assert craftItem != null; + subscriptions.put(slot, buttonMirror.mirror(button, craftItem, locale)); + } + }; for (final Slot slot : gui.slots()) { - gui.slot(slot).ifPresent((button) -> { - // Essentially replaying the button sets - subscriber.slot(slot, button); - }); + gui.slot(slot) + .ifPresent( + (button) -> { + // Essentially replaying the button sets + subscriber.slot(slot, button); + }); } gui.subscribe(subscriber); diff --git a/vision-paper/src/test/java/me/sparky983/vision/paper/PaperVisionTests.java b/vision-paper/src/test/java/me/sparky983/vision/paper/PaperVisionTests.java index 15a2e103..9c35aa8c 100644 --- a/vision-paper/src/test/java/me/sparky983/vision/paper/PaperVisionTests.java +++ b/vision-paper/src/test/java/me/sparky983/vision/paper/PaperVisionTests.java @@ -31,8 +31,8 @@ void testCreateWhenPluginIsNull() { @SuppressWarnings("ConstantConditions") @Test void testOpenWhenPlayerIsNull() { - final Exception e = assertThrows( - NullPointerException.class, () -> paperVision.open(null, Gui.chest().build())); + final Exception e = + assertThrows(NullPointerException.class, () -> paperVision.open(null, Gui.chest().build())); assertEquals("player cannot be null", e.getMessage()); }