Skip to content

Commit

Permalink
fix(1.20.1-forged): fix #37 #49 #50
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlimiter committed Mar 28, 2024
1 parent a9fad1b commit 3539c95
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 114 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ curios_version=5.4.7+1.20.1
mod_id=avaritia
mod_name=Re-Avaritia-forged
mod_license=MIT
mod_version=1.3.5.4
mod_version=1.3.5.5
mod_group_id=committee.nova.mods
mod_authors=cnlimiter, Asek3, MikhailTapio
mod_description=Are you the type of modded Minecraft player that makes a beeline for the designated \"end game\" and then gives up on ever playing again once you get there? Do you wish there was a way to make the process take significantly longer? Do you love GregTech, but wish it weren't so short? Do you sit down on your chest full of Galgadorian Drills and wish there was a mod that didn't just hand things to you on a silver platter?\n\n \n\nThis might be the mod for you!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public ModShapedRecipeBuilder showNotification(boolean p_273326_) {
public void save(@NotNull Consumer<FinishedRecipe> p_126141_, @NotNull ResourceLocation p_126142_) {
this.ensureValid(p_126142_);
this.advancement.parent(ROOT_RECIPE_ADVANCEMENT).addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(p_126142_)).rewards(AdvancementRewards.Builder.recipe(p_126142_)).requirements(RequirementsStrategy.OR);
p_126141_.accept(new ModShapedRecipeBuilder.Result(p_126142_, this.result, this.count, this.group == null ? "" : this.group, determineBookCategory(this.category), this.rows, this.key, this.advancement, p_126142_.withPrefix("recipes/" + this.category.getFolderName() + "/"), this.showNotification));
p_126141_.accept(new ModShapedRecipeBuilder.Result(p_126142_, this.result, this.count, this.group == null ? "" : this.group,
determineBookCategory(this.category), this.rows, this.key, this.advancement,
p_126142_.withPrefix("recipes/" + this.category.getFolderName() + "/"), this.showNotification));
}

