Skip to content

Commit

Permalink
Add support for 1.20.5-1.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhzzzsss committed May 2, 2024
1 parent 06f3497 commit 51e365c
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SongPlayer
A Fabric mod for Minecraft that plays songs with noteblocks.
The current version is for Minecraft 1.20.4.
The current version is for Minecraft 1.20.5 - 1.20.6.

# How to install
You can grab the mod jar from releases section.
Expand Down
23 changes: 12 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
plugins {
id 'fabric-loom' version '1.0-SNAPSHOT'
id 'fabric-loom' version '1.6-SNAPSHOT'
id 'maven-publish'
}

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group

base {
archivesName = project.archives_base_name
}

repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
Expand All @@ -27,8 +27,6 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.
}

processResources {
Expand All @@ -40,27 +38,30 @@ processResources {
}

tasks.withType(JavaCompile).configureEach {
// Minecraft 1.18 (1.18-pre2) upwards uses Java 17.
it.options.release = 17
it.options.release = 21
}

java {
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()

sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

jar {
from("LICENSE") {
rename { "${it}_${project.archivesBaseName}"}
rename { "${it}_${project.base.archivesName.get()}"}
}
}

// configure the maven publication
publishing {
publications {
mavenJava(MavenPublication) {
create("mavenJava", MavenPublication) {
artifactId = project.archives_base_name
from components.java
}
}
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ org.gradle.parallel=true

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.20.4
yarn_mappings=1.20.3+build.1
loader_version=0.15.0
minecraft_version=1.20.6
yarn_mappings=1.20.6+build.1
loader_version=0.15.10

# Mod Properties
mod_version = 3.1.3
mod_version = 3.1.4
maven_group = com.github.hhhzzzsss
archives_base_name = song-player

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.91.1+1.20.4
fabric_version=0.97.8+1.20.6
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.command.CommandSource;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Hand;
Expand Down Expand Up @@ -1030,7 +1032,7 @@ public boolean processCommand(String args) {
return true;
}
String name = String.join(" ", Arrays.copyOfRange(split, 1, split.length));
songPlayerNBT.putString(SongItemUtils.DISPLAY_NAME_KEY, name);
NbtComponent.set(DataComponentTypes.CUSTOM_DATA, stack, nbt -> nbt.getCompound(SongItemUtils.SONG_ITEM_KEY).putString(SongItemUtils.DISPLAY_NAME_KEY, name));
SongItemUtils.addSongItemDisplay(stack);
MC.player.setStackInHand(Hand.MAIN_HAND, stack);
MC.interactionManager.clickCreativeStack(MC.player.getStackInHand(Hand.MAIN_HAND), 36 + MC.player.getInventory().selectedSlot);
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/github/hhhzzzsss/songplayer/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.command.CommandSource;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.LoreComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
Expand Down Expand Up @@ -222,14 +224,10 @@ public static MutableText getStyledText(String str, Style style) {
}

public static void setItemName(ItemStack stack, Text text) {
stack.getOrCreateSubNbt(ItemStack.DISPLAY_KEY).putString(ItemStack.NAME_KEY, Text.Serialization.toJsonString(text));
stack.set(DataComponentTypes.CUSTOM_NAME, text);
}

public static void setItemLore(ItemStack stack, Text... loreLines) {
NbtList lore = new NbtList();
for (Text line : loreLines) {
lore.add(NbtString.of(Text.Serialization.toJsonString(line)));
}
stack.getOrCreateSubNbt(ItemStack.DISPLAY_KEY).put(ItemStack.LORE_KEY, lore);
stack.set(DataComponentTypes.LORE, new LoreComponent(List.of(loreLines)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void addButtons(int y) {

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
this.renderBackground(context, mouseX, mouseY, delta);
super.render(context, mouseX, mouseY, delta);

context.drawCenteredTextWithShadow(textRenderer, this.title, this.width / 2, 40, 0xFFFFFF);

Expand Down Expand Up @@ -86,8 +86,6 @@ else if (loadedText == null) {
else {
unloadedText.drawCenterWithShadow(context, this.width / 2, 60);
}

super.render(context, mouseX, mouseY, delta);
}

public String getNumberColor(double number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.github.hhhzzzsss.songplayer.SongPlayer;
import com.github.hhhzzzsss.songplayer.conversion.SPConverter;
import com.github.hhhzzzsss.songplayer.song.SongLoaderThread;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.CustomModelDataComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -39,7 +41,7 @@ public void run() {
ItemStack newStack;
if (stack.isEmpty()) {
newStack = Items.PAPER.getDefaultStack();
newStack.getOrCreateNbt().putInt("CustomModelData", 751642938);
newStack.set(DataComponentTypes.CUSTOM_MODEL_DATA, new CustomModelDataComponent(751642938));
}
else {
newStack = stack.copy();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.hhhzzzsss.songplayer.item;

import com.github.hhhzzzsss.songplayer.Util;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
Expand All @@ -18,10 +20,10 @@ public class SongItemUtils {

public static ItemStack createSongItem(ItemStack stack, byte[] songData, String filename, String displayName) {
NbtCompound songPlayerNbt = new NbtCompound();
stack.setSubNbt(SONG_ITEM_KEY, songPlayerNbt);
songPlayerNbt.putString(SONG_DATA_KEY, Base64.getEncoder().encodeToString(songData));
songPlayerNbt.putString(FILE_NAME_KEY, filename);
songPlayerNbt.putString(DISPLAY_NAME_KEY, displayName);
NbtComponent.set(DataComponentTypes.CUSTOM_DATA, stack, nbt -> nbt.put(SONG_ITEM_KEY, songPlayerNbt));
addSongItemDisplay(stack);
return stack;
}
Expand All @@ -43,7 +45,12 @@ public static void addSongItemDisplay(ItemStack stack) {
}

public static NbtCompound getSongItemTag(ItemStack stack) {
return stack.getSubNbt(SONG_ITEM_KEY);
NbtCompound nbt = stack.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).copyNbt();
if (nbt.contains(SONG_ITEM_KEY, NbtElement.COMPOUND_TYPE)) {
return (NbtCompound)nbt.get(SONG_ITEM_KEY);
} else {
return null;
}
}

public static boolean isSongItem(ItemStack stack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.hhhzzzsss.songplayer.playing.ProgressDisplay;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.util.math.MatrixStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -12,18 +11,11 @@

@Mixin(InGameHud.class)
public class InGameHudMixin {
@Shadow
private int scaledWidth;

@Shadow
private int scaledHeight;

@Shadow
private int heldItemTooltipFade;

@Inject(method = "render",
at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/RenderSystem;enableBlend()V", ordinal = 3))
private void onRender(DrawContext context, float tickDelta, CallbackInfo ci) {
ProgressDisplay.getInstance().onRenderHUD(context, scaledWidth, scaledHeight, heldItemTooltipFade);
@Inject(at = @At("TAIL"), method = "renderHeldItemTooltip(Lnet/minecraft/client/gui/DrawContext;)V")
private void onRenderHeldItemTooltip(DrawContext context, CallbackInfo ci) {
ProgressDisplay.getInstance().onRenderHUD(context, heldItemTooltipFade);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ public void setText(MutableText bottomText, MutableText topText) {
fade = 100;
}

public void onRenderHUD(DrawContext context, int scaledWidth, int scaledHeight, int heldItemTooltipFade) {
public void onRenderHUD(DrawContext context, int heldItemTooltipFade) {
if (fade <= 0) {
return;
}

int bottomTextWidth = SongPlayer.MC.textRenderer.getWidth(bottomText);
int topTextWidth = SongPlayer.MC.textRenderer.getWidth(topText);
int bottomTextX = (scaledWidth - bottomTextWidth) / 2;
int topTextX = (scaledWidth - topTextWidth) / 2;
int bottomTextY = scaledHeight - 59;
int bottomTextX = (SongPlayer.MC.getWindow().getScaledWidth() - bottomTextWidth) / 2;
int topTextX = (SongPlayer.MC.getWindow().getScaledWidth() - topTextWidth) / 2;
int bottomTextY = SongPlayer.MC.getWindow().getScaledHeight() - 59;
if (!SongPlayer.MC.interactionManager.hasStatusBars()) {
bottomTextY += 14;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import com.github.hhhzzzsss.songplayer.song.*;
import net.minecraft.block.Block;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.BlockStateComponent;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket;
import net.minecraft.text.MutableText;
Expand All @@ -26,6 +29,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.Map;

public class SongHandler {
private static SongHandler instance = null;
Expand Down Expand Up @@ -457,16 +461,11 @@ private void holdNoteblock(int id, int slot) {
((ClientPlayerInteractionManagerAccessor) SongPlayer.MC.interactionManager).invokeSyncSelectedSlot();
int instrument = id/25;
int note = id%25;
NbtCompound nbt = new NbtCompound();
nbt.putString("id", "minecraft:note_block");
nbt.putByte("Count", (byte) 1);
NbtCompound tag = new NbtCompound();
NbtCompound bsTag = new NbtCompound();
bsTag.putString("instrument", instrumentNames[instrument]);
bsTag.putString("note", Integer.toString(note));
tag.put("BlockStateTag", bsTag);
nbt.put("tag", tag);
ItemStack noteblockStack = ItemStack.fromNbt(nbt);
ItemStack noteblockStack = Items.NOTE_BLOCK.getDefaultStack();
noteblockStack.set(DataComponentTypes.BLOCK_STATE, new BlockStateComponent(Map.of(
"instrument", instrumentNames[instrument],
"note", Integer.toString(note)
)));
inventory.main.set(slot, noteblockStack);
SongPlayer.MC.interactionManager.clickCreativeStack(noteblockStack, 36 + slot);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"depends": {
"fabricloader": ">=0.14.11",
"fabric": "*",
"minecraft": "~1.20.3",
"minecraft": "~1.20.5",
"java": ">=17"
},
"suggests": {
Expand Down

0 comments on commit 51e365c

Please sign in to comment.