This commit is contained in:
parent
2930d65441
commit
6a3674dd7e
121
LICENSE
121
LICENSE
|
@ -1,121 +0,0 @@
|
||||||
Creative Commons Legal Code
|
|
||||||
|
|
||||||
CC0 1.0 Universal
|
|
||||||
|
|
||||||
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
||||||
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
||||||
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
||||||
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
||||||
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
||||||
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
||||||
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
||||||
HEREUNDER.
|
|
||||||
|
|
||||||
Statement of Purpose
|
|
||||||
|
|
||||||
The laws of most jurisdictions throughout the world automatically confer
|
|
||||||
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
||||||
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
||||||
authorship and/or a database (each, a "Work").
|
|
||||||
|
|
||||||
Certain owners wish to permanently relinquish those rights to a Work for
|
|
||||||
the purpose of contributing to a commons of creative, cultural and
|
|
||||||
scientific works ("Commons") that the public can reliably and without fear
|
|
||||||
of later claims of infringement build upon, modify, incorporate in other
|
|
||||||
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
||||||
and for any purposes, including without limitation commercial purposes.
|
|
||||||
These owners may contribute to the Commons to promote the ideal of a free
|
|
||||||
culture and the further production of creative, cultural and scientific
|
|
||||||
works, or to gain reputation or greater distribution for their Work in
|
|
||||||
part through the use and efforts of others.
|
|
||||||
|
|
||||||
For these and/or other purposes and motivations, and without any
|
|
||||||
expectation of additional consideration or compensation, the person
|
|
||||||
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
||||||
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
||||||
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
||||||
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
||||||
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
||||||
|
|
||||||
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
||||||
protected by copyright and related or neighboring rights ("Copyright and
|
|
||||||
Related Rights"). Copyright and Related Rights include, but are not
|
|
||||||
limited to, the following:
|
|
||||||
|
|
||||||
i. the right to reproduce, adapt, distribute, perform, display,
|
|
||||||
communicate, and translate a Work;
|
|
||||||
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
||||||
iii. publicity and privacy rights pertaining to a person's image or
|
|
||||||
likeness depicted in a Work;
|
|
||||||
iv. rights protecting against unfair competition in regards to a Work,
|
|
||||||
subject to the limitations in paragraph 4(a), below;
|
|
||||||
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
||||||
in a Work;
|
|
||||||
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
||||||
European Parliament and of the Council of 11 March 1996 on the legal
|
|
||||||
protection of databases, and under any national implementation
|
|
||||||
thereof, including any amended or successor version of such
|
|
||||||
directive); and
|
|
||||||
vii. other similar, equivalent or corresponding rights throughout the
|
|
||||||
world based on applicable law or treaty, and any national
|
|
||||||
implementations thereof.
|
|
||||||
|
|
||||||
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
||||||
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
||||||
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
||||||
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
||||||
of action, whether now known or unknown (including existing as well as
|
|
||||||
future claims and causes of action), in the Work (i) in all territories
|
|
||||||
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
||||||
treaty (including future time extensions), (iii) in any current or future
|
|
||||||
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
||||||
including without limitation commercial, advertising or promotional
|
|
||||||
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
||||||
member of the public at large and to the detriment of Affirmer's heirs and
|
|
||||||
successors, fully intending that such Waiver shall not be subject to
|
|
||||||
revocation, rescission, cancellation, termination, or any other legal or
|
|
||||||
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
||||||
as contemplated by Affirmer's express Statement of Purpose.
|
|
||||||
|
|
||||||
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
||||||
be judged legally invalid or ineffective under applicable law, then the
|
|
||||||
Waiver shall be preserved to the maximum extent permitted taking into
|
|
||||||
account Affirmer's express Statement of Purpose. In addition, to the
|
|
||||||
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
||||||
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
||||||
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
||||||
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
||||||
maximum duration provided by applicable law or treaty (including future
|
|
||||||
time extensions), (iii) in any current or future medium and for any number
|
|
||||||
of copies, and (iv) for any purpose whatsoever, including without
|
|
||||||
limitation commercial, advertising or promotional purposes (the
|
|
||||||
"License"). The License shall be deemed effective as of the date CC0 was
|
|
||||||
applied by Affirmer to the Work. Should any part of the License for any
|
|
||||||
reason be judged legally invalid or ineffective under applicable law, such
|
|
||||||
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
||||||
of the License, and in such case Affirmer hereby affirms that he or she
|
|
||||||
will not (i) exercise any of his or her remaining Copyright and Related
|
|
||||||
Rights in the Work or (ii) assert any associated claims and causes of
|
|
||||||
action with respect to the Work, in either case contrary to Affirmer's
|
|
||||||
express Statement of Purpose.
|
|
||||||
|
|
||||||
4. Limitations and Disclaimers.
|
|
||||||
|
|
||||||
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
||||||
surrendered, licensed or otherwise affected by this document.
|
|
||||||
b. Affirmer offers the Work as-is and makes no representations or
|
|
||||||
warranties of any kind concerning the Work, express, implied,
|
|
||||||
statutory or otherwise, including without limitation warranties of
|
|
||||||
title, merchantability, fitness for a particular purpose, non
|
|
||||||
infringement, or the absence of latent or other defects, accuracy, or
|
|
||||||
the present or absence of errors, whether or not discoverable, all to
|
|
||||||
the greatest extent permissible under applicable law.
|
|
||||||
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
||||||
that may apply to the Work or any use thereof, including without
|
|
||||||
limitation any person's Copyright and Related Rights in the Work.
|
|
||||||
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
||||||
consents, permissions or other rights required for any use of the
|
|
||||||
Work.
|
|
||||||
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
||||||
party to this document and has no duty or obligation with respect to
|
|
||||||
this CC0 or use of the Work.
|
|
|
@ -1,9 +0,0 @@
|
||||||
# Fabric Example Mod
|
|
||||||
|
|
||||||
## Setup
|
|
||||||
|
|
||||||
For setup instructions please see the [fabric wiki page](https://fabricmc.net/wiki/tutorial:setup) that relates to the IDE that you are using.
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
This template is available under the CC0 license. Feel free to learn from it and incorporate it in your own projects.
|
|
|
@ -10,8 +10,8 @@ loader_version=0.15.10
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version=1.0.0
|
mod_version=1.0.0
|
||||||
maven_group=com.example
|
maven_group=ly.hall.rose.healthsync
|
||||||
archives_base_name=modid
|
archives_base_name=healthsync
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
fabric_version=0.97.8+1.20.6
|
fabric_version=0.97.8+1.20.6
|
|
@ -1,10 +0,0 @@
|
||||||
package com.example;
|
|
||||||
|
|
||||||
import net.fabricmc.api.ClientModInitializer;
|
|
||||||
|
|
||||||
public class ExampleModClient implements ClientModInitializer {
|
|
||||||
@Override
|
|
||||||
public void onInitializeClient() {
|
|
||||||
// This entrypoint is suitable for setting up client-specific logic, such as rendering.
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
package com.example.mixin.client;
|
|
||||||
|
|
||||||
import net.minecraft.client.MinecraftClient;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|
||||||
|
|
||||||
@Mixin(MinecraftClient.class)
|
|
||||||
public class ExampleClientMixin {
|
|
||||||
@Inject(at = @At("HEAD"), method = "run")
|
|
||||||
private void run(CallbackInfo info) {
|
|
||||||
// This code is injected into the start of MinecraftClient.run()V
|
|
||||||
}
|
|
||||||
}
|
|
204
src/client/java/ly/hall/rose/healthsync/HealthSyncClient.java
Normal file
204
src/client/java/ly/hall/rose/healthsync/HealthSyncClient.java
Normal file
|
@ -0,0 +1,204 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||||
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||||
|
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
|
||||||
|
import net.minecraft.entity.Entity;
|
||||||
|
import net.minecraft.entity.EntityType;
|
||||||
|
import net.minecraft.entity.EquipmentSlot;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
import net.minecraft.entity.damage.DamageSources;
|
||||||
|
import net.minecraft.entity.damage.DamageType;
|
||||||
|
import net.minecraft.entity.damage.DamageTypes;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.registry.RegistryKeys;
|
||||||
|
import net.minecraft.registry.entry.RegistryEntry;
|
||||||
|
import net.minecraft.text.Style;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.text.Texts;
|
||||||
|
import net.minecraft.util.Arm;
|
||||||
|
import net.minecraft.util.math.Vec3d;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
public class HealthSyncClient implements ClientModInitializer {
|
||||||
|
private Text getReasonString(LivingEntityHealReason r) {
|
||||||
|
if (r == null)
|
||||||
|
return Texts.join(Text.of("Generic").getWithStyle(Style.EMPTY.withItalic(true)), Text.of(""));
|
||||||
|
|
||||||
|
return switch (r) {
|
||||||
|
case Generic -> Texts.join(Text.of("Generic").getWithStyle(Style.EMPTY.withItalic(true)), Text.of(""));
|
||||||
|
case NaturalRegeneration -> Text.of("Natural Regeneration");
|
||||||
|
case RegenerationStatusEffect -> Text.of("Regeneration");
|
||||||
|
case InstantHealthStatusEffect -> Text.of("Instant Health");
|
||||||
|
case AbsorptionStatusEffect -> Text.of("Absorption");
|
||||||
|
case Synchronization -> null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private String snakeToNormal(String snake) {
|
||||||
|
String[] strings = snake.split("_");
|
||||||
|
|
||||||
|
strings[0] = Character.toUpperCase(strings[0].charAt(0)) + strings[0].substring(1);
|
||||||
|
|
||||||
|
return String.join(" ", strings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Text getReasonString(Optional<DamageSource> optSource, Text damagedUser) {
|
||||||
|
if (optSource.isEmpty())
|
||||||
|
return Texts.join(Text.of("Generic").getWithStyle(Style.EMPTY.withItalic(true)), Text.of(""));
|
||||||
|
|
||||||
|
var source = optSource.get();
|
||||||
|
|
||||||
|
String string = "death.attack." + source.getType().msgId();
|
||||||
|
|
||||||
|
if (source.getAttacker() == null && source.getSource() == null)
|
||||||
|
return Text.translatable(string, damagedUser);
|
||||||
|
|
||||||
|
Text text = source.getAttacker() == null ? source.getSource().getDisplayName() : source.getAttacker().getDisplayName();
|
||||||
|
Entity entity = source.getAttacker();
|
||||||
|
ItemStack itemStack = entity instanceof LivingEntity
|
||||||
|
? ((LivingEntity)entity).getMainHandStack()
|
||||||
|
: ItemStack.EMPTY;
|
||||||
|
|
||||||
|
if (!itemStack.isEmpty() && itemStack.hasCustomName()) {
|
||||||
|
return Text.translatable(string + ".item", damagedUser, text, itemStack.toHoverableText());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Text.translatable(string, damagedUser, text);
|
||||||
|
|
||||||
|
|
||||||
|
// var s = source.getType().msgId();
|
||||||
|
//
|
||||||
|
// if (s.endsWith(".item"))
|
||||||
|
// s = s.substring(0, s.length()-5);
|
||||||
|
//
|
||||||
|
// if (s.endsWith(".player"))
|
||||||
|
// s = s.substring(0, s.length()-7);
|
||||||
|
//
|
||||||
|
// return Text.of(snakeToNormal(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onInitializeClient() {
|
||||||
|
var o = new PlayerDamageOverlay();
|
||||||
|
HudRenderCallback.EVENT.register(o);
|
||||||
|
|
||||||
|
// var random = new Random();
|
||||||
|
//
|
||||||
|
// AtomicInteger i = new AtomicInteger();
|
||||||
|
|
||||||
|
// ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||||
|
// i.getAndIncrement();
|
||||||
|
//
|
||||||
|
// if (i.get() >= 20) {
|
||||||
|
// o.pushInstance(new PlayerDamageInstance(
|
||||||
|
// client.getGameProfile(),
|
||||||
|
// HeartType.NORMAL,
|
||||||
|
// Text.of("Demo"),
|
||||||
|
// random.ints(-10, 11).findFirst().getAsInt() < 0 ? -7 : 7
|
||||||
|
// ));
|
||||||
|
// i.set(0);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
|
ClientPlayNetworking.registerGlobalReceiver(NetworkingConstants.HEAL_EVENT_PACKET_ID, (client, handler, buf, responseSender) -> {
|
||||||
|
var uuid = buf.readUuid();
|
||||||
|
var reason = buf.readOptional(r -> LivingEntityHealReason.valueOf(r.readString()));
|
||||||
|
var deltaHealth = buf.readFloat();
|
||||||
|
var deltaAbsorption = buf.readFloat();
|
||||||
|
|
||||||
|
if (client.world == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (client.getNetworkHandler() == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var listEntry = Objects.requireNonNull(client.getNetworkHandler()).getPlayerListEntry(uuid);
|
||||||
|
|
||||||
|
if (listEntry == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (deltaHealth != 0) {
|
||||||
|
o.pushInstance(new PlayerDamageInstance(
|
||||||
|
Objects.requireNonNull(client.getNetworkHandler()).getPlayerListEntry(uuid).getProfile(),
|
||||||
|
HeartType.NORMAL,
|
||||||
|
getReasonString(reason.orElse(null)),
|
||||||
|
deltaHealth
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deltaAbsorption != 0) {
|
||||||
|
o.pushInstance(new PlayerDamageInstance(
|
||||||
|
Objects.requireNonNull(client.getNetworkHandler()).getPlayerListEntry(uuid).getProfile(),
|
||||||
|
HeartType.ABSORBING,
|
||||||
|
getReasonString(reason.orElse(null)),
|
||||||
|
deltaHealth
|
||||||
|
));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ClientPlayNetworking.registerGlobalReceiver(NetworkingConstants.DAMAGE_EVENT_PACKET_ID, (client, handler, buf, responseSender) -> {
|
||||||
|
var uuid = buf.readUuid();
|
||||||
|
var opt = buf.readOptional(bufx -> {
|
||||||
|
var sourceTypeId = bufx.readInt();
|
||||||
|
var attackerId = bufx.readInt();
|
||||||
|
var sourceId = bufx.readInt();
|
||||||
|
var storedPosition = buf.readOptional(bufxx -> new Vec3d(
|
||||||
|
bufxx.readDouble(),
|
||||||
|
bufxx.readDouble(),
|
||||||
|
bufxx.readDouble()
|
||||||
|
));
|
||||||
|
|
||||||
|
assert client.world != null;
|
||||||
|
RegistryEntry<DamageType> entry = client.world.getRegistryManager().get(RegistryKeys.DAMAGE_TYPE).getEntry(sourceTypeId).orElseThrow();
|
||||||
|
|
||||||
|
Entity entity = attackerId == 0 ? null : client.world.getEntityById(attackerId - 1);
|
||||||
|
Entity entity2 = sourceId == 0 ? null : client.world.getEntityById(sourceId - 1);
|
||||||
|
|
||||||
|
return storedPosition
|
||||||
|
.map(vec3d -> new DamageSource(entry, vec3d))
|
||||||
|
.orElseGet(() -> new DamageSource(entry, entity, entity2));
|
||||||
|
});
|
||||||
|
var deltaTotal = buf.readFloat() + buf.readFloat();
|
||||||
|
var isPoison = buf.readBoolean();
|
||||||
|
var isWither = buf.readBoolean();
|
||||||
|
var isFrozen = buf.readBoolean();
|
||||||
|
|
||||||
|
if (client.world == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (client.getNetworkHandler() == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var listEntry = Objects.requireNonNull(client.getNetworkHandler()).getPlayerListEntry(uuid);
|
||||||
|
|
||||||
|
if (listEntry == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (opt.isPresent() && opt.get().isOf(DamageTypes.PLAYER_ATTACK) && opt.get().getAttacker() != null) {
|
||||||
|
o.pushInstance(new PlayerDamageInstance(
|
||||||
|
((PlayerEntity)opt.get().getAttacker()).getGameProfile(),
|
||||||
|
HeartType.CONTAINER,
|
||||||
|
Text.of("Homicide"),
|
||||||
|
deltaTotal
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
o.pushInstance(new PlayerDamageInstance(
|
||||||
|
listEntry.getProfile(),
|
||||||
|
isPoison ? HeartType.POISONED : isWither ? HeartType.WITHERED : isFrozen ? HeartType.FROZEN : HeartType.CONTAINER,
|
||||||
|
getReasonString(opt, listEntry.getDisplayName() == null ? Text.of(listEntry.getProfile().getName()) : listEntry.getDisplayName()),
|
||||||
|
deltaTotal
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
71
src/client/java/ly/hall/rose/healthsync/HeartType.java
Normal file
71
src/client/java/ly/hall/rose/healthsync/HeartType.java
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import com.mojang.blaze3d.systems.RenderSystem;
|
||||||
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
import net.minecraft.client.gui.DrawContext;
|
||||||
|
import net.minecraft.client.render.*;
|
||||||
|
import net.minecraft.client.util.Icons;
|
||||||
|
import net.minecraft.client.util.math.MatrixStack;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import org.joml.Matrix4f;
|
||||||
|
|
||||||
|
public enum HeartType {
|
||||||
|
CONTAINER(0, false, true),
|
||||||
|
NORMAL(2, true, false),
|
||||||
|
POISONED(4, true, false),
|
||||||
|
WITHERED(6, true, false),
|
||||||
|
ABSORBING(8, false, false),
|
||||||
|
FROZEN(9, false, false);
|
||||||
|
|
||||||
|
private final int textureIndex;
|
||||||
|
private final boolean hasBlinkingTexture;
|
||||||
|
private final boolean shouldCrop;
|
||||||
|
|
||||||
|
private HeartType(int textureIndex, boolean hasBlinkingTexture, boolean shouldCrop) {
|
||||||
|
this.textureIndex = textureIndex;
|
||||||
|
this.hasBlinkingTexture = hasBlinkingTexture;
|
||||||
|
this.shouldCrop = shouldCrop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getU(boolean halfHeart, boolean blinking) {
|
||||||
|
int i;
|
||||||
|
if (this == CONTAINER) {
|
||||||
|
i = blinking ? 1 : 0;
|
||||||
|
} else {
|
||||||
|
int j = halfHeart ? 1 : 0;
|
||||||
|
int k = this.hasBlinkingTexture && blinking ? 2 : 0;
|
||||||
|
i = j + k;
|
||||||
|
}
|
||||||
|
return 16 + (this.textureIndex * 2 + i) * 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(DrawContext ctx, int x, int y, boolean hardcore, boolean half, boolean blinking, boolean last, float opacity) {
|
||||||
|
int width = (half && this.shouldCrop) ? 5 : (last ? 9 : 8);
|
||||||
|
int height = 9;
|
||||||
|
|
||||||
|
var ta = new Identifier("textures/gui/icons.png");
|
||||||
|
|
||||||
|
int x1 = x;
|
||||||
|
int y1 = y;
|
||||||
|
int x2 = x + width;
|
||||||
|
int y2 = y + height;
|
||||||
|
|
||||||
|
float u1 = this.getU(half, blinking);
|
||||||
|
float v1 = (hardcore ? 5 : 0) * 9;
|
||||||
|
float u2 = u1 + width;
|
||||||
|
float v2 = v1 + height;
|
||||||
|
|
||||||
|
RenderSystem.setShaderTexture(0, ta);
|
||||||
|
RenderSystem.setShader(GameRenderer::getPositionColorTexProgram);
|
||||||
|
RenderSystem.enableBlend();
|
||||||
|
Matrix4f matrix4f = (new MatrixStack()).peek().getPositionMatrix();
|
||||||
|
BufferBuilder bufferBuilder = Tessellator.getInstance().getBuffer();
|
||||||
|
bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE);
|
||||||
|
bufferBuilder.vertex(matrix4f, x1, y1, 0).color(1.0F, 1.0F, 1.0F, opacity).texture(u1/256, v1/256).next();
|
||||||
|
bufferBuilder.vertex(matrix4f, x1, y2, 0).color(1.0F, 1.0F, 1.0F, opacity).texture(u1/256, v2/256).next();
|
||||||
|
bufferBuilder.vertex(matrix4f, x2, y2, 0).color(1.0F, 1.0F, 1.0F, opacity).texture(u2/256, v2/256).next();
|
||||||
|
bufferBuilder.vertex(matrix4f, x2, y1, 0).color(1.0F, 1.0F, 1.0F, opacity).texture(u2/256, v1/256).next();
|
||||||
|
BufferRenderer.drawWithGlobalProgram(bufferBuilder.end());
|
||||||
|
RenderSystem.disableBlend();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import com.mojang.authlib.GameProfile;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
public class PlayerDamageInstance {
|
||||||
|
public GameProfile profile;
|
||||||
|
public Text damageReason;
|
||||||
|
public float health;
|
||||||
|
public float lifetime;
|
||||||
|
public HeartType heartType;
|
||||||
|
|
||||||
|
PlayerDamageInstance(GameProfile profile, HeartType heartType, Text damageReason, float health) {
|
||||||
|
this.profile = profile;
|
||||||
|
this.heartType = heartType;
|
||||||
|
this.damageReason = damageReason;
|
||||||
|
this.health = health;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTime(float time) {
|
||||||
|
this.lifetime += time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateHealth(float health) {
|
||||||
|
this.health += health;
|
||||||
|
this.lifetime = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHalfHeartCount() {
|
||||||
|
return health < 0 ? -((int) Math.ceil(Math.abs(health))) : ((int) Math.ceil(Math.abs(health)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean matches(PlayerDamageInstance other) {
|
||||||
|
return this.profile.equals(other.profile) && this.damageReason.getString().equals(other.damageReason.getString()) && this.heartType.equals(other.heartType);
|
||||||
|
}
|
||||||
|
}
|
167
src/client/java/ly/hall/rose/healthsync/PlayerDamageOverlay.java
Normal file
167
src/client/java/ly/hall/rose/healthsync/PlayerDamageOverlay.java
Normal file
|
@ -0,0 +1,167 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
|
||||||
|
import com.mojang.blaze3d.systems.RenderSystem;
|
||||||
|
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
|
||||||
|
import net.minecraft.block.SculkShriekerBlock;
|
||||||
|
import net.minecraft.block.entity.SculkShriekerWarningManager;
|
||||||
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
import net.minecraft.client.font.TextRenderer;
|
||||||
|
import net.minecraft.client.gui.DrawContext;
|
||||||
|
import net.minecraft.client.gui.PlayerSkinDrawer;
|
||||||
|
import net.minecraft.client.gui.hud.InGameHud;
|
||||||
|
import net.minecraft.client.network.PlayerListEntry;
|
||||||
|
import net.minecraft.client.texture.PlayerSkinProvider;
|
||||||
|
import net.minecraft.entity.effect.StatusEffects;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.text.Style;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.text.TextColor;
|
||||||
|
import net.minecraft.text.Texts;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.util.math.MathHelper;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class PlayerDamageOverlay implements HudRenderCallback {
|
||||||
|
public static final int MAX_LIFETIME = 20 * 30;
|
||||||
|
public static final int FADE_START_TIME = 20 * 25;
|
||||||
|
public static final int FADE_LENGTH = MAX_LIFETIME - FADE_START_TIME;
|
||||||
|
|
||||||
|
private final List<PlayerDamageInstance> instances = new ArrayList<>();
|
||||||
|
|
||||||
|
private final Object o = new Object();
|
||||||
|
|
||||||
|
int animationOffset = 0;
|
||||||
|
float ticks = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHudRender(DrawContext drawContext, float tickDelta) {
|
||||||
|
ticks += tickDelta;
|
||||||
|
|
||||||
|
synchronized (o) {
|
||||||
|
var client = MinecraftClient.getInstance();
|
||||||
|
|
||||||
|
if (client == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
instances.sort(Comparator.comparingDouble(o -> o.lifetime));
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
Set<PlayerDamageInstance> toRemove = new HashSet<>();
|
||||||
|
|
||||||
|
if (animationOffset > 0)
|
||||||
|
animationOffset--;
|
||||||
|
|
||||||
|
for (PlayerDamageInstance instance : instances) {
|
||||||
|
instance.addTime(tickDelta);
|
||||||
|
|
||||||
|
if (instance.getHalfHeartCount() == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (animationOffset == 0 || i == 0) {
|
||||||
|
this.renderLine(drawContext, instance, i++ * 11);
|
||||||
|
} else {
|
||||||
|
this.renderLine(drawContext, instance, (i++ * 11) - animationOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instance.lifetime >= MAX_LIFETIME)
|
||||||
|
toRemove.add(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (PlayerDamageInstance playerDamageInstance : toRemove) {
|
||||||
|
instances.remove(playerDamageInstance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushInstance(PlayerDamageInstance i) {
|
||||||
|
synchronized (o) {
|
||||||
|
for (PlayerDamageInstance instance : this.instances) {
|
||||||
|
if (instance.matches(i)) {
|
||||||
|
instance.updateHealth(i.health);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.instances.add(i);
|
||||||
|
animationOffset = 11;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private float flip(float x) { return 1 - x; }
|
||||||
|
private float easeIn(float t) { return t * t; }
|
||||||
|
private float easeOut(float t) { return flip(easeIn(flip(t))); }
|
||||||
|
private void renderLine(DrawContext drawContext, PlayerDamageInstance instance, int rawBaseY) {
|
||||||
|
var client = MinecraftClient.getInstance();
|
||||||
|
var window = client.getWindow();
|
||||||
|
var world = Objects.requireNonNull(client.getCameraEntity()).getWorld();
|
||||||
|
|
||||||
|
var hardcore = world.getLevelProperties().isHardcore();
|
||||||
|
var blinking = instance.lifetime < 20 && Math.floor(ticks / 3.0F) % 2 == 1;
|
||||||
|
|
||||||
|
float alpha = 1;
|
||||||
|
|
||||||
|
if (instance.lifetime > FADE_START_TIME) {
|
||||||
|
float progress = (instance.lifetime - FADE_START_TIME) / FADE_LENGTH;
|
||||||
|
alpha = MathHelper.clampedLerp(1, 0, easeOut(progress));
|
||||||
|
}
|
||||||
|
|
||||||
|
int completelyEmptyHeartCount;
|
||||||
|
|
||||||
|
if (instance.getHalfHeartCount() == 0) {
|
||||||
|
completelyEmptyHeartCount = 0;
|
||||||
|
} else if (instance.getHalfHeartCount() < 0) {
|
||||||
|
completelyEmptyHeartCount = Math.floorDiv(-instance.getHalfHeartCount(), 2);
|
||||||
|
} else {
|
||||||
|
completelyEmptyHeartCount = (int) Math.ceil(((double) instance.getHalfHeartCount()) / 2.0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert MinecraftClient.getInstance().player != null;
|
||||||
|
int baseHeartRows = (int) (Math.ceil((Math.ceil(MinecraftClient.getInstance().player.getMaxHealth() / 2) + Math.ceil(MinecraftClient.getInstance().player.getAbsorptionAmount() / 2)) / 10));
|
||||||
|
|
||||||
|
int baseX = (window.getScaledWidth() / 2) - 91;
|
||||||
|
int baseY = window.getScaledHeight() - 40 - rawBaseY - (baseHeartRows * 10);
|
||||||
|
|
||||||
|
for (int i = 0; i < completelyEmptyHeartCount; i++) {
|
||||||
|
HeartType.CONTAINER.draw(drawContext, baseX + (i * 8), baseY, hardcore, false, blinking, i == (completelyEmptyHeartCount - 1), alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instance.heartType.equals(HeartType.CONTAINER) && (-instance.getHalfHeartCount()) % 2 == 1) {
|
||||||
|
HeartType.CONTAINER.draw(drawContext, baseX + (completelyEmptyHeartCount * 8), baseY, hardcore, true, blinking, true, alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!instance.heartType.equals(HeartType.CONTAINER)) {
|
||||||
|
if (Math.abs(instance.getHalfHeartCount()) % 2 == 1) {
|
||||||
|
for (int i = 0; i < (Math.abs(completelyEmptyHeartCount) - 1); i++) {
|
||||||
|
instance.heartType.draw(drawContext, baseX + (i * 8), baseY, hardcore, false, blinking, false, alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.heartType.draw(drawContext, baseX + ((Math.abs(completelyEmptyHeartCount) - 1) * 8), baseY, hardcore, true, blinking, true, alpha);
|
||||||
|
} else {
|
||||||
|
for (int i = 0; i < Math.abs(completelyEmptyHeartCount); i++) {
|
||||||
|
instance.heartType.draw(drawContext, baseX + (i * 8), baseY, hardcore, false, blinking, i == Math.abs(completelyEmptyHeartCount) - 1, alpha);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PlayerIconRenderer.draw(client.getSkinProvider().loadSkin(instance.profile), baseX - 10, baseY + 1, 8, true, alpha);
|
||||||
|
|
||||||
|
Text t = Texts.join(List.of(new Text[] {
|
||||||
|
instance.damageReason,
|
||||||
|
// Optional.ofNullable(world.getPlayerByUuid(instance.profile.getId()))
|
||||||
|
// .map(PlayerEntity::getDisplayName)
|
||||||
|
// .orElse(Text.of(instance.profile.getName())),
|
||||||
|
}), Text.of(" / "));
|
||||||
|
|
||||||
|
var leftOffset = baseX - client.textRenderer.getWidth(t) - 12;
|
||||||
|
|
||||||
|
int color = (((int) (alpha * 255)) << 24) | 0xFFFFFF;
|
||||||
|
|
||||||
|
if ((alpha * 255) > 4) {
|
||||||
|
drawContext.drawText(client.textRenderer, t, leftOffset, baseY + 1, color, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import com.mojang.blaze3d.systems.RenderSystem;
|
||||||
|
import net.minecraft.client.gui.DrawContext;
|
||||||
|
import net.minecraft.client.gui.PlayerSkinDrawer;
|
||||||
|
import net.minecraft.client.render.*;
|
||||||
|
import net.minecraft.client.util.math.MatrixStack;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import org.joml.Matrix4f;
|
||||||
|
|
||||||
|
public class PlayerIconRenderer {
|
||||||
|
public static void draw(Identifier texture, int x, int y, int size, boolean hatVisible, float opacity) {
|
||||||
|
render(texture, x, y, size, size, 8.0f, 8, 8, 8, opacity);
|
||||||
|
if (hatVisible) {
|
||||||
|
drawHat(texture, x, y, size, opacity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void drawHat(Identifier texture, int x, int y, int size, float opacity) {
|
||||||
|
render(texture, x, y, size, size, 40.0f, 8, 8, 8, opacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void render(Identifier texture, int x, int y, int width, int height, float u, float v, int regionWidth, int regionHeight, float opacity) {
|
||||||
|
float u1 = u/64.0F;
|
||||||
|
float v1 = v/64.0F;
|
||||||
|
float u2 = u1 + (8.0F/64.0F);
|
||||||
|
float v2 = v1 + (8.0F/64.0F);
|
||||||
|
|
||||||
|
RenderSystem.setShaderTexture(0, texture);
|
||||||
|
RenderSystem.setShader(GameRenderer::getPositionColorTexProgram);
|
||||||
|
RenderSystem.enableBlend();
|
||||||
|
Matrix4f matrix4f = (new MatrixStack()).peek().getPositionMatrix();
|
||||||
|
BufferBuilder bufferBuilder = Tessellator.getInstance().getBuffer();
|
||||||
|
bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE);
|
||||||
|
bufferBuilder.vertex(matrix4f, x, y, 0).color(1.0F, 1.0F, 1.0F, opacity).texture(u1, v1).next();
|
||||||
|
bufferBuilder.vertex(matrix4f, x, y + height, 0).color(1.0F, 1.0F, 1.0F, opacity).texture(u1, v2).next();
|
||||||
|
bufferBuilder.vertex(matrix4f, x + width, y + height, 0).color(1.0F, 1.0F, 1.0F, opacity).texture(u2, v2).next();
|
||||||
|
bufferBuilder.vertex(matrix4f, x + width, y, 0).color(1.0F, 1.0F, 1.0F, opacity).texture(u2, v1).next();
|
||||||
|
BufferRenderer.drawWithGlobalProgram(bufferBuilder.end());
|
||||||
|
RenderSystem.disableBlend();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package ly.hall.rose.healthsync.mixin.client;
|
||||||
|
|
||||||
|
import net.minecraft.client.gui.DrawContext;
|
||||||
|
import net.minecraft.client.gui.hud.InGameHud;
|
||||||
|
import net.minecraft.scoreboard.ScoreboardObjective;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Mixin(InGameHud.class)
|
||||||
|
public class InGameHudSupportRenderingHealthsyncObjectives {
|
||||||
|
@Inject(at = @At("HEAD"), method = "renderScoreboardSidebar")
|
||||||
|
private void run(DrawContext context, ScoreboardObjective objective, CallbackInfo ci) {
|
||||||
|
// if (Objects.equals(objective.getCriterion().getName(), "healthsync_health_contributed") || Objects.equals(objective.getCriterion().getName(), "healthsync_damage_contributed")) {
|
||||||
|
// ci.cancel();
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,7 +3,7 @@
|
||||||
"package": "com.example.mixin.client",
|
"package": "com.example.mixin.client",
|
||||||
"compatibilityLevel": "JAVA_21",
|
"compatibilityLevel": "JAVA_21",
|
||||||
"client": [
|
"client": [
|
||||||
"ExampleClientMixin"
|
"ly.hall.rose.healthsync.mixin.client.ExampleClientMixin"
|
||||||
],
|
],
|
||||||
"injectors": {
|
"injectors": {
|
||||||
"defaultRequire": 1
|
"defaultRequire": 1
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
package com.example;
|
|
||||||
|
|
||||||
import net.fabricmc.api.ModInitializer;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
public class ExampleMod implements ModInitializer {
|
|
||||||
// This logger is used to write text to the console and the log file.
|
|
||||||
// It is considered best practice to use your mod id as the logger's name.
|
|
||||||
// That way, it's clear which mod wrote info, warnings, and errors.
|
|
||||||
public static final Logger LOGGER = LoggerFactory.getLogger("modid");
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onInitialize() {
|
|
||||||
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
|
||||||
// However, some things (like resources) may still be uninitialized.
|
|
||||||
// Proceed with mild caution.
|
|
||||||
|
|
||||||
LOGGER.info("Hello Fabric world!");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
package com.example.mixin;
|
|
||||||
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|
||||||
|
|
||||||
@Mixin(MinecraftServer.class)
|
|
||||||
public class ExampleMixin {
|
|
||||||
@Inject(at = @At("HEAD"), method = "loadWorld")
|
|
||||||
private void init(CallbackInfo info) {
|
|
||||||
// This code is injected into the start of MinecraftServer.loadWorld()V
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
|
||||||
|
public interface DamageCallback {
|
||||||
|
void handle(ServerPlayerEntity e, DamageSource source, float deltaHealth, float deltaAbsorption);
|
||||||
|
}
|
7
src/main/java/ly/hall/rose/healthsync/HealCallback.java
Normal file
7
src/main/java/ly/hall/rose/healthsync/HealCallback.java
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
|
||||||
|
public interface HealCallback {
|
||||||
|
void handle(ServerPlayerEntity e, LivingEntityHealReason reason, float deltaHealth, float deltaAbsorption);
|
||||||
|
}
|
294
src/main/java/ly/hall/rose/healthsync/HealthSync.java
Normal file
294
src/main/java/ly/hall/rose/healthsync/HealthSync.java
Normal file
|
@ -0,0 +1,294 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import com.mojang.authlib.GameProfile;
|
||||||
|
import com.mojang.brigadier.Command;
|
||||||
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
import net.fabricmc.api.ModInitializer;
|
||||||
|
|
||||||
|
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||||
|
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
|
||||||
|
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
|
||||||
|
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
|
||||||
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||||
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||||
|
import net.minecraft.command.CommandRegistryAccess;
|
||||||
|
import net.minecraft.command.CommandSource;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.entity.attribute.EntityAttributes;
|
||||||
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
import net.minecraft.entity.effect.StatusEffects;
|
||||||
|
import net.minecraft.nbt.NbtCompound;
|
||||||
|
import net.minecraft.registry.RegistryKeys;
|
||||||
|
import net.minecraft.scoreboard.ScoreboardCriterion;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.server.command.CommandManager;
|
||||||
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.world.GameMode;
|
||||||
|
import net.minecraft.world.PersistentState;
|
||||||
|
import net.minecraft.world.PersistentStateManager;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
|
||||||
|
|
||||||
|
class PlayerStats {
|
||||||
|
public float TotalDamageTaken;
|
||||||
|
public float TotalHealed;
|
||||||
|
}
|
||||||
|
|
||||||
|
record TaggedPlayerCoarseStats(String tag, int TotalHealthDamageTaken, int TotalHealthHealed) {}
|
||||||
|
|
||||||
|
class GlobalHealthSaverLoader extends PersistentState {
|
||||||
|
public static GlobalHealthSaverLoader getServerState(MinecraftServer server) {
|
||||||
|
PersistentStateManager psm = server.getWorld(World.OVERWORLD).getPersistentStateManager();
|
||||||
|
GlobalHealthSaverLoader state = psm.getOrCreate(GlobalHealthSaverLoader::createFromNbt, GlobalHealthSaverLoader::new, "healthsync");
|
||||||
|
state.markDirty();
|
||||||
|
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PlayerStats getPlayerStats(LivingEntity player) {
|
||||||
|
var serverState = getServerState(Objects.requireNonNull(player.getWorld().getServer()));
|
||||||
|
|
||||||
|
return serverState.players.computeIfAbsent(player.getUuid(), uuid -> new PlayerStats());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Stream<Map.Entry<UUID, PlayerStats>> getAllPlayerStats(MinecraftServer server) {
|
||||||
|
return getServerState(server).players.entrySet().stream();
|
||||||
|
}
|
||||||
|
|
||||||
|
public float GlobalHealth = 20;
|
||||||
|
public float GlobalAbsorption = 0;
|
||||||
|
|
||||||
|
public HashMap<UUID, PlayerStats> players = new HashMap<>();
|
||||||
|
|
||||||
|
public static GlobalHealthSaverLoader createFromNbt(NbtCompound tag) {
|
||||||
|
var s = new GlobalHealthSaverLoader();
|
||||||
|
s.GlobalHealth = tag.getFloat("healthsync:globalHealth");
|
||||||
|
s.GlobalAbsorption = tag.getFloat("healthsync:globalAbsorption");
|
||||||
|
NbtCompound playersNbt = tag.getCompound("healthsync:players");
|
||||||
|
playersNbt.getKeys().forEach(key -> {
|
||||||
|
PlayerStats playerData = new PlayerStats();
|
||||||
|
|
||||||
|
playerData.TotalHealed = playersNbt.getCompound(key).getFloat("totalhealed");
|
||||||
|
playerData.TotalDamageTaken = playersNbt.getCompound(key).getFloat("totaldamagetaken");
|
||||||
|
|
||||||
|
UUID uuid = UUID.fromString(key);
|
||||||
|
s.players.put(uuid, playerData);
|
||||||
|
});
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NbtCompound writeNbt(NbtCompound nbt) {
|
||||||
|
nbt.putFloat("healthsync:globalHealth", GlobalHealth);
|
||||||
|
nbt.putFloat("healthsync:globalAbsorption", GlobalAbsorption);
|
||||||
|
NbtCompound playersNbt = new NbtCompound();
|
||||||
|
players.forEach((uuid, playerData) -> {
|
||||||
|
NbtCompound playerNbt = new NbtCompound();
|
||||||
|
|
||||||
|
playerNbt.putFloat("totalhealed", playerData.TotalHealed);
|
||||||
|
playerNbt.putFloat("totaldamagetaken", playerData.TotalDamageTaken);
|
||||||
|
|
||||||
|
playersNbt.put(uuid.toString(), playerNbt);
|
||||||
|
});
|
||||||
|
nbt.put("healthsync:players", playersNbt);
|
||||||
|
return nbt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class HealthSync implements ModInitializer {
|
||||||
|
// This logger is used to write text to the console and the log file.
|
||||||
|
// It is considered best practice to use your mod id as the logger's name.
|
||||||
|
// That way, it's clear which mod wrote info, warnings, and errors.
|
||||||
|
public static final Logger LOGGER = LoggerFactory.getLogger("healthsync");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onInitialize() {
|
||||||
|
var healthHealedCriterion = ScoreboardCriterion.create("healthsync_health_contributed", true, ScoreboardCriterion.RenderType.INTEGER);
|
||||||
|
var damageTakenCriterion = ScoreboardCriterion.create("healthsync_damage_contributed", true, ScoreboardCriterion.RenderType.INTEGER);
|
||||||
|
|
||||||
|
CommandRegistrationCallback.EVENT.register((CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) -> {
|
||||||
|
LiteralArgumentBuilder<ServerCommandSource> builder = literal("healthsync");
|
||||||
|
LiteralArgumentBuilder<ServerCommandSource> builder2 = literal("scoreboard");
|
||||||
|
|
||||||
|
dispatcher.register(builder.then(builder2.executes(c -> {
|
||||||
|
try {
|
||||||
|
var source = c.getSource();
|
||||||
|
|
||||||
|
var stats = GlobalHealthSaverLoader.getAllPlayerStats(source.getServer())
|
||||||
|
.map(v -> new TaggedPlayerCoarseStats(
|
||||||
|
Objects.requireNonNull(source.getServer().getUserCache()).getByUuid(v.getKey()).map(GameProfile::getName).orElse(v.getKey().toString()),
|
||||||
|
(int) Math.ceil(v.getValue().TotalDamageTaken),
|
||||||
|
(int) Math.ceil(v.getValue().TotalHealed)
|
||||||
|
))
|
||||||
|
.sorted(Comparator.comparingInt(TaggedPlayerCoarseStats::TotalHealthDamageTaken))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
int maxNameLength = 0;
|
||||||
|
int maxHealLength = 0;
|
||||||
|
int maxDamageLength = 0;
|
||||||
|
|
||||||
|
for (TaggedPlayerCoarseStats stat : stats) {
|
||||||
|
maxNameLength = Math.max(maxNameLength, stat.tag().length());
|
||||||
|
maxHealLength = Math.max(maxHealLength, Float.toString(stat.TotalHealthHealed() / 2.0F).length());
|
||||||
|
maxDamageLength = Math.max(maxDamageLength, Float.toString(stat.TotalHealthDamageTaken() / 2.0F).length());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (TaggedPlayerCoarseStats stat : stats) {
|
||||||
|
source.sendMessage(Text.of(StringUtils.rightPad(stat.tag(), maxNameLength) + " | " + StringUtils.leftPad(Float.toString(stat.TotalHealthHealed() / 2.0F), maxHealLength) + "\uD83D\uDDA4 | " + StringUtils.leftPad(Float.toString(stat.TotalHealthDamageTaken() / 2.0F), maxDamageLength) + "\uD83D\uDC94"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} catch (Exception e) {
|
||||||
|
HealthSync.LOGGER.error(e.toString());
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
})));
|
||||||
|
});
|
||||||
|
|
||||||
|
LivingEntityDamageHandler.initializeGeneric();
|
||||||
|
|
||||||
|
LivingEntityHealHandler.handle((e, reason, deltaHealth, deltaAbsorption) -> {
|
||||||
|
var state = GlobalHealthSaverLoader.getServerState(Objects.requireNonNull(e.getServer()));
|
||||||
|
|
||||||
|
state.GlobalHealth += deltaHealth;
|
||||||
|
state.GlobalAbsorption += deltaAbsorption;
|
||||||
|
|
||||||
|
if (state.GlobalAbsorption < 0)
|
||||||
|
state.GlobalAbsorption = 0;
|
||||||
|
|
||||||
|
var stats = GlobalHealthSaverLoader.getPlayerStats(e);
|
||||||
|
|
||||||
|
stats.TotalHealed += deltaHealth + deltaAbsorption;
|
||||||
|
|
||||||
|
e.getScoreboard().forEachScore(healthHealedCriterion, e.getEntityName(), innerScore -> innerScore.setScore((int) stats.TotalHealed));
|
||||||
|
|
||||||
|
synchronizeHealth(Objects.requireNonNull(e.getServer()));
|
||||||
|
|
||||||
|
for (ServerPlayerEntity serverPlayerEntity : e.getServer().getPlayerManager().getPlayerList()) {
|
||||||
|
var buf = PacketByteBufs.create();
|
||||||
|
|
||||||
|
buf.writeUuid(e.getUuid());
|
||||||
|
|
||||||
|
buf.writeOptional(Optional.ofNullable(reason), (bufx, pos) -> {
|
||||||
|
assert reason != null;
|
||||||
|
bufx.writeString(reason.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
buf.writeFloat(deltaHealth);
|
||||||
|
buf.writeFloat(deltaAbsorption);
|
||||||
|
|
||||||
|
ServerPlayNetworking.send(serverPlayerEntity, NetworkingConstants.HEAL_EVENT_PACKET_ID, buf);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
LivingEntityDamageHandler.handle((e, source, deltaHealth, deltaAbsorption) -> {
|
||||||
|
var state = GlobalHealthSaverLoader.getServerState(Objects.requireNonNull(e.getServer()));
|
||||||
|
|
||||||
|
state.GlobalHealth += deltaHealth;
|
||||||
|
state.GlobalAbsorption += deltaAbsorption;
|
||||||
|
|
||||||
|
if (state.GlobalAbsorption < 0)
|
||||||
|
state.GlobalAbsorption = 0;
|
||||||
|
|
||||||
|
var stats = GlobalHealthSaverLoader.getPlayerStats(e);
|
||||||
|
|
||||||
|
stats.TotalDamageTaken += (-1 * deltaHealth) + (-1 * deltaAbsorption);
|
||||||
|
|
||||||
|
e.getScoreboard().forEachScore(damageTakenCriterion, e.getEntityName(), innerScore -> innerScore.setScore((int) stats.TotalDamageTaken));
|
||||||
|
|
||||||
|
synchronizeHealth(Objects.requireNonNull(e.getServer()));
|
||||||
|
|
||||||
|
for (ServerPlayerEntity serverPlayerEntity : e.getServer().getPlayerManager().getPlayerList()) {
|
||||||
|
var buf = PacketByteBufs.create();
|
||||||
|
|
||||||
|
buf.writeUuid(e.getUuid());
|
||||||
|
|
||||||
|
buf.writeOptional(Optional.ofNullable(source), (bufx, pos) -> {
|
||||||
|
assert source != null;
|
||||||
|
|
||||||
|
int sourceTypeId = e.getWorld().getRegistryManager().get(RegistryKeys.DAMAGE_TYPE).getRawId(source.getType());
|
||||||
|
int attackerId = source.getAttacker() == null ? 0 : (source.getAttacker().getId() + 1);
|
||||||
|
int sourceId = source.getSource() == null ? 0 : (source.getSource().getId() + 1);
|
||||||
|
|
||||||
|
bufx.writeInt(sourceTypeId);
|
||||||
|
bufx.writeInt(attackerId);
|
||||||
|
bufx.writeInt(sourceId);
|
||||||
|
|
||||||
|
bufx.writeOptional(Optional.ofNullable(source.getStoredPosition()), (bufxx, poss) -> {
|
||||||
|
bufxx.writeDouble(source.getStoredPosition().getX());
|
||||||
|
bufxx.writeDouble(source.getStoredPosition().getY());
|
||||||
|
bufxx.writeDouble(source.getStoredPosition().getZ());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
buf.writeFloat(deltaHealth);
|
||||||
|
buf.writeFloat(deltaAbsorption);
|
||||||
|
buf.writeBoolean(e.hasStatusEffect(StatusEffects.POISON));
|
||||||
|
buf.writeBoolean(e.hasStatusEffect(StatusEffects.WITHER));
|
||||||
|
buf.writeBoolean(e.isFrozen());
|
||||||
|
|
||||||
|
ServerPlayNetworking.send(serverPlayerEntity, NetworkingConstants.DAMAGE_EVENT_PACKET_ID, buf);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Object o = new Object();
|
||||||
|
|
||||||
|
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
||||||
|
synchronized(o) {
|
||||||
|
var state = GlobalHealthSaverLoader.getServerState(server);
|
||||||
|
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.Synchronization);
|
||||||
|
handler.player.setHealth(state.GlobalHealth);
|
||||||
|
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.Synchronization);
|
||||||
|
handler.player.setAbsorptionAmount(state.GlobalAbsorption);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> {
|
||||||
|
if (newPlayer.getWorld().getPlayers().stream().allMatch(e -> e.isDead() || e.getUuid().equals(newPlayer.getUuid()))) {
|
||||||
|
var state = GlobalHealthSaverLoader.getServerState(Objects.requireNonNull(newPlayer.getServer()));
|
||||||
|
|
||||||
|
state.GlobalHealth = 20;
|
||||||
|
state.GlobalAbsorption = 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void synchronizeHealth(MinecraftServer server) {
|
||||||
|
for (ServerPlayerEntity serverPlayerEntity : server.getPlayerManager().getPlayerList()) {
|
||||||
|
var state = GlobalHealthSaverLoader.getServerState(Objects.requireNonNull(server));
|
||||||
|
|
||||||
|
if (serverPlayerEntity.getHealth() < state.GlobalHealth) {
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.Synchronization);
|
||||||
|
serverPlayerEntity.setHealth(state.GlobalHealth);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serverPlayerEntity.getHealth() > state.GlobalHealth) {
|
||||||
|
serverPlayerEntity.setHealth(state.GlobalHealth);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serverPlayerEntity.getAbsorptionAmount() < state.GlobalAbsorption) {
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.Synchronization);
|
||||||
|
serverPlayerEntity.setAbsorptionAmount(state.GlobalAbsorption);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serverPlayerEntity.getAbsorptionAmount() > state.GlobalAbsorption) {
|
||||||
|
serverPlayerEntity.setAbsorptionAmount(state.GlobalAbsorption);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class LivingEntityDamageHandler {
|
||||||
|
private static Set<DamageCallback> callbackSet = new HashSet<>();
|
||||||
|
private static Set<LivingEntity> entitiesTakingDamage = new HashSet<>();
|
||||||
|
|
||||||
|
public static void initializeGeneric() {
|
||||||
|
ServerLivingEntityEvents.ALLOW_DAMAGE.register((LivingEntity e, DamageSource s, float a) -> {
|
||||||
|
var oldHealth = e.getHealth();
|
||||||
|
var oldAbsorption = e.getAbsorptionAmount();
|
||||||
|
|
||||||
|
entitiesTakingDamage.add(e);
|
||||||
|
|
||||||
|
LivingEntityPostDamageEvent.once(e, () -> {
|
||||||
|
var healthDelta = e.getHealth() - oldHealth;
|
||||||
|
var absorptionDelta = e.getAbsorptionAmount() - oldAbsorption;
|
||||||
|
|
||||||
|
entitiesTakingDamage.remove(e);
|
||||||
|
|
||||||
|
LivingEntityDamageHandler.fire(e, s, healthDelta, absorptionDelta);
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void fire(LivingEntity e, DamageSource source, float healthAmount, float absorptionAmount) {
|
||||||
|
if (!(e instanceof ServerPlayerEntity))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (source.isOf(ModDamageTypes.SYNCHRONIZATION_DAMAGE))
|
||||||
|
return;
|
||||||
|
|
||||||
|
callbackSet.forEach(cb -> cb.handle((ServerPlayerEntity) e, source, healthAmount, absorptionAmount));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void fireFromAbsorption(PlayerEntity e, float absorptionAmount) {
|
||||||
|
if (entitiesTakingDamage.contains(e))
|
||||||
|
return;
|
||||||
|
|
||||||
|
fire(e, ModDamageTypes.of(e.getWorld(), ModDamageTypes.ABSORPTION_CHANGED), 0, absorptionAmount);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void handle(DamageCallback cb) {
|
||||||
|
callbackSet.add(cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unhandle(DamageCallback cb) {
|
||||||
|
callbackSet.remove(cb);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class LivingEntityHealHandler {
|
||||||
|
private static Set<HealCallback> callbackSet = new HashSet<>();
|
||||||
|
private static LivingEntityHealReason appliedReason = null;
|
||||||
|
public static Set<ServerPlayerEntity> loadingPlayers = new HashSet<>();
|
||||||
|
|
||||||
|
public static void applyReason(LivingEntityHealReason reason) {
|
||||||
|
appliedReason = reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static LivingEntityHealReason consumeReason() {
|
||||||
|
var reason = appliedReason;
|
||||||
|
appliedReason = null;
|
||||||
|
return reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void fire(LivingEntity e, float healthAmount, float absorptionAmount) {
|
||||||
|
var reason = consumeReason();
|
||||||
|
|
||||||
|
if (!(e instanceof ServerPlayerEntity))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// handle setting health during construction
|
||||||
|
if (((ServerPlayerEntity) e).getGameProfile() == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (reason == LivingEntityHealReason.Synchronization)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (loadingPlayers.contains(e))
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (HealCallback healCallback : callbackSet) {
|
||||||
|
healCallback.handle((ServerPlayerEntity) e, reason, healthAmount, absorptionAmount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void handle(HealCallback cb) {
|
||||||
|
callbackSet.add(cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unhandle(HealCallback cb) {
|
||||||
|
callbackSet.remove(cb);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
public enum LivingEntityHealReason {
|
||||||
|
Generic,
|
||||||
|
NaturalRegeneration,
|
||||||
|
RegenerationStatusEffect,
|
||||||
|
InstantHealthStatusEffect,
|
||||||
|
AbsorptionStatusEffect,
|
||||||
|
Synchronization
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
class LivingEntityPostDamageEventHandlerKeyValuePair {
|
||||||
|
public LivingEntity entity;
|
||||||
|
public LivingEntityPostDamageHandler handler;
|
||||||
|
|
||||||
|
public LivingEntityPostDamageEventHandlerKeyValuePair(LivingEntity e, LivingEntityPostDamageHandler h) {
|
||||||
|
this.entity = e;
|
||||||
|
this.handler = h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LivingEntityPostDamageEvent {
|
||||||
|
private static Set<LivingEntityPostDamageEventHandlerKeyValuePair> handlers = new HashSet<LivingEntityPostDamageEventHandlerKeyValuePair>();
|
||||||
|
|
||||||
|
public static void once(LivingEntity e, LivingEntityPostDamageHandler handler) {
|
||||||
|
var keyValuePair = new LivingEntityPostDamageEventHandlerKeyValuePair(e, null);
|
||||||
|
|
||||||
|
keyValuePair.handler = () -> {
|
||||||
|
handlers.remove(keyValuePair);
|
||||||
|
handler.handle();
|
||||||
|
};
|
||||||
|
|
||||||
|
handlers.add(keyValuePair);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void emit(LivingEntity e) {
|
||||||
|
for (var handler : handlers) {
|
||||||
|
if (handler.entity.equals(e)) {
|
||||||
|
handler.handler.handle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
public interface LivingEntityPostDamageHandler {
|
||||||
|
public void handle();
|
||||||
|
}
|
17
src/main/java/ly/hall/rose/healthsync/ModDamageTypes.java
Normal file
17
src/main/java/ly/hall/rose/healthsync/ModDamageTypes.java
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
import net.minecraft.entity.damage.DamageType;
|
||||||
|
import net.minecraft.registry.RegistryKey;
|
||||||
|
import net.minecraft.registry.RegistryKeys;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
public class ModDamageTypes {
|
||||||
|
public static final RegistryKey<DamageType> SYNCHRONIZATION_DAMAGE = RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier("healthsync", "synchronization_damage"));
|
||||||
|
public static final RegistryKey<DamageType> ABSORPTION_CHANGED = RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier("healthsync", "absorption_changed"));
|
||||||
|
|
||||||
|
public static DamageSource of(World world, RegistryKey<DamageType> key) {
|
||||||
|
return new DamageSource(world.getRegistryManager().get(RegistryKeys.DAMAGE_TYPE).entryOf(key));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package ly.hall.rose.healthsync;
|
||||||
|
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
|
||||||
|
public class NetworkingConstants {
|
||||||
|
public static final Identifier HEAL_EVENT_PACKET_ID = new Identifier("healthsync", "heal_event");
|
||||||
|
public static final Identifier DAMAGE_EVENT_PACKET_ID = new Identifier("healthsync", "damage_event");
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package ly.hall.rose.healthsync.mixin;
|
||||||
|
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityPostDamageEvent;
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityPostDamageHandler;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Mixin(LivingEntity.class)
|
||||||
|
public class LivingEntityDamageCalculationMixin {
|
||||||
|
@Inject(at = @At("RETURN"), method = "damage")
|
||||||
|
private void postDamage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
|
||||||
|
LivingEntityPostDamageEvent.emit((LivingEntity)(Object)this);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package ly.hall.rose.healthsync.mixin.heal;
|
||||||
|
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityHealHandler;
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityHealReason;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.entity.attribute.AttributeContainer;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
|
||||||
|
@Mixin(targets = "net.minecraft.entity.effect.AbsorptionStatusEffect")
|
||||||
|
public class AbsorptionStatusEffectMixin {
|
||||||
|
@Inject(at = @At(value = "HEAD"), method = "onApplied")
|
||||||
|
private void handleRegen(LivingEntity entity, AttributeContainer attributes, int amplifier, CallbackInfo ci) {
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.AbsorptionStatusEffect);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package ly.hall.rose.healthsync.mixin.heal;
|
||||||
|
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityHealHandler;
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityHealReason;
|
||||||
|
import net.minecraft.entity.player.HungerManager;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
|
||||||
|
@Mixin(HungerManager.class)
|
||||||
|
public class HungerManagerMixin {
|
||||||
|
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;heal(F)V", ordinal = 0), method = "update")
|
||||||
|
private void handleRegen1(PlayerEntity player, CallbackInfo ci) {
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.NaturalRegeneration);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;heal(F)V", ordinal = 1), method = "update")
|
||||||
|
private void handleRegen2(PlayerEntity player, CallbackInfo ci) {
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.NaturalRegeneration);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package ly.hall.rose.healthsync.mixin.heal;
|
||||||
|
|
||||||
|
import ly.hall.rose.healthsync.*;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.entity.effect.StatusEffectInstance;
|
||||||
|
import net.minecraft.entity.effect.StatusEffects;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.util.math.MathHelper;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Overwrite;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
|
|
||||||
|
@Mixin(LivingEntity.class)
|
||||||
|
public class LivingEntityMixin {
|
||||||
|
@Inject(at = @At(value = "HEAD"), method = "setHealth")
|
||||||
|
private void handleHealthUpdate(float targetHealth, CallbackInfo ci) {
|
||||||
|
var currentHealth = ((LivingEntity)(Object)this).getHealth();
|
||||||
|
var difference = Float.valueOf(MathHelper.clamp(targetHealth, 0.0f, ((LivingEntity)(Object)this).getMaxHealth())) - currentHealth;
|
||||||
|
|
||||||
|
if (difference > 0) {
|
||||||
|
LivingEntityHealHandler.fire((LivingEntity)(Object)this, difference, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(at = @At("HEAD"), method = "onStatusEffectRemoved")
|
||||||
|
private void handleStatsuEffectRemoved(StatusEffectInstance effect, CallbackInfo ci) {
|
||||||
|
var self = (LivingEntity) (Object) this;
|
||||||
|
|
||||||
|
if (self instanceof ServerPlayerEntity) {
|
||||||
|
if (effect.getEffectType().equals(StatusEffects.ABSORPTION)) {
|
||||||
|
LivingEntityDamageHandler.fireFromAbsorption((ServerPlayerEntity) self, -self.getAbsorptionAmount());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package ly.hall.rose.healthsync.mixin.heal;
|
||||||
|
|
||||||
|
import ly.hall.rose.healthsync.*;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
|
|
||||||
|
@Mixin(PlayerEntity.class)
|
||||||
|
public class PlayerEntityMixin {
|
||||||
|
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;heal(F)V"), method = "tickMovement")
|
||||||
|
private void handleRegen(CallbackInfo ci) {
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.NaturalRegeneration);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(at = @At(value = "HEAD"), method = "setAbsorptionAmount")
|
||||||
|
private void handleAbsorptionUpdate(float targetAbsorption, CallbackInfo ci) {
|
||||||
|
var currentAbsorption = ((PlayerEntity)(Object)this).getAbsorptionAmount();
|
||||||
|
var difference = targetAbsorption - currentAbsorption;
|
||||||
|
|
||||||
|
if (difference > 0) {
|
||||||
|
LivingEntityHealHandler.fire((PlayerEntity)(Object)this, 0, difference);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (difference < 0) {
|
||||||
|
LivingEntityDamageHandler.fireFromAbsorption((PlayerEntity)(Object)this, difference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package ly.hall.rose.healthsync.mixin.heal;
|
||||||
|
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityHealHandler;
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityHealReason;
|
||||||
|
import net.minecraft.nbt.NbtCompound;
|
||||||
|
import net.minecraft.server.PlayerManager;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Mixin(PlayerManager.class)
|
||||||
|
public class PlayerManagerMixin {
|
||||||
|
@Inject(at = @At("HEAD"), method = "loadPlayerData")
|
||||||
|
public void onPlayerDataStart(ServerPlayerEntity player, CallbackInfoReturnable<NbtCompound> cir) {
|
||||||
|
LivingEntityHealHandler.loadingPlayers.add(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(at = @At("RETURN"), method = "loadPlayerData")
|
||||||
|
public void onPlayerDataEnd(ServerPlayerEntity player, CallbackInfoReturnable<NbtCompound> cir) {
|
||||||
|
LivingEntityHealHandler.loadingPlayers.remove(player);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package ly.hall.rose.healthsync.mixin.heal;
|
||||||
|
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityDamageHandler;
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityHealHandler;
|
||||||
|
import ly.hall.rose.healthsync.LivingEntityHealReason;
|
||||||
|
import net.minecraft.entity.Entity;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.entity.attribute.*;
|
||||||
|
import net.minecraft.entity.effect.StatusEffect;
|
||||||
|
import net.minecraft.entity.effect.StatusEffects;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Mixin(StatusEffect.class)
|
||||||
|
public class StatusEffectMixin {
|
||||||
|
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;heal(F)V"), method = "applyInstantEffect")
|
||||||
|
public void h1(Entity source, Entity attacker, LivingEntity target, int amplifier, double proximity, CallbackInfo ci) {
|
||||||
|
if (target instanceof ServerPlayerEntity) {
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.InstantHealthStatusEffect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;heal(F)V"), method = "applyUpdateEffect")
|
||||||
|
public void h1(LivingEntity entity, int amplifier, CallbackInfo ci) {
|
||||||
|
if (entity instanceof ServerPlayerEntity) {
|
||||||
|
if ((Object) this == StatusEffects.REGENERATION) {
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.RegenerationStatusEffect);
|
||||||
|
} else {
|
||||||
|
LivingEntityHealHandler.applyReason(LivingEntityHealReason.InstantHealthStatusEffect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
src/main/resources/assets/healthsync/icon.png
Normal file
BIN
src/main/resources/assets/healthsync/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 35 KiB |
Binary file not shown.
Before Width: | Height: | Size: 453 B |
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"message_id": "absorption_changed",
|
||||||
|
"exhaustion": 0,
|
||||||
|
"scaling": "never"
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"message_id": "synchronization_damage",
|
||||||
|
"exhaustion": 0,
|
||||||
|
"scaling": "never"
|
||||||
|
}
|
|
@ -1,41 +1,29 @@
|
||||||
{
|
{
|
||||||
"schemaVersion": 1,
|
"schemaVersion": 1,
|
||||||
"id": "modid",
|
"id": "healthsync",
|
||||||
"version": "${version}",
|
"version": "${version}",
|
||||||
"name": "Example mod",
|
"name": "HealthSync",
|
||||||
"description": "This is an example description! Tell everyone what your mod is about!",
|
"description": "Sync your health with other players",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Me!"
|
"Rose Kodsi-Hall"
|
||||||
],
|
],
|
||||||
"contact": {
|
"icon": "assets/healthsync/icon.png",
|
||||||
"homepage": "https://fabricmc.net/",
|
|
||||||
"sources": "https://github.com/FabricMC/fabric-example-mod"
|
|
||||||
},
|
|
||||||
"license": "CC0-1.0",
|
|
||||||
"icon": "assets/modid/icon.png",
|
|
||||||
"environment": "*",
|
"environment": "*",
|
||||||
"entrypoints": {
|
"entrypoints": {
|
||||||
"main": [
|
"main": [
|
||||||
"com.example.ExampleMod"
|
"ly.hall.rose.healthsync.HealthSync"
|
||||||
],
|
],
|
||||||
"client": [
|
"client": [
|
||||||
"com.example.ExampleModClient"
|
"ly.hall.rose.healthsync.HealthSyncClient"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"mixins": [
|
"mixins": [
|
||||||
"modid.mixins.json",
|
"healthsync.mixins.json"
|
||||||
{
|
|
||||||
"config": "modid.client.mixins.json",
|
|
||||||
"environment": "client"
|
|
||||||
}
|
|
||||||
],
|
],
|
||||||
"depends": {
|
"depends": {
|
||||||
"fabricloader": ">=0.15.10",
|
"fabricloader": ">=0.14.23",
|
||||||
"minecraft": "~1.20.6",
|
"minecraft": "~1.20.1",
|
||||||
"java": ">=21",
|
"java": ">=17",
|
||||||
"fabric-api": "*"
|
"fabric-api": "*"
|
||||||
},
|
|
||||||
"suggests": {
|
|
||||||
"another-mod": "*"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
17
src/main/resources/healthsync.mixins.json
Normal file
17
src/main/resources/healthsync.mixins.json
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"required": true,
|
||||||
|
"package": "ly.hall.rose.healthsync.mixin",
|
||||||
|
"compatibilityLevel": "JAVA_17",
|
||||||
|
"mixins": [
|
||||||
|
"LivingEntityDamageCalculationMixin",
|
||||||
|
"heal.AbsorptionStatusEffectMixin",
|
||||||
|
"heal.HungerManagerMixin",
|
||||||
|
"heal.LivingEntityMixin",
|
||||||
|
"heal.PlayerEntityMixin",
|
||||||
|
"heal.PlayerManagerMixin",
|
||||||
|
"heal.StatusEffectMixin"
|
||||||
|
],
|
||||||
|
"injectors": {
|
||||||
|
"defaultRequire": 1
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +0,0 @@
|
||||||
{
|
|
||||||
"required": true,
|
|
||||||
"package": "com.example.mixin",
|
|
||||||
"compatibilityLevel": "JAVA_21",
|
|
||||||
"mixins": [
|
|
||||||
"ExampleMixin"
|
|
||||||
],
|
|
||||||
"injectors": {
|
|
||||||
"defaultRequire": 1
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue