Skip to content

Commit

Permalink
Backhand support (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Caedis authored Jan 18, 2025
1 parent e8018dc commit bd8631c
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 10 deletions.
5 changes: 3 additions & 2 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ dependencies {
compileOnly("com.github.GTNewHorizons:NotEnoughItems:2.7.22-GTNH:dev")
compileOnly("com.github.GTNewHorizons:GT5-Unofficial:5.09.51.79:dev")
compileOnly("com.github.GTNewHorizons:Botania:1.12.5-GTNH:dev")
devOnlyNonPublishable("com.github.GTNewHorizons:StorageDrawers:2.1.0-GTNH:dev")

compileOnly("com.github.GTNewHorizons:StorageDrawers:2.1.0-GTNH:dev")
compileOnly("com.github.GTNewHorizons:Backhand:1.5.16:dev")

runtimeOnlyNonPublishable("com.github.GTNewHorizons:waila:1.8.2:dev")

compileOnly rfg.deobf('curse.maven:projecte-226410:2340786')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import com.cleanroommc.bogosorter.common.ReadableNumberConverter;
import com.cleanroommc.bogosorter.compat.loader.Mods;
import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.relauncher.Side;
import xonin.backhand.api.core.BackhandUtils;

// todo: backhand compat
@EventBusSubscriber(side = { Side.CLIENT })
public class UsageTicker {

public static boolean enableMainHand = true;
public static boolean enableOffHand = true;
public static boolean enableArmor = true;
public static boolean enableModule = true;

Expand All @@ -40,12 +44,18 @@ public static void reloadElements() {
if (enableMainHand) {
elements.add(new Element(EquipmentSlotType.MAINHAND));
}

if (Mods.Backhand.isLoaded() && enableOffHand) {
elements.add(new Element(EquipmentSlotType.OFFHAND));
}

if (enableArmor) {
elements.add(new Element(EquipmentSlotType.HEAD));
elements.add(new Element(EquipmentSlotType.CHEST));
elements.add(new Element(EquipmentSlotType.LEGS));
elements.add(new Element(EquipmentSlotType.FEET));
}

}

@EventBusSubscriber.Condition
Expand Down Expand Up @@ -97,31 +107,36 @@ public void render(ScaledResolution resolution, EntityClientPlayerMP player, flo

float anim = -animProgress * (animProgress - 2) * 19F;

float x = resolution.getScaledWidth() / 2f;
float x = resolution.getScaledWidth() / 2f - (Mods.Backhand.isLoaded() ? 30 : 0);
float y = resolution.getScaledHeight() - anim;

int barWidth = 190;
boolean armor = slot != EquipmentSlotType.MAINHAND;
boolean armor = !(slot == EquipmentSlotType.MAINHAND || slot == EquipmentSlotType.OFFHAND);

int slots = armor ? 4 : 1;
int slots = armor ? 4 : (Mods.Backhand.isLoaded() && enableOffHand) ? 2 : 1;
int index = slots - slot.ordinal() - 1;

Minecraft mc = Minecraft.getMinecraft();

x -= (barWidth / 2f) - index * 20;
x -= (barWidth / 2f) - index * 22;
x -= slots * 20;

ItemStack stack = getRenderedStack(player);
if (stack == null) return;

RenderItem renderer = new RenderItem();

GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
RenderHelper.enableGUIStandardItemLighting();
renderer.renderItemAndEffectIntoGUI(mc.fontRenderer, mc.renderEngine, stack, 0, 0);
renderer.renderItemOverlayIntoGUI(mc.fontRenderer, mc.renderEngine, stack, 0, 0);
renderer.renderItemOverlayIntoGUI(
mc.fontRenderer,
mc.renderEngine,
stack,
0,
0,
stack.stackSize > 1 ? ReadableNumberConverter.INSTANCE.toSlimReadableForm(stack.stackSize) : null);
RenderHelper.disableStandardItemLighting();
GL11.glDisable(GL12.GL_RESCALE_NORMAL);
GL11.glPopMatrix();
Expand Down Expand Up @@ -166,7 +181,11 @@ public ItemStack getStack(EntityClientPlayerMP player) {
return player.inventory.getCurrentItem();
}

return player.inventory.armorItemInSlot(slot.ordinal() - 1);
if (slot == EquipmentSlotType.OFFHAND) {
return BackhandUtils.getOffhandItem(player);
}

return player.inventory.armorItemInSlot(slot.ordinal() - 2);
}

public ItemStack getRenderedStack(EntityClientPlayerMP player) {
Expand All @@ -189,6 +208,7 @@ public ItemStack getRenderedStack(EntityClientPlayerMP player) {
public ItemStack getDisplayedStack(ItemStack stack, int count) {
if (stack == null) return null;
if (slot == EquipmentSlotType.MAINHAND && !stack.isStackable()) return null;
if (slot == EquipmentSlotType.OFFHAND && !stack.isStackable()) return null;
if (count == stack.stackSize) return null;

return stack;
Expand All @@ -214,10 +234,11 @@ private int getStackCount(EntityClientPlayerMP player, ItemStack stack) {

public enum EquipmentSlotType {
MAINHAND,
OFFHAND,
HEAD,
CHEST,
LEGS,
FEET
FEET,
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.cleanroommc.bogosorter.common;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.Format;

/**
* copy from nei copy from ae2 Converter class to convert a large number into a SI system.
*
* @author thatsIch
* @version rv2
* @since rv2
*/
public enum ReadableNumberConverter {

INSTANCE;

/**
* Defines the base for a division, non-si standard could be 1024 for kilobytes
*/
private static final int DIVISION_BASE = 1000;

/**
* String representation of the sorted postfixes
*/
private static final char[] ENCODED_POSTFIXES = "KMGTPE".toCharArray();

private final Format format;

/**
* Initializes the specific decimal format with special format for negative and positive numbers
*/
ReadableNumberConverter() {
final DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
final DecimalFormat format = new DecimalFormat(".#;0.#");
format.setDecimalFormatSymbols(symbols);
format.setRoundingMode(RoundingMode.DOWN);

this.format = format;
}

public String toSlimReadableForm(final long number) {
return this.toReadableFormRestrictedByWidth(number, 3);
}

/**
* restricts a string representation of a number to a specific width
*
* @param number to be formatted number
* @param width width limitation of the resulting number
* @return formatted number restricted by the width limitation
*/
private String toReadableFormRestrictedByWidth(final long number, final int width) {
assert number >= 0;

// handles low numbers more efficiently since no format is needed
final String numberString = Long.toString(number);
int numberSize = numberString.length();
if (numberSize <= width) {
return numberString;
}

long base = number;
double last = base * 1000;
int exponent = -1;
String postFix = "";

while (numberSize > width) {
last = base;
base /= DIVISION_BASE;

exponent++;

// adds +1 due to the postfix
numberSize = Long.toString(base)
.length() + 1;
postFix = String.valueOf(ENCODED_POSTFIXES[exponent]);
}

final String withPrecision = this.format.format(last / DIVISION_BASE) + postFix;
final String withoutPrecision = Long.toString(base) + postFix;

final String slimResult = (withPrecision.length() <= width) ? withPrecision : withoutPrecision;

// post condition
assert slimResult.length() <= width;

return slimResult;
}

public String toWideReadableForm(final long number) {
return this.toReadableFormRestrictedByWidth(number, 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static void save(JsonObject json) {
JsonObject usageTicker = new JsonObject();
usageTicker.addProperty("enableModule", UsageTicker.enableModule);
usageTicker.addProperty("enableMainHand", UsageTicker.enableMainHand);
usageTicker.addProperty("enableOffHand", UsageTicker.enableOffHand);
usageTicker.addProperty("enableArmor", UsageTicker.enableArmor);
json.add("UsageTicker", usageTicker);
}
Expand Down Expand Up @@ -162,6 +163,7 @@ public static void load(JsonObject json) {
JsonObject ticker = json.getAsJsonObject("UsageTicker");
UsageTicker.enableModule = JsonHelper.getBoolean(ticker, true, "enableModule");
UsageTicker.enableMainHand = JsonHelper.getBoolean(ticker, true, "enableMainHand");
UsageTicker.enableOffHand = JsonHelper.getBoolean(ticker, true, "enableOffHand");
UsageTicker.enableArmor = JsonHelper.getBoolean(ticker, true, "enableArmor");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,25 @@ public IWidget createGeneralConfigUI(GuiContext context) {
.height(14)
.marginLeft(10)
.expanded()))
.child(
new Row().widthRel(1f)
.height(14)
.margin(0, 2)
.child(new CycleButtonWidget().value(new BoolValue.Dynamic(() -> UsageTicker.enableOffHand, val -> {
UsageTicker.enableOffHand = val;
UsageTicker.reloadElements();
}))
.stateOverlay(TOGGLE_BUTTON)
.disableHoverBackground()
.size(14, 14)
.margin(8, 0)
.background(IDrawable.EMPTY))
.child(
IKey.lang("bogosort.gui.usageticker_offhand")
.asWidget()
.height(14)
.marginLeft(10)
.expanded()))
.child(
new Row().widthRel(1f)
.height(14)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum Mods {
AdventureBackpack2("adventurebackpack"),
Ae2("appliedenergistics2"),
AvaritiaAddons("avaritiaddons"),
Backhand("backhand"),
Backpack("Backpack"),
Bibliocraft("BiblioCraft"),
CookingForBlockheads("cookingforblockheads"),
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/bogosorter/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bogosort.gui.dropoff_render=Show Dropoff Targets
bogosort.gui.dropoff_chatmessage=Show Dropoff Chat Message
bogosort.gui.usageticker_enable=Enable Usage Ticker
bogosort.gui.usageticker_mainhand=Enable Usage Ticker Mainhand
bogosort.gui.usageticker_offhand=Enable Usage Ticker Offhand
bogosort.gui.usageticker_armor=Enable Usage Ticker Armor


Expand Down

0 comments on commit bd8631c

Please sign in to comment.