Better sleep conversion process

This commit is contained in:
DBotThePony 2021-12-03 12:34:56 +07:00
parent c9d3212a82
commit 9e1650c70d
Signed by: DBot
GPG Key ID: DCC23B5715498507
5 changed files with 112 additions and 15 deletions

View File

@ -185,12 +185,14 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
}
@SubscribeEvent
@SuppressWarnings("unused")
public static void onLivingTick(LivingEvent.LivingUpdateEvent event) {
var ent = event.getEntity();
ent.getCapability(MatteryCapability.ANDROID).ifPresent(ent.level.isClientSide ? IAndroidCapability::tickClient : IAndroidCapability::tick);
}
@SubscribeEvent(priority = EventPriority.LOWEST)
@SuppressWarnings("unused")
public static void onHurtEvent(LivingHurtEvent event) {
if (event.isCanceled())
return;

View File

@ -249,7 +249,21 @@ public class AndroidCapabilityPlayer extends AndroidCapability {
public static final BigDecimal ENERGY_FOR_HUNGER_POINT = new BigDecimal(1000);
private int sleep_ticks = 0;
public int sleep_ticks = 0;
public static final int SLEEP_TICKS_LIMIT = 80;
@Override
protected void tickInnerClientAlways() {
super.tickInnerClientAlways();
if (will_become_android) {
if (ent.isSleeping()) {
sleep_ticks++;
} else {
sleep_ticks = 0;
}
}
}
@Override
protected void tickServerAlways() {
@ -259,7 +273,7 @@ public class AndroidCapabilityPlayer extends AndroidCapability {
if (ent.isSleeping()) {
sleep_ticks++;
if (sleep_ticks > 40) {
if (sleep_ticks > SLEEP_TICKS_LIMIT) {
becomeAndroid();
sleep_ticks = 0;

View File

@ -2,23 +2,33 @@ package ru.dbotthepony.mc.otm.client;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.DeathScreen;
import net.minecraft.client.gui.screens.InBedChatScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.player.LocalPlayer;
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.ScreenEvent;
import net.minecraftforge.client.event.ScreenOpenEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.gui.ForgeIngameGui;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import ru.dbotthepony.mc.otm.capability.android.AndroidCapabilityPlayer;
import ru.dbotthepony.mc.otm.capability.android.IAndroidCapability;
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
import java.math.BigDecimal;
import java.util.Optional;
import java.util.Random;
public class AndroidGui {
private static final Minecraft mc = Minecraft.getInstance();
@ -26,23 +36,94 @@ public class AndroidGui {
public static final ResourceLocation PLAYER_GUI_LOCATION = new ResourceLocation(OverdriveThatMatters.MOD_ID, "textures/gui/player_gui.png");
private IAndroidCapability last_state;
private AndroidCapabilityPlayer last_state;
@SubscribeEvent
public static void onOpenGUI(ScreenOpenEvent event) {
if (!(event.getScreen() instanceof DeathScreen screen))
private static int known_button_x = -1;
private static int known_button_y = -1;
private static Button known_button;
private static InBedChatScreen known_button_screen;
private static final Random button_shaker = new Random();
@SubscribeEvent(priority = EventPriority.NORMAL)
@SuppressWarnings("unused")
public void onTick(ScreenEvent.DrawScreenEvent.Pre event) {
if (known_button_screen != null && known_button == null) {
for (var widget : known_button_screen.renderables) {
if (widget instanceof Button btn) {
known_button_x = btn.x;
known_button_y = btn.y;
known_button = btn;
break;
}
}
if (known_button == null) {
known_button_screen = null;
}
}
if (known_button_screen != null && known_button_screen != mc.screen) {
known_button_x = -1;
known_button_y = -1;
known_button = null;
known_button_screen = null;
}
if (known_button_screen == null || mc.player == null) {
return;
}
mc.player.getCapability(MatteryCapability.ANDROID).ifPresent(cap -> {
if (!((AndroidCapabilityPlayer) cap).will_become_android) {
known_button.x = known_button_x;
known_button.y = known_button_y;
known_button_x = -1;
known_button_y = -1;
known_button = null;
known_button_screen = null;
return;
}
int dispersion = (int) ((10.0 * Math.max(0, ((AndroidCapabilityPlayer) cap).sleep_ticks - 20)) / (AndroidCapabilityPlayer.SLEEP_TICKS_LIMIT - 20));
known_button.x = known_button_x - dispersion / 2 + (int) (button_shaker.nextDouble() * dispersion);
known_button.y = known_button_y - dispersion / 2 + (int) (button_shaker.nextDouble() * dispersion);
});
}
@SubscribeEvent(priority = EventPriority.LOWEST)
@SuppressWarnings("unused")
public static void onOpenGUI(ScreenOpenEvent event) {
known_button_x = -1;
known_button_y = -1;
known_button = null;
known_button_screen = null;
if (mc.player == null)
return;
mc.player.getCapability(MatteryCapability.ANDROID).ifPresent(cap -> {
if (cap.isAndroid())
screen.title = new TranslatableComponent("otm.death_reason");
});
if (event.getScreen() instanceof DeathScreen screen) {
mc.player.getCapability(MatteryCapability.ANDROID).ifPresent(cap -> {
if (cap.isAndroid())
screen.title = new TranslatableComponent("otm.death_reason");
});
}
if (event.getScreen() instanceof InBedChatScreen screen) {
known_button_screen = screen;
mc.player.getCapability(MatteryCapability.ANDROID).ifPresent(cap -> {
if (((AndroidCapabilityPlayer) cap).will_become_android) {
known_button_screen = screen;
}
});
}
}
@SubscribeEvent
private static final Logger LOGGER = LogManager.getLogger();
@SubscribeEvent(priority = EventPriority.HIGH)
@SuppressWarnings("unused")
public void onLayerRenderEvent(RenderGameOverlayEvent.PreLayer event) {
if (event.getOverlay() != ForgeIngameGui.FOOD_LEVEL_ELEMENT && event.getOverlay() != ForgeIngameGui.AIR_LEVEL_ELEMENT)
return;
@ -61,10 +142,10 @@ public class AndroidGui {
return;
Optional<IAndroidCapability> lazy = ply.getCapability(MatteryCapability.ANDROID).resolve();
IAndroidCapability android = null;
AndroidCapabilityPlayer android = null;
if (lazy.isPresent()) {
android = lazy.get();
android = (AndroidCapabilityPlayer) lazy.get();
} else if (!ply.isAlive()) {
android = last_state;
}

View File

@ -89,7 +89,7 @@ public class ItemPill extends Item {
if (ply instanceof ServerPlayer)
((AndroidCapabilityPlayer) resolver.get()).becomeAndroidSoft();
} else if (this.pill_type == PillType.BECOME_HUMANE && resolver.get().isAndroid()) {
} else if (this.pill_type == PillType.BECOME_HUMANE && ((AndroidCapabilityPlayer) resolver.get()).isEverAndroid()) {
if (!ply.getAbilities().instabuild)
stack.shrink(1);

View File

@ -4,7 +4,7 @@
"otm.pill.warning": "WARNING: This will INSTANTLY decommission you upon ingestion!",
"otm.pill.android": "Take this pill and lose what is holding you back.",
"otm.pill.humane": "Take this pill and wake up in bed none the wiser.",
"otm.pill.message": "You feel exhausted, but nothing happens?.. Maybe get a bed rest.",
"otm.pill.message": "Nothing happened, but you feel exhausted?.. Maybe get bed rest.",
"otm.pill.message_finish": "§kONE OF US ONE OF US ONE OF US ONE OF US ONE OF US",
"otm.gui.power.percentage_level": "Energy level: %s%%",