Skip to content

Commit

Permalink
Implemented configuration suggestions: fire chance and consume torch.…
Browse files Browse the repository at this point in the history
… Improved sever sideness.
  • Loading branch information
Crystal-Spider committed Sep 14, 2022
1 parent ffc9e2f commit 4ae47e9
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 144 deletions.
6 changes: 3 additions & 3 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ tasks.withType(JavaCompile).configureEach {
it.options.release = 17
}

def changelog = "See [Changelog](https://github.com/${github_user}/${modid_kebab}/blob/master/CHANGELOG.md#${minecraft_version.replaceAll('\\.', '')}-${mod_version.replaceAll('\\.', '')}---${new Date().format("yyyyMMdd")})."
def body_changelog = "See [Changelog](https://github.com/${github_user}/${modid_kebab}/blob/master/CHANGELOG.md#${minecraft_version.replaceAll('\\.', '')}-${mod_version.replaceAll('\\.', '')}---${new Date().format("yyyyMMdd")})."

file("../api-keys.properties").withReader {
Properties props = new Properties()
Expand All @@ -109,7 +109,7 @@ curseforge {
project {
id = "${curseforge_id}"
changelogType = 'markdown'
changelog = "${changelog}"
changelog = body_changelog
releaseType = 'release'
addGameVersion "${loader}"
addGameVersion "${minecraft_version}"
Expand All @@ -134,7 +134,7 @@ githubRelease {
targetCommitish "${minecraft_version}"
releaseName "v${minecraft_version}-${mod_version}"
generateReleaseNotes false
body changelog
body body_changelog
draft true
prerelease false
releaseAssets remapJar
Expand Down
2 changes: 1 addition & 1 deletion fabric/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ org.gradle.daemon = false

# Mod Properties
mod_title = Torch hit!
mod_version = 4.0.0.0
mod_version = 5.0.0.0
author = Crystal Spider
group = crystalspider
modid = torchhit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import net.minecraft.enchantment.Enchantments;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.ForgeConfigSpec.ConfigValue;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import net.minecraftforge.common.ForgeConfigSpec.ConfigValue;
import net.minecraftforge.common.ForgeConfigSpec.IntValue;

/**
* Torch hit! Configuration.
Expand Down Expand Up @@ -79,26 +81,53 @@ public static Boolean getAllowCandles() {
}

/**
* Returns the value of {@link CommonConfig#breakCandles}.
* Returns the value of {@link CommonConfig#consumeCandle}.
*
* @return {@link CommonConfig#breakCandles} as read from the {@link #COMMON common} configuration file.
* @return {@link CommonConfig#consumeCandle} as read from the {@link #COMMON common} configuration file.
*/
public static Boolean getBreakCandles() {
return COMMON.breakCandles.get();
public static Boolean getConsumeCandle() {
return COMMON.consumeCandle.get();
}

/**
* Returns the value of {@link CommonConfig#consumeTorch}.
*
* @return {@link CommonConfig#consumeTorch} as read from the {@link #COMMON common} configuration file.
*/
public static Boolean getConsumeTorch() {
return COMMON.consumeTorch.get();
}

/**
* Returns the value of {@link CommonConfig#consumeWithoutFire}.
*
* @return {@link CommonConfig#consumeWithoutFire} as read from the {@link #COMMON common} configuration file.
*/
public static Boolean getConsumeWithoutFire() {
return COMMON.consumeWithoutFire.get();
}

/**
* Returns the value of {@link CommonConfig#fireChance}.
*
* @return {@link CommonConfig#fireChance} as read from the {@link #COMMON common} configuration file.
*/
public static Integer getFireChance() {
return COMMON.fireChance.get();
}

/**
* Common Configuration for Torch hit!.
*/
public static class CommonConfig {
/**
* Fire Aspect Duration for Direct Hits.
*/
private final ConfigValue<Integer> directHitDuration;
private final IntValue directHitDuration;
/**
* Fire Aspect Duration for Indirect Hits.
*/
private final ConfigValue<Integer> indirectHitDuration;
private final IntValue indirectHitDuration;
/**
* List of tools that can be used to deal Indirect Hits.
* Empty if Indirect Hits are disabled.
Expand All @@ -121,7 +150,19 @@ public static class CommonConfig {
/**
* Whether candles should break upon use.
*/
private final ConfigValue<Boolean> breakCandles;
private final ConfigValue<Boolean> consumeCandle;
/**
* Whether to break the torch upon use.
*/
private final BooleanValue consumeTorch;
/**
* Whether to break the torch upon use even if no fire was set.
*/
private final BooleanValue consumeWithoutFire;
/**
* Chance (in percentage) for torches to set on fire targets.
*/
private final IntValue fireChance;

/**
* Defines the configuration options, their default values and their comments.
Expand All @@ -130,16 +171,16 @@ public static class CommonConfig {
*/
public CommonConfig(ForgeConfigSpec.Builder builder) {
int maxDuration = Enchantments.FIRE_ASPECT.getMaxLevel() * 4;
directHitDuration = builder.comment("Fire damage duration for direct (main hand) hits.").defineInRange("directHitDuration", 4, 1, maxDuration);
indirectHitDuration = builder.comment("Fire damage duration for indirect (off hand + tool) hits.").defineInRange("indirectHitDuration", 2, 1, maxDuration);
directHitDuration = builder.comment("Fire damage duration for direct (main hand) hits.").defineInRange("direct hit duration", 4, 1, maxDuration);
indirectHitDuration = builder.comment("Fire damage duration for indirect (off hand + tool) hits.").defineInRange("indirect hit duration", 2, 1, maxDuration);
indirectHitToolList = builder
.comment(
"List of tools that allow for an indirect hit when a torch is being held in the off hand.",
"Leave empty to disable indirect hits.",
"Insert either item categories or specific item IDs."
)
.define("indirectHitToolList", new ArrayList<String>(List.of("sword", "axe", "pickaxe", "shovel", "hoe")));
moddedTorchList = builder.comment("List of item ids that should be considered as a Torch.").define("moddedTorchList", new ArrayList<String>(List.of(
.define("indirect tools", new ArrayList<String>(List.of("sword", "axe", "pickaxe", "shovel", "hoe")));
moddedTorchList = builder.comment("List of item ids that should be considered as a Torch.").define("torch list", new ArrayList<String>(List.of(
"bonetorch:bonetorch",
"torchmaster:megatorch",
"hardcore_torches:lit_torch",
Expand Down Expand Up @@ -167,7 +208,7 @@ public CommonConfig(ForgeConfigSpec.Builder builder) {
"pgwbandedtorches:banded_torch_red",
"pgwbandedtorches:banded_torch_black"
)));
moddedSoulTorchList = builder.comment("List of item ids that should be considered as a Soul Torch.").define("moddedSoulTorchList", new ArrayList<String>(List.of(
moddedSoulTorchList = builder.comment("List of item ids that should be considered as a Soul Torch.").define("soul torch list", new ArrayList<String>(List.of(
"pgwbandedtorches:banded_soul_torch_white",
"pgwbandedtorches:banded_soul_torch_orange",
"pgwbandedtorches:banded_soul_torch_magenta",
Expand All @@ -185,8 +226,16 @@ public CommonConfig(ForgeConfigSpec.Builder builder) {
"pgwbandedtorches:banded_soul_torch_red",
"pgwbandedtorches:banded_soul_torch_black"
)));
allowCandles = builder.comment("Whether to allow candles to act as torches.").define("allowCandles", true);
breakCandles = builder.comment("Whether candles should break upon use.", "Effective only if [allowCandles] is enabled.").define("breakCandles", true);
allowCandles = builder.comment("Whether to allow candles to act as torches.").define("allow candles", true);
consumeCandle = builder.comment("Whether candles should break upon use.", "Effective only if [allowCandles] is enabled.").define("consume candle", true);
consumeTorch = builder.comment("Whether torches should break upon use.").define("consume torch", false);
consumeWithoutFire = builder
.comment(
"Whether to break the torch/candle upon use even if no fire was set.",
"Effective only if [fire chance] and at least one of [consume torch] and [consume candle] are set different from default."
)
.define("consume without fire", false);
fireChance = builder.comment("Chance (in percentage) for torches/candles to set targets on fire.").defineInRange("fire chance", 100, 1, 100);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,71 +34,79 @@ public class AttackEntityHandler {
* @param player
* @param world
* @param hand
* @param entity
* @param target
* @param hitResult
* @return {@link ActionResult}.
*/
public ActionResult handle(PlayerEntity player, World world, Hand hand, Entity entity, EntityHitResult hitResult) {
if (!player.isSpectator()) {
Hand torchHand = getTorchHand(player);
if (torchHand != null && !entity.isFireImmune()) {
ItemStack torch = player.getStackInHand(torchHand);
if (torchHand == Hand.MAIN_HAND) {
attack(player, entity, torch, TorchHitConfig.getDirectHitDuration());
public ActionResult handle(PlayerEntity player, World world, Hand hand, Entity target, EntityHitResult hitResult) {
if (!player.world.isClient && !player.isSpectator()) {
Hand interactionHand = getHand(player);
if (interactionHand != null && !target.isFireImmune()) {
ItemStack item = player.getStackInHand(interactionHand);
if (hand == Hand.MAIN_HAND) {
attack(player, target, item, TorchHitConfig.getDirectHitDuration());
} else if (isAllowedTool(player.getMainHandStack().getItem())) {
attack(player, entity, torch, TorchHitConfig.getIndirectHitDuration());
}
attack(player, target, item, TorchHitConfig.getIndirectHitDuration());
}
}
}
return ActionResult.PASS;
}


/**
* Attack the entity with the torch setting it on fire.
* Attack the target entity with the torch item it on fire.
*
* @param player
* @param entity
* @param torch
* @param target
* @param item
* @param defaultDuration
*/
private void attack(PlayerEntity player, Entity entity, ItemStack torch, int defaultDuration) {
burn(entity, torch, defaultDuration);
breakCandle(player, torch);
private void attack(PlayerEntity player, Entity target, ItemStack item, int defaultDuration) {
consumeItem(player, item, burn(target, item, defaultDuration));
}

/**
* Breaks the used candle if enabled.
* Consumes the used item if enabled.
*
* @param torch
* @param player
* @param item
* @param fireSeconds
*/
private void breakCandle(PlayerEntity player, ItemStack torch) {
if (!player.isCreative() && isCandle(torch) && TorchHitConfig.getBreakCandles()) {
torch.decrement(1);
private void consumeItem(PlayerEntity player, ItemStack item, int fireSeconds) {
if (
!player.isCreative() &&
((isCandle(item) && TorchHitConfig.getConsumeCandle()) || (isTorch(item) && TorchHitConfig.getConsumeTorch())) &&
(TorchHitConfig.getConsumeWithoutFire() || fireSeconds > 0)
) {
item.decrement(1);
}
}

/**
* Sets the entity on fire.
*
* @param entity
* @param target
* @param torch
* @param defaultDuration
*/
private void burn(Entity entity, ItemStack torch, int defaultDuration) {
entity.setOnFireFor(getFireSeconds(torch, entity, defaultDuration));
setFireId(entity, torch);
private int burn(Entity target, ItemStack item, int defaultDuration) {
int fireSeconds = getFireSeconds(item, target, defaultDuration);
if (fireSeconds > 0) {
target.setOnFireFor(fireSeconds);
setFireId(target, item);
}
return fireSeconds;
}

/**
* If Soul Fire'd is installed, sets the correct Fire Id.
*
* @param entity
* @param torch
* @param item
*/
private void setFireId(Entity entity, ItemStack torch) {
private void setFireId(Entity entity, ItemStack item) {
if (isSoulfiredInstalled) {
if (isSoulTorch(torch)) {
if (isSoulTorch(item)) {
SoulFired.setOnSoulFire(entity);
} else {
SoulFired.setOnFire(entity);
Expand All @@ -109,22 +117,25 @@ private void setFireId(Entity entity, ItemStack torch) {
/**
* Returns the amount of seconds the given entity should stay on fire.
*
* @param torch
* @param entity
* @param item
* @param target
* @param fireDuration
* @return the amount of seconds the given entity should stay on fire.
*/
private int getFireSeconds(ItemStack torch, Entity entity, int fireDuration) {
if (isSoulTorch(torch)) {
if (isSoulfiredInstalled) {
return fireDuration;
}
if (entity instanceof AbstractPiglinEntity) {
return fireDuration * 2;
private int getFireSeconds(ItemStack item, Entity target, int fireDuration) {
if ((Math.random() * 100) < TorchHitConfig.getFireChance()) {
if (isSoulTorch(item)) {
if (isSoulfiredInstalled) {
return fireDuration;
}
if (target instanceof AbstractPiglinEntity) {
return fireDuration * 2;
}
return fireDuration + 1;
}
return fireDuration + 1;
return fireDuration;
}
return fireDuration;
return 0;
}

/**
Expand All @@ -145,44 +156,54 @@ private boolean isAllowedTool(Item item) {
* @return {@link Hand} holding a torch or null.
*/
@Nullable
private Hand getTorchHand(PlayerEntity player) {
if (isTorch(player.getMainHandStack())) {
private Hand getHand(PlayerEntity player) {
if (isValidItem(player.getMainHandStack())) {
return Hand.MAIN_HAND;
}
if (isTorch(player.getOffHandStack())) {
if (isValidItem(player.getOffHandStack())) {
return Hand.OFF_HAND;
}
return null;
}

/**
* Checks whether the given {@link ItemStack} is a valid item (torch or candle).
*
* @param item
* @return whether the given {@link ItemStack} is a valid item.
*/
private boolean isValidItem(ItemStack item) {
return isTorch(item) || isCandle(item);
}

/**
* Checks whether the given {@link ItemStack} is a torch.
*
* @param itemStack
* @param item
* @return whether the given {@link ItemStack} is a torch.
*/
private boolean isTorch(ItemStack itemStack) {
return itemStack.isOf(Items.TORCH) || isCandle(itemStack) || TorchHitConfig.getModdedTorchList().contains(getKey(itemStack.getItem())) || isSoulTorch(itemStack);
private boolean isTorch(ItemStack item) {
return item.isOf(Items.TORCH) || TorchHitConfig.getModdedTorchList().contains(getKey(item.getItem())) || isSoulTorch(item);
}

/**
* Checks whether the given {@link ItemStack} is a soul torch.
*
* @param itemStack
* @param item
* @return whether the given {@link ItemStack} is a soul torch.
*/
private boolean isSoulTorch(ItemStack itemStack) {
return itemStack.isOf(Items.SOUL_TORCH) || TorchHitConfig.getModdedSoulTorchList().contains(getKey(itemStack.getItem()));
private boolean isSoulTorch(ItemStack item) {
return item.isOf(Items.SOUL_TORCH) || TorchHitConfig.getModdedSoulTorchList().contains(getKey(item.getItem()));
}

/**
* Checks whether the given {@link ItemStack} is a candle.
*
* @param itemStack
* @param item
* @return whether the given {@link ItemStack} is a candle.
*/
private boolean isCandle(ItemStack itemStack) {
return TorchHitConfig.getAllowCandles() && itemStack.isIn(ItemTags.CANDLES);
private boolean isCandle(ItemStack item) {
return TorchHitConfig.getAllowCandles() && item.isIn(ItemTags.CANDLES);
}

/**
Expand Down
Loading

0 comments on commit 4ae47e9

Please sign in to comment.