Skip to content

Commit

Permalink
Small improvements 2
Browse files Browse the repository at this point in the history
cleanup
  • Loading branch information
Darkona committed Jun 10, 2024
1 parent 82cf6d1 commit 213d26a
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 73 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/darkona/feathers/FeathersManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public static void onPlayerCloned(PlayerEvent.Clone event) {
Player oldPlayer = event.getOriginal();
oldPlayer.reviveCaps();

event.getOriginal().getCapability(Capabilities.PLAYER_FEATHERS).ifPresent(oldStore -> event.getOriginal().getCapability(Capabilities.PLAYER_FEATHERS).ifPresent(newStore -> newStore.copyFrom(oldStore)));
event.getOriginal().getCapability(Capabilities.PLAYER_FEATHERS)
.ifPresent(oldStore -> event.getOriginal().getCapability(Capabilities.PLAYER_FEATHERS).ifPresent(newStore -> newStore.copyFrom(oldStore)));

oldPlayer.invalidateCaps();
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/darkona/feathers/api/ICapabilityPlugin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.darkona.feathers.api;

import com.darkona.feathers.event.FeatherEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.EntityJoinLevelEvent;

Expand All @@ -14,8 +13,5 @@ public interface ICapabilityPlugin {

void onPlayerTickAfter(TickEvent.PlayerTickEvent event);

void onFeathersUsed(FeatherEvent.Use event);



}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static RegistryObject<Attribute> registerAttribute(String name, Function<
}

public static RegistryObject<Attribute> registerAttribute(String name, Function<String, Attribute> attribute, UUID uuid) {
RegistryObject<Attribute> registryObject = ATTRIBUTES.register(name,() -> attribute.apply(name));
RegistryObject<Attribute> registryObject = ATTRIBUTES.register(name, () -> attribute.apply(name));
UUIDS.put(registryObject, uuid);
return registryObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,18 @@ public Double getCounter(String name) {
}

public void setCounter(String name, double value) {
if(name.length() > 16) name = name.substring(0, 16);
if (name.length() > 16) name = name.substring(0, 16);
counters.put(name, value);
}

public void incrementCounterBy(String name, double amount){
counters.computeIfPresent(name, (k,v) -> v + amount);
public void incrementCounterBy(String name, double amount) {
counters.computeIfPresent(name, (k, v) -> v + amount);
}

public void multiplyCounterBy(String name, double amount){
counters.computeIfPresent(name, (k,v) -> v * amount);
public void multiplyCounterBy(String name, double amount) {
counters.computeIfPresent(name, (k, v) -> v * amount);
}

/* Markers */
@Override
public int getMaxFeathers() {
Expand Down Expand Up @@ -309,7 +310,7 @@ public void tick(Player player) {
doStaminaChange(player);
}

if(player.tickCount % 20 == 0){
if (player.tickCount % 20 == 0) {
FeathersMessages.sendToPlayer(new FeatherSTCSyncPacket(this), player);
}
}
Expand All @@ -327,7 +328,7 @@ private void doStaminaChange(Player player) {
var preChangeEvent = new StaminaChangeEvent.Pre(player, staminaDelta, stamina);
if (MinecraftForge.EVENT_BUS.post(preChangeEvent)) return;

if (preChangeEvent.getResult() == Event.Result.DEFAULT){
if (preChangeEvent.getResult() == Event.Result.DEFAULT) {

staminaDelta = preChangeEvent.staminaDelta;
stamina = preChangeEvent.stamina;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static ClientFeathersData getInstance() {
return instance;
}

public void update(Player player, IFeathers f){
public void update(Player player, IFeathers f) {
stamina = f.getStamina();
maxStamina = f.getMaxStamina();
feathers = f.getFeathers();
Expand All @@ -61,8 +61,8 @@ public void update(Player player, IFeathers f){

synchronizeEffects(player);

strainFeathers = (int)Math.ceil(f.getCounter(StrainEffect.STRAIN_COUNTER));
enduranceFeathers = (int)Math.ceil(f.getCounter(EnduranceEffect.ENDURANCE_COUNTER));
strainFeathers = (int) Math.ceil(f.getCounter(StrainEffect.STRAIN_COUNTER));
enduranceFeathers = (int) Math.ceil(f.getCounter(EnduranceEffect.ENDURANCE_COUNTER));
}

public void update(FeatherSTCSyncPacket message, Supplier<NetworkEvent.Context> supplier) {
Expand Down Expand Up @@ -108,7 +108,7 @@ public boolean isOverflowing() {
public void tick() {
var player = Minecraft.getInstance().player;

if(player != null)
if (player != null)
player.getCapability(Capabilities.PLAYER_FEATHERS).ifPresent(f -> update(player, f));

if (animationCooldown > 0) animationCooldown--;
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/com/darkona/feathers/commands/FeathersCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,29 @@ private void feathersCommands(LiteralArgumentBuilder<CommandSourceStack> stack)
IntegerArgumentType.getInteger(source, "amount"))
)));
}

private int executeGetFeathers(CommandSourceStack source, Collection<? extends Entity> entities) {
Integer x = checkError(source, entities);
for (Entity entity : entities) {
if (entity instanceof ServerPlayer player) {
source.sendSuccess(() -> Component.literal(
String.format("%s has %d feathers, %d available.",
player.getName().getString(),
FeathersAPI.getFeathers(player),
FeathersAPI.getAvailableFeathers(player))),
String.format("%s has %d feathers, %d available.",
player.getName().getString(),
FeathersAPI.getFeathers(player),
FeathersAPI.getAvailableFeathers(player))),
true);
}
}
return entities.size();
}

private int executeSetFeathers(CommandSourceStack source, Collection<? extends Entity> entities, int amount) {
Integer x = checkError(source, entities);
if (x != null) return x;
for (Entity entity : entities) {
if (entity instanceof ServerPlayer player) {
source.sendSuccess(() -> Component.literal(
String.format("Set %s feathers to %d", player.getName().getString(), FeathersAPI.setFeathers(player, amount))),true);
String.format("Set %s feathers to %d", player.getName().getString(), FeathersAPI.setFeathers(player, amount))), true);
}
}
return entities.size();
Expand All @@ -89,29 +91,32 @@ private void maxFeathersCommands(LiteralArgumentBuilder<CommandSourceStack> stac
executeGetMaxFeathers(source.getSource(), EntityArgument.getEntities(source, "entities"))
)));
}

private int executeGetMaxFeathers(CommandSourceStack source, Collection<? extends Entity> entities) {

Integer x = checkError(source, entities);

for (Entity entity : entities) {
if (entity instanceof ServerPlayer player) {
source.sendSuccess(() -> Component.literal(
String.format("%s has a maximum of %d feathers",
player.getName(),
FeathersAPI.getPlayerMaxFeathers(player))),
String.format("%s has a maximum of %d feathers",
player.getName(),
FeathersAPI.getPlayerMaxFeathers(player))),
true);
}
}
return entities.size();
}

private int executeSetMaxFeathers(CommandSourceStack source, Collection<? extends Entity> entities, int amount) {
Integer x = checkError(source, entities);
if (x != null) return x;
for (Entity entity : entities) {
if (entity instanceof ServerPlayer player) {
FeathersAPI.setMaxFeathers(player, amount);
source.sendSuccess(() -> Component.literal(
String.format("Set maximum feathers to %d feathers for %s",FeathersAPI.getPlayerMaxFeathers(player),player.getName().getString())),true);
String.format("Set maximum feathers to %d feathers for %s", FeathersAPI.getPlayerMaxFeathers(player), player.getName()
.getString())), true);
}
}
return entities.size();
Expand All @@ -132,6 +137,7 @@ private void regenCommands(LiteralArgumentBuilder<CommandSourceStack> stack) {
DoubleArgumentType.getDouble(source, "amount"))
)));
}

