Skip to content

Commit 084a1ea

Browse files
committed
Add passive charging; fix charge shells data
1 parent 703d44f commit 084a1ea

9 files changed

Lines changed: 98 additions & 20 deletions

File tree

src/main/java/falseresync/wizcraft/client/hud/ChargeDisplayHudItem.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public class ChargeDisplayHudItem implements HudItem {
3535
private final TextRenderer textRenderer;
3636
private int currentCharge = 0;
3737
private int maxCharge = 0;
38-
private int chargeInShells = 0;
39-
private int maxChargeInShells = 0;
38+
private int chargeInShells = -1;
39+
private int maxChargeInShells = -1;
4040
private boolean isVisible = false;
4141
private ItemStack wand;
4242
private boolean animating = false;
@@ -57,7 +57,7 @@ public void render(BetterDrawContext context, RenderTickCounter tickCounter) {
5757
RenderSystem.enableBlend();
5858
context.setShaderColor(1, 1, 1, opacity);
5959

60-
if (maxChargeInShells != 0) {
60+
if (maxChargeInShells >= 0) {
6161
var tint = ColorHelper.Argb.lerp((float) chargeInShells / maxChargeInShells, SHELL_NO_CHARGE_TINT, SHELL_FULL_CHARGE_TINT);
6262
context.setShaderColor(
6363
ColorHelper.Argb.getRed(tint) / 255f, ColorHelper.Argb.getGreen(tint) / 255f,
@@ -111,8 +111,8 @@ public void tick() {
111111
currentCharge = wand.getOrDefault(WizcraftComponents.WAND_CHARGE, 0);
112112
maxCharge = wand.getOrDefault(WizcraftComponents.WAND_MAX_CHARGE, 0);
113113

114-
var shells = client.player.getAttachedOrCreate(WizcraftAttachments.CHARGE_SHELLS);
115-
if (shells.maxCharge() > 0) {
114+
var shells = client.player.getAttached(WizcraftAttachments.CHARGE_SHELLS);
115+
if (shells != null) {
116116
chargeInShells = shells.currentCharge();
117117
maxChargeInShells = shells.maxCharge();
118118
}

src/main/java/falseresync/wizcraft/common/ChargeManager.java

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package falseresync.wizcraft.common;
22

33
import com.google.common.base.*;
4+
import falseresync.wizcraft.common.config.*;
45
import falseresync.wizcraft.common.data.attachment.*;
56
import falseresync.wizcraft.common.data.component.*;
67
import net.fabricmc.fabric.api.event.*;
78
import net.minecraft.entity.player.*;
89
import net.minecraft.item.*;
10+
import net.minecraft.world.*;
911
import org.jetbrains.annotations.*;
1012

1113
public class ChargeManager {
@@ -24,7 +26,12 @@ public class ChargeManager {
2426
public ChargeManager() {
2527
WAND_CHARGE_SPENT.register((wandStack, cost, user) -> {
2628
if (user != null) {
27-
var chargeShells = user.getAttachedOrCreate(WizcraftAttachments.CHARGE_SHELLS);
29+
// Maybe only compensate the cost? But that would be confusing
30+
var chargeShells = user.getAttached(WizcraftAttachments.CHARGE_SHELLS);
31+
if (chargeShells == null) {
32+
return;
33+
}
34+
2835
var wandCurrent = wandStack.getOrDefault(WizcraftComponents.WAND_CHARGE, 0);
2936
var wandMax = wandStack.getOrDefault(WizcraftComponents.WAND_MAX_CHARGE, 0);
3037
var compensation = wandMax - wandCurrent;
@@ -44,11 +51,17 @@ public ChargeManager() {
4451
}
4552

4653
public boolean areShellsFull(PlayerEntity player) {
47-
return player.getAttachedOrCreate(WizcraftAttachments.CHARGE_SHELLS).areShellsFull();
54+
//noinspection DataFlowIssue
55+
return player.hasAttached(WizcraftAttachments.CHARGE_SHELLS)
56+
&& player.getAttached(WizcraftAttachments.CHARGE_SHELLS).areShellsFull();
4857
}
4958

5059
public void applyShellCharge(PlayerEntity player, int amount) {
51-
var newShells = player.getAttachedOrCreate(WizcraftAttachments.CHARGE_SHELLS).withChargeChange(amount);
60+
var shells = player.getAttached(WizcraftAttachments.CHARGE_SHELLS);
61+
if (shells == null) {
62+
return;
63+
}
64+
var newShells = shells.withChargeChange(amount);
5265
if (newShells != null) {
5366
player.setAttached(WizcraftAttachments.CHARGE_SHELLS, newShells);
5467
}
@@ -58,6 +71,10 @@ public boolean isWandFullyCharged(ItemStack wandStack) {
5871
return wandStack.getOrDefault(WizcraftComponents.WAND_CHARGE, 0) >= wandStack.getOrDefault(WizcraftComponents.WAND_MAX_CHARGE, 0);
5972
}
6073

74+
public boolean cannotAddAnyCharge(ItemStack wandStack, PlayerEntity player) {
75+
return isWandFullyCharged(wandStack) && areShellsFull(player);
76+
}
77+
6178
public boolean tryExpendWandCharge(ItemStack wandStack, int cost, @Nullable PlayerEntity user) {
6279
if (user != null && (user.isCreative() && Wizcraft.getConfig().infiniteCharge.isCreativeOnly() || Wizcraft.getConfig().infiniteCharge.isAlways())) {
6380
return true;
@@ -81,6 +98,36 @@ public void chargeWand(ItemStack wandStack, int amount, @Nullable PlayerEntity u
8198
}
8299
}
83100

101+
public void tryChargeWandPassively(ItemStack wandStack, World world, PlayerEntity player) {
102+
if (Wizcraft.getChargeManager().cannotAddAnyCharge(wandStack, player)) {
103+
return;
104+
}
105+
106+
var config = Wizcraft.getConfig().passiveCharge;
107+
if (config == WizcraftConfig.PassiveCharge.DISABLED) {
108+
return;
109+
}
110+
111+
var environmentCoefficient = 1f;
112+
var worldType = world.getRegistryKey();
113+
if (worldType == World.NETHER) {
114+
environmentCoefficient *= 0.1f;
115+
} else if (worldType == World.END) {
116+
environmentCoefficient *= 3f;
117+
} else {
118+
environmentCoefficient *= world.isNight() ? 1 : 0.5f;
119+
environmentCoefficient *= 1 - world.getRainGradient(1);
120+
environmentCoefficient *= world.getLightLevel(LightType.SKY, player.getBlockPos()) / (world.getMaxLightLevel() * 0.5f);
121+
}
122+
123+
var usageCoefficient = ItemStack.areEqual(player.getMainHandStack(), wandStack) ? 1f : 0.25f;
124+
125+
// At most 10% of the time, i.e. up to 2 times per second
126+
if (world.random.nextFloat() < Math.clamp(0.005f * environmentCoefficient * config.coefficient * usageCoefficient, 0, 0.1f)) {
127+
Wizcraft.getChargeManager().chargeWand(wandStack, 1, player);
128+
}
129+
}
130+
84131
@FunctionalInterface
85132
public interface WandExpend {
86133
void onWandChargeSpent(ItemStack wandStack, int cost, @Nullable PlayerEntity user);

src/main/java/falseresync/wizcraft/common/WizcraftUtil.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,25 @@
1414
import org.jetbrains.annotations.*;
1515

1616
import java.util.*;
17+
import java.util.function.*;
1718

1819
public class WizcraftUtil {
20+
private static final Function<World, Integer> memo$findViewDistance = Util.memoize((World world) -> world.isClient()
21+
? MinecraftClient.getInstance().options.getClampedViewDistance()
22+
: ((ServerWorld) world).getChunkManager().chunkLoadingManager.watchDistance);
23+
1924
public static <T> Optional<T> nextRandomEntry(ServerWorld world, TagKey<T> tag, Random random) {
2025
return world.getRegistryManager()
2126
.getOptional(tag.registry())
2227
.map(registry -> registry.getOrCreateEntryList(tag))
2328
.flatMap(entries -> entries.getRandom(random).map(RegistryEntry::value));
2429
}
2530

31+
/**
32+
* @return memoized(!) view distance
33+
*/
2634
public static int findViewDistance(World world) {
27-
return world.isClient()
28-
? MinecraftClient.getInstance().options.getClampedViewDistance()
29-
: ((ServerWorld) world).getChunkManager().chunkLoadingManager.watchDistance;
35+
return memo$findViewDistance.apply(world);
3036
}
3137

3238
public static long exchangeStackInSlotWithHand(PlayerEntity player, Hand hand, InventoryStorage storage, int slot, int maxAmount, @Nullable TransactionContext transaction) {

src/main/java/falseresync/wizcraft/common/config/WizcraftConfig.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public final class WizcraftConfig implements ConfigData {
1111
@TranslatableEnum
1212
public InfiniteCharge infiniteCharge = InfiniteCharge.CREATIVE_ONLY;
1313

14+
@ConfigEntry.Category("general")
15+
@TranslatableEnum
16+
public PassiveCharge passiveCharge = PassiveCharge.DEFAULT;
17+
1418
@ConfigEntry.Category("general")
1519
@ConfigEntry.Gui.Tooltip(count = 3)
1620
@ConfigEntry.Gui.CollapsibleObject
@@ -28,15 +32,15 @@ public static class Transmutation {
2832

2933
@ConfigEntry.Category("performance")
3034
@TranslatableEnum
31-
public ParticlesAmountModifier animationParticlesAmount = ParticlesAmountModifier.DEFAULT;
35+
public ParticlesAmount animationParticlesAmount = ParticlesAmount.DEFAULT;
3236

3337
@ConfigEntry.Category("performance")
3438
@TranslatableEnum
3539
public AnimationQuality animationQuality = AnimationQuality.DEFAULT;
3640

3741
@ConfigEntry.Category("accessibility")
3842
@TranslatableEnum
39-
public TransparencyModifier fullscreenEffectsTransparency = TransparencyModifier.DEFAULT;
43+
public Transparency fullscreenEffectsTransparency = Transparency.DEFAULT;
4044

4145
public enum InfiniteCharge {
4246
NEVER, CREATIVE_ONLY, ALWAYS;
@@ -50,12 +54,12 @@ public boolean isAlways() {
5054
}
5155
}
5256

53-
public enum ParticlesAmountModifier {
57+
public enum ParticlesAmount {
5458
REDUCED(0.6f), DEFAULT(1);
5559

5660
public final float modifier;
5761

58-
ParticlesAmountModifier(float modifier) {
62+
ParticlesAmount(float modifier) {
5963
this.modifier = modifier;
6064
}
6165
}
@@ -64,13 +68,23 @@ public enum AnimationQuality {
6468
FAST, DEFAULT
6569
}
6670

67-
public enum TransparencyModifier {
71+
public enum Transparency {
6872
INCREASED(2f), DEFAULT(1);
6973

7074
public final float modifier;
7175

72-
TransparencyModifier(float modifier) {
76+
Transparency(float modifier) {
7377
this.modifier = modifier;
7478
}
7579
}
80+
81+
public enum PassiveCharge {
82+
DISABLED(0), DEFAULT(1), FASTER(2);
83+
84+
public final float coefficient;
85+
86+
PassiveCharge(float modifier) {
87+
this.coefficient = modifier;
88+
}
89+
}
7690
}

src/main/java/falseresync/wizcraft/common/data/attachment/ChargeShellsAttachment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public record ChargeShellsAttachment(int currentCharge, IntList shells, int maxC
1414
public static final int MAX_CHARGE = 1000;
1515

1616
public static final Codec<ChargeShellsAttachment> CODEC = RecordCodecBuilder.create(instance -> instance.group(
17-
Codecs.NONNEGATIVE_INT.fieldOf("maxCharge").forGetter(ChargeShellsAttachment::currentCharge),
17+
Codecs.NONNEGATIVE_INT.fieldOf("currentCharge").forGetter(ChargeShellsAttachment::currentCharge),
1818
Codecs.NONNEGATIVE_INT.sizeLimitedListOf(MAX_SHELLS)
1919
.xmap(it -> (IntList) new IntImmutableList(it), it -> it)
2020
.fieldOf("shells").forGetter(ChargeShellsAttachment::shells),

src/main/java/falseresync/wizcraft/common/item/ChargeShellItem.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public ChargeShellItem(Settings settings) {
2020
@Override
2121
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
2222
var stack = user.getStackInHand(hand);
23-
if (user.getAttachedOrCreate(WizcraftAttachments.CHARGE_SHELLS).canAddShell(DEFAULT_CAPACITY)) {
23+
var chargeShells = user.getAttached(WizcraftAttachments.CHARGE_SHELLS);
24+
if (chargeShells == null || chargeShells.canAddShell(DEFAULT_CAPACITY)) {
2425
user.setCurrentHand(hand);
2526
return TypedActionResult.consume(stack);
2627
}

src/main/java/falseresync/wizcraft/common/item/WandItem.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package falseresync.wizcraft.common.item;
22

33
import falseresync.wizcraft.client.*;
4+
import falseresync.wizcraft.common.*;
45
import falseresync.wizcraft.common.data.component.*;
56
import falseresync.wizcraft.common.item.focus.*;
67
import net.fabricmc.fabric.api.client.keybinding.v1.*;
@@ -11,6 +12,7 @@
1112
import net.minecraft.item.*;
1213
import net.minecraft.item.tooltip.*;
1314
import net.minecraft.screen.slot.*;
15+
import net.minecraft.server.network.*;
1416
import net.minecraft.text.*;
1517
import net.minecraft.util.*;
1618
import net.minecraft.util.math.*;
@@ -131,8 +133,12 @@ public void inventoryTick(ItemStack stack, World world, Entity entity, int slot,
131133
if (!focusStack.isEmpty() && focusStack.getItem() instanceof FocusItem focusItem) {
132134
focusItem.focusInventoryTick(stack, focusStack, world, entity, slot, selected);
133135
}
136+
if (entity instanceof ServerPlayerEntity player) {
137+
Wizcraft.getChargeManager().tryChargeWandPassively(stack, world, player);
138+
}
134139
}
135140

141+
136142
// Focus properties processing
137143

138144
@Override

src/main/java/falseresync/wizcraft/common/item/focus/ChargingFocusItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public TypedActionResult<ItemStack> focusUse(ItemStack wandStack, ItemStack focu
3434
return TypedActionResult.fail(wandStack);
3535
}
3636

37-
if (Wizcraft.getChargeManager().isWandFullyCharged(wandStack) && Wizcraft.getChargeManager().areShellsFull(player)) {
37+
if (Wizcraft.getChargeManager().cannotAddAnyCharge(wandStack, player)) {
3838
WizcraftReports.WAND_ALREADY_FULLY_CHARGED.sendTo(player);
3939
return TypedActionResult.pass(wandStack);
4040
}

src/main/resources/assets/wizcraft/lang/en_us.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
"text.autoconfig.wizcraft.option.infiniteCharge.NEVER": "Never",
5959
"text.autoconfig.wizcraft.option.infiniteCharge.CREATIVE_ONLY": "Creative only",
6060
"text.autoconfig.wizcraft.option.infiniteCharge.ALWAYS": "Always",
61+
"text.autoconfig.wizcraft.option.passiveCharge": "Tools charge passively",
62+
"text.autoconfig.wizcraft.option.passiveCharge.DISABLED": "Disabled",
63+
"text.autoconfig.wizcraft.option.passiveCharge.DEFAULT": "Enabled",
64+
"text.autoconfig.wizcraft.option.passiveCharge.FASTER": "Enabled (faster)",
6165
"text.autoconfig.wizcraft.option.transmutation": "Transmutation",
6266
"text.autoconfig.wizcraft.option.transmutation.@Tooltip[0]": "Configure the weight (i.e. relative chance) of each possible outcome of a transmutation.",
6367
"text.autoconfig.wizcraft.option.transmutation.@Tooltip[1]": "IMPORTANT: To configure which entities a transmutation affects and which it produces,",

0 commit comments

Comments
 (0)