Compare commits
41 Commits
Author | SHA1 | Date | |
---|---|---|---|
22bfa12981 | |||
9d44d319d1 | |||
d8373f5469 | |||
b11561b7c8 | |||
91dd7f9811 | |||
4ea309bbe9 | |||
f186c66ad7 | |||
f88b167931 | |||
45c65560f1 | |||
83d42630d8 | |||
62bccc2452 | |||
92f9a82ee5 | |||
![]() |
17aa218b6c | ||
![]() |
7114e853b8 | ||
![]() |
4d6dbc594d | ||
![]() |
d79f9b4edc | ||
![]() |
064f65d50d | ||
![]() |
cffea2e19e | ||
![]() |
643014f9ac | ||
![]() |
687d5fd29e | ||
![]() |
3774b51f62 | ||
![]() |
dbd31e73d5 | ||
![]() |
f3631bdedc | ||
![]() |
737656c28d | ||
![]() |
47792bf8b9 | ||
![]() |
d701906701 | ||
![]() |
a4d23cb3f5 | ||
![]() |
a2c9a3a000 | ||
![]() |
116b98edf0 | ||
![]() |
f7d3b293e7 | ||
![]() |
e402bd0749 | ||
![]() |
11dd1cf701 | ||
![]() |
1d18ffbe27 | ||
![]() |
d7e5e2846f | ||
![]() |
e9bc7e34d5 | ||
![]() |
f6257060f0 | ||
![]() |
818c061195 | ||
![]() |
8a55f94d37 | ||
![]() |
533dc47607 | ||
![]() |
d119e650e1 | ||
![]() |
bad438ad38 |
38
.gitea/workflows/gradle.yml
Normal file
38
.gitea/workflows/gradle.yml
Normal file
@ -0,0 +1,38 @@
|
||||
name: Java CI with Gradle
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ["master"]
|
||||
pull_request:
|
||||
branches: ["master"]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: archlinux-latest
|
||||
|
||||
steps:
|
||||
- name: Install prerequisite packages
|
||||
run: pacman -Syu --noconfirm && pacman -S --noconfirm git nodejs
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up JDK 21
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: "21"
|
||||
distribution: "temurin"
|
||||
|
||||
- name: Setup Gradle
|
||||
uses: gradle/actions/setup-gradle@v4
|
||||
|
||||
- name: Build with Gradle Wrapper
|
||||
run: ./gradlew build
|
||||
|
||||
# - name: Upload build artifacts
|
||||
# uses: actions/upload-artifact@v4
|
||||
# with:
|
||||
# name: JARs
|
||||
# path: build/libs
|
||||
# retention-days: 30
|
||||
# compression-level: 9
|
20
.gitignore
vendored
20
.gitignore
vendored
@ -1,11 +1,21 @@
|
||||
# Ignore Gradle project-specific cache directory
|
||||
# IntelliJ IDEA stuff
|
||||
.idea/
|
||||
|
||||
# VSCode stuff
|
||||
.vscode/
|
||||
|
||||
# Dolphin stuff
|
||||
.directory
|
||||
|
||||
# Gradle project-specific cache directory
|
||||
.gradle/
|
||||
|
||||
# Ignore Gradle build output directory
|
||||
# Gradle build output directory
|
||||
build/
|
||||
bin/
|
||||
|
||||
# Ignore run directory
|
||||
# Run directory
|
||||
run/
|
||||
|
||||
# IntelliJ IDEA
|
||||
.idea/
|
||||
# Remapped source file directory
|
||||
remappedSrc/
|
||||
|
30
LICENSE
30
LICENSE
@ -1,15 +1,21 @@
|
||||
HotbarReplace Minecraft Mod
|
||||
Copyright (C) 2023 Adam Macdonald
|
||||
MIT License
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
Copyright (c) 2024 Adam Macdonald
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
16
README.md
16
README.md
@ -1,8 +1,18 @@
|
||||
## HotbarReplace
|
||||
|
||||
HotbarReplace is a Minecraft mod for the Fabric modding framework which replaces blocks in your hotbar with blocks of the same type from your inventory.
|
||||
[](https://git.2khz.xyz/adam/HotbarReplace/actions/workflows/gradle.yml)
|
||||
|
||||
HotbarReplace is a Minecraft mod for the Fabric modding framework which replaces blocks in your hotbar with blocks of the same type from your inventory upon running out while placing, see demo GIF below to see how it works!
|
||||
|
||||
### Known issues/Improvements
|
||||
|
||||
* The mod won't refill your hotbar slot with other stacks in other hotbar slots
|
||||
* Make the mod more resilient to network delay and packet loss (don't hardcode a 50 ms delay)
|
||||
* [You tell me!](https://git.2khz.xyz/adam/HotbarReplace/issues) I'd like to see this mod be as simple & bug-free as possible!
|
||||
|
||||
### Where to find it?
|
||||
|
||||
- Here! On GitHub you can grab whichever release you like from the sidebar on the right-hand side.
|
||||
- Otherwise, also on [Modrinth](https://modrinth.com/mod/hotbarreplace).
|
||||
|
||||
#### Demo GIF
|
||||
|
||||

|
||||
|
58
build.gradle
58
build.gradle
@ -1,38 +1,37 @@
|
||||
// https://github.com/FabricMC/fabric-example-mod/blob/9b028bdcd43190d4f848d5898b89177de6776535/build.gradle#L1
|
||||
|
||||
plugins {
|
||||
id 'fabric-loom' version '1.1-SNAPSHOT'
|
||||
id 'fabric-loom' version '1.10-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
|
||||
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
|
||||
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
|
||||
// for more information about repositories.
|
||||
// ...
|
||||
}
|
||||
|
||||
loom {
|
||||
splitEnvironmentSourceSets()
|
||||
|
||||
mods {
|
||||
"hotbarreplace" {
|
||||
// sourceSet sourceSets.main
|
||||
sourceSet sourceSets.client
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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}"
|
||||
|
||||
// Fabric API. This is technically optional, but you probably want it anyway.
|
||||
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}"
|
||||
}
|
||||
|
||||
processResources {
|
||||
@ -44,36 +43,31 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
|
||||
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
|
||||
// retrieving dependencies.
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
BIN
docs/hotbarreplace-demo.gif
Normal file
BIN
docs/hotbarreplace-demo.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.7 MiB |
@ -1,19 +1,16 @@
|
||||
# https://github.com/FabricMC/fabric-example-mod/blob/9b028bdcd43190d4f848d5898b89177de6776535/gradle.properties#L1
|
||||
|
||||
# Done to increase the memory available to gradle.
|
||||
# Done to increase the memory available to Gradle
|
||||
org.gradle.jvmargs=-Xmx1G
|
||||
org.gradle.parallel=true
|
||||
|
||||
# Fabric Properties
|
||||
# check these on https://fabricmc.net/develop
|
||||
minecraft_version=1.19.3
|
||||
yarn_mappings=1.19.3+build.5
|
||||
loader_version=0.14.17
|
||||
minecraft_version=1.21.5
|
||||
yarn_mappings=1.21.5+build.1
|
||||
loader_version=0.16.13
|
||||
|
||||
# Mod Properties
|
||||
mod_version = 0.1.1
|
||||
maven_group = io.github.twokilohertz.hotbarreplace
|
||||
mod_version = 0.1.5
|
||||
maven_group = xyz.twokilohertz
|
||||
archives_base_name = HotbarReplace
|
||||
|
||||
# Dependencies
|
||||
fabric_version=0.76.0+1.19.3
|
||||
fabric_version=0.121.0+1.21.5
|
||||
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
3
gradle/wrapper/gradle-wrapper.properties
vendored
3
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,7 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
33
gradlew
vendored
Normal file → Executable file
33
gradlew
vendored
Normal file → Executable file
@ -15,6 +15,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
@ -55,7 +57,7 @@
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (3) This script is generated from the Groovy template
|
||||
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# within the Gradle project.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
@ -83,10 +85,8 @@ done
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
@ -133,18 +133,21 @@ location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD=java
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
if ! command -v java >/dev/null 2>&1
|
||||
then
|
||||
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC3045
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
MAX_FD=$( ulimit -H -n ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
@ -152,7 +155,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC3045
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
ulimit -n "$MAX_FD" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
@ -197,11 +200,15 @@ if "$cygwin" || "$msys" ; then
|
||||
done
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command;
|
||||
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
||||
# shell script including quotes and variable substitutions, so put them in
|
||||
# double quotes to make sure that they get re-expanded; and
|
||||
# * put everything else in single quotes, so that it's not re-expanded.
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Collect all arguments for the java command:
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||
# and any embedded shellness will be escaped.
|
||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||
# treated as '${Hostname}' itself on the command line.
|
||||
|
||||
set -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
|
22
gradlew.bat
vendored
22
gradlew.bat
vendored
@ -13,6 +13,8 @@
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
@rem SPDX-License-Identifier: Apache-2.0
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%"=="" @echo off
|
||||
@rem ##########################################################################
|
||||
@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if %ERRORLEVEL% equ 0 goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
|
@ -1,14 +1,3 @@
|
||||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* The settings file is used to specify which projects to include in your build.
|
||||
*
|
||||
* Detailed information about configuring a multi-project build in Gradle can be found
|
||||
* in the user manual at https://docs.gradle.org/8.0.2/userguide/multi_project_builds.html
|
||||
*/
|
||||
|
||||
// https://github.com/FabricMC/fabric-example-mod/blob/9b028bdcd43190d4f848d5898b89177de6776535/settings.gradle#L1
|
||||
|
||||
pluginManagement {
|
||||
repositories {
|
||||
maven {
|
||||
|
68
src/client/java/xyz/twokilohertz/HotbarReplace.java
Normal file
68
src/client/java/xyz/twokilohertz/HotbarReplace.java
Normal file
@ -0,0 +1,68 @@
|
||||
package xyz.twokilohertz;
|
||||
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.screen.slot.SlotActionType;
|
||||
import net.minecraft.util.Hand;
|
||||
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class HotbarReplace implements ClientModInitializer {
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger("hotbarreplace");
|
||||
private static final MinecraftClient client = MinecraftClient.getInstance();
|
||||
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
LOGGER.info("HotbarReplace v0.1.5 initialised");
|
||||
}
|
||||
|
||||
public static void tryReplaceSlot(PlayerEntity player, Hand hand, Item item) {
|
||||
// Return immediately if player is a spectator or in creative
|
||||
if (player == null || player.isSpectator() || player.getAbilities().creativeMode)
|
||||
return;
|
||||
|
||||
// Get reference to player's current inventory
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
|
||||
// Return if the inventory is empty
|
||||
if (inventory == null || inventory.isEmpty() || player.currentScreenHandler == null)
|
||||
return;
|
||||
|
||||
// Attempt to find a stack of matching items in the player's inventory
|
||||
for (int i = 0; i < player.currentScreenHandler.slots.size(); i++) {
|
||||
if (player.currentScreenHandler.slots.get(i).getStack().isOf(item)) {
|
||||
// Simulate moving the stack from one slot to another
|
||||
|
||||
if (client == null)
|
||||
return;
|
||||
|
||||
// TODO: This still feels like a bit of a hack
|
||||
// I honestly do not know Minecraft internals enough to be sure that there won't
|
||||
// be de-sync issues.
|
||||
|
||||
final int click_delay_ms = 50;
|
||||
|
||||
client.interactionManager.clickSlot(player.currentScreenHandler.syncId, i, GLFW.GLFW_MOUSE_BUTTON_1,
|
||||
SlotActionType.PICKUP, player);
|
||||
|
||||
int slot = (hand == Hand.OFF_HAND ? 9 : inventory.getSelectedSlot());
|
||||
|
||||
scheduler.schedule(() -> client.interactionManager.clickSlot(player.currentScreenHandler.syncId,
|
||||
slot + PlayerInventory.MAIN_SIZE, GLFW.GLFW_MOUSE_BUTTON_1,
|
||||
SlotActionType.PICKUP, player), click_delay_ms, TimeUnit.MILLISECONDS);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
136
src/client/java/xyz/twokilohertz/mixin/ItemStackMixin.java
Normal file
136
src/client/java/xyz/twokilohertz/mixin/ItemStackMixin.java
Normal file
@ -0,0 +1,136 @@
|
||||
package xyz.twokilohertz.mixin;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemUsageContext;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
import xyz.twokilohertz.HotbarReplace;
|
||||
|
||||
@Mixin(ItemStack.class)
|
||||
public class ItemStackMixin {
|
||||
Item lastUsedItem = null;
|
||||
|
||||
@Inject(at = @At("HEAD"), method = "use")
|
||||
private void ItemStack_use_HEAD(World world, PlayerEntity player, Hand hand,
|
||||
CallbackInfoReturnable<ActionResult> info) {
|
||||
if (!world.isClient())
|
||||
return;
|
||||
|
||||
Item item = player.getStackInHand(hand).getItem();
|
||||
if (item == null)
|
||||
return;
|
||||
|
||||
lastUsedItem = item;
|
||||
}
|
||||
|
||||
@Inject(at = @At("HEAD"), method = "useOnBlock")
|
||||
private void ItemStack_useOnBlock_HEAD(ItemUsageContext context,
|
||||
CallbackInfoReturnable<ActionResult> info) {
|
||||
if (!context.getWorld().isClient())
|
||||
return;
|
||||
|
||||
Item item = context.getPlayer().getStackInHand(context.getHand()).getItem();
|
||||
if (item == null)
|
||||
return;
|
||||
|
||||
lastUsedItem = item;
|
||||
}
|
||||
|
||||
@Inject(at = @At("HEAD"), method = "useOnEntity")
|
||||
private void ItemStack_useOnEntity_HEAD(PlayerEntity player, LivingEntity entity, Hand hand,
|
||||
CallbackInfoReturnable<ActionResult> info) {
|
||||
if (!player.getWorld().isClient())
|
||||
return;
|
||||
|
||||
Item item = player.getStackInHand(hand).getItem();
|
||||
if (item == null)
|
||||
return;
|
||||
|
||||
lastUsedItem = item;
|
||||
}
|
||||
|
||||
@Inject(at = @At("TAIL"), method = "use")
|
||||
private void ItemStack_use_TAIL(World world, PlayerEntity player, Hand hand,
|
||||
CallbackInfoReturnable<ActionResult> info) {
|
||||
if (!world.isClient())
|
||||
return;
|
||||
|
||||
if (info.getReturnValue() != ActionResult.SUCCESS)
|
||||
return;
|
||||
|
||||
ItemStack stack = player.getStackInHand(hand);
|
||||
|
||||
if (stack.getCount() > 1)
|
||||
return;
|
||||
|
||||
HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem);
|
||||
}
|
||||
|
||||
@Inject(at = @At("TAIL"), method = "useOnBlock")
|
||||
private void ItemStack_useOnBlock_TAIL(ItemUsageContext context, CallbackInfoReturnable<ActionResult> info) {
|
||||
if (!context.getWorld().isClient())
|
||||
return;
|
||||
|
||||
if (info.getReturnValue() != ActionResult.SUCCESS)
|
||||
return;
|
||||
|
||||
PlayerEntity player = context.getPlayer();
|
||||
Hand hand = context.getHand();
|
||||
ItemStack stack = player.getStackInHand(hand);
|
||||
|
||||
if (stack.getCount() != 0)
|
||||
return;
|
||||
|
||||
HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem);
|
||||
}
|
||||
|
||||
@Inject(at = @At("TAIL"), method = "useOnEntity")
|
||||
private void ItemStack_useOnEntity_TAIL(PlayerEntity player, LivingEntity entity, Hand hand,
|
||||
CallbackInfoReturnable<ActionResult> info) {
|
||||
if (info.getReturnValue() != ActionResult.SUCCESS)
|
||||
return;
|
||||
|
||||
ItemStack stack = player.getStackInHand(hand);
|
||||
|
||||
if (stack.getCount() > 1)
|
||||
return;
|
||||
|
||||
HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem);
|
||||
}
|
||||
|
||||
@Inject(at = @At("HEAD"), method = "Lnet/minecraft/item/ItemStack;finishUsing(Lnet/minecraft/world/World;Lnet/minecraft/entity/LivingEntity;)Lnet/minecraft/item/ItemStack;")
|
||||
private void ItemStack_finishUsing_HEAD(World world, LivingEntity user,
|
||||
CallbackInfoReturnable<ItemStack> info) {
|
||||
if (!world.isClient())
|
||||
return;
|
||||
|
||||
ItemStack stack = (ItemStack) (Object) this;
|
||||
// ItemStack stack = info.getReturnValue(); -- only applicable when injecting at TAIL
|
||||
|
||||
if (lastUsedItem != stack.getItem())
|
||||
return;
|
||||
|
||||
// Food items disappear into thin (minecraft:)air before their stack size reaches 0
|
||||
if (stack.getCount() > 1)
|
||||
return;
|
||||
|
||||
if (!user.isPlayer())
|
||||
return;
|
||||
|
||||
PlayerEntity player = (PlayerEntity) user;
|
||||
|
||||
if (!player.isMainPlayer())
|
||||
return;
|
||||
|
||||
HotbarReplace.tryReplaceSlot(player, player.getActiveHand(), lastUsedItem);
|
||||
}
|
||||
}
|
BIN
src/client/resources/assets/hotbarreplace/icon.png
Normal file
BIN
src/client/resources/assets/hotbarreplace/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.8 KiB |
@ -1,8 +1,7 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "hotbarreplace",
|
||||
"version": "${version}",
|
||||
|
||||
"version": "0.1.5",
|
||||
"name": "HotbarReplace",
|
||||
"description": "Replace blocks in your hotbar when you run out with blocks from your inventory",
|
||||
"authors": [
|
||||
@ -13,24 +12,24 @@
|
||||
"sources": "https://github.com/twokilohertz/HotbarReplace",
|
||||
"issues": "https://github.com/twokilohertz/HotbarReplace/issues"
|
||||
},
|
||||
|
||||
"license": "GPLv3",
|
||||
"license": "MIT",
|
||||
"icon": "assets/hotbarreplace/icon.png",
|
||||
|
||||
"environment": "*",
|
||||
"environment": "client",
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"io.github.twokilohertz.hotbarreplace.HotbarReplace"
|
||||
"client": [
|
||||
"xyz.twokilohertz.HotbarReplace"
|
||||
]
|
||||
},
|
||||
"mixins": [
|
||||
"hotbarreplace.mixins.json"
|
||||
{
|
||||
"config": "hotbarreplace.mixins.json",
|
||||
"environment": "client"
|
||||
}
|
||||
],
|
||||
|
||||
"depends": {
|
||||
"fabricloader": ">=0.14.17",
|
||||
"fabric-api": "*",
|
||||
"minecraft": "~1.19.3",
|
||||
"java": ">=17"
|
||||
"fabricloader": ">=0.15.11",
|
||||
"minecraft": "~1.21",
|
||||
"java": ">=21",
|
||||
"fabric-api": "*"
|
||||
}
|
||||
}
|
13
src/client/resources/hotbarreplace.mixins.json
Normal file
13
src/client/resources/hotbarreplace.mixins.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"required": true,
|
||||
"package": "xyz.twokilohertz.mixin",
|
||||
"compatibilityLevel": "JAVA_21",
|
||||
"mixins": [],
|
||||
"client": [
|
||||
"ItemStackMixin"
|
||||
],
|
||||
"server": [],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
package io.github.twokilohertz.hotbarreplace;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemPlacementContext;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.screen.slot.SlotActionType;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class HotbarReplace implements ModInitializer {
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger("hotbarreplace");
|
||||
private static final MinecraftClient client = MinecraftClient.getInstance();
|
||||
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
LOGGER.info("HotbarReplace v0.1.1 initialised");
|
||||
}
|
||||
|
||||
public static void tryReplaceSlot(ItemPlacementContext context, Item item) {
|
||||
// Return immediately if player is a spectator
|
||||
PlayerEntity player = context.getPlayer();
|
||||
if (player.isSpectator()) return;
|
||||
|
||||
// Creative inventories don't run out of anyway
|
||||
if (player.getAbilities().creativeMode) return;
|
||||
|
||||
// Get reference to player's current inventory
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
if (inventory == null) return;
|
||||
|
||||
// Return if the inventory is empty
|
||||
if (inventory.isEmpty()) return;
|
||||
|
||||
// Attempt to find a stack of matching items
|
||||
for (int i = 0; i < inventory.main.size(); i++) {
|
||||
if (i == inventory.selectedSlot) continue;
|
||||
|
||||
ItemStack stack = inventory.main.get(i);
|
||||
|
||||
if (stack.isOf(item)) {
|
||||
// Simulate moving the stack from one slot to another
|
||||
if (client != null) {
|
||||
client.interactionManager.clickSlot(player.currentScreenHandler.syncId, i, GLFW.GLFW_MOUSE_BUTTON_1, SlotActionType.PICKUP, player);
|
||||
|
||||
/*
|
||||
Wait 50 seconds (on another thread) before attempting to move the new stack
|
||||
PlayerInventory.MAIN_SIZE added to the selected slot (hotbar slot) is the correct slot ID
|
||||
*/
|
||||
scheduler.schedule(() -> {
|
||||
client.interactionManager.clickSlot(player.currentScreenHandler.syncId, inventory.selectedSlot + PlayerInventory.MAIN_SIZE, GLFW.GLFW_MOUSE_BUTTON_1, SlotActionType.PICKUP, player);
|
||||
}, 50, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package io.github.twokilohertz.hotbarreplace.mixin;
|
||||
|
||||
import net.minecraft.item.BlockItem;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemPlacementContext;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
import io.github.twokilohertz.hotbarreplace.HotbarReplace;
|
||||
|
||||
@Mixin(BlockItem.class)
|
||||
public class BlockItemMixin {
|
||||
private Item lastPlacedItem;
|
||||
|
||||
@Inject(at = @At("HEAD"), method = "Lnet/minecraft/item/BlockItem;place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;")
|
||||
private void BlockItem_place_head(ItemPlacementContext context, CallbackInfoReturnable<ActionResult> info) {
|
||||
lastPlacedItem = context.getStack().getItem();
|
||||
}
|
||||
@Inject(at = @At("TAIL"), method = "Lnet/minecraft/item/BlockItem;place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;")
|
||||
private void mixin_BlockItem_place_tail(ItemPlacementContext context, CallbackInfoReturnable<ActionResult> info) {
|
||||
// Early return if the block place action would fail
|
||||
if (info.getReturnValue() != ActionResult.SUCCESS) return;
|
||||
|
||||
// Check if the stack is not empty, return if so
|
||||
if (context.getStack().getCount() != 0) return;
|
||||
|
||||
// Try to replace the hotbar slot
|
||||
HotbarReplace.tryReplaceSlot(context, lastPlacedItem);
|
||||
}
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 1.2 KiB |
@ -1,14 +0,0 @@
|
||||
{
|
||||
"required": true,
|
||||
"minVersion": "0.8",
|
||||
"package": "io.github.twokilohertz.hotbarreplace.mixin",
|
||||
"compatibilityLevel": "JAVA_17",
|
||||
"mixins": [
|
||||
],
|
||||
"client": [
|
||||
"BlockItemMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user