private int executeGetFeatherRegen(CommandSourceStack source, Collection<? extends Entity> entities) {
Integer x = checkError(source, entities);
for (Entity entity : entities) {
Expand All @@ -142,6 +148,7 @@ private int executeGetFeatherRegen(CommandSourceStack source, Collection<? exten
}
return entities.size();
}

private int executeSetFeatherRegen(CommandSourceStack source, Collection<? extends Entity> entities, double amount) {
Integer x = checkError(source, entities);
if (x != null) return x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ public void onPlayerTickBefore(TickEvent.PlayerTickEvent event) {
public void onPlayerTickAfter(TickEvent.PlayerTickEvent event) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
public class ThirstManager implements ICapabilityPlugin {


private static ICapabilityPlugin instance;
private static final UUID QUENCH_UUID = UUID.fromString("08c665cf-0c4a-4f87-92da-c63972f19b73");
private static final UUID THIRST_UUID = UUID.fromString("9f2141c4-33bc-4245-97bd-b64a3eceafda");

private static final String LAST_THIRST_LEVEL = "lastThirst";
private static final String LAST_QUENCH_LEVEL = "lastQuench";
private static final String THIRST_FEATHER_COUNTER = "thirstFeather";
private static final String THIRST_ACCUMULATOR = "thirstAcc";
private static ICapabilityPlugin instance;

public static ICapabilityPlugin getInstance() {
if (instance == null) {
Expand All @@ -45,6 +44,26 @@ public static ICapabilityPlugin getInstance() {
return instance;
}

@SubscribeEvent
public static void onStaminaChanged(StaminaChangeEvent.Post event) {
var player = event.getEntity();
var f = event.feathers;
if (f.getStamina() > f.getPrevStamina()) {

f.incrementCounterBy(THIRST_FEATHER_COUNTER, f.getStaminaDelta());

if (f.getCounter(THIRST_FEATHER_COUNTER) >= Constants.STAMINA_PER_FEATHER) {
f.incrementCounterBy(THIRST_FEATHER_COUNTER, -Constants.STAMINA_PER_FEATHER);
f.incrementCounterBy(THIRST_ACCUMULATOR, FeathersThirstConfig.THIRST_CONSUMPTION_BY_FEATHER.get());
}

if (f.getCounter(THIRST_ACCUMULATOR) >= 1) {
f.incrementCounterBy(THIRST_ACCUMULATOR, -1);
player.getCapability(PLAYER_THIRST).ifPresent(thirst -> thirst.setThirst(thirst.getThirst() - 1));
}
}
}

@Override
public void onPlayerJoin(EntityJoinLevelEvent event) {
if (event.getEntity() instanceof Player player) {
Expand Down Expand Up @@ -122,26 +141,6 @@ public void onPlayerTickAfter(TickEvent.PlayerTickEvent event) {

}

@SubscribeEvent
public static void onStaminaChanged(StaminaChangeEvent.Post event) {
var player = event.getEntity();
var f = event.feathers;
if (f.getStamina() > f.getPrevStamina()) {

f.incrementCounterBy(THIRST_FEATHER_COUNTER, f.getStaminaDelta());

if (f.getCounter(THIRST_FEATHER_COUNTER) >= Constants.STAMINA_PER_FEATHER) {
f.incrementCounterBy(THIRST_FEATHER_COUNTER, -Constants.STAMINA_PER_FEATHER);
f.incrementCounterBy(THIRST_ACCUMULATOR, FeathersThirstConfig.THIRST_CONSUMPTION_BY_FEATHER.get());
}

if (f.getCounter(THIRST_ACCUMULATOR) >= 1) {
f.incrementCounterBy(THIRST_ACCUMULATOR, -1);
player.getCapability(PLAYER_THIRST).ifPresent(thirst -> thirst.setThirst(thirst.getThirst() - 1));
}
}
}

public static class ThirstEvent extends PlayerEvent {

public double calculationResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void removeAttributeModifiers(@NotNull LivingEntity target, @NotNull Attr
}

@Override
public boolean canApply(Player player){
public boolean canApply(Player player) {
return super.canApply(player) && FeathersCommonConfig.ENABLE_FATIGUE.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void removeAttributeModifiers(@NotNull LivingEntity target, @NotNull Attr
}

@Override
public boolean canApply(Player player){
public boolean canApply(Player player) {
return super.canApply(player) && FeathersCommonConfig.ENABLE_MOMENTUM.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void onRemove(PlayerFeathers iFeathers) {

@Override
public void applyToDelta(Player player, PlayerFeathers f, AtomicInteger staminaDelta) {
int currentStrain = (int)Math.ceil(f.getCounter(STRAIN_COUNTER));
int currentStrain = (int) Math.ceil(f.getCounter(STRAIN_COUNTER));
if (currentStrain > 0) {
int recover = currentStrain - staminaDelta.get();
if (recover <= 0) {
Expand All @@ -57,7 +57,7 @@ public void applyToDelta(Player player, PlayerFeathers f, AtomicInteger staminaD
public void applyToUsage(Player player, PlayerFeathers f, AtomicInteger staminaToUse, AtomicBoolean approve) {
if (approve.get()) return;
int use = f.getAvailableStamina() - staminaToUse.get();
int strain = (int)Math.ceil(f.getCounter(STRAIN_COUNTER));
int strain = (int) Math.ceil(f.getCounter(STRAIN_COUNTER));
int maxStrainStamina = FeathersCommonConfig.MAX_STRAIN.get() * Constants.STAMINA_PER_FEATHER;

if (use < 0 && (strain - use <= maxStrainStamina)) {
Expand Down Expand Up @@ -106,7 +106,7 @@ public void removeAttributeModifiers(@NotNull LivingEntity target, @NotNull Attr
}

@Override
public boolean canApply(Player player){
public boolean canApply(Player player) {
return super.canApply(player) && FeathersCommonConfig.ENABLE_STRAIN.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public FeatherGainCTSPacket(FriendlyByteBuf buf) {
this.feathersToGain = buf.readInt();
}

public void toBytes(FriendlyByteBuf buf) {
buf.writeInt(feathersToGain);
}

public static void handle(FeatherGainCTSPacket message, Supplier<NetworkEvent.Context> contextSupplier) {
NetworkEvent.Context context = contextSupplier.get();
if (context.getDirection().getReceptionSide().isServer()) {
Expand All @@ -37,4 +33,8 @@ public static void handle(FeatherGainCTSPacket message, Supplier<NetworkEvent.Co
}
context.setPacketHandled(true);
}

public void toBytes(FriendlyByteBuf buf) {
buf.writeInt(feathersToGain);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public FeatherSTCSyncPacket(FriendlyByteBuf buf) {
}
}

public static void handle(FeatherSTCSyncPacket message, Supplier<NetworkEvent.Context> contextSupplier) {
NetworkEvent.Context context = contextSupplier.get();
if (context.getDirection().getReceptionSide().isClient()) {
context.enqueueWork(() -> DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> FeatherSTCSyncPacket.handle(message, contextSupplier)));
}
}

public void toBytes(FriendlyByteBuf buf) {
buf.writeInt(stamina);
buf.writeInt(maxStamina);
Expand All @@ -67,11 +74,4 @@ public void toBytes(FriendlyByteBuf buf) {
buf.writeDouble(v);
});
}

public static void handle(FeatherSTCSyncPacket message, Supplier<NetworkEvent.Context> contextSupplier) {
NetworkEvent.Context context = contextSupplier.get();
if (context.getDirection().getReceptionSide().isClient()) {
context.enqueueWork(() -> DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> FeatherSTCSyncPacket.handle(message, contextSupplier)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ public FeatherSpendCTSPacket(int feathersToSpend, int cooldown) {
this.cooldown = cooldown;
}

public void toBytes(FriendlyByteBuf buf) {
buf.writeInt(feathersToSpend);
buf.writeInt(cooldown);
}

public FeatherSpendCTSPacket(FriendlyByteBuf buf) {
this.feathersToSpend = buf.readInt();
this.cooldown = buf.readInt();
Expand All @@ -35,7 +30,7 @@ public static void handle(FeatherSpendCTSPacket message, Supplier<NetworkEvent.C
if (context.getDirection().getReceptionSide().isServer()) {
context.enqueueWork(() -> {
Player player = context.getSender();
if(player != null) {
if (player != null) {
Level level = player.level();
FeathersAPI.spendFeathers(player, message.feathersToSpend, message.cooldown);
}
Expand All @@ -44,4 +39,9 @@ public static void handle(FeatherSpendCTSPacket message, Supplier<NetworkEvent.C

context.setPacketHandled(true);
}

public void toBytes(FriendlyByteBuf buf) {
buf.writeInt(feathersToSpend);
buf.writeInt(cooldown);
}
}
Loading

0 comments on commit 213d26a

Please sign in to comment.