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;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.command.CommandSource;
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
@Mod("bscv")
public class BoSCoVicino {
public class BoSCoVicino implements ICommons {
public static String MOD_ID = "bscv";
// Directly reference a log4j logger.
public static final Logger LOGGER = LogManager.getLogger();
public static Minecraft minecraft;
public static List<Module> mods;
private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
@ -53,9 +50,6 @@ public class BoSCoVicino {
// Register the setup method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);
// Store minecraft instance
BoSCoVicino.minecraft = Minecraft.getInstance();
// load and register mods
BoSCoVicino.mods = new ArrayList<Module>();
@ -81,7 +75,7 @@ public class BoSCoVicino {
}
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) {
@ -95,7 +89,7 @@ public class BoSCoVicino {
dispatcher.register(
Commands.literal("hints")
.executes(ctx -> {
ClientPlayerEntity player = BoSCoVicino.minecraft.player;
ClientPlayerEntity player = MC.player;
if (player != null) {
try {
Field commands = player.connection.getClass().getDeclaredField("field_195517_n"); // "commands", it's obfuscated
@ -125,11 +119,11 @@ public class BoSCoVicino {
@SubscribeEvent
public void onClientChatEvent(ClientChatEvent event) {
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 {
LOGGER.info(String.format("Running command %s", event.getMessage()));
this.dispatcher.execute(event.getMessage().substring(1), source);
BoSCoVicino.minecraft.gui.getChat().addRecentChat(event.getMessage());
MC.gui.getChat().addRecentChat(event.getMessage());
event.setCanceled(true);
} catch (CommandSyntaxException e) {
LOGGER.error(String.format("Syntax error in command : %s", e.toString()));
@ -140,7 +134,7 @@ public class BoSCoVicino {
@SubscribeEvent
public void onWorldLoad(WorldEvent.Load event) {
// TEMPORARY! add command to regenerate suggestions
ClientPlayerEntity player = BoSCoVicino.minecraft.player;
ClientPlayerEntity player = MC.player;
if (player != null) {
try {
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 ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.ICommons;
import net.minecraft.command.CommandSource;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
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.Module;
public class ActiveModules extends HudModule {
public class ActiveModules extends HudModule implements ICommons {
public ActiveModules(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
super("ActiveModules", builder, dispatcher);
@ -32,7 +33,7 @@ public class ActiveModules extends HudModule {
.y(this.y.get() + offset)
.scale(this.scale.get())
.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 net.minecraft.client.Minecraft;
import net.minecraft.command.CommandSource;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
@ -11,10 +10,10 @@ import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import static ftbsc.bscv.tools.Text.TextBuilder;
import ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.ICommons;
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) {
super("Coordinates", builder, dispatcher);
@ -22,9 +21,8 @@ public class Coordinates extends HudModule {
@SubscribeEvent
public void onRenderOverlay(RenderGameOverlayEvent event) {
Minecraft mc = BoSCoVicino.minecraft;
if (event.getType() == ElementType.TEXT && mc.player != null) {
Vector3d position = mc.player.position();
if (event.getType() == ElementType.TEXT && MC.player != null) {
Vector3d position = MC.player.position();
TextBuilder()
.txt(String.format("[ X %.1f | %.1f Z ] %.1f Y", position.x(), position.z(), position.y()))
.anchor(this.anchor.get())

View file

@ -9,7 +9,7 @@ import java.util.stream.Collectors;
import com.mojang.brigadier.CommandDispatcher;
import ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.module.HudModule;
import net.minecraft.command.CommandSource;
import net.minecraft.entity.Entity;
@ -18,7 +18,7 @@ import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.common.ForgeConfigSpec;
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) {
super("EntityList", builder, dispatcher);
@ -29,7 +29,7 @@ public class EntityList extends HudModule {
if (event.getType() != ElementType.TEXT) return;
List<String> entities = new ArrayList<>();
for (Entity e : BoSCoVicino.minecraft.level.entitiesForRendering()) {
for (Entity e : MC.level.entitiesForRendering()) {
// TODO do some filtering here?
entities.add(e.getName().getString());
}
@ -53,7 +53,7 @@ public class EntityList extends HudModule {
.y(this.y.get() + offset)
.scale(this.scale.get())
.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 ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.module.HudModule;
import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandSource;
@ -20,7 +21,7 @@ import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.event.TickEvent;
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);
@ -68,10 +69,10 @@ public class InfoDisplay extends HudModule {
@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) {
if (!this.speed.get()) return;
if (BoSCoVicino.minecraft.player != null) {
if (MC.player != null) {
this.instant_speed =
this.last_position.distanceTo(BoSCoVicino.minecraft.player.position());
this.last_position = BoSCoVicino.minecraft.player.position();
this.last_position.distanceTo(MC.player.position());
this.last_position = MC.player.position();
} else {
this.instant_speed = 0.0;
}
@ -90,7 +91,7 @@ public class InfoDisplay extends HudModule {
public void onRenderOverlay(RenderGameOverlayEvent event) {
if (event.getType() != ElementType.TEXT) return;
Minecraft mc = BoSCoVicino.minecraft;
Minecraft mc = MC;
int offset = 0;
double scale = this.scale.get();
if (this.logo.get()) {

View file

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

View file

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

View file

@ -13,9 +13,9 @@ import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
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;
@ -31,17 +31,17 @@ public class Fullbright extends QuickModule {
@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) {
if (BoSCoVicino.minecraft == null) return;
if (BoSCoVicino.minecraft.player == null) return;
if (MC == null) return;
if (MC.player == null) return;
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
protected void onDisabled() {
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;
import ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.ICommons;
import net.minecraft.client.MainWindow;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.util.math.vector.Vector2f;
public enum Anchor {
public enum Anchor implements ICommons {
TOPLEFT("TOPLEFT"), TOPCENTER("TOPCENTER"), TOPRIGHT("TOPRIGHT"),
MIDDLELEFT("MIDDLELEFT"), MIDDLECENTER("MIDDLECENTER"), MIDDLERIGHT("MIDDLERIGHT"),
@ -19,7 +19,7 @@ public enum Anchor {
case BOTTOMLEFT:
case BOTTOMCENTER:
case BOTTOMRIGHT:
if (BoSCoVicino.minecraft.screen instanceof ChatScreen) {
if (MC.screen instanceof ChatScreen) {
offset = 15;
}
default:

View file

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