Better android charge handling

This commit is contained in:
DBotThePony 2021-08-19 11:23:49 +07:00
parent 01f3268e68
commit 6cc17216a3
Signed by: DBot
GPG Key ID: DCC23B5715498507
5 changed files with 21 additions and 37 deletions

View File

@ -63,8 +63,6 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
network_battery = ItemStack.EMPTY; network_battery = ItemStack.EMPTY;
} }
public static final BigDecimal ENERGY_FOR_HUNGER_POINT = new BigDecimal(200);
public AndroidCapability(LivingEntity ent) { public AndroidCapability(LivingEntity ent) {
this.ent = ent; this.ent = ent;
} }

View File

@ -182,6 +182,8 @@ public class AndroidCapabilityPlayer extends AndroidCapability {
MatteryNetworking.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) ent), packet); MatteryNetworking.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) ent), packet);
} }
public static final BigDecimal ENERGY_FOR_HUNGER_POINT = new BigDecimal(200);
@Override @Override
public void tickInner() { public void tickInner() {
super.tickInner(); super.tickInner();
@ -193,41 +195,22 @@ public class AndroidCapabilityPlayer extends AndroidCapability {
ply.setSwimming(false); ply.setSwimming(false);
} }
FoodData stats = ply.getFoodData(); var stats = ply.getFoodData();
if (stats.needsFood()) { while (stats.getFoodLevel() < 20) {
int food_points_missing = 20 - stats.getFoodLevel(); if (this.extractEnergyInner(ENERGY_FOR_HUNGER_POINT, true).compareTo(ENERGY_FOR_HUNGER_POINT) == 0) {
boolean food_changed = false; this.extractEnergyInner(ENERGY_FOR_HUNGER_POINT, false);
float saturation_points_missing = 20F - stats.getSaturationLevel(); stats.setFoodLevel(stats.getFoodLevel() + 1);
boolean saturation_changed = false; } else {
break;
while (food_points_missing > 0) {
if (this.extractEnergyInner(ENERGY_FOR_HUNGER_POINT, true).compareTo(ENERGY_FOR_HUNGER_POINT) == 0) {
this.extractEnergyInner(ENERGY_FOR_HUNGER_POINT, false);
food_points_missing--;
food_changed = true;
} else {
break;
}
} }
}
while (saturation_points_missing >= 1F) { var food_level = (float) stats.getFoodLevel();
if (this.extractEnergyInner(ENERGY_FOR_HUNGER_POINT, true).compareTo(ENERGY_FOR_HUNGER_POINT) == 0) {
this.extractEnergyInner(ENERGY_FOR_HUNGER_POINT, false);
saturation_points_missing--;
saturation_changed = true;
} else {
break;
}
}
if (food_changed) { if (stats.getSaturationLevel() < food_level) {
stats.setFoodLevel(20 - food_points_missing); BigDecimal extracted = this.extractEnergyInner(ENERGY_FOR_HUNGER_POINT.multiply(new BigDecimal(Float.toString(food_level - stats.getSaturationLevel()))), false);
} stats.setSaturation(stats.getSaturationLevel() + extracted.divide(ENERGY_FOR_HUNGER_POINT, MatteryCapability.ROUND_RULES).floatValue());
if (saturation_changed) {
stats.setSaturation(20F - saturation_points_missing);
}
} }
} }
} }

View File

@ -8,6 +8,7 @@ import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.TextComponent; import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraftforge.client.event.GuiOpenEvent; import net.minecraftforge.client.event.GuiOpenEvent;
@ -78,6 +79,8 @@ public class AndroidGui {
return; return;
} }
int y_offset = ply.hasEffect(MobEffects.HUNGER) ? 18 : 0;
float level; float level;
if (android.getMaxBatteryLevel().compareTo(BigDecimal.ZERO) == 0) { if (android.getMaxBatteryLevel().compareTo(BigDecimal.ZERO) == 0) {
@ -104,9 +107,9 @@ public class AndroidGui {
// Stack, x, y, blitOffset?, (float) image_x, (float) image_y, rect_x, rect_y, total_image_width, total_image_height // Stack, x, y, blitOffset?, (float) image_x, (float) image_y, rect_x, rect_y, total_image_width, total_image_height
// Stack, x, y, image_x, image_y, rect_x, rect_y // Stack, x, y, image_x, image_y, rect_x, rect_y
gui.blit(event.getMatrixStack(), left, top, 0, 0, 80, 9); gui.blit(event.getMatrixStack(), left, top, 0, y_offset, 80, 9);
int shift_left = (int) Math.ceil(level * 79f - 0.5f); int shift_left = (int) Math.ceil(level * 79f - 0.5f);
gui.blit(event.getMatrixStack(), left + 79 - shift_left, top, 0, 9, shift_left, 9); gui.blit(event.getMatrixStack(), left + 79 - shift_left, top, 0, y_offset + 9, shift_left, 9);
} }
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 841 B

After

Width:  |  Height:  |  Size: 932 B