Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use the declaring class to access fields in child class #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

deepsource-autofix[bot]
Copy link

@deepsource-autofix deepsource-autofix bot commented Oct 28, 2023

Non-private static members of the parent class are accessible by child classes. However, it is a bad practice to do so, because it obscures where a value was actually declared. Always use only the declaring class to access static members.

Summary by CodeRabbit

  • Refactor: Updated the way the client object is accessed across multiple modules, improving code organization and modularity. This change affects the MCE, AutoRespawn, Automount, ChestIndexer, Test, NoFall, FreeLook, Freecam, GodBridge, Nuker, and SurvivalNuker modules.
  • User Impact: These changes are primarily under-the-hood improvements and should not directly affect the user experience. However, they lay the groundwork for more efficient code updates and maintenance in the future.

Non-private static members of the parent class are accessible by child classes. However, it is a bad practice to do so, because it obscures where a value was actually declared. Always use only the declaring class to access static members.
@coderabbitai
Copy link

coderabbitai bot commented Oct 28, 2023

Walkthrough

The codebase has undergone a significant refactoring, replacing direct access to the client object with access through a Module class across multiple files. This change suggests an effort to improve code organization and modularity, possibly within a Minecraft mod or plugin.

Changes

File Path Summary
.../combat/MCE.java Updated references to client and Module.client to Module.client.
.../misc/AutoRespawn.java Updated client.currentScreen and client.player.requestRespawn() to Module.client.currentScreen and Module.client.player.requestRespawn().
.../misc/Automount.java Replaced client with Module.client in several places.
.../misc/ChestIndexer.java Replaced client and client.world with Module.client and Module.client.world.
.../misc/Test.java Replaced client.player.getBlockPos() with Module.client.player.getBlockPos().
.../movement/NoFall.java Replaced client.player and client.player.fallDistance with Module.client.player and Module.client.player.fallDistance.
.../render/FreeLook.java Updated access to variables before and newyaw through Module class.
.../render/Freecam.java Modified client.player.getAbilities().setFlySpeed(0.05f); to client.Module.player.getAbilities().setFlySpeed(0.05f);.
.../world/GodBridge.java Updated the code to use Module.client.player instead of client.player.
.../world/Nuker.java Replaced client with Module.client in multiple places.
.../world/SurvivalNuker.java Replaced client.world with Module.client.world.

"In the land of code, where the shadows lie,

A rabbit hopped, its eyes on the sky. 🐇

With each leap, a change was made,

In the realm of modules, where the old client fade. 🌌

Now the code is clean, the structure is neat,

A feat of refactoring, truly a treat! 🎉

So here's to the coder, with their wisdom so grand,

In the world of Minecraft, they make their stand." 🎮


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

@buiawpkgew1
Copy link
Owner

@coderabbitai review

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 10

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 876c912 and fe83b3c.
Files selected for processing (11)
  • src/main/java/coffee/client/feature/module/impl/combat/MCE.java (3 hunks)
  • src/main/java/coffee/client/feature/module/impl/misc/AutoRespawn.java (1 hunks)
  • src/main/java/coffee/client/feature/module/impl/misc/Automount.java (1 hunks)
  • src/main/java/coffee/client/feature/module/impl/misc/ChestIndexer.java (5 hunks)
  • src/main/java/coffee/client/feature/module/impl/misc/Test.java (1 hunks)
  • src/main/java/coffee/client/feature/module/impl/movement/NoFall.java (1 hunks)
  • src/main/java/coffee/client/feature/module/impl/render/FreeLook.java (1 hunks)
  • src/main/java/coffee/client/feature/module/impl/render/Freecam.java (1 hunks)
  • src/main/java/coffee/client/feature/module/impl/world/GodBridge.java (1 hunks)
  • src/main/java/coffee/client/feature/module/impl/world/Nuker.java (4 hunks)
  • src/main/java/coffee/client/feature/module/impl/world/SurvivalNuker.java (1 hunks)
