Skip to content

Commit

Permalink
Optimize EnchantingPowerInfoProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
strubium committed Oct 18, 2024
1 parent 7731c89 commit 78dec64
Showing 1 changed file with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import mcjty.theoneprobe.api.*;
import mcjty.theoneprobe.Utilities;
import net.minecraft.block.BlockBookshelf;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
Expand All @@ -23,21 +24,31 @@ public String getID() {

@Override
public void addProbeInfo(ProbeMode mode, @Nonnull IProbeInfo probeInfo, EntityPlayer player, @Nonnull World world, @Nonnull IBlockState blockState, @Nonnull IProbeHitData data) {
float enchantingPower = ForgeHooks.getEnchantPower(world, data.getPos());
if (blockState.getBlock().hasTileEntity(blockState) && world.getTileEntity(data.getPos()) instanceof TileEntityEnchantmentTable) {
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos(data.getPos());
enchantingPower = 0.0F;
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
if ((x != 0 || z != 0) && world.isAirBlock(pos.add(z, 0, x)) && world.isAirBlock(pos.add(z, 1, x))) {
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z * 2, 0, x * 2));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z * 2, 1, x * 2));
if (z != 0 && x != 0) {
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z * 2, 0, x));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z * 2, 1, x));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z, 0, x * 2));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z, 1, x * 2));
}
// Only proceed if the block has a TileEntity and is an enchantment table
if (!blockState.getBlock().hasTileEntity(blockState) || !(world.getTileEntity(data.getPos()) instanceof TileEntityEnchantmentTable)) {
return;
}

float enchantingPower = 0.0F;
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();

// Calculate enchanting power based on surrounding blocks
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
if (x == 0 && z == 0) continue; // Skip the center block

// Set position to current check (air blocks around the enchantment table)
if (world.isAirBlock(pos.setPos(data.getPos().add(z, 0, x))) && world.isAirBlock(pos.setPos(data.getPos().add(z, 1, x)))) {

// Using MutableBlockPos#setPos to avoid unnecessary object creation
enchantingPower += ForgeHooks.getEnchantPower(world, pos.setPos(data.getPos().add(z * 2, 0, x * 2)));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.setPos(data.getPos().add(z * 2, 1, x * 2)));

if (x != 0 && z != 0) {
enchantingPower += ForgeHooks.getEnchantPower(world, pos.setPos(data.getPos().add(z * 2, 0, x)));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.setPos(data.getPos().add(z * 2, 1, x)));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.setPos(data.getPos().add(z, 0, x * 2)));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.setPos(data.getPos().add(z, 1, x * 2)));
}
}
}
Expand Down

0 comments on commit 78dec64

Please sign in to comment.