Skip to content

Commit

Permalink
refactor: 优化代码
Browse files Browse the repository at this point in the history
feat: 自动重置滚动 (#72)

fix: 修复只有玩家物品栏才生效的问题 (#75)

feat: 兼容 Apple Skin (#82)
  • Loading branch information
flowerinsnowdh committed Oct 25, 2024
1 parent 6c49164 commit 9da820d
Show file tree
Hide file tree
Showing 32 changed files with 566 additions and 281 deletions.
69 changes: 41 additions & 28 deletions 1.17-1.17.1-fabric/build.gradle
Original file line number Diff line number Diff line change
@@ -1,78 +1,91 @@
plugins {
id 'fabric-loom' version '1.5-SNAPSHOT'
id 'maven-publish'
plugins() {
id('fabric-loom').version("${loom_version}")
id('maven-publish')
}

version = project.mod_version
group = project.maven_group

base {
base() {
archivesName = project.archives_base_name
}

repositories {
maven {
url 'https://repo.flowerinsnow.online/repository/maven-public/'
repositories() {
maven() {
url = 'https://maven.pkg.github.com/flowerinsnowdh/GreatScrollableTooltips'
credentials() {
username = "${System.getenv('GITHUB_USERNAME')}"
password = "${System.getenv('GITHUB_TOKEN')}"
}
}

maven() {
url = 'https://www.cursemaven.com/'
}

maven {
url 'https://maven.terraformersmc.com/releases/'
maven() {
url = 'https://maven.terraformersmc.com/releases/'
}
}

dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
minecraft("com.mojang:minecraft:${project.minecraft_version}")
mappings("net.fabricmc:yarn:${project.yarn_mappings}:v2")
modImplementation("net.fabricmc:fabric-loader:${project.loader_version}")

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modImplementation("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")

// Uncomment the following line to enable the deprecated Fabric API modules.
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.

// modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}"
modImplementation 'com.terraformersmc:modmenu:2.0.17'
include(api('online.flowerinsnow.fnml:fnml4j-core:1.0.3'))
modImplementation("com.terraformersmc:modmenu:${modmenu_version}")
modImplementation('curse.maven:legendary-tooltips-fabric-542478:3537454')
include(api("cn.flowerinsnow.greatscrollabletooltips:common:${common_module_version}"))
include(api("com.electronwill.night-config:core:${night_config_version}"))
include(api("com.electronwill.night-config:toml:${night_config_version}"))
}

processResources {
inputs.property "version", project.version
processResources() {
LinkedHashMap<String, ?> props = ['version': project.version]
props.forEach(inputs::property)

filesMatching("fabric.mod.json") {
expand "version": project.version
filesMatching('fabric.mod.json') {
expand(props)
}
}

tasks.withType(JavaCompile).configureEach {
it.options.release = 16
tasks.withType(JavaCompile).configureEach() {
options.release = 16
}

java {
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()
// withSourcesJar()

sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
}

jar {
from 'LICENSE'
from('../LICENSE')
from('../NOTICE')
}

// configure the maven publication
publishing {
publications {
publishing() {
publications() {
mavenJava(MavenPublication) {
from components.java
from(components.java)
}
}

// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
repositories {
repositories() {
// Add repositories to publish to here.
// Notice: This block does NOT have the same function as the block in the top level.
// The repositories here will be used for publishing your artifact, not for
Expand Down
12 changes: 8 additions & 4 deletions 1.17-1.17.1-fabric/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ org.gradle.parallel=true
# check these on https://fabricmc.net/develop
minecraft_version=1.17.1
yarn_mappings=1.17.1+build.65
loader_version=0.15.7
loader_version=0.16.7

# Mod Properties
mod_version=8.0.0
maven_group=online.flowerinsnow.greatscrollabletooltips
mod_version=8.1.0+fabric
maven_group=cn.flowerinsnow.greatscrollabletooltips
archives_base_name=great-scrollable-tooltips

# Dependencies
fabric_version=0.46.1+1.17
loom_version=1.8.10
fabric_version=0.46.1+1.17
modmenu_version=2.0.17
night_config_version=3.8.1
common_module_version=1.1.0
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-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cn.flowerinsnow.greatscrollabletooltips;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemStack;
import net.minecraft.util.crash.CrashReport;
import cn.flowerinsnow.greatscrollabletooltips.common.config.GreatScrollableTooltipsConfig;
import cn.flowerinsnow.greatscrollabletooltips.common.object.ScrollSession;
import cn.flowerinsnow.greatscrollabletooltips.common.provider.ModEnvironmentProvider;
import cn.flowerinsnow.greatscrollabletooltips.event.PreScreenKeyPressedEvent;
import cn.flowerinsnow.greatscrollabletooltips.event.PreScreenMouseScrollEvent;
import cn.flowerinsnow.greatscrollabletooltips.event.RenderTooltipEvent;
import cn.flowerinsnow.greatscrollabletooltips.event.ScreenCloseEvent;
import cn.flowerinsnow.greatscrollabletooltips.listener.EventTriggerListener;
import cn.flowerinsnow.greatscrollabletooltips.listener.KeyScrollListener;
import cn.flowerinsnow.greatscrollabletooltips.listener.MouseScrollListener;
import cn.flowerinsnow.greatscrollabletooltips.listener.ScrollingStatusListener;
import cn.flowerinsnow.greatscrollabletooltips.manager.KeyBindingManager;

import java.io.InputStream;
import java.nio.file.Path;

@Environment(EnvType.CLIENT)
public class GreatScrollableTooltips implements ClientModInitializer {
public static final String MODID = "great-scrollable-tooltips";

private static GreatScrollableTooltips instance;

private GreatScrollableTooltipsConfig config;

private ScrollSession<ItemStack> scrollSession;

@Override
public void onInitializeClient() {
GreatScrollableTooltips.instance = this;
this.scrollSession = new ScrollSession<>();

this.initConfig();
this.initListeners();
this.initKeyBindings();
}

private void initConfig() {
this.config = new GreatScrollableTooltipsConfig(new ModEnvironmentProvider() {
@Override
public InputStream getDefaultConfigAsStream() {
return GreatScrollableTooltips.class.getResourceAsStream("/config.toml");
}

@Override
public Path getConfigFile() {
return FabricLoader.getInstance().getConfigDir().resolve(GreatScrollableTooltips.MODID + ".toml");
}

@Override
public void crash(Throwable throwable, String msg) {
MinecraftClient.printCrashReport(CrashReport.create(throwable, msg));
}
});
this.config.saveDefaultConfig();
this.config.load();
}

private void initListeners() {
ClientTickEvents.END_CLIENT_TICK.register(new EventTriggerListener());
PreScreenKeyPressedEvent.EVENT.register(new KeyScrollListener(this));
PreScreenMouseScrollEvent.EVENT.register(new MouseScrollListener(this));
ScrollingStatusListener scrollingStatusListener = new ScrollingStatusListener(this);
RenderTooltipEvent.Pre.EVENT.register(scrollingStatusListener);
RenderTooltipEvent.Miss.EVENT.register(scrollingStatusListener);
ScreenCloseEvent.EVENT.register(scrollingStatusListener);
}

private void initKeyBindings() {
KeyBindingManager.registerAll();
}

public static GreatScrollableTooltips getInstance() {
return GreatScrollableTooltips.instance;
}

public GreatScrollableTooltipsConfig getConfig() {
return this.config;
}

public ScrollSession<ItemStack> getScrollSession() {
return this.scrollSession;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cn.flowerinsnow.greatscrollabletooltips.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.util.ActionResult;

public interface PreScreenKeyPressedEvent {
Event<PreScreenKeyPressedEvent> EVENT = EventFactory.createArrayBacked(PreScreenKeyPressedEvent.class, listeners -> (screen, keyCode, scanCode, modifiers) -> {
for (PreScreenKeyPressedEvent listener : listeners) {
ActionResult actionResult = listener.preScreenKeyPressed(screen, keyCode, scanCode, modifiers);
if (actionResult != ActionResult.PASS) {
return actionResult;
}
}
return ActionResult.PASS;
});

ActionResult preScreenKeyPressed(HandledScreen<?> screen, int keyCode, int scanCode, int modifiers);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cn.flowerinsnow.greatscrollabletooltips.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.util.ActionResult;

public interface PreScreenMouseScrollEvent {
Event<PreScreenMouseScrollEvent> EVENT = EventFactory.createArrayBacked(PreScreenMouseScrollEvent.class, listeners -> (screen, mouseX, mouseY, amount) -> {
for (PreScreenMouseScrollEvent listener : listeners) {
ActionResult actionResult = listener.preScreenMouseScrolled(screen, mouseX, mouseY, amount);
if (actionResult != ActionResult.PASS) {
return actionResult;
}
}
return ActionResult.PASS;
});

ActionResult preScreenMouseScrolled(HandledScreen<?> screen, double mouseX, double mouseY, double amount);
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package online.flowerinsnow.greatscrollabletooltips.event;
package cn.flowerinsnow.greatscrollabletooltips.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.ActionResult;

public interface RenderTooltipEvent {
interface Post {
Event<Post> EVENT = EventFactory.createArrayBacked(Post.class, listeners -> (screen, matrices, stack, x, y) -> {
for (Post listener : listeners) {
ActionResult actionResult = listener.startDrawMouseoverTooltip(screen, matrices, stack, x, y);
interface Pre {
Event<Pre> EVENT = EventFactory.createArrayBacked(Pre.class, listeners -> (screen, matrices, x, y, focusedSlot) -> {
for (Pre listener : listeners) {
ActionResult actionResult = listener.preRenderTooltip(screen, matrices, x, y, focusedSlot);
if (actionResult != ActionResult.PASS) {
return actionResult;
}
}
return ActionResult.PASS;
});

ActionResult startDrawMouseoverTooltip(HandledScreen<?> screen, MatrixStack matrices, ItemStack stack, int x, int y);
ActionResult preRenderTooltip(HandledScreen<?> screen, MatrixStack matrices, int x, int y, Slot focusedSlot);
}

interface Miss {
Event<Miss> EVENT = EventFactory.createArrayBacked(Miss.class, listeners -> screen -> {
for (Miss listener : listeners) {
ActionResult actionResult = listener.onMiss(screen);
ActionResult actionResult = listener.missRenderTooltip(screen);
if (actionResult != ActionResult.PASS) {
return actionResult;
}
}
return ActionResult.PASS;
});

ActionResult onMiss(HandledScreen<?> screen);
ActionResult missRenderTooltip(HandledScreen<?> screen);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cn.flowerinsnow.greatscrollabletooltips.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.ActionResult;

public interface ScreenCloseEvent {
Event<ScreenCloseEvent> EVENT = EventFactory.createArrayBacked(ScreenCloseEvent.class, listeners -> (screen) -> {
for (ScreenCloseEvent event : listeners) {
ActionResult actionResult = event.onScreenClose(screen);
if (actionResult != ActionResult.PASS) {
return actionResult;
}
}
return ActionResult.PASS;
});

ActionResult onScreenClose(Screen oldScreen);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package online.flowerinsnow.greatscrollabletooltips.listener;
package cn.flowerinsnow.greatscrollabletooltips.listener;

import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import online.flowerinsnow.greatscrollabletooltips.event.ScreenCloseEvent;
import cn.flowerinsnow.greatscrollabletooltips.event.ScreenCloseEvent;

public class EventTriggerListener implements ClientTickEvents.EndTick {
private Screen oldScreen;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package online.flowerinsnow.greatscrollabletooltips.listener;
package cn.flowerinsnow.greatscrollabletooltips.listener;

import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import online.flowerinsnow.greatscrollabletooltips.GreatScrollableTooltips;
import online.flowerinsnow.greatscrollabletooltips.common.object.ScrollSession;
import online.flowerinsnow.greatscrollabletooltips.event.PreScreenKeyPressedEvent;
import online.flowerinsnow.greatscrollabletooltips.manager.KeyBindingManager;
import cn.flowerinsnow.greatscrollabletooltips.GreatScrollableTooltips;
import cn.flowerinsnow.greatscrollabletooltips.common.object.ScrollSession;
import cn.flowerinsnow.greatscrollabletooltips.event.PreScreenKeyPressedEvent;
import cn.flowerinsnow.greatscrollabletooltips.manager.KeyBindingManager;

public class KeyScrollingListener implements PreScreenKeyPressedEvent {
public record KeyScrollListener(GreatScrollableTooltips main) implements PreScreenKeyPressedEvent {
@Override
public ActionResult preScreenKeyPressed(HandledScreen<?> screen, int keyCode, int scanCode, int modifiers) {
ScrollSession<ItemStack> session = GreatScrollableTooltips.getInstance().getScrollSession();
ScrollSession<ItemStack> session = this.main.getScrollSession();
if (session.isRendering()) {
if (KeyBindingManager.KEY_BINDING_SCROLL_UP.get().matchesKey(keyCode, scanCode)) {
session.addVertical(1);
Expand Down
Loading

0 comments on commit 9da820d

Please sign in to comment.