Combit/src/main/java/io/gitlab/jfronny/combit/Combit.java

97 lines
4.0 KiB
Java
Raw Normal View History

2020-10-27 20:13:31 +01:00
package io.gitlab.jfronny.combit;
2022-06-08 11:46:10 +02:00
import io.gitlab.jfronny.combit.events.*;
2023-03-14 22:11:32 +01:00
import net.fabricmc.api.ModInitializer;
2022-06-08 11:46:10 +02:00
import net.minecraft.entity.*;
2023-03-14 22:11:32 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
2020-10-27 20:13:31 +01:00
2023-03-14 22:11:32 +01:00
import java.util.Set;
2020-10-27 20:13:31 +01:00
public class Combit implements ModInitializer {
@Override
public void onInitialize() {
2021-12-29 23:36:10 +01:00
EntityHurtEvent.EVENT.register((entity, source, amount) -> {
if (entity.getEntityWorld().isClient) return ActionResult.PASS;
if (CombitConfig.debug && entity instanceof PlayerEntity) {
2020-10-27 20:13:31 +01:00
String debugSource;
Entity trueSource = source.getAttacker();
if (trueSource == null || EntityType.getId(trueSource.getType()) == null) {
debugSource = "null";
} else {
debugSource = EntityType.getId(trueSource.getType()).toString();
}
String message = String.format("Type of damage received: %s\nAmount: %.3f\nTrue Source (mob id): %s\n",
source.getName(), amount, debugSource);
2022-06-08 11:46:10 +02:00
entity.sendMessage(Text.literal(message));
2020-10-27 20:13:31 +01:00
}
2021-12-29 23:36:10 +01:00
if (CombitConfig.excludePlayers && entity instanceof PlayerEntity) return ActionResult.PASS;
if (CombitConfig.excludeAllMobs && !(entity instanceof PlayerEntity)) return ActionResult.PASS;
if (idMatches(getId(entity), CombitConfig.targetEntityWhitelist)) return ActionResult.PASS;
if (idMatches(source.getName(), CombitConfig.damageSourceWhitelist)) return ActionResult.PASS;
2020-10-27 20:13:31 +01:00
Entity attacker = source.getAttacker();
2021-12-29 23:36:10 +01:00
if (attacker != null && idMatches(getId(attacker), CombitConfig.attackerWhitelist))
2020-10-27 20:13:31 +01:00
return ActionResult.PASS;
2022-01-08 22:27:12 +01:00
if (CombitConfig.iFrameInterval >= 0)
entity.timeUntilRegen = CombitConfig.iFrameInterval;
2020-10-27 20:13:31 +01:00
return ActionResult.PASS;
});
// Knockback module part 1
2021-12-29 23:36:10 +01:00
EntityKnockbackEvent.EVENT.register((entity, amp, dx, dz) -> {
2020-10-27 20:13:31 +01:00
if (entity.getEntityWorld().isClient) {
return ActionResult.PASS;
}
2021-12-29 23:36:10 +01:00
2020-10-27 20:13:31 +01:00
if (entity.handSwinging) {
entity.handSwinging = false;
return ActionResult.FAIL;
}
return ActionResult.PASS;
});
// Attack + knockback cancel module
2021-12-29 23:36:10 +01:00
PlayerAttackEvent.EVENT.register((player, target) -> {
if (player.getEntityWorld().isClient)
2020-10-27 20:13:31 +01:00
return ActionResult.PASS;
if (CombitConfig.debug) {
2022-06-08 11:46:10 +02:00
player.sendMessage(Text.literal(
target instanceof LivingEntity le
? String.format("LivingEntity attacked: %s (%s/%s)", EntityType.getId(target.getType()), le.getHealth(), le.getMaxHealth())
: String.format("Entity attacked: %s", EntityType.getId(target.getType()))
2021-12-29 23:36:10 +01:00
), false);
}
2020-10-27 20:13:31 +01:00
float str = player.getAttackCooldownProgress(0);
2021-12-29 23:36:10 +01:00
if (str <= CombitConfig.attackCancelThreshold)
2020-10-27 20:13:31 +01:00
return ActionResult.FAIL;
2021-12-29 23:36:10 +01:00
if (str <= CombitConfig.knockbackCancelThreshold && target instanceof LivingEntity le)
le.handSwinging = true;
2020-10-27 20:13:31 +01:00
return ActionResult.PASS;
});
}
public static String getId(Entity entity) {
2020-10-27 20:13:31 +01:00
return EntityType.getId(entity.getType()).toString();
}
public static boolean idMatches(String entityId, Set<String> matches) {
2021-12-29 23:36:10 +01:00
for (String id : matches) {
2020-10-27 20:13:31 +01:00
int starIndex = id.indexOf('*');
if (starIndex != -1) {
2021-12-29 23:36:10 +01:00
if (entityId.contains(id.substring(0, starIndex))) return true;
} else {
if (entityId.equals(id)) return true;
if (("minecraft:" + entityId).equals(id)) return true;
if (entityId.equals("minecraft:" + id)) return true;
2020-10-27 20:13:31 +01:00
}
}
return false;
}
}