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 13f58a8 commit c29d112
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 119 deletions.
6 changes: 3 additions & 3 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ tasks.withType(JavaCompile).configureEach {
it.options.encoding = 'UTF-8'
}

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 @@ -108,7 +108,7 @@ curseforge {
project {
id = "${curseforge_id}"
changelogType = 'markdown'
changelog = "${changelog}"
changelog = body_changelog
releaseType = 'release'
addGameVersion "${loader}"
addGameVersion "${minecraft_version}"
Expand All @@ -133,7 +133,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-final
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 All @@ -14,73 +16,99 @@ public class TorchHitConfig {
/**
* {@link ForgeConfigSpec} {@link ForgeConfigSpec.Builder Builder}.
*/
private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
/**
* Common Configuration as read from the configuration file.
*/
public static final CommonConfig COMMON = new CommonConfig(BUILDER);
public static final CommonConfig COMMON = new CommonConfig(BUILDER);
/**
* {@link ForgeConfigSpec}.
*/
public static final ForgeConfigSpec SPEC = BUILDER.build();
public static final ForgeConfigSpec SPEC = BUILDER.build();

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

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

/**
* Returns the value of {@link CommonConfig#indirectHitToolList}.
*
* @return {@link CommonConfig#indirectHitToolList} as read from the {@link #COMMON common} configuration file.
*/
public static List<String> getIndirectHitToolList() {
return COMMON.indirectHitToolList.get();
}
return COMMON.indirectHitToolList.get();
}

/**
* Returns the value of {@link CommonConfig#moddedTorchList}.
*
* @return {@link CommonConfig#moddedTorchList} as read from the {@link #COMMON common} configuration file.
*/
public static List<String> getModdedTorchList() {
return COMMON.moddedTorchList.get();
}
return COMMON.moddedTorchList.get();
}

/**
* Returns the value of {@link CommonConfig#moddedSoulTorchList}.
*
* @return {@link CommonConfig#moddedSoulTorchList} as read from the {@link #COMMON common} configuration file.
*/
public static List<String> getModdedSoulTorchList() {
return COMMON.moddedSoulTorchList.get();
return COMMON.moddedSoulTorchList.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 @@ -96,17 +124,29 @@ public static class CommonConfig {
* Defaults to a list of the most common modded torches.
*/
private final ConfigValue<List<String>> moddedSoulTorchList;
/**
* Whether torches should break 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.
*
* @param builder
*/
public CommonConfig(ForgeConfigSpec.Builder builder) {
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);
indirectHitToolList = builder
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);
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.",
Expand Down Expand Up @@ -159,6 +199,14 @@ public CommonConfig(ForgeConfigSpec.Builder builder) {
"pgwbandedtorches:banded_soul_torch_red",
"pgwbandedtorches:banded_soul_torch_black"
));
}
}
consumeTorch = builder.comment("Whether torches should break upon use.").define("consume torch", false);
consumeWithoutFire = builder
.comment(
"Whether to break the torch upon use even if no fire was set.",
"Effective only if [fire chance] and [consume torch] are set different from default."
)
.define("consume without fire", false);
fireChance = builder.comment("Chance (in percentage) for torches to set targets on fire.").defineInRange("fire chance", 100, 1, 100);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,80 @@ 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) {
burn(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())) {
burn(entity, torch, TorchHitConfig.getIndirectHitDuration());
}
attack(player, target, item, TorchHitConfig.getIndirectHitDuration());
}
}
}
return ActionResult.PASS;
}

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

/**
* Consumes the used item if enabled.
*
* @param player
* @param item
* @param fireSeconds
*/
private void consumeItem(PlayerEntity player, ItemStack item, int fireSeconds) {
if (
!player.isCreative() &&
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 @@ -83,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 @@ -119,7 +156,7 @@ private boolean isAllowedTool(Item item) {
* @return {@link Hand} holding a torch or null.
*/
@Nullable
private Hand getTorchHand(PlayerEntity player) {
private Hand getHand(PlayerEntity player) {
if (isTorch(player.getMainHandStack())) {
return Hand.MAIN_HAND;
}
Expand All @@ -132,21 +169,21 @@ private Hand getTorchHand(PlayerEntity player) {
/**
* 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.getItem() == Items.TORCH || TorchHitConfig.getModdedTorchList().contains(getKey(itemStack.getItem())) || isSoulTorch(itemStack);
private boolean isTorch(ItemStack item) {
return item.getItem() == 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.getItem() == Items.SOUL_TORCH || TorchHitConfig.getModdedSoulTorchList().contains(getKey(itemStack.getItem()));
private boolean isSoulTorch(ItemStack item) {
return item.getItem() == Items.SOUL_TORCH || TorchHitConfig.getModdedSoulTorchList().contains(getKey(item.getItem()));
}

/**
Expand Down
Loading

0 comments on commit c29d112

Please sign in to comment.