refactor: added ICommons for minecraft access

This commit is contained in:
əlemi 2023-02-13 18:25:45 +01:00
parent 114cd09dc9
commit 4a4224042d
Signed by: alemi
GPG key ID: A4895B84D311642C
11 changed files with 54 additions and 52 deletions

View file

@ -1,7 +1,6 @@
package ftbsc.bscv; package ftbsc.bscv;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity; import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands; import net.minecraft.command.Commands;
@ -35,14 +34,12 @@ import ftbsc.bscv.module.hud.*;
// The value here should match an entry in the META-INF/mods.toml file // The value here should match an entry in the META-INF/mods.toml file
@Mod("bscv") @Mod("bscv")
public class BoSCoVicino { public class BoSCoVicino implements ICommons {
public static String MOD_ID = "bscv"; public static String MOD_ID = "bscv";
// Directly reference a log4j logger. // Directly reference a log4j logger.
public static final Logger LOGGER = LogManager.getLogger(); public static final Logger LOGGER = LogManager.getLogger();
public static Minecraft minecraft;
public static List<Module> mods; public static List<Module> mods;
private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>(); private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
@ -53,9 +50,6 @@ public class BoSCoVicino {
// Register the setup method for modloading // Register the setup method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);
// Store minecraft instance
BoSCoVicino.minecraft = Minecraft.getInstance();
// load and register mods // load and register mods
BoSCoVicino.mods = new ArrayList<Module>(); BoSCoVicino.mods = new ArrayList<Module>();
@ -81,7 +75,7 @@ public class BoSCoVicino {
} }
public static void log(String message) { public static void log(String message) {
BoSCoVicino.minecraft.gui.getChat().addMessage(new StringTextComponent(message)); MC.gui.getChat().addMessage(new StringTextComponent(message));
} }
private void clientSetup(final FMLClientSetupEvent event) { private void clientSetup(final FMLClientSetupEvent event) {
@ -95,7 +89,7 @@ public class BoSCoVicino {
dispatcher.register( dispatcher.register(
Commands.literal("hints") Commands.literal("hints")
.executes(ctx -> { .executes(ctx -> {
ClientPlayerEntity player = BoSCoVicino.minecraft.player; ClientPlayerEntity player = MC.player;
if (player != null) { if (player != null) {
try { try {
Field commands = player.connection.getClass().getDeclaredField("field_195517_n"); // "commands", it's obfuscated Field commands = player.connection.getClass().getDeclaredField("field_195517_n"); // "commands", it's obfuscated
@ -125,11 +119,11 @@ public class BoSCoVicino {
@SubscribeEvent @SubscribeEvent
public void onClientChatEvent(ClientChatEvent event) { public void onClientChatEvent(ClientChatEvent event) {
if (event.getMessage().startsWith("/")) { if (event.getMessage().startsWith("/")) {
CommandSource source = BoSCoVicino.minecraft.player.createCommandSourceStack(); // TODO player could be NULL CommandSource source = MC.player.createCommandSourceStack(); // TODO player could be NULL
try { try {
LOGGER.info(String.format("Running command %s", event.getMessage())); LOGGER.info(String.format("Running command %s", event.getMessage()));
this.dispatcher.execute(event.getMessage().substring(1), source); this.dispatcher.execute(event.getMessage().substring(1), source);
BoSCoVicino.minecraft.gui.getChat().addRecentChat(event.getMessage()); MC.gui.getChat().addRecentChat(event.getMessage());
event.setCanceled(true); event.setCanceled(true);
} catch (CommandSyntaxException e) { } catch (CommandSyntaxException e) {
LOGGER.error(String.format("Syntax error in command : %s", e.toString())); LOGGER.error(String.format("Syntax error in command : %s", e.toString()));
@ -140,7 +134,7 @@ public class BoSCoVicino {
@SubscribeEvent @SubscribeEvent
public void onWorldLoad(WorldEvent.Load event) { public void onWorldLoad(WorldEvent.Load event) {
// TEMPORARY! add command to regenerate suggestions // TEMPORARY! add command to regenerate suggestions
ClientPlayerEntity player = BoSCoVicino.minecraft.player; ClientPlayerEntity player = MC.player;
if (player != null) { if (player != null) {
try { try {
Field commands = player.connection.getClass().getDeclaredField("field_195517_n"); // "commands", it's obfuscated Field commands = player.connection.getClass().getDeclaredField("field_195517_n"); // "commands", it's obfuscated

View file

@ -0,0 +1,7 @@
package ftbsc.bscv;
import net.minecraft.client.Minecraft;
public interface ICommons {
public static final Minecraft MC = Minecraft.getInstance();
}

View file

@ -5,6 +5,7 @@ import static ftbsc.bscv.tools.Text.TextBuilder;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.ICommons;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType; import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
@ -13,7 +14,7 @@ import net.minecraftforge.eventbus.api.SubscribeEvent;
import ftbsc.bscv.module.HudModule; import ftbsc.bscv.module.HudModule;
import ftbsc.bscv.module.Module; import ftbsc.bscv.module.Module;
public class ActiveModules extends HudModule { public class ActiveModules extends HudModule implements ICommons {
public ActiveModules(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) { public ActiveModules(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
super("ActiveModules", builder, dispatcher); super("ActiveModules", builder, dispatcher);
@ -32,7 +33,7 @@ public class ActiveModules extends HudModule {
.y(this.y.get() + offset) .y(this.y.get() + offset)
.scale(this.scale.get()) .scale(this.scale.get())
.render(event.getMatrixStack(), event.getWindow()); .render(event.getMatrixStack(), event.getWindow());
offset += BoSCoVicino.minecraft.font.lineHeight; offset += MC.font.lineHeight;
} }
} }
} }

View file

@ -2,7 +2,6 @@ package ftbsc.bscv.module.hud;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.math.vector.Vector3d;
import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent;
@ -11,10 +10,10 @@ import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import static ftbsc.bscv.tools.Text.TextBuilder; import static ftbsc.bscv.tools.Text.TextBuilder;
import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.ICommons;
import ftbsc.bscv.module.HudModule; import ftbsc.bscv.module.HudModule;
public class Coordinates extends HudModule { public class Coordinates extends HudModule implements ICommons {
public Coordinates(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) { public Coordinates(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
super("Coordinates", builder, dispatcher); super("Coordinates", builder, dispatcher);
@ -22,9 +21,8 @@ public class Coordinates extends HudModule {
@SubscribeEvent @SubscribeEvent
public void onRenderOverlay(RenderGameOverlayEvent event) { public void onRenderOverlay(RenderGameOverlayEvent event) {
Minecraft mc = BoSCoVicino.minecraft; if (event.getType() == ElementType.TEXT && MC.player != null) {
if (event.getType() == ElementType.TEXT && mc.player != null) { Vector3d position = MC.player.position();
Vector3d position = mc.player.position();
TextBuilder() TextBuilder()
.txt(String.format("[ X %.1f | %.1f Z ] %.1f Y", position.x(), position.z(), position.y())) .txt(String.format("[ X %.1f | %.1f Z ] %.1f Y", position.x(), position.z(), position.y()))
.anchor(this.anchor.get()) .anchor(this.anchor.get())

View file

@ -9,7 +9,7 @@ import java.util.stream.Collectors;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.ICommons;
import ftbsc.bscv.module.HudModule; import ftbsc.bscv.module.HudModule;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@ -18,7 +18,7 @@ import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
public class EntityList extends HudModule { public class EntityList extends HudModule implements ICommons {
public EntityList(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) { public EntityList(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
super("EntityList", builder, dispatcher); super("EntityList", builder, dispatcher);
@ -29,7 +29,7 @@ public class EntityList extends HudModule {
if (event.getType() != ElementType.TEXT) return; if (event.getType() != ElementType.TEXT) return;
List<String> entities = new ArrayList<>(); List<String> entities = new ArrayList<>();
for (Entity e : BoSCoVicino.minecraft.level.entitiesForRendering()) { for (Entity e : MC.level.entitiesForRendering()) {
// TODO do some filtering here? // TODO do some filtering here?
entities.add(e.getName().getString()); entities.add(e.getName().getString());
} }
@ -53,7 +53,7 @@ public class EntityList extends HudModule {
.y(this.y.get() + offset) .y(this.y.get() + offset)
.scale(this.scale.get()) .scale(this.scale.get())
.render(event.getMatrixStack(), event.getWindow()); .render(event.getMatrixStack(), event.getWindow());
offset += BoSCoVicino.minecraft.font.lineHeight; offset += MC.font.lineHeight;
} }
} }
} }

View file

@ -8,6 +8,7 @@ import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.BoolArgumentType;
import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.module.HudModule; import ftbsc.bscv.module.HudModule;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
@ -20,7 +21,7 @@ import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.event.TickEvent; import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
public class InfoDisplay extends HudModule { public class InfoDisplay extends HudModule implements ICommons {
private Vector3d last_position = new Vector3d(0.0, 0.0, 0.0); private Vector3d last_position = new Vector3d(0.0, 0.0, 0.0);
@ -68,10 +69,10 @@ public class InfoDisplay extends HudModule {
@SubscribeEvent @SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) { public void onTick(TickEvent.ClientTickEvent event) {
if (!this.speed.get()) return; if (!this.speed.get()) return;
if (BoSCoVicino.minecraft.player != null) { if (MC.player != null) {
this.instant_speed = this.instant_speed =
this.last_position.distanceTo(BoSCoVicino.minecraft.player.position()); this.last_position.distanceTo(MC.player.position());
this.last_position = BoSCoVicino.minecraft.player.position(); this.last_position = MC.player.position();
} else { } else {
this.instant_speed = 0.0; this.instant_speed = 0.0;
} }
@ -90,7 +91,7 @@ public class InfoDisplay extends HudModule {
public void onRenderOverlay(RenderGameOverlayEvent event) { public void onRenderOverlay(RenderGameOverlayEvent event) {
if (event.getType() != ElementType.TEXT) return; if (event.getType() != ElementType.TEXT) return;
Minecraft mc = BoSCoVicino.minecraft; Minecraft mc = MC;
int offset = 0; int offset = 0;
double scale = this.scale.get(); double scale = this.scale.get();
if (this.logo.get()) { if (this.logo.get()) {

View file

@ -6,6 +6,7 @@ import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType;
import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.module.QuickModule; import ftbsc.bscv.module.QuickModule;
import net.minecraft.client.entity.player.ClientPlayerEntity; import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
@ -13,7 +14,7 @@ import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.event.TickEvent; import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
public class VanillaFlight extends QuickModule { public class VanillaFlight extends QuickModule implements ICommons {
public final ForgeConfigSpec.ConfigValue<Boolean> force; public final ForgeConfigSpec.ConfigValue<Boolean> force;
public final ForgeConfigSpec.ConfigValue<Double> speed; public final ForgeConfigSpec.ConfigValue<Double> speed;
@ -64,7 +65,7 @@ public class VanillaFlight extends QuickModule {
@SubscribeEvent @SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) { public void onTick(TickEvent.ClientTickEvent event) {
ClientPlayerEntity player = BoSCoVicino.minecraft.player; ClientPlayerEntity player = MC.player;
if (player == null) return; if (player == null) return;
player.abilities.mayfly = true; player.abilities.mayfly = true;
@ -87,16 +88,16 @@ public class VanillaFlight extends QuickModule {
@Override @Override
protected void onEnabled() { protected void onEnabled() {
if (BoSCoVicino.minecraft.player != null) { if (MC.player != null) {
this.couldFlyBefore = BoSCoVicino.minecraft.player.abilities.mayfly; this.couldFlyBefore = MC.player.abilities.mayfly;
this.flyingSpeedBefore = BoSCoVicino.minecraft.player.abilities.getFlyingSpeed(); this.flyingSpeedBefore = MC.player.abilities.getFlyingSpeed();
BoSCoVicino.log(String.format("Flying speed before = %f", this.flyingSpeedBefore)); BoSCoVicino.log(String.format("Flying speed before = %f", this.flyingSpeedBefore));
} }
} }
@Override @Override
protected void onDisabled() { protected void onDisabled() {
ClientPlayerEntity player = BoSCoVicino.minecraft.player; ClientPlayerEntity player = MC.player;
if (player != null) { if (player != null) {
player.abilities.mayfly = this.couldFlyBefore; player.abilities.mayfly = this.couldFlyBefore;
player.abilities.setFlyingSpeed(this.flyingSpeedBefore); player.abilities.setFlyingSpeed(this.flyingSpeedBefore);

View file

@ -12,9 +12,9 @@ import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import ftbsc.bscv.module.QuickModule; import ftbsc.bscv.module.QuickModule;
import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.ICommons;
public class FastInteract extends QuickModule { public class FastInteract extends QuickModule implements ICommons {
Field delayField; Field delayField;
@ -25,7 +25,7 @@ public class FastInteract extends QuickModule {
@Override @Override
protected void onEnabled() { protected void onEnabled() {
try { try {
delayField = BoSCoVicino.minecraft.getClass().getDeclaredField("field_71467_ac"); delayField = MC.getClass().getDeclaredField("field_71467_ac");
delayField.setAccessible(true); delayField.setAccessible(true);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
log("! failed accessing delay field"); log("! failed accessing delay field");
@ -35,9 +35,9 @@ public class FastInteract extends QuickModule {
@SubscribeEvent @SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) { public void onTick(TickEvent.ClientTickEvent event) {
if (BoSCoVicino.minecraft == null) return; if (MC == null) return;
try { try {
this.delayField.set(BoSCoVicino.minecraft, 0); this.delayField.set(MC, 0);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
log("! failed accessing delay field"); log("! failed accessing delay field");
this.disable(); this.disable();

View file

@ -13,9 +13,9 @@ import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import ftbsc.bscv.module.QuickModule; import ftbsc.bscv.module.QuickModule;
import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.ICommons;
public class Fullbright extends QuickModule { public class Fullbright extends QuickModule implements ICommons {
private final ForgeConfigSpec.ConfigValue<String> mode; private final ForgeConfigSpec.ConfigValue<String> mode;
@ -31,17 +31,17 @@ public class Fullbright extends QuickModule {
@SubscribeEvent @SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) { public void onTick(TickEvent.ClientTickEvent event) {
if (BoSCoVicino.minecraft == null) return; if (MC == null) return;
if (BoSCoVicino.minecraft.player == null) return; if (MC.player == null) return;
if (this.mode.get().equals("potion")) { if (this.mode.get().equals("potion")) {
BoSCoVicino.minecraft.player.addEffect(new EffectInstance(Effect.byId(16), 5204)); MC.player.addEffect(new EffectInstance(Effect.byId(16), 5204));
} }
} }
@Override @Override
protected void onDisabled() { protected void onDisabled() {
if (this.mode.get().equals("potion")) { if (this.mode.get().equals("potion")) {
BoSCoVicino.minecraft.player.removeEffect(Effect.byId(16)); MC.player.removeEffect(Effect.byId(16));
} }
} }
} }

View file

@ -1,11 +1,11 @@
package ftbsc.bscv.tools; package ftbsc.bscv.tools;
import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.ICommons;
import net.minecraft.client.MainWindow; import net.minecraft.client.MainWindow;
import net.minecraft.client.gui.screen.ChatScreen; import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.util.math.vector.Vector2f; import net.minecraft.util.math.vector.Vector2f;
public enum Anchor { public enum Anchor implements ICommons {
TOPLEFT("TOPLEFT"), TOPCENTER("TOPCENTER"), TOPRIGHT("TOPRIGHT"), TOPLEFT("TOPLEFT"), TOPCENTER("TOPCENTER"), TOPRIGHT("TOPRIGHT"),
MIDDLELEFT("MIDDLELEFT"), MIDDLECENTER("MIDDLECENTER"), MIDDLERIGHT("MIDDLERIGHT"), MIDDLELEFT("MIDDLELEFT"), MIDDLECENTER("MIDDLECENTER"), MIDDLERIGHT("MIDDLERIGHT"),
@ -19,7 +19,7 @@ public enum Anchor {
case BOTTOMLEFT: case BOTTOMLEFT:
case BOTTOMCENTER: case BOTTOMCENTER:
case BOTTOMRIGHT: case BOTTOMRIGHT:
if (BoSCoVicino.minecraft.screen instanceof ChatScreen) { if (MC.screen instanceof ChatScreen) {
offset = 15; offset = 15;
} }
default: default:

View file

@ -3,7 +3,7 @@ package ftbsc.bscv.tools;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.ICommons;
import net.minecraft.client.MainWindow; import net.minecraft.client.MainWindow;
import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.math.vector.Vector2f; import net.minecraft.util.math.vector.Vector2f;
@ -13,7 +13,7 @@ import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.Style; import net.minecraft.util.text.Style;
public final class Text { public final class Text implements ICommons {
String text; String text;
Anchor anchor; Anchor anchor;
Style style; Style style;
@ -65,7 +65,7 @@ public final class Text {
} }
public void render(MatrixStack stack, MainWindow window) { public void render(MatrixStack stack, MainWindow window) {
FontRenderer font = BoSCoVicino.minecraft.font; FontRenderer font = MC.font;
ITextComponent text = new StringTextComponent(this.text).setStyle(this.style); ITextComponent text = new StringTextComponent(this.text).setStyle(this.style);
Vector2f abs_coords = this.anchor.translate( Vector2f abs_coords = this.anchor.translate(
new Vector2f(this.x, this.y), new Vector2f(this.x, this.y),