Skip to content

Commit 4101e77

Browse files
committed
fix discontinuous cast
1 parent 85e5670 commit 4101e77

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

src/main/java/com/robertx22/mine_and_slash/database/data/spells/components/Spell.java

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import net.minecraft.network.chat.Component;
5252
import net.minecraft.network.chat.MutableComponent;
5353
import net.minecraft.resources.ResourceLocation;
54+
import net.minecraft.server.level.ServerPlayer;
5455
import net.minecraft.world.entity.LivingEntity;
5556
import net.minecraft.world.entity.player.Player;
5657
import net.minecraft.world.level.Level;
@@ -157,28 +158,80 @@ public WeaponTypes getWeapon(LivingEntity en) {
157158
}
158159

159160
public final void onCastingTick(SpellCastContext ctx) {
160-
int timesToCast = (int) ctx.spell.getConfig().times_to_cast;
161-
if (timesToCast > 1) {
162-
int castTimeTicks = (int) getCastTimeTicks(ctx);
163-
// if i didnt do this then cast time reduction would reduce amount of spell hits.
164-
int castEveryXTicks = castTimeTicks / timesToCast;
165-
if (timesToCast > 1) {
161+
162+
int timesToCast = ctx.spell.getConfig().times_to_cast;
163+
if (timesToCast > 1){
164+
165+
166+
if (ctx.caster instanceof ServerPlayer player){
167+
168+
if (ctx.ticksInUse > 0){
169+
//do more precise time calculation here, so we don't miss each single cast.
170+
SpellCastingData spellCastingData = Load.player(player).spellCastingData;
171+
//already done all casts
172+
if (spellCastingData.castTimesDone >= timesToCast) {
173+
return;
174+
}
175+
176+
//don't invoke getCastTimeTicks(ctx) to get the imprecise tick!
177+
//for internal calc, use the precise one
178+
float allCastsTime = ctx.event.data.getNumber(EventData.CAST_TICKS).number;
179+
float singleCastTime = allCastsTime / timesToCast;
180+
float usedCastedTime = spellCastingData.castTimesDone * singleCastTime;
181+
182+
183+
184+
//when it's last tick but there are still casts not finished
185+
if (ctx.isLastCastTick && spellCastingData.castTimesDone < timesToCast) {
186+
int leftTimes = timesToCast - spellCastingData.castTimesDone;
187+
for (int i = 0; i < leftTimes; i++) {
188+
this.cast(ctx);
189+
spellCastingData.castTimesDone++;
190+
}
191+
return;
192+
}
193+
194+
195+
int hitTime = 0;
196+
while (ctx.ticksInUse - usedCastedTime >= singleCastTime) {
197+
hitTime++;
198+
usedCastedTime += singleCastTime;
199+
}
200+
for (int time = hitTime; time > 0; time--) {
201+
this.cast(ctx);
202+
spellCastingData.castTimesDone++;
203+
}
204+
205+
206+
}
207+
208+
} else {
209+
int castTimeTicks = (int) getCastTimeTicks(ctx);
210+
211+
212+
// if i didnt do this then cast time reduction would reduce amount of spell hits.
213+
int castEveryXTicks = castTimeTicks / timesToCast;
214+
166215
if (castEveryXTicks < 1) {
167216
castEveryXTicks = 1;
168217
}
169-
}
170-
if (ctx.ticksInUse > 0 && ctx.ticksInUse % castEveryXTicks == 0) {
171-
this.cast(ctx);
218+
219+
if (ctx.ticksInUse > 0 && ctx.ticksInUse % castEveryXTicks == 0) {
220+
this.cast(ctx);
221+
}
222+
172223
}
173224
} else if (timesToCast < 1) {
174225
ExileLog.get().warn("Times to cast spell is: " + timesToCast + " . this seems like a bug.");
175226
}
227+
228+
ctx.castedThisTick = true;
176229
}
177230

178231
public void cast(SpellCastContext ctx) {
179232
LivingEntity caster = ctx.caster;
180-
ctx.castedThisTick = true;
181-
/*
233+
234+
/*
182235
if (MMORPG.RUN_DEV_TOOLS_REMOVE_WHEN_DONE && this.config.swing_arm) {
183236
// caster.swingTime = -1; // this makes sure hand swings
184237
// caster.swing(InteractionHand.MAIN_HAND);
@@ -195,9 +248,10 @@ public final int getChargeCooldownTicks(SpellCastContext ctx) {
195248
return (int) ctx.event.data.getNumber(EventData.CHARGE_COOLDOWN_TICKS).number;
196249
}
197250

198-
public final int getCastTimeTicks(SpellCastContext ctx) {
251+
public int getCastTimeTicks(SpellCastContext ctx) {
199252
// if it casts 5 times a cast, it should take at least 5 ticks to cast it
200-
return MathHelper.clamp((int) ctx.event.data.getNumber(EventData.CAST_TICKS).number, config.times_to_cast, 10000);
253+
//Clefal: we can actually allow multiple cast on 1 tick. But we at least need to let the cast time a little longer than the real cast time.
254+
return ((int) Math.ceil(ctx.event.data.getNumber(EventData.CAST_TICKS).number));
201255
}
202256

203257
@Override
@@ -277,7 +331,7 @@ public final List<Component> GetTooltipString(StatRangeInfo info) {
277331
if (casttime == 0) {
278332
list.add(Words.INSTANT_CAST.locName().withStyle(ChatFormatting.GREEN));
279333
} else {
280-
list.add(Words.CAST_TIME.locName(casttime / 20).withStyle(ChatFormatting.GREEN));
334+
list.add(Words.CAST_TIME.locName(Math.round(casttime / 20.0f * 100f) / 100f).withStyle(ChatFormatting.GREEN));
281335
}
282336

283337
Set<String> radiuses = new LinkedHashSet<>();

src/main/java/com/robertx22/mine_and_slash/saveclasses/spells/SpellCastingData.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ public SpellInputBufferEntry(int number) {
223223

224224
public int castTickLeft = 0;
225225
public int castTicksDone = 0;
226+
public int castTimesDone;
226227
public int spellTotalCastTicks = 0;
227228
public CalculatedSpellData calcSpell = null;
228229
public Boolean casting = false;
@@ -317,6 +318,7 @@ public void cancelCast(LivingEntity entity) {
317318
castTickLeft = 0;
318319
spellTotalCastTicks = 0;
319320
castTicksDone = 0;
321+
castTimesDone = 0;
320322
this.casting = false;
321323

322324
if (entity instanceof ServerPlayer p) {
@@ -386,7 +388,6 @@ public void onTimePass(LivingEntity entity) {
386388
tryCast(ctx);
387389

388390
lastSpell = spell;
389-
390391
castTickLeft--;
391392
castTicksDone++;
392393

@@ -430,6 +431,7 @@ public void setToCast(SpellCastContext ctx) {
430431
this.castTickLeft = ctx.spell.getCastTimeTicks(ctx);
431432
this.spellTotalCastTicks = this.castTickLeft;
432433
this.castTicksDone = 0;
434+
castTimesDone = 0;
433435
this.casting = true;
434436

435437
if (ctx.caster instanceof ServerPlayer p) {

src/main/java/com/robertx22/mine_and_slash/uncommon/effectdatas/rework/action/DecreaseNumberByPercentEffect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public DecreaseNumberByPercentEffect(String num) {
2121
@Override
2222
public void activate(EffectEvent event, EffectSides statSource, StatData data, Stat stat) {
2323
event.data.getNumber(num_id).number -= event.data.getOriginalNumber(num_id).number * data.getValue() / 100F;
24+
event.data.getNumber(num_id).number = Math.round(event.data.getNumber(num_id).number * 100.0f) / 100.0f;
2425
}
2526

2627
@Override

0 commit comments

Comments
 (0)