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 a076647 + e279aff commit e24c087
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 76 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ parchment_mappings=1.20.1:2023.07.02
loader_version=0.14.21

# Mod Properties
mod_version=3.2.2.1
mod_version=3.2.3
maven_group=com.cstav.genshinstrument
archives_base_name=genshinstrument

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/cstav/genshinstrument/GInstrumentMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.cstav.genshinstrument.block.ModBlockEntities;
import com.cstav.genshinstrument.block.ModBlocks;
import com.cstav.genshinstrument.criteria.ModCriteria;
import com.cstav.genshinstrument.event.ServerEvents;
import com.cstav.genshinstrument.item.ModItems;
import com.cstav.genshinstrument.networking.ModPacketHandler;
import com.cstav.genshinstrument.sound.ModSounds;
Expand All @@ -19,6 +20,8 @@ public class GInstrumentMod implements ModInitializer {
@Override
public void onInitialize() {
ModPacketHandler.registerServerPackets();
ServerEvents.register();

ModCriteria.register();

ModSounds.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.cstav.genshinstrument.networking.ModPacketHandler;
import com.cstav.genshinstrument.networking.OpenInstrumentPacketSender;
import com.cstav.genshinstrument.networking.packets.instrument.NotifyInstrumentOpenPacket;
import com.cstav.genshinstrument.util.ModEntityData;
import com.cstav.genshinstrument.util.InstrumentEntityData;
import com.cstav.genshinstrument.util.ServerUtil;

import net.fabricmc.api.EnvType;
Expand Down Expand Up @@ -70,7 +70,7 @@ public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState

for (final Player player : pLevel.players()) {
ibe.users.forEach((user) -> {
ModEntityData.setInstrumentClosed(pLevel.getPlayerByUUID(user));
InstrumentEntityData.setClosed(pLevel.getPlayerByUUID(user));
ModPacketHandler.sendToClient(new NotifyInstrumentOpenPacket(user, false), (ServerPlayer)player);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.cstav.genshinstrument.client.gui.screens.instrument.windsonglyre.WindsongLyreScreen;
import com.cstav.genshinstrument.event.ClientEvents;
import com.cstav.genshinstrument.event.ResourcesLoadedEvent;
import com.cstav.genshinstrument.item.ItemPoseModifier;
import com.cstav.genshinstrument.networking.ModPacketHandler;

import fuzs.forgeconfigapiport.api.config.v2.ForgeConfigRegistry;
Expand All @@ -31,9 +32,11 @@ public class ClientInitiator implements ClientModInitializer {
@Override
public void onInitializeClient() {
ModPacketHandler.registerClientPackets();

ForgeConfigRegistry.INSTANCE.register(GInstrumentMod.MODID, ModConfig.Type.CLIENT, ModClientConfigs.CONFIGS);

ClientEvents.register();
ItemPoseModifier.register();


// Load necessary classes, as listed above
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.cstav.genshinstrument.event.PosePlayerArmEvent.HandType;
import com.cstav.genshinstrument.event.PosePlayerArmEvent.PosePlayerArmEventArgs;
import com.cstav.genshinstrument.util.ModEntityData;
import com.cstav.genshinstrument.util.InstrumentEntityData;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -13,7 +13,7 @@ public abstract class ModArmPose {
public static final float HAND_HEIGHT_ROT = .9f;

public static void poseForItemInstrument(final PosePlayerArmEventArgs args) {
if (!ModEntityData.isInstrumentOpen(args.player) || !ModEntityData.isInstrumentItem(args.player))
if (!InstrumentEntityData.isOpen(args.player) || !InstrumentEntityData.isItem(args.player))
return;

final ModelPart arm = args.arm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
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.packets.instrument.CloseInstrumentPacket;
import com.cstav.genshinstrument.sound.NoteSound;
import com.cstav.genshinstrument.util.ModEntityData;
import com.cstav.genshinstrument.util.InstrumentEntityData;
import com.mojang.blaze3d.platform.InputConstants.Key;
import com.mojang.blaze3d.platform.InputConstants.Type;

Expand All @@ -29,23 +28,20 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;

@Environment(EnvType.CLIENT)
public abstract class AbstractInstrumentScreen extends Screen {
public static final String[] DEFAULT_NOTE_LAYOUT = new String[] {"C", "D", "E", "F", "G", "A", "B"};

@SuppressWarnings("resource")
public int getNoteSize() {
final int guiScale = Minecraft.getInstance().options.guiScale().get();

return switch (guiScale) {
return switch (Minecraft.getInstance().options.guiScale().get()) {
case 0 -> 40;
case 1 -> 35;
case 2 -> 46;
case 3 -> 48;
case 4 -> 41;
default -> guiScale * 18;
case 4 -> 40;
default -> 20;
};
}

Expand Down Expand Up @@ -106,25 +102,9 @@ public String[] noteLayout() {
* 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 (!ModEntityData.isInstrumentOpen(player)) {
public void handleAbruptClosing() {
if (!InstrumentEntityData.isOpen(minecraft.player))
onClose(false);
return true;
}

// 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 (
(ModEntityData.isInstrumentItem(player) && interactionHand.isPresent())
&& !(player.getItemInHand(interactionHand.get()).getItem() instanceof InstrumentItem)
) {
onClose(true);
return true;
}

return false;
}


Expand Down Expand Up @@ -306,7 +286,7 @@ public void onClose() {
}
public void onClose(final boolean notify) {
if (notify) {
ModEntityData.setInstrumentClosed(minecraft.player);
InstrumentEntityData.setClosed(minecraft.player);
ModPacketHandler.sendToServer(new CloseInstrumentPacket());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.cstav.genshinstrument.networking.buttonidentifier.NoteButtonIdentifier;
import com.cstav.genshinstrument.networking.packets.instrument.InstrumentPacket;
import com.cstav.genshinstrument.sound.NoteSound;
import com.cstav.genshinstrument.util.ModEntityData;
import com.cstav.genshinstrument.util.InstrumentEntityData;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down Expand Up @@ -153,9 +153,9 @@ public void play() {

final Player player = minecraft.player;

final BlockPos pos = ModEntityData.isInstrumentItem(player)
final BlockPos pos = InstrumentEntityData.isItem(player)
? player.blockPosition()
: ModEntityData.getInstrumentBlockPos(player);
: InstrumentEntityData.getBlockPos(player);

// Send sound packet to server
ModPacketHandler.sendToServer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.cstav.genshinstrument.event.InstrumentPlayedEvent.InstrumentPlayedEventArgs;
import com.cstav.genshinstrument.event.PosePlayerArmEvent.PosePlayerArmEventArgs;
import com.cstav.genshinstrument.sound.NoteSound;
import com.cstav.genshinstrument.util.ModEntityData;
import com.cstav.genshinstrument.util.InstrumentEntityData;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -32,11 +32,11 @@ public static void register() {
public static void posePlayerArmEvent(final PosePlayerArmEventArgs args) {
final Player player = args.player;

if (!ModEntityData.isInstrumentOpen(player) || ModEntityData.isInstrumentItem(player))
if (!InstrumentEntityData.isOpen(player) || InstrumentEntityData.isItem(player))
return;


final Block block = player.level().getBlockState(ModEntityData.getInstrumentBlockPos(player)).getBlock();
final Block block = player.level().getBlockState(InstrumentEntityData.getBlockPos(player)).getBlock();
if (block instanceof AbstractInstrumentBlock blockInstrument)
blockInstrument.onPosePlayerArm(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.cstav.genshinstrument.event.impl.ModEvent;
import com.cstav.genshinstrument.networking.buttonidentifier.NoteButtonIdentifier;
import com.cstav.genshinstrument.sound.NoteSound;
import com.cstav.genshinstrument.util.ModEntityData;
import com.cstav.genshinstrument.util.InstrumentEntityData;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
Expand Down Expand Up @@ -95,7 +95,7 @@ public ByPlayerArgs(NoteSound sound, int pitch, Player player, BlockPos pos, Opt
} else {
itemInstrument = Optional.empty();
this.hand = Optional.empty();
blockInstrumentPos = Optional.ofNullable(ModEntityData.getInstrumentBlockPos(player));
blockInstrumentPos = Optional.ofNullable(InstrumentEntityData.getBlockPos(player));
}
}
}
Expand Down
37 changes: 37 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,37 @@
package com.cstav.genshinstrument.event;

import com.cstav.genshinstrument.item.InstrumentItem;
import com.cstav.genshinstrument.util.CommonUtil;
import com.cstav.genshinstrument.util.InstrumentEntityData;
import com.cstav.genshinstrument.util.ServerUtil;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;

public abstract class ServerEvents {

public static void register() {
ServerTickEvents.START_WORLD_TICK.register(ServerEvents::onServerTick);
}

public static void onServerTick(final Level level) {
level.players().forEach(ServerEvents::handleAbruptInstrumentClose);
}

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

if (InstrumentEntityData.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 (InstrumentEntityData.getBlockPos(player).closerToCenterThan(player.position(), 5))
ServerUtil.setInstrumentClosed(player);
}
}

}
19 changes: 3 additions & 16 deletions src/main/java/com/cstav/genshinstrument/item/InstrumentItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import com.cstav.genshinstrument.client.ModArmPose;
import com.cstav.genshinstrument.client.gui.screens.instrument.partial.AbstractInstrumentScreen;
import com.cstav.genshinstrument.event.PosePlayerArmEvent;
import com.cstav.genshinstrument.event.PosePlayerArmEvent.PosePlayerArmEventArgs;
import com.cstav.genshinstrument.networking.OpenInstrumentPacketSender;
import com.cstav.genshinstrument.util.CommonUtil;
import com.cstav.genshinstrument.util.ServerUtil;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
Expand All @@ -22,7 +19,7 @@
/**
* An item responsible for opening an {@link AbstractInstrumentScreen}.
*/
public class InstrumentItem extends Item {
public class InstrumentItem extends Item implements ItemPoseModifier {

protected final OpenInstrumentPacketSender onOpenRequest;
/**
Expand Down Expand Up @@ -55,19 +52,9 @@ public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, Inte
}


static {
if (FabricLoader.getInstance().getEnvironmentType() != EnvType.SERVER)
PosePlayerArmEvent.EVENT.register(InstrumentItem::itemArmPose);
}
@Environment(EnvType.CLIENT)
private static void itemArmPose(final PosePlayerArmEventArgs args) {
CommonUtil.getItemInHands(InstrumentItem.class, args.player).ifPresent((item) ->
item.onPosePlayerArm(args)
);
}

@Override
@Environment(EnvType.CLIENT)
public void onPosePlayerArm(PosePlayerArmEventArgs args) {
public void onPosePlayerArm(final PosePlayerArmEventArgs args) {
ModArmPose.poseForItemInstrument(args);
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/cstav/genshinstrument/item/ItemPoseModifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cstav.genshinstrument.item;

import com.cstav.genshinstrument.event.PosePlayerArmEvent;
import com.cstav.genshinstrument.event.PosePlayerArmEvent.PosePlayerArmEventArgs;
import com.cstav.genshinstrument.util.CommonUtil;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

public interface ItemPoseModifier {

@Environment(EnvType.CLIENT)
public static void register() {
PosePlayerArmEvent.EVENT.register((args) -> {

CommonUtil.getItemInHands(ItemPoseModifier.class, args.player).ifPresent((item) ->
item.onPosePlayerArm(args)
);

});
}

@Environment(EnvType.CLIENT)
void onPosePlayerArm(final PosePlayerArmEventArgs args);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.cstav.genshinstrument.networking.IModPacket;
import com.cstav.genshinstrument.networking.ModPacketHandler;
import com.cstav.genshinstrument.util.ModEntityData;
import com.cstav.genshinstrument.util.InstrumentEntityData;

import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.network.FriendlyByteBuf;
Expand All @@ -17,7 +17,7 @@ public CloseInstrumentPacket(FriendlyByteBuf buf) {}

@Override
public void handle(Player player, PacketSender responseSender) {
ModEntityData.setInstrumentClosed(player);
InstrumentEntityData.setClosed(player);

for (final Player oPlayer : player.level().players())
ModPacketHandler.sendToClient(new NotifyInstrumentOpenPacket(player.getUUID(), false), (ServerPlayer)oPlayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.cstav.genshinstrument.networking.buttonidentifier.NoteButtonIdentifier;
import com.cstav.genshinstrument.networking.packets.INoteIdentifierSender;
import com.cstav.genshinstrument.sound.NoteSound;
import com.cstav.genshinstrument.util.ModEntityData;
import com.cstav.genshinstrument.util.InstrumentEntityData;
import com.cstav.genshinstrument.util.ServerUtil;

import net.fabricmc.fabric.api.networking.v1.PacketSender;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void write(final FriendlyByteBuf buf) {

@Override
public void handle(Player player, PacketSender responseSender) {
if (!ModEntityData.isInstrumentOpen(player))
if (!InstrumentEntityData.isOpen(player))
return;

ServerUtil.sendPlayNotePackets((ServerPlayer)player, pos, hand, sound, instrumentId, noteIdentifier, pitch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.UUID;

import com.cstav.genshinstrument.networking.IModPacket;
import com.cstav.genshinstrument.util.ModEntityData;
import com.cstav.genshinstrument.util.InstrumentEntityData;

import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -57,12 +57,12 @@ public void handle(Player player, PacketSender responseSender) {
if (isOpen) {

if (pos.isPresent())
ModEntityData.setInstrumentOpen(_player, pos.get());
InstrumentEntityData.setOpen(_player, pos.get());
else
ModEntityData.setInstrumentOpen(_player);
InstrumentEntityData.setOpen(_player);

} else
ModEntityData.setInstrumentClosed(_player);
InstrumentEntityData.setClosed(_player);
}

}
Loading

0 comments on commit e24c087

Please sign in to comment.