refactor sanity and insanity commands; add max sanity handling and improve syncing logic
This commit is contained in:
@@ -15,27 +15,54 @@ public class SanityCommand {
|
|||||||
dispatcher.register(Commands.literal("sanity")
|
dispatcher.register(Commands.literal("sanity")
|
||||||
.requires(source -> source.hasPermission(2))
|
.requires(source -> source.hasPermission(2))
|
||||||
.then(Commands.literal("set")
|
.then(Commands.literal("set")
|
||||||
.then(Commands.argument("sanity", FloatArgumentType.floatArg(0, 80))
|
.then(Commands.argument("sanity", FloatArgumentType.floatArg(0))
|
||||||
.then(Commands.argument("insanity", FloatArgumentType.floatArg(0, 80))
|
.executes(ctx -> {
|
||||||
.executes(ctx -> {
|
ServerPlayer player = ctx.getSource().getPlayerOrException();
|
||||||
ServerPlayer player = ctx.getSource().getPlayer();
|
float requestedSanity = FloatArgumentType.getFloat(ctx, "sanity");
|
||||||
float sanity = FloatArgumentType.getFloat(ctx, "sanity");
|
final float[] appliedSanity = new float[] {0f};
|
||||||
float insanity = FloatArgumentType.getFloat(ctx, "insanity");
|
player.getCapability(PlayerCapabilityProvider.SANITY).ifPresent(cap -> {
|
||||||
assert player != null;
|
float clampedSanity = Math.min(cap.getMaxSanity(), requestedSanity);
|
||||||
player.getCapability(PlayerCapabilityProvider.SANITY).ifPresent(cap -> {
|
if (clampedSanity > 0f && cap.getInsanity() > 0f) {
|
||||||
cap.setSanity(sanity);
|
cap.setInsanity(0f);
|
||||||
cap.setInsanity(insanity);
|
}
|
||||||
});
|
cap.setSanity(clampedSanity);
|
||||||
|
appliedSanity[0] = cap.getSanity();
|
||||||
|
});
|
||||||
|
|
||||||
ctx.getSource().sendSuccess(() ->
|
ctx.getSource().sendSuccess(() ->
|
||||||
net.minecraft.network.chat.Component.literal(
|
net.minecraft.network.chat.Component.literal("Set sanity = " + appliedSanity[0]),
|
||||||
"Set sanity = " + sanity + ", insanity = " + insanity),
|
true
|
||||||
true
|
);
|
||||||
);
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
dispatcher.register(Commands.literal("insanity")
|
||||||
|
.requires(source -> source.hasPermission(2))
|
||||||
|
.then(Commands.literal("set")
|
||||||
|
.then(Commands.argument("insanity", FloatArgumentType.floatArg(0))
|
||||||
|
.executes(ctx -> {
|
||||||
|
ServerPlayer player = ctx.getSource().getPlayerOrException();
|
||||||
|
float requestedInsanity = FloatArgumentType.getFloat(ctx, "insanity");
|
||||||
|
final float[] appliedInsanity = new float[] {0f};
|
||||||
|
player.getCapability(PlayerCapabilityProvider.SANITY).ifPresent(cap -> {
|
||||||
|
float clampedInsanity = Math.min(cap.getMaxSanity(), requestedInsanity);
|
||||||
|
if (clampedInsanity > 0f && cap.getSanity() > 0f) {
|
||||||
|
cap.setSanity(0f);
|
||||||
|
}
|
||||||
|
cap.setInsanity(clampedInsanity);
|
||||||
|
appliedInsanity[0] = cap.getInsanity();
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.getSource().sendSuccess(() ->
|
||||||
|
net.minecraft.network.chat.Component.literal("Set insanity = " + appliedInsanity[0]),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
})
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.fretux.mindmotion.event;
|
package net.fretux.mindmotion.event;
|
||||||
|
|
||||||
import net.fretux.mindmotion.player.PlayerCapabilityProvider;
|
import net.fretux.mindmotion.player.PlayerCapabilityProvider;
|
||||||
|
import net.fretux.mindmotion.player.SanityCapability;
|
||||||
import net.minecraftforge.event.entity.living.LivingHurtEvent;
|
import net.minecraftforge.event.entity.living.LivingHurtEvent;
|
||||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
@@ -21,8 +22,17 @@ public class CommonEvents {
|
|||||||
event.getEntity().getCapability(PlayerCapabilityProvider.SANITY).ifPresent(newCap ->
|
event.getEntity().getCapability(PlayerCapabilityProvider.SANITY).ifPresent(newCap ->
|
||||||
event.getOriginal().getCapability(PlayerCapabilityProvider.SANITY)
|
event.getOriginal().getCapability(PlayerCapabilityProvider.SANITY)
|
||||||
.ifPresent(oldCap -> {
|
.ifPresent(oldCap -> {
|
||||||
newCap.setSanity(oldCap.getSanity());
|
if (newCap instanceof SanityCapability newImpl && oldCap instanceof SanityCapability oldImpl) {
|
||||||
newCap.setInsanity(oldCap.getInsanity());
|
newImpl.setBaseMaxSanity(oldImpl.getBaseMaxSanity());
|
||||||
|
newImpl.setBonusMaxSanity(oldImpl.getBonusMaxSanity());
|
||||||
|
}
|
||||||
|
if (event.isWasDeath()) {
|
||||||
|
newCap.setInsanity(0f);
|
||||||
|
newCap.setSanity(newCap.getMaxSanity());
|
||||||
|
} else {
|
||||||
|
newCap.setSanity(oldCap.getSanity());
|
||||||
|
newCap.setInsanity(oldCap.getInsanity());
|
||||||
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
event.getEntity().getCapability(PlayerCapabilityProvider.TEMPO).ifPresent(newCap ->
|
event.getEntity().getCapability(PlayerCapabilityProvider.TEMPO).ifPresent(newCap ->
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ public class PlayerCapabilityProvider {
|
|||||||
@Override
|
@Override
|
||||||
public CompoundTag serializeNBT() {
|
public CompoundTag serializeNBT() {
|
||||||
CompoundTag tag = new CompoundTag();
|
CompoundTag tag = new CompoundTag();
|
||||||
|
tag.putFloat("BaseMaxSanity", instance.getBaseMaxSanity());
|
||||||
|
tag.putFloat("BonusMaxSanity", instance.getBonusMaxSanity());
|
||||||
tag.putFloat("Sanity", instance.getSanity());
|
tag.putFloat("Sanity", instance.getSanity());
|
||||||
tag.putFloat("Insanity", instance.getInsanity());
|
tag.putFloat("Insanity", instance.getInsanity());
|
||||||
return tag;
|
return tag;
|
||||||
@@ -47,6 +49,12 @@ public class PlayerCapabilityProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deserializeNBT(CompoundTag nbt) {
|
public void deserializeNBT(CompoundTag nbt) {
|
||||||
|
if (nbt.contains("BaseMaxSanity")) {
|
||||||
|
instance.setBaseMaxSanity(nbt.getFloat("BaseMaxSanity"));
|
||||||
|
}
|
||||||
|
if (nbt.contains("BonusMaxSanity")) {
|
||||||
|
instance.setBonusMaxSanity(nbt.getFloat("BonusMaxSanity"));
|
||||||
|
}
|
||||||
instance.setSanity(nbt.getFloat("Sanity"));
|
instance.setSanity(nbt.getFloat("Sanity"));
|
||||||
instance.setInsanity(nbt.getFloat("Insanity"));
|
instance.setInsanity(nbt.getFloat("Insanity"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ public class SanityCapability implements ISanity {
|
|||||||
setInsanity(this.insanity);
|
setInsanity(this.insanity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float getBaseMaxSanity() {
|
||||||
|
return baseMaxSanity;
|
||||||
|
}
|
||||||
|
|
||||||
public void setBonusMaxSanity(float bonusMaxSanity) {
|
public void setBonusMaxSanity(float bonusMaxSanity) {
|
||||||
this.bonusMaxSanity = Math.max(0f, bonusMaxSanity);
|
this.bonusMaxSanity = Math.max(0f, bonusMaxSanity);
|
||||||
clampToMax();
|
clampToMax();
|
||||||
|
|||||||
Reference in New Issue
Block a user