private void ensureValid(ResourceLocation p_126144_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import committee.nova.mods.avaritia.init.registry.ModDamageTypes;
import committee.nova.mods.avaritia.init.registry.ModItems;
import committee.nova.mods.avaritia.util.AbilityUtil;
import committee.nova.mods.avaritia.util.PlayerUtil;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
Expand All @@ -14,16 +16,13 @@
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

import static committee.nova.mods.avaritia.util.AbilityUtil.isPlayerWearing;
import static net.minecraft.world.entity.EquipmentSlot.*;
Expand All @@ -38,151 +37,134 @@
public class AbilityHandler {

public static final Set<String> entitiesWithHelmets = new HashSet<>();
public static final Set<String> entitiesWithChest = new HashSet<>();
public static final Set<String> entitiesWithLeggings = new HashSet<>();
public static final Set<String> entitiesWithBoots = new HashSet<>();
public static final Set<String> entitiesWithFlight = new HashSet<>();


public static final Map<String, FlightInfo> entitiesWithFlight = new HashMap<>();


@SubscribeEvent
public static void updateAbilities(LivingEvent.LivingTickEvent event) {
if (event.getEntity() instanceof Player entity) {
String key = entity.getGameProfile().getName() + ":" + entity.level().isClientSide;
if (event.getEntity() instanceof Player player) {
String key = player.getGameProfile().getName() + ":" + player.level().isClientSide;

boolean hasHelmet = isPlayerWearing(event.getEntity(), HEAD, item -> item instanceof ArmorInfinityItem);
boolean hasChest = isPlayerWearing(event.getEntity(), CHEST, item -> item instanceof ArmorInfinityItem);
boolean hasLeggings = isPlayerWearing(event.getEntity(), LEGS, item -> item instanceof ArmorInfinityItem);
boolean hasBoots = isPlayerWearing(event.getEntity(), FEET, item -> item instanceof ArmorInfinityItem);


if (hasHelmet) {
if (entitiesWithHelmets.contains(key)) {
handleHelmetStateChange(entity);
} else {
entitiesWithHelmets.add(key);
}
} else {
entitiesWithHelmets.remove(key);
}
handleHelmetStateChange(player, key, hasHelmet);
handleChestStateChange(player, key, hasChest);
handleLeggingsStateChange(player, key, hasLeggings);
handleBootsStateChange(player, key, hasBoots);

if (hasChest) {
if (entitiesWithChest.contains(key)) {
handleChestStateChange(entity);
} else {
entitiesWithChest.add(key);
}
} else {
if (!entity.isCreative() && !entity.isSpectator()){
entity.getAbilities().mayfly = false;
entity.getAbilities().flying = false;
}
}

private static void handleChestStateChange(Player player, String key, boolean hasChest) {
boolean isFlyingGameMode = !PlayerUtil.isPlayingMode(player);
FlightInfo flightInfo = entitiesWithFlight.computeIfAbsent(key, uuid -> new FlightInfo());
if (isFlyingGameMode || hasChest) {
if (!flightInfo.hadFlightItem) {
if (!player.getAbilities().mayfly) {
updateClientServerFlight(player, true);
}
entitiesWithChest.remove(key);
flightInfo.hadFlightItem = true;
} else if (flightInfo.wasFlyingGameMode && !isFlyingGameMode) {
updateClientServerFlight(player, true, flightInfo.wasFlying);
} else if (flightInfo.wasFlyingAllowed && !player.getAbilities().mayfly) {
updateClientServerFlight(player, true, flightInfo.wasFlying);
}

if (hasLeggings) {
if (entitiesWithLeggings.contains(key)) {
handleLeggingsStateChange(entity);
} else {
entitiesWithLeggings.add(key);
flightInfo.wasFlyingGameMode = isFlyingGameMode;
flightInfo.wasFlying = player.getAbilities().flying;
flightInfo.wasFlyingAllowed = player.getAbilities().mayfly;
if (player.getAbilities().flying && hasChest){
List<MobEffectInstance> effects = Lists.newArrayList(player.getActiveEffects());
for (MobEffectInstance potion : Collections2.filter(effects, potion -> !potion.getEffect().isBeneficial())) {
player.removeEffect(potion.getEffect());
}
} else {
entitiesWithLeggings.remove(key);
}

if (hasBoots) {
if (entitiesWithBoots.contains(key)) {
handleBootsStateChange(entity);
} else {
entitiesWithBoots.add(key);
} else {
if (flightInfo.hadFlightItem) {
if (player.getAbilities().mayfly) {
updateClientServerFlight(player, false);
}
} else {
entity.setMaxUpStep(0.5F);
entitiesWithBoots.remove(key);
flightInfo.hadFlightItem = false;
}

flightInfo.wasFlyingGameMode = false;
flightInfo.wasFlying = player.getAbilities().flying;
flightInfo.wasFlyingAllowed = player.getAbilities().mayfly;
}
}

private static void stripAbilities(Player entity) {
String key = entity.getGameProfile().getName() + ":" + entity.level().isClientSide;

if (entitiesWithHelmets.remove(key)) {
}
private static void handleHelmetStateChange(Player player, String key, boolean hasHelmet) {
if (hasHelmet) {
if (entitiesWithHelmets.contains(key)) {
player.setAirSupply(300);
player.getFoodData().setFoodLevel(20);
player.getFoodData().setSaturation(20f);
MobEffectInstance nv = player.getEffect(MobEffects.NIGHT_VISION);
if (nv == null) {
nv = new MobEffectInstance(MobEffects.NIGHT_VISION, 300, 0, false, false);
player.addEffect(nv);
}
nv.duration = 300;

if (entitiesWithChest.remove(key)) {
if (!entity.isCreative() && !entity.isSpectator()){
entity.getAbilities().mayfly = false;
entity.getAbilities().flying = false;
} else {
entitiesWithHelmets.add(key);
}
}

if (entitiesWithLeggings.remove(key)) {
}

if (entitiesWithBoots.remove(key)) {
entity.setMaxUpStep(0.5F);
} else {
entitiesWithHelmets.remove(key);
}
}

private static void handleHelmetStateChange(LivingEntity entity) {
if (entity instanceof Player player) {
player.setAirSupply(300);
player.getFoodData().setFoodLevel(20);
player.getFoodData().setSaturation(20f);
MobEffectInstance nv = player.getEffect(MobEffects.NIGHT_VISION);
if (nv == null) {
nv = new MobEffectInstance(MobEffects.NIGHT_VISION, 300, 0, false, false);
player.addEffect(nv);
private static void handleLeggingsStateChange(Player player, String key, boolean hasLeggings) {
if (hasLeggings) {
if (entitiesWithLeggings.contains(key)) {
if (player.isOnFire()) {
player.clearFire();
player.fireImmune();
}
nv.duration = 300;
} else {
entitiesWithLeggings.add(key);
}
} else {
entitiesWithLeggings.remove(key);
}
}

private static void handleChestStateChange(LivingEntity entity) {
if (entity instanceof Player player) {
player.getAbilities().mayfly = true;
List<MobEffectInstance> effects = Lists.newArrayList(player.getActiveEffects());
for (MobEffectInstance potion : Collections2.filter(effects, potion -> !potion.getEffect().isBeneficial())) {
player.removeEffect(potion.getEffect());
private static void handleBootsStateChange(Player player, String key, boolean hasBoots) {
if (hasBoots) {
if (entitiesWithBoots.contains(key)) {
player.setMaxUpStep(1.25F);//Step 17 pixels, Allows for stepping directly from a path to the top of a block next to the path.
boolean flying = player.getAbilities().flying;
boolean swimming = player.isInWater();
if (player.onGround() || flying || swimming) {
boolean sneaking = player.isCrouching();

float speed = 0.1f * (flying ? 1.1f : 1.0f)
* (swimming ? 1.2f : 1.0f)
* (sneaking ? 0.1f : 1.0f);

if (player.zza > 0f) {
player.moveRelative(speed, new Vec3(0, 0, 1));
} else if (player.zza < 0f) {
player.moveRelative(-speed * 0.3f, new Vec3(0, 0, 1));
}

if (player.xxa != 0f) {
player.moveRelative(speed * 0.5f * Math.signum(player.xxa), new Vec3(1, 0, 0));
}
}


}
}

private static void handleLeggingsStateChange(LivingEntity entity) {
if (entity.isOnFire()) {
entity.clearFire();
entity.fireImmune();
}
}

private static void handleBootsStateChange(LivingEntity entity) {
entity.setMaxUpStep(1.25F);//Step 17 pixels, Allows for stepping directly from a path to the top of a block next to the path.
boolean flying = entity instanceof Player player && player.getAbilities().flying;
boolean swimming = entity.isInWater();
if (entity.onGround() || flying || swimming) {
boolean sneaking = entity.isCrouching();

float speed = 0.1f * (flying ? 1.1f : 1.0f)
* (swimming ? 1.2f : 1.0f)
* (sneaking ? 0.1f : 1.0f);

if (entity.zza > 0f) {
entity.moveRelative(speed, new Vec3(0, 0, 1));
} else if (entity.zza < 0f) {
entity.moveRelative(-speed * 0.3f, new Vec3(0, 0, 1));
}

if (entity.xxa != 0f) {
entity.moveRelative(speed * 0.5f * Math.signum(entity.xxa), new Vec3(1, 0, 0));
} else {
entitiesWithBoots.add(key);
}
} else {
player.setMaxUpStep(0.5F);
entitiesWithBoots.remove(key);
}
}


//特殊效果(附魔)
@SubscribeEvent
public static void opTool(PlayerEvent.ItemCraftedEvent event) {
Expand Down Expand Up @@ -228,6 +210,7 @@ public static void jumpBoost(LivingEvent.LivingJumpEvent event) {
@SubscribeEvent
public static void onPlayerDimensionChange(PlayerEvent.PlayerChangedDimensionEvent event) {
stripAbilities(event.getEntity());
reapplyFly(event.getEntity());
}

@SubscribeEvent
Expand All @@ -238,6 +221,7 @@ public static void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event) {
@SubscribeEvent
public static void onPlayerLoggedOut(PlayerEvent.PlayerLoggedOutEvent event) {
stripAbilities(event.getEntity());
clearFly(event.getEntity());
}

@SubscribeEvent
Expand All @@ -251,4 +235,53 @@ public static void onEntityDeath(LivingDeathEvent event) {
stripAbilities(entity);
}
}

private static void stripAbilities(Player player) {
String key = player.getGameProfile().getName() + ":" + player.level().isClientSide;

entitiesWithHelmets.remove(key);

entitiesWithFlight.remove(key);

entitiesWithLeggings.remove(key);

if (entitiesWithBoots.remove(key)) {
player.setMaxUpStep(0.5F);
}
}


public static class FlightInfo {
public boolean hadFlightItem;
public boolean wasFlyingGameMode;
public boolean wasFlyingAllowed;
public boolean wasFlying;
}

private static void clearFly(Player player) {
entitiesWithFlight.remove(player.getGameProfile().getName() + ":" + player.level().isClientSide);
}

private static void reapplyFly(Player player) {
//For when the dimension changes/we need to reapply the flight info values to the client
FlightInfo flightInfo = entitiesWithFlight.get(player.getGameProfile().getName() + ":" + player.level().isClientSide);
if (flightInfo != null) {
if (flightInfo.wasFlyingAllowed || flightInfo.wasFlying) {
updateClientServerFlight(player, flightInfo.wasFlyingAllowed, flightInfo.wasFlying);
}
}
}

private static void updateClientServerFlight(Player player, boolean allowFlying) {
updateClientServerFlight(player, allowFlying, allowFlying && player.getAbilities().flying);
}

private static void updateClientServerFlight(Player player, boolean allowFlying, boolean isFlying) {
player.getAbilities().mayfly = allowFlying;
player.getAbilities().flying = isFlying;
if (player instanceof ServerPlayer serverPlayer) {
serverPlayer.onUpdateAbilities();
}
}

}
17 changes: 17 additions & 0 deletions src/main/java/committee/nova/mods/avaritia/util/PlayerUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package committee.nova.mods.avaritia.util;

import net.minecraft.world.entity.player.Player;

/**
* PlayerUtil
*
* @author cnlimiter
* @version 1.0
* @description
* @date 2024/3/28 12:58
*/
public class PlayerUtil {
public static boolean isPlayingMode(Player player) {
return !player.isCreative() && !player.isSpectator();
}
}

0 comments on commit 3539c95

Please sign in to comment.