mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-12-22 19:39:24 +01:00
Modified functionalBlock, added batterybox basic func and TER
This commit is contained in:
parent
a742536948
commit
87deb66cce
9 changed files with 184 additions and 20 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<BatteryBoxTile> {
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
package de.ellpeck.actuallyadditions.client.render.tiles;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
|
@ -25,12 +25,10 @@ import java.util.function.Supplier;
|
|||
*/
|
||||
public abstract class FunctionalBlock extends ActuallyBlock {
|
||||
private final Supplier<TileEntity> createTile;
|
||||
private final Class<? extends TileEntity> tileClass;
|
||||
|
||||
public FunctionalBlock(Properties properties, Supplier<TileEntity> tile, Class<? extends TileEntity> tileClass) {
|
||||
public FunctionalBlock(Properties properties, Supplier<TileEntity> 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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<Integer, Boolean, String> PRETTY = (value, alt) ->
|
||||
alt ? NumberFormat.getIntegerInstance().format(value) : Help.compressedValue(value);
|
||||
alt ? NumberFormat.getIntegerInstance().format(value) : Help.humanReadableValue(value);
|
||||
|
||||
private final Supplier<Integer> 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)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -11,4 +11,6 @@ public final class ActuallyTiles {
|
|||
public static final DeferredRegister<TileEntityType<?>> TILES = DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, ActuallyAdditions.MOD_ID);
|
||||
|
||||
public static final RegistryObject<TileEntityType<FeederTileEntity>> FEEDER_TILE = TILES.register("feeder_tile", () -> TileEntityType.Builder.create(FeederTileEntity::new, ActuallyBlocks.FEEDER.get()).build(null));
|
||||
public static final RegistryObject<TileEntityType<BatteryBoxTile>> BATTERY_BOX_TILE = TILES.register("battery_bot_tile", () -> TileEntityType.Builder.create(BatteryBoxTile::new, ActuallyBlocks.BATTERY_BOX.get()).build(null));
|
||||
|
||||
}
|
||||
|
|
|
@ -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<IItemHandler> handler = LazyOptional.of(() -> itemStackHandler);
|
||||
|
||||
public BatteryBoxTile() {
|
||||
super(ActuallyTiles.BATTERY_BOX_TILE.get());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> 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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue