Move stuff to appropriate places
This commit is contained in:
parent
610e01f0ea
commit
c980bdc272
@ -42,6 +42,7 @@ import ru.dbotthepony.mc.otm.capability.AndroidCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.AndroidCapabilityPlayer;
|
||||
import ru.dbotthepony.mc.otm.capability.IAndroidCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
import ru.dbotthepony.mc.otm.client.AndroidGui;
|
||||
import ru.dbotthepony.mc.otm.menu.AndroidStationMenu;
|
||||
import ru.dbotthepony.mc.otm.network.MatteryNetwork;
|
||||
import ru.dbotthepony.mc.otm.screen.AndroidStationScreen;
|
||||
@ -57,20 +58,18 @@ public class OverdriveThatMatters {
|
||||
public static final String MOD_ID = "overdrive_that_matters";
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
public static final ResourceLocation PLAYER_GUI_LOCATION = new ResourceLocation(OverdriveThatMatters.MOD_ID, "textures/gui/player_gui.png");
|
||||
|
||||
public static Block ANDROID_STATION = new BlockAndroidStation();
|
||||
public static BlockEntityType.Builder<BlockEntityAndroidStation> ANDROID_STATION_BUILDER = BlockEntityType.Builder.of(BlockEntityAndroidStation::new, ANDROID_STATION);
|
||||
public static BlockEntityType<BlockEntityAndroidStation> ANDROID_STATION_FACTORY = null;
|
||||
public static Item ANDROID_STATION_ITEM = new BlockItem(ANDROID_STATION, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC)).setRegistryName(BlockAndroidStation.REGISTRY_NAME);
|
||||
public static MenuType<AndroidStationMenu> ANDROID_STATION_MENU_TYPE = new MenuType<>(AndroidStationMenu::new);
|
||||
|
||||
public static AndroidGui ANDROID_GUI = new AndroidGui();
|
||||
|
||||
static {
|
||||
ANDROID_STATION_MENU_TYPE.setRegistryName(BlockAndroidStation.REGISTRY_NAME);
|
||||
}
|
||||
|
||||
// public static final Block STONE = register("stone", new Block(BlockBehaviour.Properties.of(Material.STONE, MaterialColor.STONE).requiresCorrectToolForDrops().strength(1.5F, 6.0F)));
|
||||
|
||||
public OverdriveThatMatters() {
|
||||
// Register the setup method for modloading
|
||||
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
|
||||
@ -81,6 +80,9 @@ public class OverdriveThatMatters {
|
||||
|
||||
// Register ourselves for server and other game events we are interested in
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
MinecraftForge.EVENT_BUS.register(AndroidCapabilityPlayer.class);
|
||||
MinecraftForge.EVENT_BUS.register(AndroidCapability.class);
|
||||
MinecraftForge.EVENT_BUS.register(ANDROID_GUI);
|
||||
}
|
||||
|
||||
private void setup(final FMLCommonSetupEvent event) {
|
||||
@ -107,104 +109,6 @@ public class OverdriveThatMatters {
|
||||
// do something when the server starts
|
||||
}
|
||||
|
||||
private final ResourceLocation android_cap_location = new ResourceLocation(OverdriveThatMatters.MOD_ID, "android_capability");
|
||||
|
||||
@SubscribeEvent
|
||||
public void onAttachCapabilityEvent(AttachCapabilitiesEvent<Entity> event) {
|
||||
if (!(event.getObject() instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.addCapability(android_cap_location, new AndroidCapabilityPlayer((Player) event.getObject()));
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onLivingTick(LivingEvent.LivingUpdateEvent event) {
|
||||
if (event.getEntity() instanceof ServerPlayer ply) {
|
||||
ply.getCapability(MatteryCapability.ANDROID).ifPresent(IAndroidCapability::tick);
|
||||
} else if (event.getEntity() instanceof LocalPlayer ply) {
|
||||
ply.getCapability(MatteryCapability.ANDROID).ifPresent(IAndroidCapability::tickClient);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onPlayerCloneEvent(PlayerEvent.Clone event) {
|
||||
event.getPlayer().getCapability(MatteryCapability.ANDROID).ifPresent((cap) -> {
|
||||
LazyOptional<IAndroidCapability> resolver = event.getOriginal().getCapability(MatteryCapability.ANDROID);
|
||||
|
||||
if (!resolver.isPresent() || resolver.resolve().isEmpty()) {
|
||||
event.getOriginal().reviveCaps();
|
||||
resolver = event.getOriginal().getCapability(MatteryCapability.ANDROID);
|
||||
LOGGER.info("revived cap {}", resolver.resolve());
|
||||
}
|
||||
|
||||
if (!resolver.isPresent() || resolver.resolve().isEmpty()) {
|
||||
event.getOriginal().invalidateCaps();
|
||||
return;
|
||||
}
|
||||
|
||||
cap.deserializeNBT(resolver.resolve().get().serializeNBT());
|
||||
event.getOriginal().invalidateCaps();
|
||||
});
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onLayerRenderEvent(RenderGameOverlayEvent.PreLayer event) {
|
||||
if (event.getOverlay() != ForgeIngameGui.FOOD_LEVEL_ELEMENT)
|
||||
return;
|
||||
|
||||
Minecraft mc = Minecraft.getInstance();
|
||||
LocalPlayer ply = mc.player;
|
||||
|
||||
if (ply == null)
|
||||
return;
|
||||
|
||||
if (
|
||||
ply.getVehicle() instanceof LivingEntity ||
|
||||
mc.options.hideGui ||
|
||||
!mc.gameMode.canHurtPlayer() ||
|
||||
!(mc.getCameraEntity() instanceof Player)
|
||||
)
|
||||
return;
|
||||
|
||||
Optional<IAndroidCapability> lazy = ply.getCapability(MatteryCapability.ANDROID).resolve();
|
||||
|
||||
if (lazy.isPresent() && mc.gui instanceof ForgeIngameGui gui) {
|
||||
event.setCanceled(true);
|
||||
IAndroidCapability android = lazy.get();
|
||||
|
||||
float level;
|
||||
|
||||
if (android.getMaxBatteryLevel().compareTo(BigDecimal.ZERO) == 0) {
|
||||
level = 0f;
|
||||
} else {
|
||||
level = android.getBatteryLevel().divide(android.getMaxBatteryLevel(), MatteryCapability.ROUND_RULES).floatValue();
|
||||
|
||||
if (level >= 0.98f)
|
||||
level = 1f;
|
||||
}
|
||||
|
||||
gui.setupOverlayRenderState(true, false);
|
||||
|
||||
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
|
||||
RenderSystem.setShaderTexture(0, PLAYER_GUI_LOCATION);
|
||||
|
||||
int width = event.getWindow().getGuiScaledWidth();
|
||||
int height = event.getWindow().getGuiScaledHeight();
|
||||
|
||||
int left = width / 2 + 10;
|
||||
int top = height - gui.right_height;
|
||||
|
||||
gui.right_height += 10;
|
||||
|
||||
// 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);
|
||||
int shift_left = (int) Math.ceil(level * 80f - 0.5f);
|
||||
gui.blit(event.getMatrixStack(), left + 80 - shift_left, top, 0, 9, shift_left, 9);
|
||||
}
|
||||
}
|
||||
|
||||
// You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
|
||||
// Event bus for receiving Registry Events)
|
||||
@Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ru.dbotthepony.mc.otm.capability;
|
||||
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
@ -12,6 +13,8 @@ import net.minecraftforge.common.util.INBTSerializable;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
import net.minecraftforge.event.entity.living.LivingEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.network.AndroidCapabilityChangePacket;
|
||||
|
||||
@ -37,6 +40,14 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
|
||||
this.ent = ent;
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onLivingTick(LivingEvent.LivingUpdateEvent event) {
|
||||
if (event.getEntity().level.isClientSide)
|
||||
event.getEntity().getCapability(MatteryCapability.ANDROID).ifPresent(IAndroidCapability::tickClient);
|
||||
else
|
||||
event.getEntity().getCapability(MatteryCapability.ANDROID).ifPresent(IAndroidCapability::tick);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public CompoundTag serializeNBT() {
|
||||
|
@ -1,9 +1,14 @@
|
||||
package ru.dbotthepony.mc.otm.capability;
|
||||
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.food.FoodData;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.event.AttachCapabilitiesEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fmllegacy.network.PacketDistributor;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.network.AndroidCapabilityChangePacket;
|
||||
@ -13,6 +18,35 @@ import java.math.BigDecimal;
|
||||
import java.util.Random;
|
||||
|
||||
public class AndroidCapabilityPlayer extends AndroidCapability {
|
||||
public static final ResourceLocation ANDROID_CAPABILITY_LOCATION = new ResourceLocation(OverdriveThatMatters.MOD_ID, "android_capability");
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onAttachCapabilityEvent(AttachCapabilitiesEvent<Entity> event) {
|
||||
if (event.getObject() instanceof Player ply) {
|
||||
event.addCapability(ANDROID_CAPABILITY_LOCATION, new AndroidCapabilityPlayer(ply));
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onPlayerCloneEvent(PlayerEvent.Clone event) {
|
||||
event.getPlayer().getCapability(MatteryCapability.ANDROID).ifPresent((cap) -> {
|
||||
LazyOptional<IAndroidCapability> resolver = event.getOriginal().getCapability(MatteryCapability.ANDROID);
|
||||
|
||||
if (!resolver.isPresent() || resolver.resolve().isEmpty()) {
|
||||
event.getOriginal().reviveCaps();
|
||||
resolver = event.getOriginal().getCapability(MatteryCapability.ANDROID);
|
||||
}
|
||||
|
||||
if (!resolver.isPresent() || resolver.resolve().isEmpty()) {
|
||||
event.getOriginal().invalidateCaps();
|
||||
return;
|
||||
}
|
||||
|
||||
cap.deserializeNBT(resolver.resolve().get().serializeNBT());
|
||||
event.getOriginal().invalidateCaps();
|
||||
});
|
||||
}
|
||||
|
||||
private final Player ply;
|
||||
|
||||
public AndroidCapabilityPlayer(Player ent) {
|
||||
|
89
src/main/java/ru/dbotthepony/mc/otm/client/AndroidGui.java
Normal file
89
src/main/java/ru/dbotthepony/mc/otm/client/AndroidGui.java
Normal file
@ -0,0 +1,89 @@
|
||||
package ru.dbotthepony.mc.otm.client;
|
||||
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraftforge.client.event.RenderGameOverlayEvent;
|
||||
import net.minecraftforge.client.gui.ForgeIngameGui;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.capability.IAndroidCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
||||
public class AndroidGui {
|
||||
private final Minecraft mc = Minecraft.getInstance();
|
||||
private final ForgeIngameGui gui = mc.gui instanceof ForgeIngameGui ? (ForgeIngameGui) mc.gui : null;
|
||||
|
||||
public static final ResourceLocation PLAYER_GUI_LOCATION = new ResourceLocation(OverdriveThatMatters.MOD_ID, "textures/gui/player_gui.png");
|
||||
|
||||
private IAndroidCapability last_state;
|
||||
|
||||
@SubscribeEvent
|
||||
public void onLayerRenderEvent(RenderGameOverlayEvent.PreLayer event) {
|
||||
if (event.getOverlay() != ForgeIngameGui.FOOD_LEVEL_ELEMENT)
|
||||
return;
|
||||
|
||||
LocalPlayer ply = mc.player;
|
||||
|
||||
if (ply == null)
|
||||
return;
|
||||
|
||||
if (
|
||||
ply.getVehicle() instanceof LivingEntity ||
|
||||
mc.options.hideGui ||
|
||||
!mc.gameMode.canHurtPlayer() ||
|
||||
!(mc.getCameraEntity() instanceof Player)
|
||||
)
|
||||
return;
|
||||
|
||||
Optional<IAndroidCapability> lazy = ply.getCapability(MatteryCapability.ANDROID).resolve();
|
||||
IAndroidCapability android = null;
|
||||
|
||||
if (lazy.isPresent()) {
|
||||
android = lazy.get();
|
||||
} else if (!ply.isAlive()) {
|
||||
android = last_state;
|
||||
}
|
||||
|
||||
if (android != null && mc.gui instanceof ForgeIngameGui gui) {
|
||||
event.setCanceled(true);
|
||||
last_state = android;
|
||||
|
||||
float level;
|
||||
|
||||
if (android.getMaxBatteryLevel().compareTo(BigDecimal.ZERO) == 0) {
|
||||
level = 0f;
|
||||
} else {
|
||||
level = android.getBatteryLevel().divide(android.getMaxBatteryLevel(), MatteryCapability.ROUND_RULES).floatValue();
|
||||
|
||||
if (level >= 0.98f)
|
||||
level = 1f;
|
||||
}
|
||||
|
||||
gui.setupOverlayRenderState(true, false);
|
||||
|
||||
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
|
||||
RenderSystem.setShaderTexture(0, PLAYER_GUI_LOCATION);
|
||||
|
||||
int width = event.getWindow().getGuiScaledWidth();
|
||||
int height = event.getWindow().getGuiScaledHeight();
|
||||
|
||||
int left = width / 2 + 10;
|
||||
int top = height - gui.right_height;
|
||||
|
||||
gui.right_height += 10;
|
||||
|
||||
// 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);
|
||||
int shift_left = (int) Math.ceil(level * 79f - 0.5f);
|
||||
gui.blit(event.getMatrixStack(), left + 79 - shift_left, top, 0, 9, shift_left, 9);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user