Files skipped from review due to trivial changes (2)
  • src/main/java/coffee/client/feature/module/impl/combat/MCE.java
  • src/main/java/coffee/client/feature/module/impl/world/GodBridge.java
Additional comments: 6
src/main/java/coffee/client/feature/module/impl/misc/AutoRespawn.java (1)
  • 13-18: The changes in this hunk are consistent with the pull request summary. The client object has been replaced with Module.client to access static members. This change improves the clarity of the code by making it clear where values are declared and accessed. However, ensure that the Module.client is properly initialized before this method is called to avoid null pointer exceptions.
src/main/java/coffee/client/feature/module/impl/render/FreeLook.java (1)
  • 77-86: The changes in this hunk are consistent with the pull request summary. The client object is replaced with Module.client to access static members. This change improves the clarity of the code by making it clear where values are declared and accessed. However, please ensure that the Module.client object is properly initialized and has the same properties and methods as the client object.
src/main/java/coffee/client/feature/module/impl/render/Freecam.java (1)
  • 92-97: The change from client.player to client.Module.player seems to be a mistake. The Module class does not have a player field. The player field is part of the client object. This change could cause a NullPointerException or a NoSuchFieldError. Please verify this change.
- client.Module.player.getAbilities().setFlySpeed(0.05f);
+ client.player.getAbilities().setFlySpeed(0.05f);
src/main/java/coffee/client/feature/module/impl/misc/Test.java (1)
  • 26-32: The change from client.player.getBlockPos() to Module.client.player.getBlockPos() is consistent with the pull request's goal of accessing static members through their declaring class. This change improves code clarity and maintainability. However, ensure that Module.client.player is always initialized before this method is called to avoid a potential NullPointerException.

Also, consider the potential implications of starting a new thread every time enable() is called. If enable() is called frequently, this could lead to a large number of threads being created, which could impact performance. Consider using a thread pool or some other form of limiting the number of threads created.

src/main/java/coffee/client/feature/module/impl/world/SurvivalNuker.java (1)
  • 84-88: The change from client.world to Module.client.world is consistent with the pull request's goal of accessing static members through their declaring class. This change improves code clarity and maintainability. However, ensure that Module.client.world is correctly initialized and has the same value as the previous client.world. Also, verify that this change does not introduce any null pointer exceptions or other runtime errors.
src/main/java/coffee/client/feature/module/impl/world/Nuker.java (1)
  • 47-54: The changes made here are consistent with the pull request summary. The client object has been replaced with Module.client to access static members. This change improves the clarity of the code by making it clear where values are declared and accessed.

Comment on lines +52 to 54
if (Module.client.player == null || client.getNetworkHandler() == null) {
return;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for client.getNetworkHandler() == null has not been updated to Module.client.getNetworkHandler() == null. This should be updated for consistency and to avoid potential null pointer exceptions.

- if (Module.client.player == null || client.getNetworkHandler() == null) {
+ if (Module.client.player == null || Module.client.getNetworkHandler() == null) {

57:
The client.getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.OnGroundOnly(true)); line has not been updated to use Module.client. This should be updated for consistency.

- case Packet -> client.getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.OnGroundOnly(true));
+ case Packet -> Module.client.getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.OnGroundOnly(true));

Commitable suggestion (Beta)
Suggested change
if (Module.client.player == null || client.getNetworkHandler() == null) {
return;
}
if (Module.client.player == null || Module.client.getNetworkHandler() == null) {
return;
}

[ISSUE][REFINE]

57:
The client.getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.OnGroundOnly(true)); line has not been updated to use Module.client. This should be updated for consistency.

- case Packet -> client.getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.OnGroundOnly(true));
+ case Packet -> Module.client.getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.OnGroundOnly(true));

[ISSUE][REFINE]

Comment on lines 30 to 53

