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;
}
public static final BigDecimal ENERGY_FOR_HUNGER_POINT = new BigDecimal(200);
public AndroidCapability(LivingEntity ent) {
this.ent = ent;
}

View File

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

View File

@ -8,6 +8,7 @@ import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.client.event.GuiOpenEvent;
@ -78,6 +79,8 @@ public class AndroidGui {
return;
}
int y_offset = ply.hasEffect(MobEffects.HUNGER) ? 18 : 0;
float level;
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, 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);
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