Skip to content

Commit

Permalink
most of the stuff is ported
Browse files Browse the repository at this point in the history
  • Loading branch information
pupbrained committed Sep 15, 2024
1 parent e064853 commit 9ed6fe5
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 82 deletions.
59 changes: 32 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
# Example Mod
<div align="center">
<img src="./icon-rounded.png" alt="The DropConfirm Logo"/>
<h1>DropConfirm</h1>
<h3>Think twice before you drop.</h3>
</div>

Template for making Babric mods for BTA!
---

**Note: *DO NOT fork this repository unless you want to contribute!***
## What *is* DropConfirm, exactly?

## Prerequisites
- JDK for Java 17 ([Eclipse Temurin](https://adoptium.net/temurin/releases/) recommended)
- [Intellij IDEA](https://www.jetbrains.com/idea/download/) (Scroll down for the free community edition, if using linux **DO NOT** use the flatpak distribution)
- Minecraft Development plugin (Optional, but highly recommended)
DropConfirm is a **configurable quality-of-life mod** that adds a confirmation dialog when dropping items. I made it
because I was tired of accidentally dropping my items to other players or into lava/voids.

## Setup instructions
I use this mod personally, and I wanted to share it with others because I've noticed that a lot of my friends have had
this exact problem (and I assume others have too).

1. Click the `Use this template` button on this repo's page above (Will only appear if logged in). Choose `Create a new repository`, you will be redirected to a new page. Enter your repo's name and description, and hit `Create repository`.
To get your project, open IntelliJ IDEA and click `Get from VCS`. Select `Repository URL` and enter your repo's url
## Compatibility

2. After the project has finished importing, close it and open it again.
If that does not work, open the right sidebar with `Gradle` on it, open `Tasks` > `fabric` and run `ideaSyncTask`.
DropConfirm currently supports Minecraft 1.20-1.21 (with a less featureful backport for 1.19.4).

3. Create a new run configuration by going in `Run > Edit Configurations`.
Then click on the plus icon and select Gradle. In the `Tasks and Arguments` field enter `build`.
Running it will build your finished jar files and put them in `build/libs/`.
Newer versions will likely be supported. Older versions may or may not work with some manual modifications to the source and will ***not*** be actively supported.

4. Lastly, open `File` > `Settings` and head to `Build, Execution, Development` > `Build Tools` > `Gradle`.
Make sure `Build and run using` and `Run tests using` is set to `Gradle`.
## Roadmap

5. Done! Now, all that's left is to change every mention of `examplemod` and `turniplabs` to your own mod id and mod group, respectively. Happy modding!
- [x] Add configurations
- [x] Add a command/keybind to toggle the mod
- [x] Add sound events for toggling the mod and dropping items
- [x] Add translations
- [ ] Detect if the player is in a dangerous area (e.g. lava, void, etc.)

## Tips

1. If you haven't already you should join the BTA modding discord! https://discord.gg/FTUNJhswBT
2. You can set your username when launching the client run configuration by setting `--username <username>` in your program arguments.
3. Set `online-mode` to `false` in order to be able to join your server!
4. When launching the server run configuration you may want to remove the `nogui` program argument in order to see the regular server GUI.
5. In Intellij you can double press shift or press ctrl+N to search class files, change the search from the default `Project Files` to `All Places` you can easily explore the classes for you dependencies and even BTA itself.
6. In Intellij if ctrl+left click on a field or method you can quickly get information on when and where that field or method is assign or used.
## Configuration

- **Enable:**
- Whether to enable DropConfirm.
- **Play Sounds:**
- Whether or not to play sounds when doing various actions related to DropConfirm, e.g.:
- Enabling/Disabling
- Dropping items
- **Reset Delay:**
- Amount of time (in seconds) before the confirmation status is reset.
- **Blacklist**
- Allows for disabling the confirmation prompt on certain items.
- Can also be used like a whitelist instead.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ mod_menu_version=2.0.6
halplibe_version=4.1.3

# Mod
mod_version=1.0.0
mod_group=turniplabs
mod_name=examplemod
mod_version=3.0.0-bta
mod_group=xyz.pupbrained
mod_name=drop_confirm
Binary file added icon-rounded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package turniplabs.examplemod;
package xyz.pupbrained;

import net.fabricmc.api.ModInitializer;
import net.minecraft.client.Minecraft;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import turniplabs.halplibe.helper.BlockBuilder;
import turniplabs.halplibe.util.GameStartEntrypoint;
import turniplabs.halplibe.util.RecipeEntrypoint;


public class ExampleMod implements ModInitializer, GameStartEntrypoint, RecipeEntrypoint {
public static final String MOD_ID = "examplemod";
public class DropConfirm implements ModInitializer, GameStartEntrypoint, RecipeEntrypoint {
public static final String MOD_ID = "drop_confirm";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

@Override
public void onInitialize() {
LOGGER.info("ExampleMod initialized.");
LOGGER.info("DropConfirm initialized.");
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/xyz/pupbrained/DropConfirmUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package xyz.pupbrained;

import net.minecraft.core.entity.player.EntityPlayer;

public final class DropConfirmUtil {
public static boolean confirmed = false;
public static boolean showConfirmationPrompt = false;
public static long promptStartTime = 0;

public static boolean isMainHandStackEmpty(EntityPlayer player) {
return player.getHeldItem() == null;
}
}
19 changes: 19 additions & 0 deletions src/main/java/xyz/pupbrained/mixin/HotbarScrollMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package xyz.pupbrained.mixin;

import net.minecraft.core.player.inventory.InventoryPlayer;
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.CallbackInfo;
import xyz.pupbrained.DropConfirmUtil;

@Mixin(value = InventoryPlayer.class, remap = false)
public class HotbarScrollMixin {
@Inject(
method = "changeCurrentItem(I)V",
at = @At("HEAD")
)
private void onHotbarScroll(int i, CallbackInfo ci) {
DropConfirmUtil.confirmed = false;
}
}
52 changes: 52 additions & 0 deletions src/main/java/xyz/pupbrained/mixin/ItemDropMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package xyz.pupbrained.mixin;

import net.minecraft.client.Minecraft;
import net.minecraft.core.entity.player.EntityPlayer;
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.CallbackInfo;
import xyz.pupbrained.DropConfirm;
import xyz.pupbrained.DropConfirmUtil;

@Mixin(value = EntityPlayer.class, remap = false)
public class ItemDropMixin {
Minecraft mc = Minecraft.getMinecraft(this);

@Inject(
method="dropCurrentItem(Z)V",
at = @At("HEAD"),
cancellable = true
)
private void onItemDrop(boolean dropFullStack, CallbackInfo ci) {
if (DropConfirmUtil.isMainHandStackEmpty(mc.thePlayer))
return;

if (!DropConfirmUtil.confirmed) {
DropConfirmUtil.confirmed = true;
DropConfirmUtil.showConfirmationPrompt = true;
DropConfirmUtil.promptStartTime = System.currentTimeMillis(); // Set start time here

new Thread(() -> {
try {
Thread.sleep(1000);

synchronized (DropConfirmUtil.class) {
DropConfirmUtil.confirmed = false;
DropConfirmUtil.showConfirmationPrompt = false;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
DropConfirm.LOGGER.error("Interrupted while waiting to reset confirmation.", e);
}
}).start();

ci.cancel();
} else {
DropConfirmUtil.confirmed = false;
DropConfirmUtil.showConfirmationPrompt = false;

mc.theWorld.playSoundAtEntity(mc.thePlayer, mc.thePlayer, "random.pop", 1.0F, 1.0F);
}
}
}
62 changes: 62 additions & 0 deletions src/main/java/xyz/pupbrained/mixin/RenderMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package xyz.pupbrained.mixin;

import net.minecraft.client.Minecraft;
import org.lwjgl.opengl.GL11;
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.CallbackInfo;
import xyz.pupbrained.DropConfirmUtil;

@Mixin(value = Minecraft.class, remap = false)
public class RenderMixin {
@Inject(
method = "run",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/render/WorldRenderer;updateCameraAndRender(F)V",
shift = At.Shift.AFTER
)
)
private void onRender(CallbackInfo ci) {
if (!DropConfirmUtil.showConfirmationPrompt) return;

Minecraft mc = Minecraft.getMinecraft(this);

long currentTime = System.currentTimeMillis();
long elapsedTime = currentTime - DropConfirmUtil.promptStartTime;

// Calculate alpha value based on elapsed time (fade out over 1000ms)
float alpha = 1.0f - (elapsedTime / 1000.0f); // Alpha decreases over time
if (alpha < 0.0f) {
alpha = 0.0f; // Ensure alpha doesn't go below 0
}

// Convert alpha to a hex value (between 0 and 255) and incorporate it into the color
int alphaHex = (int) (alpha * 255) << 24;
int color = 0xFFFFFF | alphaHex; // Combine white color with alpha

// Weird GL stuff
GL11.glPushMatrix();
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_BLEND); // Enable blending for alpha transparency
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); // Standard blend function

mc.fontRenderer.drawCenteredString(
"Press Q again to drop this item.",
mc.resolution.scaledWidth / 2, // Complete center of the screen horizontally
mc.resolution.scaledHeight - 60, // A little above the hotbar
color // Color with alpha
);

// Undo weird GL stuff
GL11.glDisable(GL11.GL_BLEND); // Disable blending after rendering
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glPopMatrix();

// Stop showing the prompt after it has fully faded out
if (alpha == 0.0f) {
DropConfirmUtil.showConfirmationPrompt = false;
}
}
}
16 changes: 16 additions & 0 deletions src/main/resources/drop_confirm.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"required": true,
"minVersion": "0.8",
"package": "xyz.pupbrained.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
],
"client": [
"HotbarScrollMixin",
"ItemDropMixin",
"RenderMixin"
],
"injectors": {
"defaultRequire": 1
}
}
13 changes: 0 additions & 13 deletions src/main/resources/examplemod.mixins.json

This file was deleted.

61 changes: 28 additions & 33 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
{
"schemaVersion": 1,
"id": "examplemod",
"version": "${version}",

"name": "Example Mod",
"description": "This mod aims to help new BTA modders.",
"authors": [
"Turnip Labs"
],
"contact": {
"homepage": "",
"sources": ""
},

"schemaVersion": 1,
"id": "drop_confirm",
"version": "${version}",
"name": "DropConfirm",
"description": "Think twice before you drop.",
"authors": [
"pupbrained"
],
"contact": {
"homepage": "https://modrinth.com/mod/drop-confirm",
"issues": "https://github.com/pupbrained/drop-confirm/issues",
"sources": "https://github.com/pupbrained/drop-confirm"
},
"icon": "icon.png",
"license": "CC0-1.0",

"environment": "*",
"entrypoints": {
"main": [
"turniplabs.examplemod.ExampleMod"
],
"license": "Unlicense",
"environment": "*",
"entrypoints": {
"main": [
"xyz.pupbrained.DropConfirm"
],
"beforeGameStart": [
"turniplabs.examplemod.ExampleMod"
"xyz.pupbrained.DropConfirm"
],
"afterGameStart": [
"turniplabs.examplemod.ExampleMod"
"xyz.pupbrained.DropConfirm"
],
"recipesReady": [
"turniplabs.examplemod.ExampleMod"
"xyz.pupbrained.DropConfirm"
]
},
"mixins": [
"examplemod.mixins.json"
],

"depends": {
"mixins": [
"drop_confirm.mixins.json"
],
"depends": {
"minecraft": "^7.2-beta",
"fabricloader": ">=0.15.5"
},
"suggests": {
}
"fabricloader": ">=0.15.5"
}
}
Binary file modified src/main/resources/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/main/resources/lang/drop_confirm/en_US.lang
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
category.drop_confirm.general=General
category.drop_confirm.keybinds=DropConfirm
config.drop_confirm.title=DropConfirm Settings
drop_confirm.confirmation=Press %s again to drop this item.
drop_confirm.toggle.off=OFF
drop_confirm.toggle.on=ON
key.drop_confirm.toggle=Toggle DropConfirm
option.drop_confirm.confirmation_reset_delay=Reset Delay
option.drop_confirm.confirmation_reset_delay.description=The amount of time in seconds before the confirmation status is reset.
option.drop_confirm.enabled=Enable DropConfirm
option.drop_confirm.enabled.description=Whether DropConfirm should be enabled.
option.drop_confirm.play_sounds=Play Sounds
option.drop_confirm.play_sounds.description=Whether DropConfirm should play sounds for various events.
option.drop_confirm.treat_as_whitelist=Treat as white list
option.drop_confirm.treat_as_whitelist.description=Whether the blacklist should be used as a whitelist.\n\nNOTE=This change takes effect instantly but will only show visually upon re-entering the config screen.
option.drop_confirm.blacklisted_items=Blacklisted Items
option.drop_confirm.blacklisted_items.description=Items that should be ignored by DropConfirm.
option.drop_confirm.whitelisted_items=Whitelisted Items
option.drop_confirm.whitelisted_items.description=Items that should not be ignored by DropConfirm.
Empty file.

0 comments on commit 9ed6fe5

Please sign in to comment.