Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
btwonion committed Apr 18, 2024
1 parent 3f9b4af commit 4d09ecc
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public void redirectInventoryDrops(
return;
}

boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(
TelekinesisPolicy.MobDrops,
boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops,
serverPlayer,
serverPlayer.getMainHandItem(),
player -> instance.forEach(item -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,21 @@ private static void manipulateDrops(
if (!(level instanceof ServerLevel serverLevel)) return;
Block block = blockState.getBlock();
if (EnchantmentHelper.hasSilkTouch(itemStack)) return;
boolean hasTelekinesis = TelekinesisUtils.handleTelekinesisBlock(TelekinesisPolicy.ExpDrops,
entity,
itemStack,
player -> {
int expToAdd = 0;
if (block instanceof DropExperienceBlock expBlock)
expToAdd = ((DropExperienceBlockAccessor) expBlock).getXpRange()
.sample(level.random);
if (block instanceof RedStoneOreBlock) expToAdd = 1 + level.random.nextInt(5);
if (block instanceof SculkCatalystBlock catalystBlock)
expToAdd = ((CatalystBlockAccessor) catalystBlock).getXpRange()
.sample(level.random);
if (block instanceof SculkSensorBlock || block instanceof SculkShriekerBlock)
expToAdd = ConstantInt.of(5)
.sample(level.random);
if (block instanceof SpawnerBlock) expToAdd = level.random.nextInt(15) + level.random.nextInt(15);
if (block instanceof InfestedBlock infestedBlock)
infestedBlock.spawnAfterBreak(blockState, serverLevel, blockPos, itemStack, true);
PlayerUtils.addExpToPlayer(player, expToAdd);
}
);
boolean hasTelekinesis = TelekinesisUtils.handleTelekinesisBlock(TelekinesisPolicy.ExpDrops, entity, itemStack, player -> {
int expToAdd = 0;
if (block instanceof DropExperienceBlock expBlock) expToAdd = ((DropExperienceBlockAccessor) expBlock).getXpRange()
.sample(level.random);
if (block instanceof RedStoneOreBlock) expToAdd = 1 + level.random.nextInt(5);
if (block instanceof SculkCatalystBlock catalystBlock) expToAdd = ((CatalystBlockAccessor) catalystBlock).getXpRange()
.sample(level.random);
if (block instanceof SculkSensorBlock || block instanceof SculkShriekerBlock) expToAdd = ConstantInt.of(5)
.sample(level.random);
if (block instanceof SpawnerBlock) expToAdd = level.random.nextInt(15) + level.random.nextInt(15);
if (block instanceof InfestedBlock infestedBlock)
infestedBlock.spawnAfterBreak(blockState, serverLevel, blockPos, itemStack, true);
PlayerUtils.addExpToPlayer(player, expToAdd);
});

if (hasTelekinesis) ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ private static void registerTelekinesis(
if (!isTelekinesisRegistered) {
MainKt.setTelekinesis(new TelekinesisEnchantment());
isTelekinesisRegistered = true;
Registry.register(
BuiltInRegistries.ENCHANTMENT,
new ResourceLocation("telekinesis", "telekinesis"),
MainKt.getTelekinesis()
);
Registry.register(BuiltInRegistries.ENCHANTMENT, new ResourceLocation("telekinesis", "telekinesis"), MainKt.getTelekinesis());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ public void redirectCommonDrops(
boolean bl
) {
args.<Consumer<ItemStack>>set(2, item -> {
boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops,
damageSource,
player -> {
if (!player.addItem(item)) livingEntity.spawnAtLocation(item);
}
);
boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, damageSource, player -> {
if (!player.addItem(item)) livingEntity.spawnAtLocation(item);
});

if (!hasTelekinesis) livingEntity.spawnAtLocation(item);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ public void manipulateWoolDrops(

ItemStack item = new ItemStack(Items.CARVED_PUMPKIN);

boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.ShearingDrops,
player,
serverPlayer -> {
if (!serverPlayer.addItem(item)) instance.spawnAtLocation(item, 1.7F);
}
);
boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.ShearingDrops, player, serverPlayer -> {
if (!serverPlayer.addItem(item)) instance.spawnAtLocation(item, 1.7F);
});

if (!hasTelekinesis) instance.spawnAtLocation(item, 1.7F);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ protected ItemEntity redirectEquipmentDrop(
final var attacker = instance.getLastAttacker();
if (!(attacker instanceof ServerPlayer serverPlayer)) return original.call(instance, stack);

boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops,
serverPlayer,
null,
player -> {
if (!player.addItem(stack.asItem()
.getDefaultInstance())) instance.spawnAtLocation(stack);
}
);
boolean hasTelekinesis = TelekinesisUtils.handleTelekinesis(TelekinesisPolicy.MobDrops, serverPlayer, null, player -> {
if (!player.addItem(stack.asItem()
.getDefaultInstance())) instance.spawnAtLocation(stack);
});

if (!hasTelekinesis) return original.call(instance, stack);
else return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public class EntityUtils {

public static boolean spawnAtLocationInject(LivingEntity entity, ItemLike item) {
return spawnAtLocationInject(entity, item.asItem().getDefaultInstance());
public static boolean spawnAtLocationInject(
LivingEntity entity,
ItemLike item
) {
return spawnAtLocationInject(
entity,
item.asItem()
.getDefaultInstance()
);
}

public static boolean spawnAtLocationInject(LivingEntity entity, ItemStack item) {
public static boolean spawnAtLocationInject(
LivingEntity entity,
ItemStack item
) {
final var attacker = entity.getLastAttacker();
if (!(attacker instanceof ServerPlayer serverPlayer)) return true;

Expand All @@ -32,7 +41,10 @@ public static boolean spawnAtLocationInject(LivingEntity entity, ItemStack item)
return !hasTelekinesis;
}

public static Item getDropItemInject(Item original, DamageSource source) {
public static Item getDropItemInject(
Item original,
DamageSource source
) {
final var attacker = source.getEntity();
if (!(attacker instanceof ServerPlayer)) return original;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ private static int repairPlayerItems(
Player player,
Integer exp
) {
Map.Entry<EquipmentSlot, ItemStack> entry = EnchantmentHelper.getRandomItemWith(Enchantments.MENDING,
player,
ItemStack::isDamaged);
Map.Entry<EquipmentSlot, ItemStack> entry = EnchantmentHelper.getRandomItemWith(Enchantments.MENDING, player, ItemStack::isDamaged);
if (entry != null) {
ItemStack itemStack = entry.getValue();
int j = Math.min(exp * 2, itemStack.getDamageValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,14 @@ private static boolean playerMeetsConditions(
boolean hasArmorTelekinesis = player.getInventory().armor.stream()
.allMatch(item -> EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), item) > 0);
boolean hasMainHandTelekinesis = (itemStack != null && EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(),
itemStack) > 0) || EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(),
player.getMainHandItem()) > 0;
boolean hasOffHandTelekinesis = EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(),
player.getOffhandItem()) > 0;
itemStack
) > 0) || EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), player.getMainHandItem()) > 0;
boolean hasOffHandTelekinesis = EnchantmentHelper.getItemEnchantmentLevel(MainKt.getTelekinesis(), player.getOffhandItem()) > 0;

if (isEnabledByDefault) conditionsMet = true;
else switch (policy) {
case ExpDrops -> conditionsMet = hasArmorTelekinesis || hasMainHandTelekinesis || hasOffHandTelekinesis;
case MobDrops, ShearingDrops, VehicleDrops, FishingDrops ->
conditionsMet = hasMainHandTelekinesis || hasOffHandTelekinesis;
case MobDrops, ShearingDrops, VehicleDrops, FishingDrops -> conditionsMet = hasMainHandTelekinesis || hasOffHandTelekinesis;
case BlockDrops -> conditionsMet = hasMainHandTelekinesis;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ lateinit var configPath: Path
fun saveConfig() = configPath.writeText(Toml.encodeToString(TelekinesisConfig.serializer(), config))

fun loadConfig() {
configPath = FabricLoader.getInstance().configDir.toAbsolutePath().resolve("telekinesis.toml")
.also { if (!it.exists()) it.createFile() }
configPath =
FabricLoader.getInstance().configDir.toAbsolutePath().resolve("telekinesis.toml")
.also { if (!it.exists()) it.createFile() }

if (configPath.readText().isEmpty()) {
saveConfig()
Expand All @@ -72,4 +73,4 @@ fun loadConfig() {
} catch (e: Exception) {
saveConfig()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,113 +26,113 @@ fun YetAnotherConfigLib.Builder.appendOptionCategory() {
.name(Component.translatable("menu.telekinesis.config.general.onbydefault"))
.description(
OptionDescription
.of(Component.translatable("menu.telekinesis.config.general.onbydefault.description")),
.of(Component.translatable("menu.telekinesis.config.general.onbydefault.description"))
)
.binding(config.onByDefault, { config.onByDefault }, { config.onByDefault = it })
.controller {
TickBoxControllerBuilder.create(it)
}.build(),
}.build()
)
.option(
Option.createBuilder<Boolean>()
.name(Component.translatable("menu.telekinesis.config.general.onlywhilesneaking"))
.description(
OptionDescription
.of(
Component.translatable("menu.telekinesis.config.general.onlywhilesneaking.description"),
),
Component.translatable("menu.telekinesis.config.general.onlywhilesneaking.description")
)
)
.binding(config.onlyOnSneak, { config.onlyOnSneak }, { config.onlyOnSneak = it })
.controller {
TickBoxControllerBuilder.create(it)
}.build(),
}.build()
)
.option(
Option.createBuilder<Boolean>()
.name(Component.translatable("menu.telekinesis.config.general.enchantment"))
.description(
OptionDescription.of(
Component.translatable("menu.telekinesis.config.general.enchantment.description1"),
Component.translatable("menu.telekinesis.config.general.enchantment.description2"),
),
Component.translatable("menu.telekinesis.config.general.enchantment.description2")
)
)
.binding(config.enchantment, { config.enchantment }, { config.enchantment = it })
.controller {
TickBoxControllerBuilder.create(it)
}.build(),
}.build()
)
.option(
Option.createBuilder<Boolean>()
.name(Component.translatable("menu.telekinesis.config.general.blockdrops"))
.description(
OptionDescription
.of(Component.translatable("menu.telekinesis.config.general.blockdrops.description")),
.of(Component.translatable("menu.telekinesis.config.general.blockdrops.description"))
)
.binding(config.blockDrops, { config.blockDrops }, { config.blockDrops = it })
.controller {
TickBoxControllerBuilder.create(it)
}.build(),
}.build()
)
.option(
Option.createBuilder<Boolean>()
.name(Component.translatable("menu.telekinesis.config.general.expdrops"))
.description(
OptionDescription
.of(Component.translatable("menu.telekinesis.config.general.expdrops.description")),
.of(Component.translatable("menu.telekinesis.config.general.expdrops.description"))
)
.binding(config.expDrops, { config.expDrops }, { config.expDrops = it })
.controller {
TickBoxControllerBuilder.create(it)
}.build(),
}.build()
)
.option(
Option.createBuilder<Boolean>()
.name(Component.translatable("menu.telekinesis.config.general.mobdrops"))
.description(
OptionDescription
.of(Component.translatable("menu.telekinesis.config.general.mobdrops.description")),
.of(Component.translatable("menu.telekinesis.config.general.mobdrops.description"))
)
.binding(config.mobDrops, { config.mobDrops }, { config.mobDrops = it })
.controller {
TickBoxControllerBuilder.create(it)
}.build(),
}.build()
)
.option(
Option.createBuilder<Boolean>()
.name(Component.translatable("menu.telekinesis.config.general.vehicledrops"))
.description(
OptionDescription
.of(Component.translatable("menu.telekinesis.config.general.vehicledrops.description")),
.of(Component.translatable("menu.telekinesis.config.general.vehicledrops.description"))
)
.binding(config.vehicleDrops, { config.vehicleDrops }, { config.vehicleDrops = it })
.controller {
TickBoxControllerBuilder.create(it)
}.build(),
}.build()
)
.option(
Option.createBuilder<Boolean>()
.name(Component.translatable("menu.telekinesis.config.general.shearingdrops"))
.description(
OptionDescription
.of(Component.translatable("menu.telekinesis.config.general.shearingdrops.description")),
.of(Component.translatable("menu.telekinesis.config.general.shearingdrops.description"))
)
.binding(config.shearingDrops, { config.shearingDrops }, { config.shearingDrops = it })
.controller {
TickBoxControllerBuilder.create(it)
}.build(),
}.build()
)
.option(
Option.createBuilder<Boolean>()
.name(Component.translatable("menu.telekinesis.config.general.fishingdrops"))
.description(
OptionDescription
.of(Component.translatable("menu.telekinesis.config.general.fishingdrops.description")),
.of(Component.translatable("menu.telekinesis.config.general.fishingdrops.description"))
)
.binding(config.fishingDrops, { config.fishingDrops }, { config.fishingDrops = it })
.controller {
TickBoxControllerBuilder.create(it)
}.build(),
}.build()
)
.build(),
.build()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class TelekinesisEnchantment : Enchantment(
dynamicCost(25, 25),
dynamicCost(75, 25),
5,
*EquipmentSlot.entries.toTypedArray(),
),
*EquipmentSlot.entries.toTypedArray()
)
) {
override fun getFullname(i: Int): Component =
Component.translatable("enchantment.telekinesis.telekinesis.name").withStyle(Style.EMPTY.withColor(0xFFB64C))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ enum class TelekinesisPolicy {
ShearingDrops,
FishingDrops;

fun isEnabled(): Boolean = when (this) {
BlockDrops -> config.blockDrops
ExpDrops -> config.expDrops
MobDrops -> config.mobDrops
VehicleDrops -> config.vehicleDrops
ShearingDrops -> config.shearingDrops
FishingDrops -> config.fishingDrops
}
}
fun isEnabled(): Boolean =
when (this) {
BlockDrops -> config.blockDrops
ExpDrops -> config.expDrops
MobDrops -> config.mobDrops
VehicleDrops -> config.vehicleDrops
ShearingDrops -> config.shearingDrops
FishingDrops -> config.fishingDrops
}
}

0 comments on commit 4d09ecc

Please sign in to comment.