Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
StavWasPlayZ committed Aug 6, 2023
2 parents 661b6bd + 2a852d0 commit bab3039
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 31 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod_name=Genshin Instruments
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=CC-BY-NC 4.0
# The mod version. See https://semver.org/
mod_version=3.2.2.1
mod_version=3.2.3
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.cstav.genshinstrument.client.gui.screens.instrument.GenshinConsentScreen;
import com.cstav.genshinstrument.client.gui.screens.instrument.partial.note.NoteButton;
import com.cstav.genshinstrument.client.gui.screens.options.instrument.AbstractInstrumentOptionsScreen;
import com.cstav.genshinstrument.item.InstrumentItem;
import com.cstav.genshinstrument.networking.ModPacketHandler;
import com.cstav.genshinstrument.networking.buttonidentifier.NoteButtonIdentifier;
import com.cstav.genshinstrument.networking.packet.instrument.CloseInstrumentPacket;
Expand All @@ -27,7 +26,6 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

Expand All @@ -38,10 +36,12 @@ public abstract class AbstractInstrumentScreen extends Screen {
@SuppressWarnings("resource")
public int getNoteSize() {
return switch (Minecraft.getInstance().options.guiScale().get()) {
case 0 -> 40;
case 1 -> 35;
case 2 -> 46;
case 3 -> 48;
default -> 40;
case 4 -> 40;
default -> 20;
};
}

Expand Down Expand Up @@ -110,30 +110,9 @@ public boolean isGenshinInstrument() {
* or, if it is an item, if the item has been ripped out of the player's hands.
* @return Whether the instrument has closed as a result of this method
*/
public boolean handleAbruptClosing() {
final Player player = minecraft.player;

if (!InstrumentOpenProvider.isOpen(player)) {
public void handleAbruptClosing() {
if (!InstrumentOpenProvider.isOpen(minecraft.player))
onClose(false);
return true;
}

//TODO handle the below server-side

// Handle item not in hand seperately
// This is done like so because there is no event (that I know of) for when an item is moved/removed
if (InstrumentOpenProvider.isItem(player)) {
if (interactionHand.isPresent()
&& !(player.getItemInHand(interactionHand.get()).getItem() instanceof InstrumentItem)
) {
onClose(true);
return true;
}
} else {
//TODO add rule for block instruments to be in a distance of NoteSound.LOCAL_RANGE to play
}

return false;
}


Expand Down Expand Up @@ -302,8 +281,6 @@ public void onOptionsClose() {
public void onClose() {
onClose(true);
}
//TODO Remove this method
@Deprecated(forRemoval = true)
public void onClose(final boolean notify) {
// This should always be false after the above move to server todo is implemented
if (notify) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/cstav/genshinstrument/event/ServerEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.cstav.genshinstrument.event;

import com.cstav.genshinstrument.GInstrumentMod;
import com.cstav.genshinstrument.capability.instrumentOpen.InstrumentOpenProvider;
import com.cstav.genshinstrument.item.InstrumentItem;
import com.cstav.genshinstrument.util.CommonUtil;
import com.cstav.genshinstrument.util.ServerUtil;

import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.TickEvent.LevelTickEvent;
import net.minecraftforge.event.TickEvent.Phase;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;

@EventBusSubscriber(bus = Bus.FORGE, modid = GInstrumentMod.MODID)
public abstract class ServerEvents {

@SubscribeEvent
public static void onServerTick(final LevelTickEvent event) {
if ((event.phase != Phase.END) && (event.side == LogicalSide.SERVER))
event.level.players().forEach(ServerEvents::handleAbruptInstrumentClose);
}

private static void handleAbruptInstrumentClose(final Player player) {
if (!InstrumentOpenProvider.isOpen(player))
return;

if (InstrumentOpenProvider.isItem(player)) {
// This is done like so because there is no event (that I know of) for when an item is moved/removed
if (CommonUtil.getItemInHands(InstrumentItem.class, player).isEmpty())
ServerUtil.setInstrumentClosed(player);
} else {
// Close an instrument block if the player is too far away
if (!InstrumentOpenProvider.getBlockPos(player).closerToCenterThan(player.position(), 6))
ServerUtil.setInstrumentClosed(player);
}
}

}
20 changes: 19 additions & 1 deletion src/main/java/com/cstav/genshinstrument/util/CommonUtil.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
package com.cstav.genshinstrument.util;

import java.util.List;
import java.util.Optional;

import com.google.common.collect.Lists;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;

public abstract class CommonUtil {

@SuppressWarnings("unchecked")
public static <T> Optional<T> getItemInHands(final Class<T> item, final Player player) {
final Item mainItem = player.getItemInHand(InteractionHand.MAIN_HAND).getItem(),
offItem = player.getItemInHand(InteractionHand.OFF_HAND).getItem();

if (item.isInstance(mainItem))
return Optional.of((T)mainItem);
else if (item.isInstance(offItem))
return Optional.of((T)offItem);

return Optional.empty();
}

/**
* @return What the default level shouldve returned, but without any conditions
*/
public static List<Player> getPlayersInArea(final Level level, final AABB area) {
final List<Player> list = Lists.newArrayList();

for(Player player : level.players())
for (Player player : level.players())
if (area.contains(player.getX(), player.getY(), player.getZ()))
list.add(player);

return list;
}


/**
* @param dir The directory location at which to grab the specified resource
* @param path The desired path to obtain from the {@code dir}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/cstav/genshinstrument/util/ServerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ private static List<Player> noteListeners(Level level, BlockPos pos) {
}


public static void setInstrumentClosed(final Player player) {
// Update the the capabilty on server
InstrumentOpenProvider.setClosed(player);

// And clients
player.level().players().forEach((nearbyPlayer) ->
ModPacketHandler.sendToClient(
new NotifyInstrumentOpenPacket(player.getUUID(), false),
(ServerPlayer)nearbyPlayer
)
);
}


/**
* Gets a {@link NoteButtonIdentifier} as described by the {@code classType} destination.
* Will only return a class type if it is valid and included in the {@code acceptableIdentifiers} list.
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ issueTrackerURL="https://github.com/StavWasPlayZ/Genshin-Instruments/issues/" #o
# The modid of the mod
modId="genshinstrument" #mandatory
# The version number of the mod
version="3.2.2.1" #mandatory
version="3.2.3" #mandatory
# A display name for the mod
displayName="Genshin Instruments" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://docs.minecraftforge.net/en/latest/misc/updatechecker/
Expand Down

0 comments on commit bab3039

Please sign in to comment.