From 49ce2e113181802255d159b8a5be943db8dc0bae Mon Sep 17 00:00:00 2001 From: PinkGoosik Date: Mon, 17 Jun 2024 22:06:35 +0500 Subject: [PATCH] tweaks --- gradle.properties | 2 +- .../dimrevive/DimensionalRevive.java | 24 ++++-- .../{ => data}/VisitedDimensionsState.java | 14 +++- .../dimrevive/extension/PlayerExtension.java | 8 ++ .../dimrevive/extension/WorldExtension.java | 2 +- .../dimrevive/mixin/PlayerMixin.java | 82 +++++++++++++++++++ .../dimrevive/mixin/WorldMixin.java | 2 +- src/main/resources/dimrevive.mixins.json | 1 + 8 files changed, 124 insertions(+), 11 deletions(-) rename src/main/java/ru/pinkgoosik/dimrevive/{ => data}/VisitedDimensionsState.java (70%) create mode 100644 src/main/java/ru/pinkgoosik/dimrevive/extension/PlayerExtension.java create mode 100644 src/main/java/ru/pinkgoosik/dimrevive/mixin/PlayerMixin.java diff --git a/gradle.properties b/gradle.properties index e3cb234..2c3a44c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ org.gradle.jvmargs = -Xmx1G org.gradle.parallel = true # Mod Properties -mod_version = 0.1.2 +mod_version = 0.1.3 maven_group = ru.pinkgoosik archives_base_name = dimensional-revive diff --git a/src/main/java/ru/pinkgoosik/dimrevive/DimensionalRevive.java b/src/main/java/ru/pinkgoosik/dimrevive/DimensionalRevive.java index 63c59e0..adf50bd 100644 --- a/src/main/java/ru/pinkgoosik/dimrevive/DimensionalRevive.java +++ b/src/main/java/ru/pinkgoosik/dimrevive/DimensionalRevive.java @@ -3,9 +3,11 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.dimension.v1.FabricDimensions; import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents; +import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents; import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.effect.StatusEffects; import net.minecraft.registry.RegistryKey; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.math.Vec3d; @@ -15,6 +17,7 @@ import org.apache.commons.lang3.text.WordUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ru.pinkgoosik.dimrevive.extension.PlayerExtension; import ru.pinkgoosik.dimrevive.extension.WorldExtension; public class DimensionalRevive implements ModInitializer { @@ -24,14 +27,21 @@ public class DimensionalRevive implements ModInitializer { @Override public void onInitialize() { + ServerLivingEntityEvents.ALLOW_DEATH.register((entity, damageSource, damageAmount) -> { + if(entity instanceof ServerPlayerEntity player && player instanceof PlayerExtension ex && player.getServer().isHardcore() && !player.isSpectator()) { + ex.onSilentDeath(damageSource); + return false; + } + return true; + }); + ServerEntityWorldChangeEvents.AFTER_PLAYER_CHANGE_WORLD.register((player, origin, destination) -> { - var visitedDimensions = ((WorldExtension)player.getServer().getOverworld()).getVisitedDimensions().visitedDimensions; + var visitedDimensions = ((WorldExtension)player.getServer().getOverworld()).getVisitedDimensions(); - if(!player.isSpectator() && player.getServer().isHardcore() && !destination.getRegistryKey().equals(World.OVERWORLD) && !visitedDimensions.contains(destination.getRegistryKey().getValue().toString())) { + if(!player.isSpectator() && player.getServer().isHardcore() && !destination.getRegistryKey().equals(World.OVERWORLD) && !visitedDimensions.wasVisited(destination.getRegistryKey())) { player.getServer().getPlayerManager().broadcast(Text.literal("New dimension was discovered: §6" + formatDimensionName(destination.getRegistryKey()) + "§f. All players are revived."), false); - visitedDimensions.add(destination.getRegistryKey().getValue().toString()); - ((WorldExtension)player.getServer().getOverworld()).getVisitedDimensions().setDirty(true); + visitedDimensions.addDimension(destination.getRegistryKey()); player.getServer().getPlayerManager().getPlayerList().forEach(serverPlayerEntity -> { if(serverPlayerEntity.isSpectator()) { @@ -39,9 +49,9 @@ public void onInitialize() { serverPlayerEntity.changeGameMode(GameMode.SURVIVAL); serverPlayerEntity.setPortalCooldown(20 * 5); - serverPlayerEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.REGENERATION, 5, 5, false, true)); - serverPlayerEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.SATURATION, 5, 5, false, true)); - serverPlayerEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.RESISTANCE, 5, 255, false, true)); + serverPlayerEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.REGENERATION, 10, 5, false, true)); + serverPlayerEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.SATURATION, 10, 5, false, true)); + serverPlayerEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.RESISTANCE, 10, 255, false, true)); } }); } diff --git a/src/main/java/ru/pinkgoosik/dimrevive/VisitedDimensionsState.java b/src/main/java/ru/pinkgoosik/dimrevive/data/VisitedDimensionsState.java similarity index 70% rename from src/main/java/ru/pinkgoosik/dimrevive/VisitedDimensionsState.java rename to src/main/java/ru/pinkgoosik/dimrevive/data/VisitedDimensionsState.java index 5992083..845052c 100644 --- a/src/main/java/ru/pinkgoosik/dimrevive/VisitedDimensionsState.java +++ b/src/main/java/ru/pinkgoosik/dimrevive/data/VisitedDimensionsState.java @@ -1,7 +1,9 @@ -package ru.pinkgoosik.dimrevive; +package ru.pinkgoosik.dimrevive.data; import net.minecraft.nbt.NbtCompound; +import net.minecraft.registry.RegistryKey; import net.minecraft.world.PersistentState; +import net.minecraft.world.World; import java.util.ArrayList; @@ -30,4 +32,14 @@ public static VisitedDimensionsState fromNbt(NbtCompound nbt) { return manager; } + + + public boolean wasVisited(RegistryKey key) { + this.setDirty(true); + return visitedDimensions.contains(key.getValue().toString()); + } + + public void addDimension(RegistryKey key) { + this.visitedDimensions.add(key.getValue().toString()); + } } diff --git a/src/main/java/ru/pinkgoosik/dimrevive/extension/PlayerExtension.java b/src/main/java/ru/pinkgoosik/dimrevive/extension/PlayerExtension.java new file mode 100644 index 0000000..a090990 --- /dev/null +++ b/src/main/java/ru/pinkgoosik/dimrevive/extension/PlayerExtension.java @@ -0,0 +1,8 @@ +package ru.pinkgoosik.dimrevive.extension; + +import net.minecraft.entity.damage.DamageSource; + +public interface PlayerExtension { + + void onSilentDeath(DamageSource source); +} diff --git a/src/main/java/ru/pinkgoosik/dimrevive/extension/WorldExtension.java b/src/main/java/ru/pinkgoosik/dimrevive/extension/WorldExtension.java index b9858a8..b3d8cee 100644 --- a/src/main/java/ru/pinkgoosik/dimrevive/extension/WorldExtension.java +++ b/src/main/java/ru/pinkgoosik/dimrevive/extension/WorldExtension.java @@ -1,6 +1,6 @@ package ru.pinkgoosik.dimrevive.extension; -import ru.pinkgoosik.dimrevive.VisitedDimensionsState; +import ru.pinkgoosik.dimrevive.data.VisitedDimensionsState; public interface WorldExtension { diff --git a/src/main/java/ru/pinkgoosik/dimrevive/mixin/PlayerMixin.java b/src/main/java/ru/pinkgoosik/dimrevive/mixin/PlayerMixin.java new file mode 100644 index 0000000..011a3b8 --- /dev/null +++ b/src/main/java/ru/pinkgoosik/dimrevive/mixin/PlayerMixin.java @@ -0,0 +1,82 @@ +package ru.pinkgoosik.dimrevive.mixin; + +import com.mojang.authlib.GameProfile; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.damage.DamageSource; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.scoreboard.ScoreboardCriterion; +import net.minecraft.scoreboard.ScoreboardPlayerScore; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.stat.Stats; +import net.minecraft.text.Text; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.GlobalPos; +import net.minecraft.world.GameMode; +import net.minecraft.world.GameRules; +import net.minecraft.world.World; +import net.minecraft.world.event.GameEvent; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import ru.pinkgoosik.dimrevive.extension.PlayerExtension; + +import java.util.Optional; + +@Mixin(ServerPlayerEntity.class) +public abstract class PlayerMixin extends PlayerEntity implements PlayerExtension { + + @Shadow + @Final + public MinecraftServer server; + + @Shadow + protected abstract void forgiveMobAnger(); + + @Shadow + public abstract boolean changeGameMode(GameMode gameMode); + + public PlayerMixin(World world, BlockPos pos, float yaw, GameProfile gameProfile) { + super(world, pos, yaw, gameProfile); + } + + @Override + public void onSilentDeath(DamageSource source) { + this.setHealth(this.getMaxHealth()); + this.emitGameEvent(GameEvent.ENTITY_DIE); + boolean bl = this.getWorld().getGameRules().getBoolean(GameRules.SHOW_DEATH_MESSAGES); + if (bl) { + Text text = this.getDamageTracker().getDeathMessage(); + server.getPlayerManager().broadcast(text, false); + } + + this.dropShoulderEntities(); + if (this.getWorld().getGameRules().getBoolean(GameRules.FORGIVE_DEAD_PLAYERS)) { + forgiveMobAnger(); + } + + if (!this.isSpectator()) { + this.drop(source); + } + + this.getScoreboard().forEachScore(ScoreboardCriterion.DEATH_COUNT, this.getEntityName(), ScoreboardPlayerScore::incrementScore); + LivingEntity livingEntity = this.getPrimeAdversary(); + if (livingEntity != null) { + this.incrementStat(Stats.KILLED_BY.getOrCreateStat(livingEntity.getType())); + livingEntity.updateKilledAdvancementCriterion(this, this.scoreAmount, source); + this.onKilledBy(livingEntity); + } + + this.getWorld().sendEntityStatus(this, (byte)3); + this.incrementStat(Stats.DEATHS); + this.resetStat(Stats.CUSTOM.getOrCreateStat(Stats.TIME_SINCE_DEATH)); + this.resetStat(Stats.CUSTOM.getOrCreateStat(Stats.TIME_SINCE_REST)); + this.extinguish(); + this.setFrozenTicks(0); + this.setOnFire(false); + this.getDamageTracker().update(); + this.setLastDeathPos(Optional.of(GlobalPos.create(this.getWorld().getRegistryKey(), this.getBlockPos()))); + this.changeGameMode(GameMode.SPECTATOR); + } +} diff --git a/src/main/java/ru/pinkgoosik/dimrevive/mixin/WorldMixin.java b/src/main/java/ru/pinkgoosik/dimrevive/mixin/WorldMixin.java index 5dcc629..648dc22 100644 --- a/src/main/java/ru/pinkgoosik/dimrevive/mixin/WorldMixin.java +++ b/src/main/java/ru/pinkgoosik/dimrevive/mixin/WorldMixin.java @@ -20,7 +20,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import ru.pinkgoosik.dimrevive.VisitedDimensionsState; +import ru.pinkgoosik.dimrevive.data.VisitedDimensionsState; import ru.pinkgoosik.dimrevive.extension.WorldExtension; import java.util.List; diff --git a/src/main/resources/dimrevive.mixins.json b/src/main/resources/dimrevive.mixins.json index 94fc448..187d15d 100644 --- a/src/main/resources/dimrevive.mixins.json +++ b/src/main/resources/dimrevive.mixins.json @@ -4,6 +4,7 @@ "package": "ru.pinkgoosik.dimrevive.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ + "PlayerMixin", "WorldMixin" ], "client": [