From 5006a897f0ad1645ba9e29e60f276782e4f65d69 Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Mon, 8 May 2023 21:16:22 +0300 Subject: [PATCH 1/9] Added instruments tag Also marked the instrument field in InstrumentPlayedEvent as deprecated & for removal for the reason provided in the comment --- .../genshinstrument/event/InstrumentPlayedEvent.java | 4 ++++ .../data/genshinstrument/tags/items/instruments.json | 8 ++++++++ 2 files changed, 12 insertions(+) create mode 100644 src/main/resources/data/genshinstrument/tags/items/instruments.json diff --git a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java index a2d96922..0ac28979 100644 --- a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java +++ b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java @@ -14,6 +14,10 @@ public class InstrumentPlayedEvent extends Event { public final NoteSound sound; public final ServerPlayer player; + //FIXME: Switch with InteractionHand. ItemStack updates like a sht + //TODO: Make it so that if the item does not exist anymore in the player's hand, close the instrument's menu. + // It will be possible when the above will be fixed + @Deprecated(forRemoval = true) public final ItemStack instrument; public InstrumentPlayedEvent(ServerPlayer player, NoteSound sound, ItemStack instrument) { diff --git a/src/main/resources/data/genshinstrument/tags/items/instruments.json b/src/main/resources/data/genshinstrument/tags/items/instruments.json new file mode 100644 index 00000000..3cb1aabb --- /dev/null +++ b/src/main/resources/data/genshinstrument/tags/items/instruments.json @@ -0,0 +1,8 @@ +{ + "values": [ + "genshinstrument:windsong_lyre", + "genshinstrument:vintage_lyre", + "genshinstrument:floral_zither", + "genshinstrument:glorious_drum" + ] +} \ No newline at end of file From 49703813011b3a105652a620fbfb5a811025a4f5 Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Tue, 9 May 2023 12:02:42 +0300 Subject: [PATCH 2/9] API tweaks & gameplay fix * Added field hand for InstrumentPlayedEvent. This should fix the issue of the instrument field being outdated with the actual state of the stack. * Removed field instrument from AbstractInstrumentScreen and NoteButton. Instead, a new interactionHand field will only present in AbstractInstrumentScreen * Moved note playing functionality to ServerUtil * The lyre screen now closes when it is not present in the player's inventory --- .../instrumentOpen/InstrumentOpen.java | 9 ++++ .../AratakisGreatAndGloriousDrumScreen.java | 8 ++-- .../instrument/drum/DrumNoteButton.java | 5 +-- .../floralzither/FloralZitherScreen.java | 6 +-- .../partial/AbstractGridInstrumentScreen.java | 8 ++-- .../partial/AbstractInstrumentScreen.java | 8 ++-- .../instrument/partial/note/NoteButton.java | 14 +++--- .../instrument/partial/note/NoteGrid.java | 7 +-- .../partial/note/NoteGridButton.java | 5 +-- .../vintagelyre/VintageLyreScreen.java | 8 ++-- .../vintagelyre/VintageNoteButton.java | 7 ++- .../vintagelyre/VintageNoteGrid.java | 7 ++- .../windsonglyre/WindsongLyreScreen.java | 6 +-- .../genshinstrument/event/ClientEvents.java | 34 ++++++++++++++ .../event/InstrumentPlayedEvent.java | 25 ++++++++--- .../genshinstrument/item/InstrumentItem.java | 8 ++-- .../cstav/genshinstrument/item/ModItems.java | 8 ++-- .../packets/instrument/InstrumentPacket.java | 45 +++++-------------- .../instrument/OpenInstrumentPacket.java | 16 +++---- .../genshinstrument/util/ServerUtil.java | 26 +++++++++++ 20 files changed, 156 insertions(+), 104 deletions(-) create mode 100644 src/main/java/com/cstav/genshinstrument/event/ClientEvents.java create mode 100644 src/main/java/com/cstav/genshinstrument/util/ServerUtil.java diff --git a/src/main/java/com/cstav/genshinstrument/capability/instrumentOpen/InstrumentOpen.java b/src/main/java/com/cstav/genshinstrument/capability/instrumentOpen/InstrumentOpen.java index e4841dcf..bfeb376c 100644 --- a/src/main/java/com/cstav/genshinstrument/capability/instrumentOpen/InstrumentOpen.java +++ b/src/main/java/com/cstav/genshinstrument/capability/instrumentOpen/InstrumentOpen.java @@ -1,16 +1,25 @@ package com.cstav.genshinstrument.capability.instrumentOpen; import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.entity.player.Player; import net.minecraftforge.common.capabilities.AutoRegisterCapability; +import net.minecraftforge.common.util.LazyOptional; @AutoRegisterCapability public class InstrumentOpen { public static final String NBT_KEY = "instrumentOpen"; private boolean isOpen = false; + public static boolean isOpen(final Player player) { + final LazyOptional oIsOpen = player.getCapability(InstrumentOpenProvider.INSTRUMENT_OPEN); + return oIsOpen.isPresent() && oIsOpen.resolve().get().isOpen; + } + + public boolean isOpen() { return isOpen; } + public void setOpen(final boolean isOpen) { this.isOpen = isOpen; } diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/drum/AratakisGreatAndGloriousDrumScreen.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/drum/AratakisGreatAndGloriousDrumScreen.java index b6898c3d..6a43fb72 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/drum/AratakisGreatAndGloriousDrumScreen.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/drum/AratakisGreatAndGloriousDrumScreen.java @@ -15,7 +15,7 @@ import net.minecraft.client.gui.components.LinearLayoutWidget; import net.minecraft.client.gui.components.LinearLayoutWidget.Orientation; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; +import net.minecraft.world.InteractionHand; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; @@ -28,8 +28,8 @@ public class AratakisGreatAndGloriousDrumScreen extends AbstractInstrumentScreen { public static final String INSTRUMENT_ID = "glorious_drum"; - public AratakisGreatAndGloriousDrumScreen(ItemStack instrument) { - super(instrument); + public AratakisGreatAndGloriousDrumScreen(InteractionHand hand) { + super(hand); } /** @@ -83,7 +83,7 @@ private LinearLayoutWidget createRow(DrumButtonType type, float widthPercent) { return layout; } private NoteButton createButton(DrumButtonType btnType, LinearLayoutWidget container, Key key, boolean isRight) { - final NoteButton btn = new DrumNoteButton(instrument, btnType, isRight, this); + final NoteButton btn = new DrumNoteButton(btnType, isRight, this); container.addChild(btn); notes.put(key.getValue(), btn); diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/drum/DrumNoteButton.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/drum/DrumNoteButton.java index 9ec15ed2..ea89950c 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/drum/DrumNoteButton.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/drum/DrumNoteButton.java @@ -3,7 +3,6 @@ import com.cstav.genshinstrument.client.config.ModClientConfigs; import com.cstav.genshinstrument.client.gui.screens.instrument.partial.note.NoteButton; -import net.minecraft.world.item.ItemStack; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -13,9 +12,9 @@ public class DrumNoteButton extends NoteButton { public final DrumButtonType btnType; public final boolean isRight; - public DrumNoteButton(ItemStack drum, DrumButtonType btnType, boolean isLeft, AratakisGreatAndGloriousDrumScreen drumScreen) { + public DrumNoteButton(DrumButtonType btnType, boolean isLeft, AratakisGreatAndGloriousDrumScreen drumScreen) { super( - drum, btnType.getSound(), + btnType.getSound(), ModClientConfigs.DRUM_LABEL_TYPE.get().getLabelSupplier(), btnType.getIndex(), 2, drumScreen, 13, .34f, 1.01f diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/floralzither/FloralZitherScreen.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/floralzither/FloralZitherScreen.java index 9d6b52b0..7c4f8507 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/floralzither/FloralZitherScreen.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/floralzither/FloralZitherScreen.java @@ -10,7 +10,7 @@ import com.cstav.genshinstrument.sound.NoteSound; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; +import net.minecraft.world.InteractionHand; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; @@ -22,8 +22,8 @@ public class FloralZitherScreen extends AbstractGridInstrumentScreen { public static final String INSTRUMENT_ID = "floral_zither"; - public FloralZitherScreen(ItemStack instrument) { - super(instrument); + public FloralZitherScreen(InteractionHand hand) { + super(hand); } diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/AbstractGridInstrumentScreen.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/AbstractGridInstrumentScreen.java index 9d4dc1ce..cc0ef1a8 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/AbstractGridInstrumentScreen.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/AbstractGridInstrumentScreen.java @@ -7,15 +7,15 @@ import com.cstav.genshinstrument.client.keyMaps.KeyMappings; import net.minecraft.client.gui.components.AbstractWidget; -import net.minecraft.world.item.ItemStack; +import net.minecraft.world.InteractionHand; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @OnlyIn(Dist.CLIENT) public abstract class AbstractGridInstrumentScreen extends AbstractInstrumentScreen { public static final int DEF_ROWS = 7, DEF_COLUMNS = 3; - public AbstractGridInstrumentScreen(ItemStack instrument) { - super(instrument); + public AbstractGridInstrumentScreen(InteractionHand hand) { + super(hand); } public int columns() { @@ -33,7 +33,7 @@ public int rows() { */ public NoteGrid initNoteGrid() { return new NoteGrid( - instrument, rows(), columns(), getSounds(), this + rows(), columns(), getSounds(), this ); } diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/AbstractInstrumentScreen.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/AbstractInstrumentScreen.java index f51704bf..634b333e 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/AbstractInstrumentScreen.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/AbstractInstrumentScreen.java @@ -17,7 +17,7 @@ import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; +import net.minecraft.world.InteractionHand; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -84,11 +84,11 @@ public ResourceLocation getResourceFromRoot(final String path) { protected final AbstractInstrumentOptionsScreen optionsScreen = initInstrumentOptionsScreen(); - public final ItemStack instrument; - public AbstractInstrumentScreen(final ItemStack instrument) { + public final InteractionHand interactionHand; + public AbstractInstrumentScreen(final InteractionHand hand) { super(Component.empty()); - this.instrument = instrument; + interactionHand = hand; optionsScreen.setOnCloseRunnable(this::onOptionsClose); } diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteButton.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteButton.java index b0dbc0e4..fff2e7ec 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteButton.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteButton.java @@ -25,7 +25,6 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundSource; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -55,7 +54,7 @@ public static int getSize() { protected final Minecraft minecraft = Minecraft.getInstance(); public NoteSound sound; - public final ItemStack instrument; + public final AbstractInstrumentScreen instrumentScreen; protected final int noteTextureRow, rowsInNoteTexture; protected final Color colorTheme, pressedColorTheme; @@ -66,7 +65,7 @@ public static int getSize() { //FIXME Actually figure out a formula instead of guessing private float randomAssMultiplier1 = .9f, randomAssMultiplier2 = 1.025f; - public NoteButton(ItemStack instrument, NoteSound sound, + public NoteButton(NoteSound sound, NoteLabelSupplier labelSupplier, int noteTextureRow, int rowsInNoteTexture, AbstractInstrumentScreen instrumentScreen) { super(Button.builder(null, (iAmADissapointmentAndAFailureToMyParents) -> {}) @@ -75,9 +74,10 @@ public NoteButton(ItemStack instrument, NoteSound sound, this.sound = sound; - this.instrument = instrument; this.labelSupplier = labelSupplier; + + this.instrumentScreen = instrumentScreen; colorTheme = instrumentScreen.getThemeLoader().getNoteTheme(); pressedColorTheme = instrumentScreen.getThemeLoader().getPressedNoteTheme(); @@ -89,10 +89,10 @@ public NoteButton(ItemStack instrument, NoteSound sound, this.noteBgLocation = getResourceFromRoot(NOTE_BG_FILENAME); } - public NoteButton(ItemStack instrument, NoteSound sound, + public NoteButton(NoteSound sound, NoteLabelSupplier labelSupplier, int noteTextureRow, int rowsInNoteTexture, AbstractInstrumentScreen instrumentScreen, int noteTextureWidth, float randomAssMultiplier1, float randomAssMultiplier2) { - this(instrument, sound, labelSupplier, noteTextureRow, rowsInNoteTexture, instrumentScreen); + this(sound, labelSupplier, noteTextureRow, rowsInNoteTexture, instrumentScreen); this.noteTextureWidth = noteTextureWidth; this.randomAssMultiplier1 = randomAssMultiplier1; @@ -247,7 +247,7 @@ public void play(final boolean playLocally) { if (playLocally) playDownSound(minecraft.getSoundManager()); - ModPacketHandler.sendToServer(new InstrumentPacket(sound, instrument)); + ModPacketHandler.sendToServer(new InstrumentPacket(sound, instrumentScreen.interactionHand)); locked = true; diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteGrid.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteGrid.java index fb9b1641..77c91e99 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteGrid.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteGrid.java @@ -12,7 +12,6 @@ import net.minecraft.client.gui.components.FrameWidget; import net.minecraft.client.gui.components.GridWidget; import net.minecraft.client.gui.components.GridWidget.RowHelper; -import net.minecraft.world.item.ItemStack; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -30,13 +29,11 @@ public class NoteGrid implements Iterable { private NoteSound[] noteSounds; public final int rows, columns; - public final ItemStack instrument; - public NoteGrid(ItemStack instrument, int rows, int columns, NoteSound[] noteSounds, AbstractInstrumentScreen instrumentScreen) { + public NoteGrid(int rows, int columns, NoteSound[] noteSounds, AbstractInstrumentScreen instrumentScreen) { this.rows = rows; this.columns = columns; this.noteSounds = noteSounds; - this.instrument = instrument; // Construct the note grid notes = new NoteButton[columns][rows]; @@ -62,7 +59,7 @@ public void updatePitch() { } protected NoteButton createNote(int row, int column, AbstractInstrumentScreen instrumentScreen) { - return new NoteGridButton(instrument, row, column, + return new NoteGridButton(row, column, getSoundAt(noteSounds, row, column), getLabelSupplier(), rows , instrumentScreen); } diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteGridButton.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteGridButton.java index 7a8e802e..56553553 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteGridButton.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteGridButton.java @@ -4,7 +4,6 @@ import com.cstav.genshinstrument.client.gui.screens.instrument.partial.note.label.NoteLabelSupplier; import com.cstav.genshinstrument.sound.NoteSound; -import net.minecraft.world.item.ItemStack; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -14,9 +13,9 @@ public class NoteGridButton extends NoteButton { public final int row, column, maxRows; - public NoteGridButton(ItemStack instrument, int row, int column, NoteSound sound, NoteLabelSupplier labelSupplier, int maxRows, + public NoteGridButton(int row, int column, NoteSound sound, NoteLabelSupplier labelSupplier, int maxRows, AbstractInstrumentScreen instrumentScreen) { - super(instrument, sound, labelSupplier, row, maxRows, instrumentScreen); + super(sound, labelSupplier, row, maxRows, instrumentScreen); this.row = row; diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageLyreScreen.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageLyreScreen.java index 1c3e66ed..853fa1d2 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageLyreScreen.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageLyreScreen.java @@ -10,7 +10,7 @@ import com.cstav.genshinstrument.sound.NoteSound; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; +import net.minecraft.world.InteractionHand; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; @@ -22,8 +22,8 @@ public class VintageLyreScreen extends AbstractGridInstrumentScreen { public static final String INSTRUMENT_ID = "vintage_lyre"; - public VintageLyreScreen(ItemStack instrument) { - super(instrument); + public VintageLyreScreen(InteractionHand hand) { + super(hand); } @@ -51,7 +51,7 @@ public NoteSound[] getSounds() { @Override public NoteGrid initNoteGrid() { return new VintageNoteGrid( - instrument, rows(), columns(), getSounds(), this + rows(), columns(), getSounds(), this ); } diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageNoteButton.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageNoteButton.java index 50e8dd24..ee2a5cf7 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageNoteButton.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageNoteButton.java @@ -9,7 +9,6 @@ import net.minecraft.client.gui.GuiComponent; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -19,10 +18,10 @@ public class VintageNoteButton extends NoteGridButton { private final ResourceLocation thingyLocation = getResourceFromRoot("thing.png"); - public VintageNoteButton(final ItemStack instrument, - int row, int column, NoteSound sound, NoteLabelSupplier labelSupplier, AbstractInstrumentScreen instrumentScreen) { + public VintageNoteButton(int row, int column, + NoteSound sound, NoteLabelSupplier labelSupplier, AbstractInstrumentScreen instrumentScreen) { // We know for sure that the lyre is a 7x3 - super(instrument, row, column, sound, labelSupplier, AbstractGridInstrumentScreen.DEF_ROWS, instrumentScreen); + super(row, column, sound, labelSupplier, AbstractGridInstrumentScreen.DEF_ROWS, instrumentScreen); } diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageNoteGrid.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageNoteGrid.java index caf09dc4..9aed623f 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageNoteGrid.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/vintagelyre/VintageNoteGrid.java @@ -5,20 +5,19 @@ import com.cstav.genshinstrument.client.gui.screens.instrument.partial.note.NoteGrid; import com.cstav.genshinstrument.sound.NoteSound; -import net.minecraft.world.item.ItemStack; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @OnlyIn(Dist.CLIENT) public class VintageNoteGrid extends NoteGrid { - public VintageNoteGrid(final ItemStack vintageLyre, int rows, int columns, NoteSound[] sounds, VintageLyreScreen instrumentScreen) { - super(vintageLyre, rows, columns, sounds, instrumentScreen); + public VintageNoteGrid(int rows, int columns, NoteSound[] sounds, VintageLyreScreen instrumentScreen) { + super(rows, columns, sounds, instrumentScreen); } @Override protected NoteButton createNote(int row, int column, AbstractInstrumentScreen screen) { - return new VintageNoteButton(instrument, row, column, + return new VintageNoteButton(row, column, getSoundAt(getNoteSounds(), row, column), getLabelSupplier(), screen ); } diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/windsonglyre/WindsongLyreScreen.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/windsonglyre/WindsongLyreScreen.java index 4dc8ae39..3ad26a6d 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/windsonglyre/WindsongLyreScreen.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/windsonglyre/WindsongLyreScreen.java @@ -9,7 +9,7 @@ import com.cstav.genshinstrument.sound.NoteSound; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; +import net.minecraft.world.InteractionHand; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; @@ -21,8 +21,8 @@ public class WindsongLyreScreen extends AbstractGridInstrumentScreen { public static final String INSTRUMENT_ID = "windsong_lyre"; - public WindsongLyreScreen(ItemStack instrument) { - super(instrument); + public WindsongLyreScreen(InteractionHand hand) { + super(hand); } diff --git a/src/main/java/com/cstav/genshinstrument/event/ClientEvents.java b/src/main/java/com/cstav/genshinstrument/event/ClientEvents.java new file mode 100644 index 00000000..aa8f4d8d --- /dev/null +++ b/src/main/java/com/cstav/genshinstrument/event/ClientEvents.java @@ -0,0 +1,34 @@ +package com.cstav.genshinstrument.event; + +import com.cstav.genshinstrument.Main; +import com.cstav.genshinstrument.client.gui.screens.instrument.partial.AbstractInstrumentScreen; +import com.cstav.genshinstrument.item.InstrumentItem; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.Screen; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.event.TickEvent.ClientTickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod.EventBusSubscriber; +import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; + +@OnlyIn(Dist.CLIENT) +@EventBusSubscriber(bus = Bus.FORGE, modid = Main.MODID, value = Dist.CLIENT) +public class ClientEvents { + + @SubscribeEvent + public static void onPlayerTick(final ClientTickEvent event) { + final Minecraft minecraft = Minecraft.getInstance(); + + final Screen screen = minecraft.screen; + if (!(screen instanceof AbstractInstrumentScreen)) + return; + + if (minecraft.player.getItemInHand(((AbstractInstrumentScreen)(screen)).interactionHand).getItem() instanceof InstrumentItem) + return; + + screen.onClose(); + } + +} diff --git a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java index 0ac28979..4898c85b 100644 --- a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java +++ b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java @@ -1,8 +1,11 @@ package com.cstav.genshinstrument.event; +import javax.annotation.Nullable; + import com.cstav.genshinstrument.sound.NoteSound; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.InteractionHand; import net.minecraft.world.item.ItemStack; import net.minecraftforge.eventbus.api.Event; @@ -14,16 +17,26 @@ public class InstrumentPlayedEvent extends Event { public final NoteSound sound; public final ServerPlayer player; - //FIXME: Switch with InteractionHand. ItemStack updates like a sht - //TODO: Make it so that if the item does not exist anymore in the player's hand, close the instrument's menu. - // It will be possible when the above will be fixed - @Deprecated(forRemoval = true) + /** + * The hand where the instrument is at. + * Null for when the sound was not produced by a player. + */ + @Nullable + public final InteractionHand hand; + + /** + * The value of {@code player.getItemInHand(hand)}. + * Null for when the sound was not produced by a player. + */ + @Nullable public final ItemStack instrument; - public InstrumentPlayedEvent(ServerPlayer player, NoteSound sound, ItemStack instrument) { + public InstrumentPlayedEvent(ServerPlayer player, NoteSound sound, @Nullable InteractionHand hand) { this.player = player; this.sound = sound; - this.instrument = instrument; + this.hand = hand; + + instrument = (hand == null) ? null : player.getItemInHand(hand); } } diff --git a/src/main/java/com/cstav/genshinstrument/item/InstrumentItem.java b/src/main/java/com/cstav/genshinstrument/item/InstrumentItem.java index 3c875671..7532f807 100644 --- a/src/main/java/com/cstav/genshinstrument/item/InstrumentItem.java +++ b/src/main/java/com/cstav/genshinstrument/item/InstrumentItem.java @@ -38,15 +38,15 @@ public InstrumentItem(final ServerPlayerRunnable onOpenRequest) { this.onOpenRequest = onOpenRequest; } - static void sendOpenRequest(ServerPlayer player, ItemStack instrument, String instrumentType) { - ModPacketHandler.sendToClient(new OpenInstrumentPacket(instrumentType, instrument), player); + static void sendOpenRequest(ServerPlayer player, InteractionHand hand, String instrumentType) { + ModPacketHandler.sendToClient(new OpenInstrumentPacket(instrumentType, hand), player); } @Override public InteractionResultHolder use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) { if (!pLevel.isClientSide) { - onOpenRequest.run((ServerPlayer)pPlayer, pPlayer.getItemInHand(pUsedHand)); + onOpenRequest.run((ServerPlayer)pPlayer, pUsedHand); // Update the the capabilty on server InstrumentOpenProvider.setOpen(pPlayer, true); @@ -75,6 +75,6 @@ public void initializeClient(Consumer consumer) { @FunctionalInterface public static interface ServerPlayerRunnable { - void run(final ServerPlayer player, final ItemStack instrument); + void run(final ServerPlayer player, final InteractionHand hand); } } diff --git a/src/main/java/com/cstav/genshinstrument/item/ModItems.java b/src/main/java/com/cstav/genshinstrument/item/ModItems.java index bfd4754a..56f93cfc 100644 --- a/src/main/java/com/cstav/genshinstrument/item/ModItems.java +++ b/src/main/java/com/cstav/genshinstrument/item/ModItems.java @@ -24,23 +24,23 @@ public static void register(final IEventBus bus) { public static final RegistryObject WINDSONG_LYRE = ITEMS.register("windsong_lyre", () -> new InstrumentItem( - (player, instrument) -> InstrumentItem.sendOpenRequest(player, instrument, "windsong_lyre") + (player, hand) -> InstrumentItem.sendOpenRequest(player, hand, "windsong_lyre") ) ), VINTAGE_LYRE = ITEMS.register("vintage_lyre", () -> new InstrumentItem( - (player, instrument) -> InstrumentItem.sendOpenRequest(player, instrument, "vintage_lyre") + (player, hand) -> InstrumentItem.sendOpenRequest(player, hand, "vintage_lyre") ) ), FLORAL_ZITHER = ITEMS.register("floral_zither", () -> new InstrumentItem( - (player, instrument) -> InstrumentItem.sendOpenRequest(player, instrument, "floral_zither") + (player, hand) -> InstrumentItem.sendOpenRequest(player, hand, "floral_zither") ) ), GLORIOUS_DRUM = ITEMS.register("glorious_drum", () -> new InstrumentItem( - (player, instrument) -> InstrumentItem.sendOpenRequest(player, instrument, "glorious_drum") + (player, hand) -> InstrumentItem.sendOpenRequest(player, hand, "glorious_drum") ) ) ; diff --git a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java index be4f4d27..2fe121f7 100644 --- a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java +++ b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java @@ -1,22 +1,16 @@ package com.cstav.genshinstrument.networking.packets.instrument; -import java.util.List; -import java.util.Optional; import java.util.function.Supplier; import com.cstav.genshinstrument.capability.instrumentOpen.InstrumentOpen; -import com.cstav.genshinstrument.capability.instrumentOpen.InstrumentOpenProvider; import com.cstav.genshinstrument.event.InstrumentPlayedEvent; import com.cstav.genshinstrument.networking.ModPacket; -import com.cstav.genshinstrument.networking.ModPacketHandler; import com.cstav.genshinstrument.sound.NoteSound; -import com.cstav.genshinstrument.util.CommonUtil; +import com.cstav.genshinstrument.util.ServerUtil; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; +import net.minecraft.world.InteractionHand; import net.minecraft.world.level.gameevent.GameEvent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.network.NetworkDirection; @@ -24,26 +18,24 @@ public class InstrumentPacket implements ModPacket { public static final NetworkDirection NETWORK_DIRECTION = NetworkDirection.PLAY_TO_SERVER; - - public static final int PLAY_DISTANCE = 16; private final NoteSound sound; - private final ItemStack instrument; + private final InteractionHand hand; - public InstrumentPacket(final NoteSound sound, final ItemStack instrument) { + public InstrumentPacket(final NoteSound sound, final InteractionHand hand) { this.sound = sound; - this.instrument = instrument; + this.hand = hand; } public InstrumentPacket(FriendlyByteBuf buf) { sound = NoteSound.readFromNetwork(buf); - this.instrument = buf.readItem(); + this.hand = buf.readEnum(InteractionHand.class); } @Override public void toBytes(final FriendlyByteBuf buf) { sound.writeToNetwork(buf); - buf.writeItem(instrument); + buf.writeEnum(hand); } @@ -54,32 +46,17 @@ public boolean handle(final Supplier supplier) { context.enqueueWork(() -> { final ServerPlayer player = context.getSender(); - // The player could forcibly be trying to play a sound. - // Dunno how but ig it could happen, but we handle it here - final Optional lyreOpen = player.getCapability(InstrumentOpenProvider.INSTRUMENT_OPEN).resolve(); - if (!lyreOpen.isPresent()) - return; - if (!lyreOpen.get().isOpen()) + if (InstrumentOpen.isOpen(player)) return; - final Level level = player.getLevel(); - - // Send a play packet to everyone in the met distance - final List listeners = CommonUtil.getPlayersInArea(level, - player.getBoundingBox().inflate(PLAY_DISTANCE) - ); - for (final Player listener : listeners) - ModPacketHandler.sendToClient( - new PlayNotePacket(player.blockPosition(), sound, player.getUUID()), (ServerPlayer)listener - ); - + ServerUtil.sendPlayNotePackets(player, sound); // Fire the Forge instrument event - MinecraftForge.EVENT_BUS.post(new InstrumentPlayedEvent(player, sound, instrument)); + MinecraftForge.EVENT_BUS.post(new InstrumentPlayedEvent(player, sound, hand)); // Trigger an instrument game event // This is done so that sculk sensors can pick up the instrument's sound - level.gameEvent(GameEvent.INSTRUMENT_PLAY, player.blockPosition(), GameEvent.Context.of(player)); + player.level.gameEvent(GameEvent.INSTRUMENT_PLAY, player.blockPosition(), GameEvent.Context.of(player)); }); return true; diff --git a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/OpenInstrumentPacket.java b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/OpenInstrumentPacket.java index 95e7261c..a3372dc9 100644 --- a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/OpenInstrumentPacket.java +++ b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/OpenInstrumentPacket.java @@ -13,7 +13,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.world.item.ItemStack; +import net.minecraft.world.InteractionHand; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.network.NetworkDirection; @@ -21,7 +21,7 @@ public class OpenInstrumentPacket implements ModPacket { public static final NetworkDirection NETWORK_DIRECTION = NetworkDirection.PLAY_TO_CLIENT; - private static final Map>> OPEN_INSTRUMENT = Map.of( + private static final Map>> OPEN_INSTRUMENT = Map.of( "windsong_lyre", () -> WindsongLyreScreen::new, "vintage_lyre", () -> VintageLyreScreen::new, "floral_zither", () -> FloralZitherScreen::new, @@ -30,21 +30,21 @@ public class OpenInstrumentPacket implements ModPacket { private final String instrumentType; - private final ItemStack instrument; - public OpenInstrumentPacket(final String instrumentScreen, final ItemStack instrument) { + private final InteractionHand hand; + public OpenInstrumentPacket(final String instrumentScreen, final InteractionHand hand) { this.instrumentType = instrumentScreen; - this.instrument = instrument; + this.hand = hand; } public OpenInstrumentPacket(FriendlyByteBuf buf) { instrumentType = buf.readUtf(); - instrument = buf.readItem(); + hand = buf.readEnum(InteractionHand.class); } @Override public void toBytes(FriendlyByteBuf buf) { buf.writeUtf(instrumentType); - buf.writeItem(instrument); + buf.writeEnum(hand); } @@ -54,7 +54,7 @@ public boolean handle(final Supplier supplier) { context.enqueueWork(() -> { DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> - Minecraft.getInstance().setScreen(OPEN_INSTRUMENT.get(instrumentType).get().apply(instrument))); + Minecraft.getInstance().setScreen(OPEN_INSTRUMENT.get(instrumentType).get().apply(hand))); }); return true; diff --git a/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java b/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java new file mode 100644 index 00000000..775faf9f --- /dev/null +++ b/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java @@ -0,0 +1,26 @@ +package com.cstav.genshinstrument.util; + +import java.util.List; + +import com.cstav.genshinstrument.networking.ModPacketHandler; +import com.cstav.genshinstrument.networking.packets.instrument.PlayNotePacket; +import com.cstav.genshinstrument.sound.NoteSound; + +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.player.Player; + +public class ServerUtil { + public static final int PLAY_DISTANCE = 16; + + + public static void sendPlayNotePackets(final ServerPlayer player, final NoteSound sound) { + final List listeners = CommonUtil.getPlayersInArea(player.level, + player.getBoundingBox().inflate(PLAY_DISTANCE) + ); + for (final Player listener : listeners) + ModPacketHandler.sendToClient( + new PlayNotePacket(player.blockPosition(), sound, player.getUUID()), (ServerPlayer)listener + ); + } + +} From c0a6ef9a24304861fe6485756ab9746d38d13e5e Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Tue, 9 May 2023 12:07:13 +0300 Subject: [PATCH 3/9] Fixed instrument packets being ignored --- .../networking/packets/instrument/InstrumentPacket.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java index 2fe121f7..a8e9ad12 100644 --- a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java +++ b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java @@ -46,7 +46,7 @@ public boolean handle(final Supplier supplier) { context.enqueueWork(() -> { final ServerPlayer player = context.getSender(); - if (InstrumentOpen.isOpen(player)) + if (!InstrumentOpen.isOpen(player)) return; ServerUtil.sendPlayNotePackets(player, sound); From 58269c04e27dbb7e6be7e1ea9d424f5d3ce5ded1 Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Tue, 9 May 2023 14:36:56 +0300 Subject: [PATCH 4/9] Bump version to 2.4.1 --- build.gradle | 2 +- public/updates.json | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 1c903245..7906367e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'org.parchmentmc.librarian.forgegradle' version '1.+' } -version = '2.4' +version = '2.4.1' group = 'com.cstav.genshinstrument' // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = 'genshinstrument-1.19.3' diff --git a/public/updates.json b/public/updates.json index bafb1439..3a0fa7f1 100644 --- a/public/updates.json +++ b/public/updates.json @@ -12,11 +12,12 @@ "2.3": "- More texture fixes\n- Better API implementation", - "2.4": "- Minor techincal fixes" + "2.4": "- Minor techincal fixes", + "2.4.1": "Fixed instrument screen being present after item removal" }, "promos": { - "1.19.3-latest": "2.4", - "1.19.3-recommended": "2.4" + "1.19.3-latest": "2.4.1", + "1.19.3-recommended": "2.4.1" } } \ No newline at end of file From b712633a614b41da5ce00a7d7c9116bbb5b77422 Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Tue, 9 May 2023 14:53:52 +0300 Subject: [PATCH 5/9] Bumped protocol version to 3.5 --- .../com/cstav/genshinstrument/event/InstrumentPlayedEvent.java | 2 +- .../com/cstav/genshinstrument/networking/ModPacketHandler.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java index 4898c85b..595a7ca8 100644 --- a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java +++ b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java @@ -25,7 +25,7 @@ public class InstrumentPlayedEvent extends Event { public final InteractionHand hand; /** - * The value of {@code player.getItemInHand(hand)}. + * The value of {@link ServerPlayer#getItemInHand player.getItemInHand(hand)}. * Null for when the sound was not produced by a player. */ @Nullable diff --git a/src/main/java/com/cstav/genshinstrument/networking/ModPacketHandler.java b/src/main/java/com/cstav/genshinstrument/networking/ModPacketHandler.java index 659e71e1..363344b9 100644 --- a/src/main/java/com/cstav/genshinstrument/networking/ModPacketHandler.java +++ b/src/main/java/com/cstav/genshinstrument/networking/ModPacketHandler.java @@ -30,7 +30,7 @@ public class ModPacketHandler { }); - private static final String PROTOCOL_VERSION = "3"; + private static final String PROTOCOL_VERSION = "3.5"; private static int id; public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( From 58bf2cb98695b23f7a3323eaff97bb5e747a0327 Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Tue, 9 May 2023 17:31:09 +0300 Subject: [PATCH 6/9] Client note packet now sendable from non-player --- .../instrument/partial/note/NoteButton.java | 2 +- .../packets/instrument/PlayNotePacket.java | 12 +++++++++--- .../com/cstav/genshinstrument/util/ServerUtil.java | 14 +++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteButton.java b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteButton.java index fff2e7ec..6002640b 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteButton.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screens/instrument/partial/note/NoteButton.java @@ -272,7 +272,7 @@ public void playDownSound(SoundManager pHandler) { * A method for packets to use for playing this note on the client's end. * If {@link Minecraft#player this player} is the same as the gives player, * the method will only stop the client's background music per preference. - * @param playerUUID The UUID of the player who initiated the sound + * @param playerUUID The UUID of the player who initiated the sound. Null for when it wasn't a player. * @param pos The position at which the sound was fired from */ public static void playNoteAtPos(final NoteSound sound, final UUID playerUUID, final BlockPos pos) { diff --git a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/PlayNotePacket.java b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/PlayNotePacket.java index f7d29e47..4af03bcb 100644 --- a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/PlayNotePacket.java +++ b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/PlayNotePacket.java @@ -3,6 +3,8 @@ import java.util.UUID; import java.util.function.Supplier; +import javax.annotation.Nullable; + import com.cstav.genshinstrument.client.gui.screens.instrument.partial.note.NoteButton; import com.cstav.genshinstrument.networking.ModPacket; import com.cstav.genshinstrument.sound.NoteSound; @@ -18,8 +20,9 @@ public class PlayNotePacket implements ModPacket { private final BlockPos blockPos; private final NoteSound sound; + @Nullable private final UUID playerUUID; - public PlayNotePacket(final BlockPos pos, final NoteSound sound, final UUID playerUUID) { + public PlayNotePacket(final BlockPos pos, final NoteSound sound, @Nullable final UUID playerUUID) { this.blockPos = pos; this.sound = sound; this.playerUUID = playerUUID; @@ -27,14 +30,17 @@ public PlayNotePacket(final BlockPos pos, final NoteSound sound, final UUID play public PlayNotePacket(FriendlyByteBuf buf) { blockPos = buf.readBlockPos(); sound = NoteSound.readFromNetwork(buf); - playerUUID = buf.readUUID(); + playerUUID = buf.readBoolean() ? buf.readUUID() : null; } @Override public void toBytes(FriendlyByteBuf buf) { buf.writeBlockPos(blockPos); sound.writeToNetwork(buf); - buf.writeUUID(playerUUID); + + buf.writeBoolean(playerUUID != null); + if (playerUUID != null) + buf.writeUUID(playerUUID); } diff --git a/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java b/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java index 775faf9f..271af6ef 100644 --- a/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java +++ b/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java @@ -6,14 +6,17 @@ import com.cstav.genshinstrument.networking.packets.instrument.PlayNotePacket; import com.cstav.genshinstrument.sound.NoteSound; +import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; +import net.minecraft.world.phys.AABB; public class ServerUtil { public static final int PLAY_DISTANCE = 16; - public static void sendPlayNotePackets(final ServerPlayer player, final NoteSound sound) { + public static void sendPlayNotePackets(final Player player, final NoteSound sound) { final List listeners = CommonUtil.getPlayersInArea(player.level, player.getBoundingBox().inflate(PLAY_DISTANCE) ); @@ -22,5 +25,14 @@ public static void sendPlayNotePackets(final ServerPlayer player, final NoteSoun new PlayNotePacket(player.blockPosition(), sound, player.getUUID()), (ServerPlayer)listener ); } + public static void sendPlayNotePackets(final Level level, final BlockPos pos, final NoteSound sound) { + final List listeners = CommonUtil.getPlayersInArea(level, + new AABB(pos).inflate(PLAY_DISTANCE) + ); + for (final Player listener : listeners) + ModPacketHandler.sendToClient( + new PlayNotePacket(pos, sound, null), (ServerPlayer)listener + ); + } } From a8efb5ec7e2aae50beb052fcfbaed3983de01655 Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Tue, 9 May 2023 19:34:20 +0300 Subject: [PATCH 7/9] Seperated InstrumentPlayedEvent to players and gen --- .../genshinstrument/criteria/ModCriteria.java | 2 +- .../event/InstrumentPlayedEvent.java | 47 +++++++++++-------- .../packets/instrument/InstrumentPacket.java | 12 +---- .../genshinstrument/util/ServerUtil.java | 43 +++++++++++++---- 4 files changed, 63 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/cstav/genshinstrument/criteria/ModCriteria.java b/src/main/java/com/cstav/genshinstrument/criteria/ModCriteria.java index 7e3a80e1..9b46cd0a 100644 --- a/src/main/java/com/cstav/genshinstrument/criteria/ModCriteria.java +++ b/src/main/java/com/cstav/genshinstrument/criteria/ModCriteria.java @@ -16,7 +16,7 @@ public class ModCriteria { public static final InstrumentPlayedTrigger INSTRUMENT_PLAYED_TRIGGER = register(new InstrumentPlayedTrigger()); @SubscribeEvent - public static void onInstrumentPlayed(final InstrumentPlayedEvent event) { + public static void onInstrumentPlayed(final InstrumentPlayedEvent.ByPlayer event) { INSTRUMENT_PLAYED_TRIGGER.trigger(event.player, event.instrument); } diff --git a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java index 595a7ca8..ad6b8d3f 100644 --- a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java +++ b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java @@ -4,9 +4,12 @@ import com.cstav.genshinstrument.sound.NoteSound; +import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.Event; /** @@ -14,29 +17,33 @@ * This event is fired on the Forge event bus, on the logical server. */ public class InstrumentPlayedEvent extends Event { - + public final NoteSound sound; - public final ServerPlayer player; - /** - * The hand where the instrument is at. - * Null for when the sound was not produced by a player. - */ - @Nullable - public final InteractionHand hand; - - /** - * The value of {@link ServerPlayer#getItemInHand player.getItemInHand(hand)}. - * Null for when the sound was not produced by a player. - */ - @Nullable - public final ItemStack instrument; - - public InstrumentPlayedEvent(ServerPlayer player, NoteSound sound, @Nullable InteractionHand hand) { - this.player = player; + public final Level level; + public final BlockPos pos; + + public InstrumentPlayedEvent(NoteSound sound, final Level level, final BlockPos pos) { this.sound = sound; - this.hand = hand; + this.level = level; + this.pos = pos; + } + + + public static final class ByPlayer extends InstrumentPlayedEvent { + public final ServerPlayer player; + public final InteractionHand hand; + public final ItemStack instrument; + + public ByPlayer(NoteSound sound, ServerPlayer player, @Nullable InteractionHand hand) { + super(sound, player.level, player.blockPosition()); + this.player = player; + this.hand = hand; + + instrument = (hand == null) ? null : player.getItemInHand(hand); - instrument = (hand == null) ? null : player.getItemInHand(hand); + // Also post it on the generalized event + MinecraftForge.EVENT_BUS.post((InstrumentPlayedEvent)this); + } } } diff --git a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java index a8e9ad12..9cca06a2 100644 --- a/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java +++ b/src/main/java/com/cstav/genshinstrument/networking/packets/instrument/InstrumentPacket.java @@ -3,7 +3,6 @@ import java.util.function.Supplier; import com.cstav.genshinstrument.capability.instrumentOpen.InstrumentOpen; -import com.cstav.genshinstrument.event.InstrumentPlayedEvent; import com.cstav.genshinstrument.networking.ModPacket; import com.cstav.genshinstrument.sound.NoteSound; import com.cstav.genshinstrument.util.ServerUtil; @@ -11,8 +10,6 @@ import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; -import net.minecraft.world.level.gameevent.GameEvent; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.network.NetworkDirection; import net.minecraftforge.network.NetworkEvent.Context; @@ -49,14 +46,7 @@ public boolean handle(final Supplier supplier) { if (!InstrumentOpen.isOpen(player)) return; - ServerUtil.sendPlayNotePackets(player, sound); - - // Fire the Forge instrument event - MinecraftForge.EVENT_BUS.post(new InstrumentPlayedEvent(player, sound, hand)); - - // Trigger an instrument game event - // This is done so that sculk sensors can pick up the instrument's sound - player.level.gameEvent(GameEvent.INSTRUMENT_PLAY, player.blockPosition(), GameEvent.Context.of(player)); + ServerUtil.sendPlayNotePackets(player, hand, sound); }); return true; diff --git a/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java b/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java index 271af6ef..2c2094cf 100644 --- a/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java +++ b/src/main/java/com/cstav/genshinstrument/util/ServerUtil.java @@ -2,37 +2,62 @@ import java.util.List; +import com.cstav.genshinstrument.event.InstrumentPlayedEvent; import com.cstav.genshinstrument.networking.ModPacketHandler; import com.cstav.genshinstrument.networking.packets.instrument.PlayNotePacket; import com.cstav.genshinstrument.sound.NoteSound; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.phys.AABB; +import net.minecraftforge.common.MinecraftForge; public class ServerUtil { public static final int PLAY_DISTANCE = 16; - public static void sendPlayNotePackets(final Player player, final NoteSound sound) { - final List listeners = CommonUtil.getPlayersInArea(player.level, - player.getBoundingBox().inflate(PLAY_DISTANCE) - ); - for (final Player listener : listeners) + public static void sendPlayNotePackets(ServerPlayer player, InteractionHand hand, NoteSound sound) { + for (final Player listener : noteListeners(player.level, player.blockPosition())) ModPacketHandler.sendToClient( new PlayNotePacket(player.blockPosition(), sound, player.getUUID()), (ServerPlayer)listener ); + + // Trigger an instrument game event + // This is done so that sculk sensors can pick up the instrument's sound + player.level.gameEvent( + GameEvent.INSTRUMENT_PLAY, player.blockPosition(), + GameEvent.Context.of(player) + ); + + MinecraftForge.EVENT_BUS.post(new InstrumentPlayedEvent.ByPlayer(sound, player, hand)); } public static void sendPlayNotePackets(final Level level, final BlockPos pos, final NoteSound sound) { - final List listeners = CommonUtil.getPlayersInArea(level, - new AABB(pos).inflate(PLAY_DISTANCE) - ); - for (final Player listener : listeners) + for (final Player listener : noteListeners(level, pos)) ModPacketHandler.sendToClient( new PlayNotePacket(pos, sound, null), (ServerPlayer)listener ); + + final BlockState bs = level.getBlockState(pos); + if (bs != Blocks.AIR.defaultBlockState()) + level.gameEvent( + GameEvent.INSTRUMENT_PLAY, pos, + GameEvent.Context.of(bs) + ); + else + level.gameEvent(null, GameEvent.INSTRUMENT_PLAY, pos); + + MinecraftForge.EVENT_BUS.post(new InstrumentPlayedEvent(sound, level, pos)); + } + private static List noteListeners(Level level, BlockPos pos) { + return CommonUtil.getPlayersInArea(level, + new AABB(pos).inflate(PLAY_DISTANCE) + ); } } From 7075a5caca0b81a806ab108979a6d021ba6cb229 Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Tue, 9 May 2023 19:56:14 +0300 Subject: [PATCH 8/9] Apperantly events cannot be chained --- .../cstav/genshinstrument/event/InstrumentPlayedEvent.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java index ad6b8d3f..57183109 100644 --- a/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java +++ b/src/main/java/com/cstav/genshinstrument/event/InstrumentPlayedEvent.java @@ -9,7 +9,6 @@ import net.minecraft.world.InteractionHand; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.Event; /** @@ -40,9 +39,6 @@ public ByPlayer(NoteSound sound, ServerPlayer player, @Nullable InteractionHand this.hand = hand; instrument = (hand == null) ? null : player.getItemInHand(hand); - - // Also post it on the generalized event - MinecraftForge.EVENT_BUS.post((InstrumentPlayedEvent)this); } } From 90797f414d7d802b97ef6c8e98d2d100648ac82d Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Tue, 9 May 2023 20:06:09 +0300 Subject: [PATCH 9/9] Update updates.json --- public/updates.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public/updates.json b/public/updates.json index 3a0fa7f1..00f29b56 100644 --- a/public/updates.json +++ b/public/updates.json @@ -3,17 +3,17 @@ "1.19.3": { "1.0": "Initial release", - "1.0.1": "Fixed resources folder and package names being incorrect, implemented Forge's updates mechanism", + "1.0.1": "- Fixed resources folder and package names being incorrect\n- implemented Forge's updates mechanism", "2.0": "- New instrument: Arataki's Great and Glorious Drum!\n- Russian language support", "2.2": "- New recipes\n- New advancements", - "2.2.1": "Texture fixes", + "2.2.1": "- Texture fixes", "2.3": "- More texture fixes\n- Better API implementation", "2.4": "- Minor techincal fixes", - "2.4.1": "Fixed instrument screen being present after item removal" + "2.4.1": "- Fixed instrument screen being present after item removal\n- API tweaks & fixes" }, "promos": {