diff --git a/src/main/java/ru/dbotthepony/mc/otm/Registry.java b/src/main/java/ru/dbotthepony/mc/otm/Registry.java index 48628822b..991e8ff00 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/Registry.java +++ b/src/main/java/ru/dbotthepony/mc/otm/Registry.java @@ -16,11 +16,15 @@ import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import ru.dbotthepony.mc.otm.block.BlockAndroidStation; +import ru.dbotthepony.mc.otm.block.BlockBatteryBank; import ru.dbotthepony.mc.otm.block.entity.BlockEntityAndroidStation; +import ru.dbotthepony.mc.otm.block.entity.BlockEntityBatteryBank; import ru.dbotthepony.mc.otm.item.ItemBattery; import ru.dbotthepony.mc.otm.item.ItemPill; import ru.dbotthepony.mc.otm.menu.AndroidStationMenu; +import ru.dbotthepony.mc.otm.menu.BatteryBankMenu; import ru.dbotthepony.mc.otm.screen.AndroidStationScreen; +import ru.dbotthepony.mc.otm.screen.BatteryBankScreen; import java.math.BigDecimal; @@ -35,6 +39,7 @@ public class Registry { public static class Names { public static final ResourceLocation ANDROID_STATION = new ResourceLocation(OverdriveThatMatters.MOD_ID, "android_station"); + public static final ResourceLocation BATTERY_BANK = new ResourceLocation(OverdriveThatMatters.MOD_ID, "battery_bank"); public static final ResourceLocation ANDROID_CAPABILITY = new ResourceLocation(OverdriveThatMatters.MOD_ID, "android_capability"); public static final ResourceLocation PILL_ANDROID = new ResourceLocation(OverdriveThatMatters.MOD_ID, "pill_android"); @@ -50,16 +55,24 @@ public class Registry { public static class Blocks { public static final Block ANDROID_STATION = new BlockAndroidStation(); + public static final Block BATTERY_BANK = new BlockBatteryBank(); + + static { + ANDROID_STATION.setRegistryName(Names.ANDROID_STATION); + BATTERY_BANK.setRegistryName(Names.BATTERY_BANK); + } public static void register(final RegistryEvent.Register event) { event.getRegistry().register(ANDROID_STATION); + event.getRegistry().register(BATTERY_BANK); // OverdriveThatMatters.LOGGER.info("Registered blocks"); } } public static class Items { - public static final Item ANDROID_STATION = new BlockItem(Blocks.ANDROID_STATION, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC)).setRegistryName(Names.ANDROID_STATION); + public static final Item ANDROID_STATION = new BlockItem(Blocks.ANDROID_STATION, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC)); + public static final Item BATTERY_BANK = new BlockItem(Blocks.BATTERY_BANK, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC)); public static final ItemPill PILL_ANDROID = new ItemPill(ItemPill.PillType.BECOME_ANDROID); public static final ItemPill PILL_HUMANE = new ItemPill(ItemPill.PillType.BECOME_HUMANE); @@ -71,6 +84,9 @@ public class Registry { public static final ItemBattery BATTERY_CREATIVE = new ItemBattery(); static { + ANDROID_STATION.setRegistryName(Names.ANDROID_STATION); + BATTERY_BANK.setRegistryName(Names.BATTERY_BANK); + PILL_ANDROID.setRegistryName(Names.PILL_ANDROID); PILL_HUMANE.setRegistryName(Names.PILL_HUMANE); BATTERY_CRUDE.setRegistryName(Names.BATTERY_CRUDE); @@ -83,6 +99,8 @@ public class Registry { public static void register(final RegistryEvent.Register event) { event.getRegistry().register(ANDROID_STATION); + event.getRegistry().register(BATTERY_BANK); + event.getRegistry().register(PILL_ANDROID); event.getRegistry().register(PILL_HUMANE); event.getRegistry().register(BATTERY_CRUDE); @@ -97,35 +115,41 @@ public class Registry { } public static class BlockEntities { - public static BlockEntityType.Builder ANDROID_STATION_BUILDER = BlockEntityType.Builder.of(BlockEntityAndroidStation::new, Blocks.ANDROID_STATION); - public static BlockEntityType ANDROID_STATION = ANDROID_STATION_BUILDER.build(null); + public static BlockEntityType ANDROID_STATION = BlockEntityType.Builder.of(BlockEntityAndroidStation::new, Blocks.ANDROID_STATION).build(null); + public static BlockEntityType BATTERY_BANK = BlockEntityType.Builder.of(BlockEntityBatteryBank::new, Blocks.BATTERY_BANK).build(null); static { ANDROID_STATION.setRegistryName(Names.ANDROID_STATION); + BATTERY_BANK.setRegistryName(Names.BATTERY_BANK); } public static void register(final RegistryEvent.Register> event) { event.getRegistry().register(ANDROID_STATION); + event.getRegistry().register(BATTERY_BANK); // OverdriveThatMatters.LOGGER.info("Registered block entities"); } } public static class Menus { - public static final MenuType ANDROID_STATION = new net.minecraft.world.inventory.MenuType<>(AndroidStationMenu::new); + public static final MenuType ANDROID_STATION = new MenuType<>(AndroidStationMenu::new); + public static final MenuType BATTERY_BANK = new MenuType<>(BatteryBankMenu::new); static { ANDROID_STATION.setRegistryName(Names.ANDROID_STATION); + BATTERY_BANK.setRegistryName(Names.BATTERY_BANK); } public static void register(final RegistryEvent.Register> event) { event.getRegistry().register(ANDROID_STATION); + event.getRegistry().register(BATTERY_BANK); // OverdriveThatMatters.LOGGER.info("Registered menus"); } public static void registerScreens(final FMLClientSetupEvent event) { MenuScreens.register(ANDROID_STATION, AndroidStationScreen::new); + MenuScreens.register(BATTERY_BANK, BatteryBankScreen::new); // OverdriveThatMatters.LOGGER.info("Registered screens"); } diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/BlockAndroidStation.java b/src/main/java/ru/dbotthepony/mc/otm/block/BlockAndroidStation.java index 7ada752c1..f4b3ecc2a 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/BlockAndroidStation.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/BlockAndroidStation.java @@ -28,7 +28,6 @@ public class BlockAndroidStation extends BlockMatteryMachineBase implements Enti public BlockAndroidStation() { super(); - this.setRegistryName(Registry.Names.ANDROID_STATION); } @Override diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/BlockBatteryBank.java b/src/main/java/ru/dbotthepony/mc/otm/block/BlockBatteryBank.java new file mode 100644 index 000000000..68910faa6 --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/block/BlockBatteryBank.java @@ -0,0 +1,32 @@ +package ru.dbotthepony.mc.otm.block; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.EntityBlock; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityTicker; +import net.minecraft.world.level.block.entity.BlockEntityType; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.StateDefinition; +import net.minecraft.world.level.block.state.properties.EnumProperty; +import ru.dbotthepony.mc.otm.Registry; +import ru.dbotthepony.mc.otm.block.entity.BlockEntityAndroidStation; +import ru.dbotthepony.mc.otm.block.entity.BlockEntityBatteryBank; + +import javax.annotation.Nullable; + +public class BlockBatteryBank extends BlockMatteryRotatableMachineBase implements EntityBlock { + @Nullable + @Override + public BlockEntityTicker getTicker(Level level, BlockState p_153213_, BlockEntityType type) { + return level.isClientSide || type != Registry.BlockEntities.BATTERY_BANK ? null : BlockEntityBatteryBank::tick; + } + + @Nullable + @Override + public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { + return new BlockEntityBatteryBank(blockPos, blockState); + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/BlockMatteryRotatableMachineBase.java b/src/main/java/ru/dbotthepony/mc/otm/block/BlockMatteryRotatableMachineBase.java new file mode 100644 index 000000000..f94fe365f --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/block/BlockMatteryRotatableMachineBase.java @@ -0,0 +1,35 @@ +package ru.dbotthepony.mc.otm.block; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.context.BlockPlaceContext; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.StateDefinition; +import net.minecraft.world.level.block.state.properties.EnumProperty; + +import javax.annotation.Nullable; + +public abstract class BlockMatteryRotatableMachineBase extends BlockMatteryMachineBase { + public static final EnumProperty FACING = EnumProperty.create("facing", Direction.class); + + public BlockMatteryRotatableMachineBase() { + super(); + + registerDefaultState(this.getStateDefinition().any().setValue(FACING, Direction.SOUTH)); + } + + @Override + protected void createBlockStateDefinition(StateDefinition.Builder builder) { + builder.add(FACING); + } + + @Nullable + @Override + public BlockState getStateForPlacement(BlockPlaceContext context) { + return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection()); + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityAndroidStation.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityAndroidStation.java index d42931384..efd62b3d5 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityAndroidStation.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityAndroidStation.java @@ -1,6 +1,8 @@ package ru.dbotthepony.mc.otm.block.entity; import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.world.MenuProvider; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Inventory; @@ -29,9 +31,14 @@ public class BlockEntityAndroidStation extends BlockEntityMatteryPoweredMachine return new AndroidStationMenu(containerID, inventory, this); } + @Override + protected Component getDefaultDisplayName() { + return new TranslatableComponent("container.otm.android_station"); + } + public BlockEntityAndroidStation(BlockPos p_155229_, BlockState p_155230_) { super(Registry.BlockEntities.ANDROID_STATION, p_155229_, p_155230_); - energy = new MatteryMachineEnergyStorage(MatteryMachineEnergyStorage.MachineType.WORKER, new BigDecimal(100_000), new BigDecimal(250), new BigDecimal(250)); + energy = new MatteryMachineEnergyStorage(this, MatteryMachineEnergyStorage.MachineType.WORKER, new BigDecimal(100_000), new BigDecimal(250), new BigDecimal(250)); energy_resolver = LazyOptional.of(() -> energy); } diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityBatteryBank.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityBatteryBank.java new file mode 100644 index 000000000..2f4f43422 --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityBatteryBank.java @@ -0,0 +1,342 @@ +package ru.dbotthepony.mc.otm.block.entity; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; +import net.minecraft.world.level.block.entity.TickingBlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.util.LazyOptional; +import net.minecraftforge.energy.CapabilityEnergy; +import net.minecraftforge.energy.IEnergyStorage; +import ru.dbotthepony.mc.otm.OverdriveThatMatters; +import ru.dbotthepony.mc.otm.Registry; +import ru.dbotthepony.mc.otm.block.BlockMatteryRotatableMachineBase; +import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage; +import ru.dbotthepony.mc.otm.capability.MatteryCapability; +import ru.dbotthepony.mc.otm.capability.MatteryMachineEnergyStorage; +import ru.dbotthepony.mc.otm.container.SimpleSerializableContainer; +import ru.dbotthepony.mc.otm.menu.BatteryBankMenu; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.math.BigDecimal; +import java.util.Optional; + +public class BlockEntityBatteryBank extends BlockEntityMatteryMachine { + // 5 на 3 + public SimpleSerializableContainer battery_container = new SimpleSerializableContainer(this::setChanged, 5 * 3); + + public static class BatteryBankDistribution { + private final BigDecimal[] distribution; + private final BigDecimal max_throughput; + + public BigDecimal max_throughput() { + return max_throughput; + } + + public BigDecimal[] distribution() { + return distribution; + } + + public BatteryBankDistribution(BigDecimal[] distribution, BigDecimal max_throughput) { + this.distribution = distribution; + this.max_throughput = max_throughput; + } + } + + public static class BatteryBankEnergy implements IMatteryEnergyStorage { + public enum BankMode { + RECEIVE, + EXTRACT, + BIDIRECTIONAL + } + + private final BlockEntityBatteryBank tile; + private final BankMode mode; + + BatteryBankEnergy(BlockEntityBatteryBank tile, BankMode mode) { + this.tile = tile; + this.mode = mode; + } + + @Override + public boolean canExtractOuter() { + return mode == BankMode.EXTRACT || mode == BankMode.BIDIRECTIONAL; + } + + @Override + public boolean canReceiveOuter() { + return mode == BankMode.RECEIVE || mode == BankMode.BIDIRECTIONAL; + } + + @Override + public boolean canExtractInner() { + return true; + } + + @Override + public boolean canReceiveInner() { + return true; + } + + @Nonnull + @Override + public BigDecimal extractEnergyOuter(BigDecimal howMuch, boolean simulate) { + if (mode == BankMode.RECEIVE) + return BigDecimal.ZERO; + + return extractEnergyInner(howMuch, simulate); + } + + BatteryBankDistribution getDistribution(boolean mode) { + BigDecimal[] distribution = new BigDecimal[tile.battery_container.getContainerSize()]; + BigDecimal summ = BigDecimal.ZERO; + + for (int i = 0; i < tile.battery_container.getContainerSize(); i++) { + ItemStack stack = tile.battery_container.getItem(i); + + if (!stack.isEmpty()) { + Optional cap = stack.getCapability(MatteryCapability.ENERGY).resolve(); + + if (cap.isPresent()) { + BigDecimal diff = mode ? cap.get().receiveEnergyOuter(MatteryCapability.LONG_MAX_VALUE, true) : cap.get().extractEnergyOuter(MatteryCapability.LONG_MAX_VALUE, true); + distribution[i] = diff; + summ = summ.add(diff); + } else { + Optional cap2 = stack.getCapability(CapabilityEnergy.ENERGY).resolve(); + + if (cap2.isPresent()) { + BigDecimal diff = new BigDecimal(mode ? cap2.get().receiveEnergy(Integer.MAX_VALUE, true) : cap2.get().extractEnergy(Integer.MAX_VALUE, true)); + distribution[i] = diff; + summ = summ.add(diff); + } + } + } else { + distribution[i] = BigDecimal.ZERO; + } + } + + if (summ.compareTo(BigDecimal.ZERO) != 0) { + for (int i = 0; i < tile.battery_container.getContainerSize(); i++) { + distribution[i] = distribution[i].divide(summ, MatteryCapability.ROUND_RULES); + } + } + + return new BatteryBankDistribution(distribution, summ); + } + + private BigDecimal distributeEnergy(boolean mode, BigDecimal howMuch, boolean simulate) { + BatteryBankDistribution distribution = getDistribution(mode); + + if (distribution.max_throughput.compareTo(BigDecimal.ZERO) == 0) + return BigDecimal.ZERO; + + BigDecimal[] distList = distribution.distribution; + BigDecimal summ = BigDecimal.ZERO; + + for (int i = 0; i < tile.battery_container.getContainerSize(); i++) { + if (!distList[i].equals(BigDecimal.ZERO)) { + ItemStack stack = tile.battery_container.getItem(i); + + if (!stack.isEmpty()) { + Optional cap = stack.getCapability(MatteryCapability.ENERGY).resolve(); + + if (cap.isPresent()) { + BigDecimal diff = mode ? cap.get().receiveEnergyOuter(howMuch.multiply(distList[i], MatteryCapability.ROUND_RULES), simulate) : cap.get().extractEnergyOuter(howMuch.multiply(distList[i], MatteryCapability.ROUND_RULES), simulate); + summ = summ.add(diff); + } else { + Optional cap2 = stack.getCapability(CapabilityEnergy.ENERGY).resolve(); + + if (cap2.isPresent()) { + BigDecimal diff = mode ? MatteryCapability.floodFE(cap2.get(), howMuch.multiply(distList[i], MatteryCapability.ROUND_RULES), simulate) : MatteryCapability.drainFE(cap2.get(), howMuch.multiply(distList[i], MatteryCapability.ROUND_RULES), simulate); + summ = summ.add(diff); + } + } + } + } + } + + return summ; + } + + @Nonnull + @Override + public BigDecimal extractEnergyInner(BigDecimal howMuch, boolean simulate) { + return distributeEnergy(false, howMuch, simulate); + } + + @Nonnull + @Override + public BigDecimal receiveEnergyOuter(BigDecimal howMuch, boolean simulate) { + if (mode == BankMode.EXTRACT) + return BigDecimal.ZERO; + + return receiveEnergyInner(howMuch, simulate); + } + + @Nonnull + @Override + public BigDecimal receiveEnergyInner(BigDecimal howMuch, boolean simulate) { + return distributeEnergy(true, howMuch, simulate); + } + + @Nonnull + @Override + public BigDecimal getBatteryLevel() { + BigDecimal result = BigDecimal.ZERO; + + for (int i = 0; i < tile.battery_container.getContainerSize(); i++) { + ItemStack stack = tile.battery_container.getItem(i); + + if (!stack.isEmpty()) { + Optional cap = stack.getCapability(MatteryCapability.ENERGY).resolve(); + + if (cap.isPresent()) { + result = result.add(cap.get().getBatteryLevel(), MatteryCapability.ROUND_RULES); + } else { + Optional cap2 = stack.getCapability(CapabilityEnergy.ENERGY).resolve(); + + if (cap2.isPresent()) { + result = result.add(new BigDecimal(cap2.get().getEnergyStored()), MatteryCapability.ROUND_RULES); + } + } + } + } + + return result; + } + + @Nonnull + @Override + public BigDecimal getMaxBatteryLevel() { + BigDecimal result = BigDecimal.ZERO; + + for (int i = 0; i < tile.battery_container.getContainerSize(); i++) { + ItemStack stack = tile.battery_container.getItem(i); + + if (!stack.isEmpty()) { + Optional cap = stack.getCapability(MatteryCapability.ENERGY).resolve(); + + if (cap.isPresent()) { + result = result.add(cap.get().getMaxBatteryLevel(), MatteryCapability.ROUND_RULES); + } else { + Optional cap2 = stack.getCapability(CapabilityEnergy.ENERGY).resolve(); + + if (cap2.isPresent()) { + result = result.add(new BigDecimal(cap2.get().getMaxEnergyStored()), MatteryCapability.ROUND_RULES); + } + } + } + } + + return result; + } + } + + public final BatteryBankEnergy energy_receiver = new BatteryBankEnergy(this, BatteryBankEnergy.BankMode.RECEIVE); + public final LazyOptional energy_receiver_resolver = LazyOptional.of(() -> energy_receiver); + + public final BatteryBankEnergy energy_extractor = new BatteryBankEnergy(this, BatteryBankEnergy.BankMode.EXTRACT); + public final LazyOptional energy_extractor_resolver = LazyOptional.of(() -> energy_extractor); + + public final BatteryBankEnergy energy = new BatteryBankEnergy(this, BatteryBankEnergy.BankMode.BIDIRECTIONAL); + public final LazyOptional energy_resolver = LazyOptional.of(() -> energy); + + protected boolean valid = true; + + public BlockEntityBatteryBank(BlockPos p_155229_, BlockState p_155230_) { + super(Registry.BlockEntities.BATTERY_BANK, p_155229_, p_155230_); + } + + @Override + public CompoundTag save(CompoundTag nbt) { + nbt.put("battery_bank", battery_container.serializeNBT()); + return super.save(nbt); + } + + @Override + public void load(CompoundTag nbt) { + if (nbt.contains("battery_bank") && nbt.get("battery_bank") instanceof CompoundTag tag) + battery_container = SimpleSerializableContainer.of(this::setChanged, tag, battery_container.getContainerSize()); + + super.load(nbt); + } + + @Override + protected Component getDefaultDisplayName() { + return new TranslatableComponent("container.otm.battery_bank"); + } + + @Nullable + @Override + public AbstractContainerMenu createMenu(int containerID, Inventory inventory, Player ply) { + return new BatteryBankMenu(containerID, inventory, this, this.battery_container); + } + + @Override + public void invalidateCaps() { + super.invalidateCaps(); + valid = false; + } + + @Override + public void reviveCaps() { + super.reviveCaps(); + valid = true; + } + + @Nonnull + public LazyOptional getCapability(@Nonnull Capability cap, @Nullable Direction side) { + if (valid && (cap == MatteryCapability.ENERGY || cap == CapabilityEnergy.ENERGY)) { + if (side == null) + return energy_resolver.cast(); + + if (side == getBlockState().getValue(BlockMatteryRotatableMachineBase.FACING)) + return energy_extractor_resolver.cast(); + + return energy_receiver_resolver.cast(); + } + + return super.getCapability(cap, side); + } + + public static void tick(Level level, BlockPos blockPos, BlockState blockState, T t) { + if (t instanceof BlockEntityBatteryBank tile) { + BlockEntity get_entity = level.getBlockEntity(blockPos.offset(tile.getBlockState().getValue(BlockMatteryRotatableMachineBase.FACING).getNormal())); + + if (get_entity != null) { + Optional cap = get_entity.getCapability(MatteryCapability.ENERGY).resolve(); + + if (cap.isPresent()) { + if (cap.get().canReceiveOuter()) { + BatteryBankDistribution distribution = tile.energy.getDistribution(false); + BigDecimal diff = cap.get().receiveEnergyOuter(distribution.max_throughput, true); + diff = tile.energy.extractEnergyOuter(diff, false); + cap.get().receiveEnergyOuter(diff, false); + } + } else { + Optional cap2 = get_entity.getCapability(CapabilityEnergy.ENERGY).resolve(); + + if (cap2.isPresent()) { + if (cap2.get().canReceive()) { + BatteryBankDistribution distribution = tile.energy.getDistribution(false); + BigDecimal diff = MatteryCapability.floodFE(cap2.get(), distribution.max_throughput, true); + diff = tile.energy.extractEnergyOuter(diff, false); + MatteryCapability.floodFE(cap2.get(), diff, false); + } + } + } + } + } + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatteryMachine.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatteryMachine.java index f72b1b4cb..8b344605d 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatteryMachine.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatteryMachine.java @@ -26,9 +26,13 @@ public abstract class BlockEntityMatteryMachine extends BlockEntity implements M display_name = text; } + protected Component getDefaultDisplayName() { + return new TranslatableComponent("container.otm.unknown"); + } + @Override public Component getDisplayName() { - return display_name != null ? display_name : new TranslatableComponent("container.otm.android_station"); + return display_name != null ? display_name : getDefaultDisplayName(); } @Nullable diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatteryPoweredMachine.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatteryPoweredMachine.java index a8838c449..e262bb8d6 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatteryPoweredMachine.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatteryPoweredMachine.java @@ -28,7 +28,7 @@ abstract public class BlockEntityMatteryPoweredMachine extends BlockEntityMatter protected LazyOptional energy_resolver = null; private boolean valid = true; - public SimpleSerializableContainer battery_container = new SimpleSerializableContainer(1); + public SimpleSerializableContainer battery_container = new SimpleSerializableContainer(this::setChanged, 1); protected void tickBatteryCharge() { if (energy.getMissingPower().compareTo(BigDecimal.ZERO) <= 0) @@ -48,7 +48,7 @@ abstract public class BlockEntityMatteryPoweredMachine extends BlockEntityMatter BigDecimal drain = storage.extractEnergyOuter(demand, true); - if (!drain.equals(BigDecimal.ZERO)) { + if (drain.compareTo(BigDecimal.ZERO) != 0) { BigDecimal receive = energy.receiveEnergyOuter(drain, true); storage.extractEnergyOuter(receive, false); @@ -64,7 +64,7 @@ abstract public class BlockEntityMatteryPoweredMachine extends BlockEntityMatter BigDecimal drain = MatteryCapability.drainFE(energy_fe, demand, true); - if (!drain.equals(BigDecimal.ZERO)) { + if (drain.compareTo(BigDecimal.ZERO) != 0) { BigDecimal receive = energy.receiveEnergyOuter(drain, true); MatteryCapability.drainFE(energy_fe, receive, false); @@ -116,7 +116,7 @@ abstract public class BlockEntityMatteryPoweredMachine extends BlockEntityMatter energy.deserializeNBT(tag); if (nbt.contains("battery_container") && nbt.get("battery_container") instanceof CompoundTag tag) - battery_container = SimpleSerializableContainer.of(tag, 1); + battery_container = SimpleSerializableContainer.of(this::setChanged, tag, 1); super.load(nbt); } diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapabilityPlayer.java b/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapabilityPlayer.java index a237882d6..e03b96f91 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapabilityPlayer.java +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapabilityPlayer.java @@ -164,7 +164,7 @@ public class AndroidCapabilityPlayer extends AndroidCapability { boolean saturation_changed = false; while (food_points_missing > 0) { - if (this.extractEnergyInner(ENERGY_FOR_HUNGER_POINT, true).equals(ENERGY_FOR_HUNGER_POINT)) { + 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; @@ -174,7 +174,7 @@ public class AndroidCapabilityPlayer extends AndroidCapability { } while (saturation_points_missing >= 1F) { - if (this.extractEnergyInner(ENERGY_FOR_HUNGER_POINT, true).equals(ENERGY_FOR_HUNGER_POINT)) { + 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; diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/MatteryMachineEnergyStorage.java b/src/main/java/ru/dbotthepony/mc/otm/capability/MatteryMachineEnergyStorage.java index 981744b24..8924d292a 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/capability/MatteryMachineEnergyStorage.java +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/MatteryMachineEnergyStorage.java @@ -1,6 +1,7 @@ package ru.dbotthepony.mc.otm.capability; import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.common.util.INBTSerializable; import javax.annotation.Nonnull; @@ -22,16 +23,18 @@ public class MatteryMachineEnergyStorage implements IMatteryEnergyStorage, INBTS protected BigDecimal max_input; protected BigDecimal max_output; protected final MachineType machine_type; + protected final BlockEntity listener; - public MatteryMachineEnergyStorage(MachineType type, BigDecimal capacity) { - this(type, capacity, DEFAULT_MAX_RECEIVE, DEFAULT_MAX_EXTRACT); + public MatteryMachineEnergyStorage(BlockEntity listener, MachineType type, BigDecimal capacity) { + this(listener, type, capacity, DEFAULT_MAX_RECEIVE, DEFAULT_MAX_EXTRACT); } - public MatteryMachineEnergyStorage(MachineType type) { - this(type, DEFAULT_MAX_CAPACITY); + public MatteryMachineEnergyStorage(BlockEntity listener, MachineType type) { + this(listener, type, DEFAULT_MAX_CAPACITY); } - public MatteryMachineEnergyStorage(MachineType type, BigDecimal capacity, BigDecimal maxReceive, BigDecimal maxExtract) { + public MatteryMachineEnergyStorage(BlockEntity listener, MachineType type, BigDecimal capacity, BigDecimal maxReceive, BigDecimal maxExtract) { + this.listener = listener; energy_stored_max = capacity; max_input = maxReceive; max_output = maxExtract; @@ -54,8 +57,9 @@ public class MatteryMachineEnergyStorage implements IMatteryEnergyStorage, INBTS BigDecimal new_energy = energy_stored.subtract(howMuch.min(max_output), MatteryCapability.ROUND_RULES).max(BigDecimal.ZERO); BigDecimal diff = energy_stored.subtract(new_energy); - if (!simulate) { + if (!simulate && !energy_stored.equals(new_energy)) { energy_stored = new_energy; + listener.setChanged(); } return diff; @@ -77,8 +81,9 @@ public class MatteryMachineEnergyStorage implements IMatteryEnergyStorage, INBTS BigDecimal new_energy = energy_stored.add(howMuch.min(max_input), MatteryCapability.ROUND_RULES).min(energy_stored_max); BigDecimal diff = new_energy.subtract(energy_stored); - if (!simulate) { + if (!simulate && !energy_stored.equals(new_energy)) { energy_stored = new_energy; + listener.setChanged(); } return diff; diff --git a/src/main/java/ru/dbotthepony/mc/otm/container/SimpleSerializableContainer.java b/src/main/java/ru/dbotthepony/mc/otm/container/SimpleSerializableContainer.java index 25f657ff5..8ab8fcca7 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/container/SimpleSerializableContainer.java +++ b/src/main/java/ru/dbotthepony/mc/otm/container/SimpleSerializableContainer.java @@ -6,12 +6,21 @@ import net.minecraft.world.SimpleContainer; import net.minecraft.world.item.ItemStack; public class SimpleSerializableContainer extends SimpleContainer { - public SimpleSerializableContainer(int i) { + protected final Runnable watcher; + public SimpleSerializableContainer(Runnable watcher, int i) { super(i); + this.watcher = watcher; } - public SimpleSerializableContainer(ItemStack... p_19152_) { + public SimpleSerializableContainer(Runnable watcher, ItemStack... p_19152_) { super(p_19152_); + this.watcher = watcher; + } + + @Override + public void setChanged() { + super.setChanged(); + watcher.run(); } public CompoundTag serializeNBT() { @@ -26,16 +35,16 @@ public class SimpleSerializableContainer extends SimpleContainer { return tag; } - public static SimpleSerializableContainer of(CompoundTag tag, int fallback_size) { + public static SimpleSerializableContainer of(Runnable watcher, CompoundTag tag, int fallback_size) { if (tag == null) - return new SimpleSerializableContainer(fallback_size); + return new SimpleSerializableContainer(watcher, fallback_size); SimpleSerializableContainer container; if (tag.contains("size")) - container = new SimpleSerializableContainer(tag.getInt("size")); + container = new SimpleSerializableContainer(watcher, tag.getInt("size")); else - container = new SimpleSerializableContainer(fallback_size); + container = new SimpleSerializableContainer(watcher, fallback_size); if (tag.get("items") instanceof ListTag list) for (int i = 0; i < Math.min(list.size(), container.getContainerSize()); i++) diff --git a/src/main/java/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.java b/src/main/java/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.java index 44cf7e75a..d3ef1033e 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.java +++ b/src/main/java/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.java @@ -3,7 +3,6 @@ package ru.dbotthepony.mc.otm.menu; import net.minecraft.world.SimpleContainer; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; import net.minecraftforge.common.util.LazyOptional; import ru.dbotthepony.mc.otm.Registry; @@ -14,7 +13,7 @@ import ru.dbotthepony.mc.otm.menu.slot.BatterySlot; import javax.annotation.Nullable; -public class AndroidStationMenu extends PoweredMachineMenu { +public class AndroidStationMenu extends PoweredMatteryMenu { public static class AndroidStationContainer extends SimpleContainer { public final Player player; private final IAndroidCapability android; @@ -73,32 +72,12 @@ public class AndroidStationMenu extends PoweredMachineMenu { } @Override - public ItemStack quickMoveStack(Player ply, int slot_index) { - ItemStack moved = ItemStack.EMPTY; - Slot get_slot = this.slots.get(slot_index); + protected int getWorkingSlotStart() { + return battery_slot_index; + } - if (get_slot.hasItem()) { - ItemStack slot_item = get_slot.getItem(); - moved = slot_item.copy(); - - if (slot_index < inventory_slot_index_start) { - // Moving FROM machine TO inventory - - if (!moveItemStackTo(slot_item, inventory_slot_index_start, inventory_slot_index_end + 1, false)) { - return ItemStack.EMPTY; - } - } else if (!moveItemStackTo(slot_item, battery_slot_index, battery_slot_index + 1, true)) { - // Moving FROM inventory TO machine - return ItemStack.EMPTY; - } - - if (slot_item.isEmpty()) { - get_slot.set(ItemStack.EMPTY); - } else { - get_slot.setChanged(); - } - } - - return moved; + @Override + protected int getWorkingSlotEnd() { + return battery_slot_index + 1; } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/menu/BatteryBankMenu.java b/src/main/java/ru/dbotthepony/mc/otm/menu/BatteryBankMenu.java new file mode 100644 index 000000000..eb9800e27 --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/menu/BatteryBankMenu.java @@ -0,0 +1,69 @@ +package ru.dbotthepony.mc.otm.menu; + +import net.minecraft.world.Container; +import net.minecraft.world.SimpleContainer; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraftforge.common.util.LazyOptional; +import ru.dbotthepony.mc.otm.Registry; +import ru.dbotthepony.mc.otm.block.entity.BlockEntityBatteryBank; +import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage; +import ru.dbotthepony.mc.otm.capability.MatteryCapability; +import ru.dbotthepony.mc.otm.menu.data.BigDecimalDataContainer; +import ru.dbotthepony.mc.otm.menu.slot.BatterySlot; + +import javax.annotation.Nullable; + +public class BatteryBankMenu extends MatteryMenu { + protected BlockEntityBatteryBank tile; + + public BigDecimalDataContainer energy; + public BigDecimalDataContainer max_energy; + + public BatteryBankMenu(int p_38852_, Inventory inventory) { + this(p_38852_, inventory, null, new SimpleContainer(5 * 3)); + } + + public BatteryBankMenu(int p_38852_, Inventory inventory, BlockEntityBatteryBank tile, Container batteries) { + super(Registry.Menus.BATTERY_BANK, p_38852_, inventory, tile); + this.tile = tile; + + if (tile == null) { + energy = new BigDecimalDataContainer(); + max_energy = new BigDecimalDataContainer(); + } else { + energy = new BigDecimalDataContainer() { + @Override + protected void updateValue() { + setDecimal(tile.energy.getBatteryLevel()); + } + }; + max_energy = new BigDecimalDataContainer() { + @Override + protected void updateValue() { + setDecimal(tile.energy.getMaxBatteryLevel()); + } + }; + } + + this.addDataSlots(energy); + this.addDataSlots(max_energy); + + for (int row = 0; row < 3; row++) + for (int column = 0; column < 5; column++) + this.addSlot(new BatterySlot(batteries, row * 5 + column, 62 + column * 18, 24 + row * 18)); + + addInventorySlots(); + } + + @Override + protected int getWorkingSlotStart() { + return 0; + } + + @Override + protected int getWorkingSlotEnd() { + return 5 * 3; + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMachineMenu.java b/src/main/java/ru/dbotthepony/mc/otm/menu/MatteryMenu.java similarity index 53% rename from src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMachineMenu.java rename to src/main/java/ru/dbotthepony/mc/otm/menu/MatteryMenu.java index 2d59670d4..b3e29f360 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMachineMenu.java +++ b/src/main/java/ru/dbotthepony/mc/otm/menu/MatteryMenu.java @@ -1,77 +1,32 @@ package ru.dbotthepony.mc.otm.menu; import net.minecraft.core.BlockPos; -import net.minecraft.world.Container; -import net.minecraft.world.SimpleContainer; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; -import net.minecraftforge.common.util.LazyOptional; -import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatteryPoweredMachine; -import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage; -import ru.dbotthepony.mc.otm.capability.MatteryCapability; -import ru.dbotthepony.mc.otm.menu.data.BigDecimalDataContainer; -import ru.dbotthepony.mc.otm.menu.slot.BatterySlot; +import net.minecraft.world.level.block.entity.BlockEntity; +import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatteryMachine; import javax.annotation.Nullable; -abstract public class PoweredMachineMenu extends AbstractContainerMenu { - protected BlockEntityMatteryPoweredMachine tile; +public abstract class MatteryMenu extends AbstractContainerMenu { + protected BlockEntity tile; + protected Inventory inventory; protected Player ply; - public BigDecimalDataContainer energy; - public BigDecimalDataContainer max_energy; - protected PoweredMachineMenu( - @Nullable MenuType menuType, - int containerID, - Inventory inventory, - @Nullable BlockEntityMatteryPoweredMachine tile - ) { - super(menuType, containerID); - this.tile = tile; + protected MatteryMenu(@Nullable MenuType p_38851_, int p_38852_, Inventory inventory) { + super(p_38851_, p_38852_); this.inventory = inventory; this.ply = inventory.player; - - if (tile == null) { - energy = new BigDecimalDataContainer(); - max_energy = new BigDecimalDataContainer(); - } else { - energy = new BigDecimalDataContainer() { - @Override - protected void updateValue() { - LazyOptional capability = tile.getCapability(MatteryCapability.ENERGY); - - if (capability.isPresent()) - setDecimal(capability.resolve().get().getBatteryLevel()); - } - }; - - max_energy = new BigDecimalDataContainer() { - @Override - protected void updateValue() { - LazyOptional capability = tile.getCapability(MatteryCapability.ENERGY); - - if (capability.isPresent()) - setDecimal(capability.resolve().get().getMaxBatteryLevel()); - } - }; - } - - this.addDataSlots(energy); - this.addDataSlots(max_energy); } - protected void addBatterySlot() { - addBatterySlot(8, 66); - } - - protected void addBatterySlot(int x, int y) { - Container battery_container = tile != null ? tile.battery_container : new SimpleContainer(1); - this.addSlot(new BatterySlot(battery_container, 0, x, y)); + protected MatteryMenu(@Nullable MenuType p_38851_, int p_38852_, Inventory inventory, BlockEntity tile) { + this(p_38851_, p_38852_, inventory); + this.tile = tile; } protected void addInventorySlots() { @@ -117,6 +72,9 @@ abstract public class PoweredMachineMenu extends AbstractContainerMenu { return player.distanceToSqr((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D) <= 64.0D; } + abstract protected int getWorkingSlotStart(); + abstract protected int getWorkingSlotEnd(); + // This method receive Player interactor and slot_index where Shift + Right click occurred // It shall return item stack that got moved @Override @@ -128,6 +86,31 @@ abstract public class PoweredMachineMenu extends AbstractContainerMenu { // if TRUE, iteration order is end_slot_index -> start_slot_index // if FALSE iteration order is start_slot_index -> end_slot_index - return ItemStack.EMPTY; + ItemStack moved = ItemStack.EMPTY; + Slot get_slot = this.slots.get(slot_index); + + if (get_slot.hasItem()) { + ItemStack slot_item = get_slot.getItem(); + moved = slot_item.copy(); + + if (slot_index < inventory_slot_index_start) { + // Moving FROM machine TO inventory + + if (!moveItemStackTo(slot_item, inventory_slot_index_start, inventory_slot_index_end + 1, false)) { + return ItemStack.EMPTY; + } + } else if (!moveItemStackTo(slot_item, getWorkingSlotStart(), getWorkingSlotEnd(), false)) { + // Moving FROM inventory TO machine + return ItemStack.EMPTY; + } + + if (slot_item.isEmpty()) { + get_slot.set(ItemStack.EMPTY); + } else { + get_slot.setChanged(); + } + } + + return moved; } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMatteryMenu.java b/src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMatteryMenu.java new file mode 100644 index 000000000..dc067b296 --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMatteryMenu.java @@ -0,0 +1,67 @@ +package ru.dbotthepony.mc.otm.menu; + +import net.minecraft.world.Container; +import net.minecraft.world.SimpleContainer; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.MenuType; +import net.minecraftforge.common.util.LazyOptional; +import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatteryPoweredMachine; +import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage; +import ru.dbotthepony.mc.otm.capability.MatteryCapability; +import ru.dbotthepony.mc.otm.menu.data.BigDecimalDataContainer; +import ru.dbotthepony.mc.otm.menu.slot.BatterySlot; + +import javax.annotation.Nullable; + +abstract public class PoweredMatteryMenu extends MatteryMenu { + protected BlockEntityMatteryPoweredMachine tile; + + public BigDecimalDataContainer energy; + public BigDecimalDataContainer max_energy; + + protected PoweredMatteryMenu( + @Nullable MenuType menuType, + int containerID, + Inventory inventory, + @Nullable BlockEntityMatteryPoweredMachine tile + ) { + super(menuType, containerID, inventory, tile); + + if (tile == null) { + energy = new BigDecimalDataContainer(); + max_energy = new BigDecimalDataContainer(); + } else { + energy = new BigDecimalDataContainer() { + @Override + protected void updateValue() { + LazyOptional capability = tile.getCapability(MatteryCapability.ENERGY); + + if (capability.isPresent()) + setDecimal(capability.resolve().get().getBatteryLevel()); + } + }; + + max_energy = new BigDecimalDataContainer() { + @Override + protected void updateValue() { + LazyOptional capability = tile.getCapability(MatteryCapability.ENERGY); + + if (capability.isPresent()) + setDecimal(capability.resolve().get().getMaxBatteryLevel()); + } + }; + } + + this.addDataSlots(energy); + this.addDataSlots(max_energy); + } + + protected void addBatterySlot() { + addBatterySlot(8, 66); + } + + protected void addBatterySlot(int x, int y) { + Container battery_container = tile != null ? tile.battery_container : new SimpleContainer(1); + this.addSlot(new BatterySlot(battery_container, 0, x, y)); + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/screen/AndroidStationScreen.java b/src/main/java/ru/dbotthepony/mc/otm/screen/AndroidStationScreen.java index 2020e365d..88ba6c857 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/screen/AndroidStationScreen.java +++ b/src/main/java/ru/dbotthepony/mc/otm/screen/AndroidStationScreen.java @@ -1,20 +1,11 @@ package ru.dbotthepony.mc.otm.screen; -import com.mojang.blaze3d.systems.RenderSystem; -import com.mojang.blaze3d.vertex.PoseStack; -import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; -import net.minecraft.client.renderer.GameRenderer; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Inventory; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import ru.dbotthepony.mc.otm.OverdriveThatMatters; -import ru.dbotthepony.mc.otm.capability.MatteryCapability; import ru.dbotthepony.mc.otm.menu.AndroidStationMenu; -import java.math.BigDecimal; - public class AndroidStationScreen extends PoweredMachineScreen { private static final ResourceLocation CONTAINER_BACKGROUND = new ResourceLocation(OverdriveThatMatters.MOD_ID, "textures/gui/android_station.png"); diff --git a/src/main/java/ru/dbotthepony/mc/otm/screen/BatteryBankScreen.java b/src/main/java/ru/dbotthepony/mc/otm/screen/BatteryBankScreen.java new file mode 100644 index 000000000..b1d615a3c --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/screen/BatteryBankScreen.java @@ -0,0 +1,38 @@ +package ru.dbotthepony.mc.otm.screen; + +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Inventory; +import ru.dbotthepony.mc.otm.OverdriveThatMatters; +import ru.dbotthepony.mc.otm.menu.AndroidStationMenu; +import ru.dbotthepony.mc.otm.menu.BatteryBankMenu; +import ru.dbotthepony.mc.otm.menu.MatteryMenu; + +public class BatteryBankScreen extends MatteryScreen { + private static final ResourceLocation CONTAINER_BACKGROUND = new ResourceLocation(OverdriveThatMatters.MOD_ID, "textures/gui/battery_bank.png"); + + @Override + protected ResourceLocation CONTAINER_BACKGROUND() { + return CONTAINER_BACKGROUND; + } + + public BatteryBankScreen(BatteryBankMenu p_97741_, Inventory p_97742_, Component p_97743_) { + super(p_97741_, p_97742_, p_97743_); + } + + @Override + protected void renderTooltip(PoseStack p_97791_, int mouseX, int mouseY) { + super.renderTooltip(p_97791_, mouseX, mouseY); + + if (this.hoveredSlot == null && menu.getCarried().isEmpty() && hovering_power_gauge) { + this.renderComponentToolTip(p_97791_, getFormattedPowerLevel(() -> menu.energy.getDecimal(), () -> menu.max_energy.getDecimal()), mouseX, mouseY, font); + } + } + + @Override + protected void renderBg(PoseStack pose, float p_97788_, int mouseX, int mouseY) { + super.renderBg(pose, p_97788_, mouseX, mouseY); + renderPowerGauge(pose, 15, 24, mouseX, mouseY, () -> menu.energy.getDecimal(), () -> menu.max_energy.getDecimal()); + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/screen/MatteryScreen.java b/src/main/java/ru/dbotthepony/mc/otm/screen/MatteryScreen.java new file mode 100644 index 000000000..ea5a56ab1 --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/screen/MatteryScreen.java @@ -0,0 +1,110 @@ +package ru.dbotthepony.mc.otm.screen; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.FormattedText; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Inventory; +import ru.dbotthepony.mc.otm.OverdriveThatMatters; +import ru.dbotthepony.mc.otm.capability.MatteryCapability; +import ru.dbotthepony.mc.otm.menu.FormattingHelper; +import ru.dbotthepony.mc.otm.menu.MatteryMenu; +import ru.dbotthepony.mc.otm.menu.PoweredMatteryMenu; + +import java.math.BigDecimal; +import java.util.List; +import java.util.function.Supplier; + +public class MatteryScreen extends AbstractContainerScreen { + protected static final ResourceLocation CONTAINER_BASE = new ResourceLocation(OverdriveThatMatters.MOD_ID, "textures/gui/mattery_machine_base.png"); + + protected ResourceLocation CONTAINER_BACKGROUND() { + return CONTAINER_BASE; + } + + public MatteryScreen(T p_97741_, Inventory p_97742_, Component p_97743_) { + super(p_97741_, p_97742_, p_97743_); + + imageWidth = 176; + imageHeight = 178; + titleLabelY = 4; + + inventoryLabelY = imageHeight - 92; + } + + protected boolean hovering_power_gauge = false; + + protected List getFormattedPowerLevel(Supplier s_energy, Supplier s_max_energy) { + BigDecimal energy = s_energy.get(); + BigDecimal max_energy = s_max_energy.get(); + + float level; + + if (max_energy.compareTo(BigDecimal.ZERO) == 0) { + level = 0f; + } else { + level = energy.divide(max_energy, MatteryCapability.ROUND_RULES).floatValue(); + } + + return List.of( + new TranslatableComponent("otm.gui.power.percentage_level", String.format("%.2f", level * 100d)), + FormattingHelper.formatPowerLevel(energy, max_energy) + ); + } + + protected boolean renderPowerGauge(PoseStack pose, int x, int y, int mouseX, int mouseY, Supplier s_energy, Supplier s_max_energy) { + BigDecimal energy = s_energy.get(); + BigDecimal max_energy = s_max_energy.get(); + + int i = (width - imageWidth) / 2; + int j = (height - imageHeight) / 2; + + float level; + + if (max_energy.compareTo(BigDecimal.ZERO) == 0) { + level = 0f; + } else { + level = energy.divide(max_energy, MatteryCapability.ROUND_RULES).floatValue(); + + if (level >= 0.98) + level = 1f; + } + + if (level > 0.01) { + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + RenderSystem.setShaderTexture(0, CONTAINER_BASE); + + int intHeight = (int) Math.floor(level * 46d + 0.5d); + + this.blit(pose, i + x, j + y + 46 - intHeight, 176, 0, 7, intHeight); + } + + x += leftPos; + y += topPos; + hovering_power_gauge = x <= mouseX && x + 7 >= mouseX && y <= mouseY && y + 46 >= mouseY; + return hovering_power_gauge; + } + + @Override + public void render(PoseStack p_98418_, int mouseX, int mouseY, float p_98421_) { + this.renderBackground(p_98418_); + super.render(p_98418_, mouseX, mouseY, p_98421_); + this.renderTooltip(p_98418_, mouseX, mouseY); + } + + @Override + protected void renderBg(PoseStack pose, float p_97788_, int mouseX, int mouseY) { + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + RenderSystem.setShaderTexture(0, CONTAINER_BACKGROUND()); + int i = (width - imageWidth) / 2; + int j = (height - imageHeight) / 2; + // PoseStack, x, y, image_start_x, image_start_y, rect_size_x, rect_size_y + this.blit(pose, i, j, 0, 0, this.imageWidth, this.imageHeight); + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/screen/PoweredMachineScreen.java b/src/main/java/ru/dbotthepony/mc/otm/screen/PoweredMachineScreen.java index 5266f959f..9816b1864 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/screen/PoweredMachineScreen.java +++ b/src/main/java/ru/dbotthepony/mc/otm/screen/PoweredMachineScreen.java @@ -1,60 +1,20 @@ package ru.dbotthepony.mc.otm.screen; -import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; -import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; -import net.minecraft.client.renderer.GameRenderer; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.FormattedText; -import net.minecraft.network.chat.TextComponent; -import net.minecraft.network.chat.TranslatableComponent; -import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Inventory; -import ru.dbotthepony.mc.otm.OverdriveThatMatters; -import ru.dbotthepony.mc.otm.capability.MatteryCapability; -import ru.dbotthepony.mc.otm.menu.FormattingHelper; -import ru.dbotthepony.mc.otm.menu.PoweredMachineMenu; +import ru.dbotthepony.mc.otm.menu.PoweredMatteryMenu; -import java.math.BigDecimal; import java.util.List; -public class PoweredMachineScreen extends AbstractContainerScreen { - private static final ResourceLocation CONTAINER_BACKGROUND = new ResourceLocation(OverdriveThatMatters.MOD_ID, "textures/gui/mattery_machine_base.png"); - +public class PoweredMachineScreen extends MatteryScreen { protected boolean auto_draw_gauge = true; protected int power_gauge_x = 13; protected int power_gauge_y = 15; - protected ResourceLocation CONTAINER_BACKGROUND() { - return CONTAINER_BACKGROUND; - } - public PoweredMachineScreen(T p_97741_, Inventory p_97742_, Component p_97743_) { super(p_97741_, p_97742_, p_97743_); - - imageWidth = 176; - imageHeight = 178; - titleLabelY = 4; - - inventoryLabelY = imageHeight - 92; - } - - protected List getFormattedPowerLevel() { - BigDecimal energy = menu.energy.getDecimal(); - BigDecimal max_energy = menu.max_energy.getDecimal(); - - float level; - - if (max_energy.equals(BigDecimal.ZERO)) { - level = 0f; - } else { - level = energy.divide(max_energy, MatteryCapability.ROUND_RULES).floatValue(); - } - - return List.of( - new TranslatableComponent("otm.gui.power.percentage_level", String.format("%.2f", level * 100d)), - FormattingHelper.formatPowerLevel(energy, max_energy) - ); } @Override @@ -66,57 +26,17 @@ public class PoweredMachineScreen extends Abstract } } - @Override - public void render(PoseStack p_98418_, int mouseX, int mouseY, float p_98421_) { - this.renderBackground(p_98418_); - super.render(p_98418_, mouseX, mouseY, p_98421_); - this.renderTooltip(p_98418_, mouseX, mouseY); + protected List getFormattedPowerLevel() { + return getFormattedPowerLevel(() -> menu.energy.getDecimal(), () -> menu.max_energy.getDecimal()); } - protected boolean hovering_power_gauge = false; - protected void renderPowerGauge(PoseStack pose, int x, int y, int mouseX, int mouseY) { - BigDecimal energy = menu.energy.getDecimal(); - BigDecimal max_energy = menu.max_energy.getDecimal(); - - int i = (width - imageWidth) / 2; - int j = (height - imageHeight) / 2; - - float level; - - if (max_energy.equals(BigDecimal.ZERO)) { - level = 0f; - } else { - level = energy.divide(max_energy, MatteryCapability.ROUND_RULES).floatValue(); - - if (level >= 0.98) - level = 1f; - } - - if (level > 0.01) { - RenderSystem.setShader(GameRenderer::getPositionTexShader); - RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - RenderSystem.setShaderTexture(0, CONTAINER_BACKGROUND); - - int intHeight = (int) Math.floor(level * 46d + 0.5d); - - this.blit(pose, i + 13, j + 15 + 46 - intHeight, 176, 0, 7, intHeight); - } - - x += leftPos; - y += topPos; - hovering_power_gauge = x <= mouseX && x + 7 >= mouseX && y <= mouseY && y + 46 >= mouseY; + renderPowerGauge(pose, x, y, mouseX, mouseY, () -> menu.energy.getDecimal(), () -> menu.max_energy.getDecimal()); } @Override protected void renderBg(PoseStack pose, float p_97788_, int mouseX, int mouseY) { - RenderSystem.setShader(GameRenderer::getPositionTexShader); - RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - RenderSystem.setShaderTexture(0, CONTAINER_BACKGROUND()); - int i = (width - imageWidth) / 2; - int j = (height - imageHeight) / 2; - // PoseStack, x, y, image_start_x, image_start_y, rect_size_x, rect_size_y - this.blit(pose, i, j, 0, 0, this.imageWidth, this.imageHeight); + super.renderBg(pose, p_97788_, mouseX, mouseY); if (auto_draw_gauge) renderPowerGauge(pose, power_gauge_x, power_gauge_y, mouseX, mouseY); diff --git a/src/main/resources/assets/overdrive_that_matters/lang/en_us.json b/src/main/resources/assets/overdrive_that_matters/lang/en_us.json index b30e0431a..f9f80b21c 100644 --- a/src/main/resources/assets/overdrive_that_matters/lang/en_us.json +++ b/src/main/resources/assets/overdrive_that_matters/lang/en_us.json @@ -33,7 +33,11 @@ "otm.suffix.yocto": "%s y%s", "container.otm.android_station": "Android station", + "container.otm.battery_bank": "Battery bank", + "block.overdrive_that_matters.android_station": "Android station", + "block.overdrive_that_matters.battery_bank": "Battery bank", + "item.overdrive_that_matters.pill_android": "Android pill", "item.overdrive_that_matters.pill_humane": "Humane pill", diff --git a/src/main/resources/assets/overdrive_that_matters/textures/gui/battery_bank.png b/src/main/resources/assets/overdrive_that_matters/textures/gui/battery_bank.png new file mode 100644 index 000000000..8339cb0f2 Binary files /dev/null and b/src/main/resources/assets/overdrive_that_matters/textures/gui/battery_bank.png differ diff --git a/src/main/resources/assets/overdrive_that_matters/textures/gui/battery_bank.xcf b/src/main/resources/assets/overdrive_that_matters/textures/gui/battery_bank.xcf new file mode 100644 index 000000000..7321a9955 Binary files /dev/null and b/src/main/resources/assets/overdrive_that_matters/textures/gui/battery_bank.xcf differ diff --git a/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets.xcf b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets.xcf new file mode 100644 index 000000000..20f1f2acc Binary files /dev/null and b/src/main/resources/assets/overdrive_that_matters/textures/gui/widgets.xcf differ diff --git a/src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json b/src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json index 662aedfdb..4d4aba90f 100644 --- a/src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json +++ b/src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json @@ -1,6 +1,7 @@ { "replace": false, "values": [ - "overdrive_that_matters:android_station" + "overdrive_that_matters:android_station", + "overdrive_that_matters:battery_bank" ] } \ No newline at end of file diff --git a/src/main/resources/data/overdrive_that_matters/loot_tables/blocks/battery_bank.json b/src/main/resources/data/overdrive_that_matters/loot_tables/blocks/battery_bank.json new file mode 100644 index 000000000..2c840b563 --- /dev/null +++ b/src/main/resources/data/overdrive_that_matters/loot_tables/blocks/battery_bank.json @@ -0,0 +1,37 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "rolls": 1, + "bonus_rolls": 0.0, + "entries": [ + { + "type": "minecraft:item", + "functions": [ + { + "function": "minecraft:copy_name", + "source": "block_entity" + }, + { + "function": "minecraft:copy_nbt", + "source": "block_entity", + "ops": [ + { + "source": "battery_bank", + "target": "BlockEntityTag.battery_bank", + "op": "replace" + }, + { + "source": "Name", + "target": "BlockEntityTag.Name", + "op": "replace" + } + ] + } + ], + "name": "overdrive_that_matters:battery_bank" + } + ] + } + ] +} \ No newline at end of file