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

183 lines
7.1 KiB
Java

package io.gitlab.jfronny.combit;
import io.gitlab.jfronny.combit.config.Cfg;
import io.gitlab.jfronny.combit.config.Invulnerability;
import io.gitlab.jfronny.combit.nodami.EntityHurtCallback;
import io.gitlab.jfronny.combit.nodami.EntityKnockbackCallback;
import io.gitlab.jfronny.combit.nodami.PlayerAttackCallback;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer;
import net.fabricmc.api.ModInitializer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.util.ActionResult;
import java.util.Set;
import java.util.UUID;
public class Combit implements ModInitializer {
private static final UUID debugUUID = new UUID(0L, 0L);
@Override
public void onInitialize() {
Invulnerability invulnerability = getCfg().invulnerability;
EntityHurtCallback.EVENT.register((entity, source, amount) -> {
if (entity.getEntityWorld().isClient) {
return ActionResult.PASS;
}
if (invulnerability.debug && entity instanceof PlayerEntity) {
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);
entity.sendSystemMessage(new LiteralText(message), debugUUID);
}
if (invulnerability.excludePlayers && entity instanceof PlayerEntity) {
return ActionResult.PASS;
}
if (invulnerability.excludeAllMobs && !(entity instanceof PlayerEntity)) {
return ActionResult.PASS;
}
/*for (String id : NodamiConfig.dmgReceiveExcludedEntities) {
Identifier loc = EntityType.getId(entity.getType());
if (loc == null)
break;
int starIndex = id.indexOf('*');
if (starIndex != -1) {
if (loc.toString().indexOf(id.substring(0, starIndex)) != -1) {
return ActionResult.PASS;
}
} else if (loc.toString().equals(id)) {
return ActionResult.PASS;
}
}*/
if (isEntityPass(getId(entity), invulnerability.dmgReceiveExcludedEntities))
return ActionResult.PASS;
// May have more DoTs missing in this list
// Not anymore (/s)
// Damage SOURCES that needs to be put in check
if (isEntityPass(source.getName(), invulnerability.damageSrcWhitelist))
return ActionResult.PASS;
/*for (String dmgType : NodamiConfig.damageSrcWhitelist) {
if (source.getName().equals(dmgType)) {
return ActionResult.PASS;
}
}*/
// THINGS, MOBS that needs to apply i-frames when attacking
Entity attacker = source.getAttacker();
//if (attacker != null) {
if (attacker != null && isEntityPass(getId(attacker), invulnerability.attackExcludedEntities))
return ActionResult.PASS;
/*for (String id : NodamiConfig.attackExcludedEntities) {
Identifier loc = EntityType.getId(attacker.getType());
if (loc == null)
break;
int starIndex = id.indexOf('*');
if (starIndex != -1) {
if (loc.toString().indexOf(id.substring(0, starIndex)) != -1) {
return ActionResult.PASS;
}
} else if (loc.toString().equals(id)) {
return ActionResult.PASS;
}
}*/
//}
entity.timeUntilRegen = invulnerability.iFrameInterval;
return ActionResult.PASS;
});
// Knockback module part 1
EntityKnockbackCallback.EVENT.register((entity, amp, dx, dz) -> {
if (entity.getEntityWorld().isClient) {
return ActionResult.PASS;
}
/*if (entity.getEntityWorld().isClient) {
return ActionResult.PASS;
}
if (source != null) {
// IT'S ONLY MAGIC
if (source instanceof PlayerEntity && ((PlayerEntity) source).hurtTime == -1) {
((PlayerEntity) source).hurtTime = 0;
return ActionResult.FAIL;
}
}
return ActionResult.PASS;*/
if (entity.handSwinging) {
entity.handSwinging = false;
return ActionResult.FAIL;
}
return ActionResult.PASS;
});
// Attack + knockback cancel module
PlayerAttackCallback.EVENT.register((player, target) -> {
if (player.getEntityWorld().isClient) {
return ActionResult.PASS;
}
if (invulnerability.debug) {
String message = String.format("Entity attacked: %s",
EntityType.getId(target.getType()));
player.sendMessage(new LiteralText(message), false);
}
float str = player.getAttackCooldownProgress(0);
if (str <= invulnerability.attackCancelThreshold) {
return ActionResult.FAIL;
}
if (str <= invulnerability.knockbackCancelThreshold) {
// Don't worry, it's only magic
//player.hurtTime = -1;
if (target instanceof LivingEntity)
((LivingEntity)target).handSwinging = true;
}
return ActionResult.PASS;
});
}
private String getId(Entity entity) {
return EntityType.getId(entity.getType()).toString();
}
private boolean isEntityPass(String oId, Set<String> ids) {
for (String id : ids) {
int starIndex = id.indexOf('*');
if (starIndex != -1) {
if (oId.contains(id.substring(0, starIndex))) {
return true;
}
} else if (oId.equals(id)) {
return true;
} else if (("minecraft:" + oId).equals(id)) {
return true;
} else if (oId.equals("minecraft:" + id)) {
return true;
}
}
return false;
}
private static Cfg cfg = null;
public static Cfg getCfg() {
if (cfg == null) {
AutoConfig.register(Cfg.class, JanksonConfigSerializer::new);
cfg = AutoConfig.getConfigHolder(Cfg.class).getConfig();
}
return cfg;
}
}