@Override
public void onFastTick() {
if (client.player.hasVehicle()) return;
if (Module.client.player.hasVehicle()) return;

for (Entity entity : client.world.getEntities()){
if (entity.distanceTo(client.player) <= 4){
for (Entity entity : Module.client.world.getEntities()){
if (entity.distanceTo(Module.client.player) <= 4){
if (entity instanceof HorseEntity && horse.getValue()){
client.interactionManager.interactEntity(client.player, entity, Hand.MAIN_HAND);
Module.client.interactionManager.interactEntity(Module.client.player, entity, Hand.MAIN_HAND);
} else if (entity instanceof DonkeyEntity && donkey.getValue()){
client.interactionManager.interactEntity(client.player, entity, Hand.MAIN_HAND);
Module.client.interactionManager.interactEntity(Module.client.player, entity, Hand.MAIN_HAND);
} else if (entity instanceof MuleEntity && mule.getValue()){
client.interactionManager.interactEntity(client.player, entity, Hand.MAIN_HAND);
Module.client.interactionManager.interactEntity(Module.client.player, entity, Hand.MAIN_HAND);
} else if (entity instanceof BoatEntity && boat.getValue()){
client.interactionManager.interactEntity(client.player, entity, Hand.MAIN_HAND);
Module.client.interactionManager.interactEntity(Module.client.player, entity, Hand.MAIN_HAND);
} else if (entity instanceof MinecartEntity && minecart.getValue()){
client.interactionManager.interactEntity(client.player, entity, Hand.MAIN_HAND);
Module.client.interactionManager.interactEntity(Module.client.player, entity, Hand.MAIN_HAND);
} else if (entity instanceof PigEntity && pig.getValue()){
client.interactionManager.interactEntity(client.player, entity, Hand.MAIN_HAND);
Module.client.interactionManager.interactEntity(Module.client.player, entity, Hand.MAIN_HAND);
} else if (entity instanceof LlamaEntity && llama.getValue()){
client.interactionManager.interactEntity(client.player, entity, Hand.MAIN_HAND);
Module.client.interactionManager.interactEntity(Module.client.player, entity, Hand.MAIN_HAND);
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is repetitive and could be refactored to improve readability and maintainability. Consider creating a map of entity classes and their corresponding settings, then use a loop to check and interact with the entities. This would reduce the number of if statements and make the code easier to extend in the future.

Map<Class<? extends Entity>, BooleanSetting> entitySettings = new HashMap<>();
entitySettings.put(HorseEntity.class, horse);
entitySettings.put(DonkeyEntity.class, donkey);
entitySettings.put(MuleEntity.class, mule);
entitySettings.put(BoatEntity.class, boat);
entitySettings.put(MinecartEntity.class, minecart);
entitySettings.put(PigEntity.class, pig);
entitySettings.put(LlamaEntity.class, llama);

for (Entity entity : Module.client.world.getEntities()){
    if (entity.distanceTo(Module.client.player) <= 4){
        BooleanSetting setting = entitySettings.get(entity.getClass());
        if (setting != null && setting.getValue()){
            Module.client.interactionManager.interactEntity(Module.client.player, entity, Hand.MAIN_HAND);
        }
    }
}

Comment on lines 60 to 66
@Override
public void tick() {
switch (modeSetting.getValue()) {
case Packet -> iterateOverRange(client.interactionManager.getReachDistance(), vec3d -> {
case Packet -> iterateOverRange(Module.client.interactionManager.getReachDistance(), vec3d -> {
BlockPos bp = new BlockPos(BlockPos.ofFloored(vec3d));
BlockState bs = client.world.getBlockState(bp);
if (bs.isAir()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client object has been replaced with Module.client in the tick method. However, there is an inconsistency on line 65 where client.world is used instead of Module.client.world. This should be updated for consistency.

- BlockState bs = client.world.getBlockState(bp);
+ BlockState bs = Module.client.world.getBlockState(bp);

Commitable suggestion (Beta)
Suggested change
@Override
public void tick() {
switch (modeSetting.getValue()) {
case Packet -> iterateOverRange(client.interactionManager.getReachDistance(), vec3d -> {
case Packet -> iterateOverRange(Module.client.interactionManager.getReachDistance(), vec3d -> {
BlockPos bp = new BlockPos(BlockPos.ofFloored(vec3d));
BlockState bs = client.world.getBlockState(bp);
if (bs.isAir()) {
@Override
public void tick() {
switch (modeSetting.getValue()) {
case Packet -> iterateOverRange(Module.client.interactionManager.getReachDistance(), vec3d -> {
BlockPos bp = new BlockPos(BlockPos.ofFloored(vec3d));
BlockState bs = Module.client.world.getBlockState(bp);
if (bs.isAir()) {

Comment on lines 78 to 84
new Vec3d(1, 1, 1),
500);
});
case Griefing -> iterateOverRange(client.interactionManager.getReachDistance(), vec3d -> {
case Griefing -> iterateOverRange(Module.client.interactionManager.getReachDistance(), vec3d -> {
BlockPos bp = new BlockPos(BlockPos.ofFloored(vec3d));
BlockState bs = client.world.getBlockState(bp);
if (bs.isAir()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous hunk, client.world is used instead of Module.client.world on line 83. This should be updated for consistency.

- BlockState bs = client.world.getBlockState(bp);
+ BlockState bs = Module.client.world.getBlockState(bp);

Commitable suggestion (Beta)
Suggested change
new Vec3d(1, 1, 1),
500);
});
case Griefing -> iterateOverRange(client.interactionManager.getReachDistance(), vec3d -> {
case Griefing -> iterateOverRange(Module.client.interactionManager.getReachDistance(), vec3d -> {
BlockPos bp = new BlockPos(BlockPos.ofFloored(vec3d));
BlockState bs = client.world.getBlockState(bp);
if (bs.isAir()) {
new Vec3d(1, 1, 1),
500);
});
case Griefing -> iterateOverRange(Module.client.interactionManager.getReachDistance(), vec3d -> {
BlockPos bp = new BlockPos(BlockPos.ofFloored(vec3d));
- BlockState bs = client.world.getBlockState(bp);
+ BlockState bs = Module.client.world.getBlockState(bp);
if (bs.isAir()) {

Comment on lines 94 to 100
500);
}
});
case Interaction -> iterateOverRange(client.interactionManager.getReachDistance(), vec3d -> {
case Interaction -> iterateOverRange(Module.client.interactionManager.getReachDistance(), vec3d -> {
BlockPos bp = new BlockPos(BlockPos.ofFloored(vec3d));
BlockState bs = client.world.getBlockState(bp);
if (bs.isAir()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, client.world is used instead of Module.client.world on line 99. This should be updated for consistency.

- BlockState bs = client.world.getBlockState(bp);
+ BlockState bs = Module.client.world.getBlockState(bp);

Commitable suggestion (Beta)
Suggested change
500);
}
});
case Interaction -> iterateOverRange(client.interactionManager.getReachDistance(), vec3d -> {
case Interaction -> iterateOverRange(Module.client.interactionManager.getReachDistance(), vec3d -> {
BlockPos bp = new BlockPos(BlockPos.ofFloored(vec3d));
BlockState bs = client.world.getBlockState(bp);
if (bs.isAir()) {
500);
}
});
case Interaction -> iterateOverRange(Module.client.interactionManager.getReachDistance(), vec3d -> {
BlockPos bp = new BlockPos(BlockPos.ofFloored(vec3d));
BlockState bs = Module.client.world.getBlockState(bp);
if (bs.isAir()) {

Comment on lines 122 to 126
boolean isValidChestNeighbor(BlockPos owner, BlockPos bp) {
BlockState bs = client.world.getBlockState(bp);
BlockState bs1 = client.world.getBlockState(owner);
BlockState bs = Module.client.world.getBlockState(bp);
BlockState bs1 = Module.client.world.getBlockState(owner);
return bs1.getBlock() == bs.getBlock();
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function isValidChestNeighbor checks if the block at the given position bp is the same as the block at the position owner. This function assumes that the world state is consistent and that the blocks at the given positions exist. If the world state changes between the two calls to getBlockState, this function may return incorrect results. Consider adding error handling to account for this possibility.

Comment on lines 134 to 140
if (childrenMap.values().stream().anyMatch(blockPos -> blockPos.contains(lastPos))) {
return; // already part of another chest
}
BlockState bs = client.world.getBlockState(lastPos);
BlockState bs = Module.client.world.getBlockState(lastPos);
Block block = bs.getBlock();
List<Block> blocks = Arrays.asList(ALLOW_LIST);
if (blocks.contains(block)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function send checks if the block at the given position lastPos is in the ALLOW_LIST. This function assumes that the world state is consistent and that the block at the given position exists. If the world state changes between the call to getBlockState and the check against ALLOW_LIST, this function may return incorrect results. Consider adding error handling to account for this possibility.

Comment on lines 155 to 161
int x = (int) (-Math.round(Math.sin(v1)));
int z = (int) Math.round(Math.cos(v1));
BlockPos secondPos = lastPos.add(x, 0, z);
BlockState bs1 = client.world.getBlockState(secondPos);
BlockState bs1 = Module.client.world.getBlockState(secondPos);
if (bs1.getBlock() == block && bs1.get(ChestBlock.CHEST_TYPE) == chestType.getOpposite() && bs1.get(ChestBlock.FACING) == direction) {
stacks.remove(secondPos); // if the neighbour was already indexed, remove him and use this pos instead
childrenMap.computeIfAbsent(lastPos, blockPos -> new ArrayList<>()).add(secondPos);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function send updates the stacks and childrenMap based on the state of the world. This function assumes that the world state is consistent and that the blocks at the given positions exist. If the world state changes between the calls to getBlockState and the updates to stacks and childrenMap, this function may behave incorrectly. Consider adding error handling to account for this possibility.

Comment on lines 176 to 182
}
this.stacks.putIfAbsent(this.currentPosClicked, new Int2ObjectArrayMap<>());
Int2ObjectMap<ItemStack> itemStackInt2ObjectMap = this.stacks.get(this.currentPosClicked);
DefaultedList<Slot> slots = client.player.currentScreenHandler.slots;
DefaultedList<Slot> slots = Module.client.player.currentScreenHandler.slots;
int contentLength = slots.size() - 9 * 4; // this one includes the actual inventory so we remove it from the index
for (int i = 0; i < contentLength; i++) {
Slot slot = slots.get(i);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function send updates the stacks based on the state of the player's inventory. This function assumes that the player's inventory state is consistent and that the slots in the inventory exist. If the inventory state changes between the call to getStack and the updates to stacks, this function may behave incorrectly. Consider adding error handling to account for this possibility.

Comment on lines 194 to 203
public void tick() {
if (updateTimer.hasExpired(5_000)) {
for (BlockPos blockPos : new ArrayList<>(stacks.keySet())) {
if (!blockPos.isWithinDistance(client.player.getBlockPos(), 64)) {
if (!blockPos.isWithinDistance(Module.client.player.getBlockPos(), 64)) {
continue; // dont update this one we have no fucking idea whats in it
}
BlockState bs = client.world.getBlockState(blockPos);
BlockState bs = Module.client.world.getBlockState(blockPos);
if (!Arrays.asList(ALLOW_LIST).contains(bs.getBlock())) {
stacks.remove(blockPos);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function tick updates the stacks based on the state of the world. This function assumes that the world state is consistent and that the blocks at the given positions exist. If the world state changes between the calls to getBlockState and the updates to stacks, this function may behave incorrectly. Consider adding error handling to account for this possibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant