From 87deb66ccebcfb9f5d0f854d2fc40a5380b8e943 Mon Sep 17 00:00:00 2001 From: Michael Hillcox Date: Sun, 17 Jan 2021 13:45:56 +0000 Subject: [PATCH] Modified functionalBlock, added batterybox basic func and TER --- .../actuallyadditions/client/ClientSetup.java | 10 +++ .../render/tiles/BatteryBoxTileRender.java | 81 +++++++++++++++++++ .../client/render/tiles/package-info.java | 7 ++ .../common/blocks/FunctionalBlock.java | 22 +++-- .../blocks/functional/BatteryBoxBlock.java | 2 +- .../common/blocks/functional/FeederBlock.java | 2 +- .../common/items/CrystalFluxItem.java | 7 +- .../common/tiles/ActuallyTiles.java | 2 + .../common/tiles/BatteryBoxTile.java | 71 +++++++++++++++- 9 files changed, 184 insertions(+), 20 deletions(-) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/BatteryBoxTileRender.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/package-info.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/client/ClientSetup.java b/src/main/java/de/ellpeck/actuallyadditions/client/ClientSetup.java index 9e49a30e6..289a8c3a4 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/client/ClientSetup.java +++ b/src/main/java/de/ellpeck/actuallyadditions/client/ClientSetup.java @@ -1,17 +1,27 @@ package de.ellpeck.actuallyadditions.client; +import de.ellpeck.actuallyadditions.client.render.tiles.BatteryBoxTileRender; import de.ellpeck.actuallyadditions.client.screens.DrillScreen; import de.ellpeck.actuallyadditions.client.screens.FeederScreen; +import de.ellpeck.actuallyadditions.common.ActuallyAdditions; import de.ellpeck.actuallyadditions.common.container.ActuallyContainers; +import de.ellpeck.actuallyadditions.common.tiles.ActuallyTiles; import net.minecraft.client.gui.ScreenManager; +import net.minecraftforge.fml.client.registry.ClientRegistry; public class ClientSetup { public static void setup() { setupScreens(); + setupTileRenders(); } private static void setupScreens() { ScreenManager.registerFactory(ActuallyContainers.DRILL_CONTAINER.get(), DrillScreen::new); ScreenManager.registerFactory(ActuallyContainers.FEEDER_CONTAINER.get(), FeederScreen::new); } + + private static void setupTileRenders() { + ActuallyAdditions.LOGGER.debug("Setting up tile entity renderers"); + ClientRegistry.bindTileEntityRenderer(ActuallyTiles.BATTERY_BOX_TILE.get(), BatteryBoxTileRender::new); + } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/BatteryBoxTileRender.java b/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/BatteryBoxTileRender.java new file mode 100644 index 000000000..03b67446d --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/BatteryBoxTileRender.java @@ -0,0 +1,81 @@ +package de.ellpeck.actuallyadditions.client.render.tiles; + +import com.mojang.blaze3d.matrix.MatrixStack; +import de.ellpeck.actuallyadditions.common.ActuallyAdditions; +import de.ellpeck.actuallyadditions.common.items.useables.BatteryItem; +import de.ellpeck.actuallyadditions.common.tiles.BatteryBoxTile; +import de.ellpeck.actuallyadditions.common.utilities.ClientHelp; +import de.ellpeck.actuallyadditions.common.utilities.Help; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.renderer.IRenderTypeBuffer; +import net.minecraft.client.renderer.model.ItemCameraTransforms; +import net.minecraft.client.renderer.tileentity.TileEntityRenderer; +import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; +import net.minecraft.client.resources.I18n; +import net.minecraft.item.BlockItem; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Util; +import net.minecraft.util.math.vector.Quaternion; +import net.minecraft.util.math.vector.Vector3f; +import net.minecraftforge.energy.CapabilityEnergy; + +import java.text.NumberFormat; + +public class BatteryBoxTileRender extends TileEntityRenderer { + public BatteryBoxTileRender(TileEntityRendererDispatcher rendererDispatcherIn) { + super(rendererDispatcherIn); + } + + @Override + public void render(BatteryBoxTile tile, float partialTicks, MatrixStack matrices, IRenderTypeBuffer buffer, int combinedLight, int combinedOverlay) { + ItemStack stack = tile.getItemStackHandler().getStackInSlot(0); + if (stack.isEmpty() || !(stack.getItem() instanceof BatteryItem)) + return; + + matrices.push(); + matrices.translate(.5f, .35f, .5f); + matrices.rotate(Vector3f.ZP.rotationDegrees(180)); + + matrices.push(); + matrices.scale(0.0075F, 0.0075F, 0.0075F); + matrices.translate(0F, 0F, -60F); + + // Display the energy value on each side of the block. + stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(energy -> { + FontRenderer font = Minecraft.getInstance().fontRenderer; + + String energyTotal = Help.cleanEnergyValues(energy, false); + String energyName = ClientHelp.i18n("energy.crystal-flux-long"); + + for (int i = 0; i < 4; i++) { + font.drawString(matrices, energyTotal, -font.getStringWidth(energyTotal) / 2F, 10F, 0xFFFFFF); + font.drawString(matrices, energyName, -font.getStringWidth(energyName) / 2F, 20F, 0xFFFFFF); + + matrices.translate(-60F, 0F, 60F); + matrices.rotate(Vector3f.YP.rotationDegrees(90)); + } + }); + + matrices.pop(); // text rotation + matrices.pop(); // rotation + centering + + double boop = Util.milliTime() / 800D; + float scale = stack.getItem() instanceof BlockItem ? 0.85F : 0.65F; + + matrices.push(); + matrices.translate(.5f, 1f + Math.sin(boop % (2 * Math.PI)) * 0.065, .5f); + matrices.rotate(Vector3f.YP.rotationDegrees((float) (boop * 40D % 360))); + matrices.scale(scale, scale, scale); + + try { + Minecraft.getInstance().getItemRenderer().renderItem( + stack, ItemCameraTransforms.TransformType.FIXED, combinedLight, combinedOverlay, matrices, buffer + ); + } catch (Exception e) { + ActuallyAdditions.LOGGER.error("Something went wrong trying to render an item in a battery box! The item is " + stack.getItem().getRegistryName() + "!", e); + } + + matrices.pop(); + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/package-info.java b/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/package-info.java new file mode 100644 index 000000000..5dd601d03 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/package-info.java @@ -0,0 +1,7 @@ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +package de.ellpeck.actuallyadditions.client.render.tiles; + +import mcp.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/FunctionalBlock.java b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/FunctionalBlock.java index 8d853f2fc..94553c8a4 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/FunctionalBlock.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/FunctionalBlock.java @@ -25,12 +25,10 @@ import java.util.function.Supplier; */ public abstract class FunctionalBlock extends ActuallyBlock { private final Supplier createTile; - private final Class tileClass; - public FunctionalBlock(Properties properties, Supplier tile, Class tileClass) { + public FunctionalBlock(Properties properties, Supplier tile) { super(properties); this.createTile = tile; - this.tileClass = tileClass; } /** @@ -41,7 +39,7 @@ public abstract class FunctionalBlock extends ActuallyBlock { public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) { // We should always have a tile TileEntity tile = worldIn.getTileEntity(pos); - if (tile == null || !tile.getClass().isInstance(this.tileClass)) { + if (tile == null) { return ActionResultType.FAIL; } @@ -67,6 +65,14 @@ public abstract class FunctionalBlock extends ActuallyBlock { return this.createTile.get(); } + /** + * Controls if the tile drops items when being broken. By default we'll try and drop anything in and inv cap + * unless told not to. + */ + public boolean dropsInventory(World world, BlockPos pos) { + return true; + } + @Override public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) { super.onReplaced(state, worldIn, pos, newState, isMoving); @@ -77,14 +83,6 @@ public abstract class FunctionalBlock extends ActuallyBlock { } } - /** - * Controls if the tile drops items when being broken. By default we'll try and drop anything in and inv cap - * unless told not to. - */ - public boolean dropsInventory(World world, BlockPos pos) { - return true; - } - public static class ActivatedContext { TileEntity entity; BlockState state; diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/functional/BatteryBoxBlock.java b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/functional/BatteryBoxBlock.java index ee8db45b5..09327090f 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/functional/BatteryBoxBlock.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/functional/BatteryBoxBlock.java @@ -15,7 +15,7 @@ import net.minecraft.world.IBlockReader; public class BatteryBoxBlock extends FunctionalBlock { public BatteryBoxBlock() { - super(Properties.create(Material.ROCK), BatteryBoxTile::new, BatteryBoxTile.class); + super(Properties.create(Material.ROCK), BatteryBoxTile::new); } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/functional/FeederBlock.java b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/functional/FeederBlock.java index d464f4bf8..f11838630 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/functional/FeederBlock.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/functional/FeederBlock.java @@ -16,7 +16,7 @@ import net.minecraftforge.fml.network.NetworkHooks; public class FeederBlock extends FunctionalBlock { public FeederBlock() { - super(Properties.create(Material.ROCK), FeederTileEntity::new, FeederTileEntity.class); + super(Properties.create(Material.ROCK), FeederTileEntity::new); } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/CrystalFluxItem.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/CrystalFluxItem.java index 0e98760c4..5a45e8a56 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/CrystalFluxItem.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/CrystalFluxItem.java @@ -33,7 +33,7 @@ import java.util.function.Supplier; public abstract class CrystalFluxItem extends ActuallyItem { // Handles modifying an int to a string of either alt ? 100,000 : 100K private static final BiFunction PRETTY = (value, alt) -> - alt ? NumberFormat.getIntegerInstance().format(value) : Help.compressedValue(value); + alt ? NumberFormat.getIntegerInstance().format(value) : Help.humanReadableValue(value); private final Supplier maxFlux; private final int transfer; @@ -97,9 +97,8 @@ public abstract class CrystalFluxItem extends ActuallyItem { */ private TranslationTextComponent getEnergyPretty(IEnergyStorage energy, boolean showCompressed) { return Help.trans( - "storage.crystal-flux", - PRETTY.apply(energy.getEnergyStored(), showCompressed), - PRETTY.apply(energy.getMaxEnergyStored(), showCompressed) + "energy.crystal-flux-single", + Help.cleanEnergyValues(energy, showCompressed) ); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/tiles/ActuallyTiles.java b/src/main/java/de/ellpeck/actuallyadditions/common/tiles/ActuallyTiles.java index c8ef28287..c9848950e 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/tiles/ActuallyTiles.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/tiles/ActuallyTiles.java @@ -11,4 +11,6 @@ public final class ActuallyTiles { public static final DeferredRegister> TILES = DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, ActuallyAdditions.MOD_ID); public static final RegistryObject> FEEDER_TILE = TILES.register("feeder_tile", () -> TileEntityType.Builder.create(FeederTileEntity::new, ActuallyBlocks.FEEDER.get()).build(null)); + public static final RegistryObject> BATTERY_BOX_TILE = TILES.register("battery_bot_tile", () -> TileEntityType.Builder.create(BatteryBoxTile::new, ActuallyBlocks.BATTERY_BOX.get()).build(null)); + } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/tiles/BatteryBoxTile.java b/src/main/java/de/ellpeck/actuallyadditions/common/tiles/BatteryBoxTile.java index ee55db26d..d0cb9e8de 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/tiles/BatteryBoxTile.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/tiles/BatteryBoxTile.java @@ -1,6 +1,73 @@ package de.ellpeck.actuallyadditions.common.tiles; -import net.minecraft.tileentity.TileEntity; +import de.ellpeck.actuallyadditions.common.items.useables.BatteryItem; +import net.minecraft.block.BlockState; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.Direction; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.util.LazyOptional; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.ItemStackHandler; -public class BatteryBoxTile extends TileEntity { +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class BatteryBoxTile extends ActuallyTile { + private final ItemStackHandler itemStackHandler = new ItemStackHandler(1) { + @Override + protected void onContentsChanged(int slot) { + BatteryBoxTile.this.markDirty(); + } + + @Override + public int getSlotLimit(int slot) { + return 1; + } + + @Override + public boolean isItemValid(int slot, @Nonnull ItemStack stack) { + return stack.getItem() instanceof BatteryItem; + } + }; + + private final LazyOptional handler = LazyOptional.of(() -> itemStackHandler); + + public BatteryBoxTile() { + super(ActuallyTiles.BATTERY_BOX_TILE.get()); + } + + @Nonnull + @Override + public LazyOptional getCapability(@Nonnull Capability cap, @Nullable Direction side) { + return cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY + ? handler.cast() + : LazyOptional.empty(); + } + + public ItemStackHandler getItemStackHandler() { + return itemStackHandler; + } + + @Override + public void read(BlockState state, CompoundNBT nbt) { + super.read(state, nbt); + + if (nbt.contains("items")) { + itemStackHandler.deserializeNBT(nbt.getCompound("items")); + } + } + + @Override + public CompoundNBT write(CompoundNBT compound) { + compound.put("items", itemStackHandler.serializeNBT()); + return super.write(compound); + } + + @Override + public void remove() { + super.remove(); + this.handler.invalidate(); + } }