Advertisement
Vaerys_Dawn

AnimatedImageComponent

Feb 13th, 2021 (edited)
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. public class AnimatedImageComponent implements ICustomComponent {
  2.  
  3.     IVariable image;
  4.  
  5.     IVariable ticks_per_frame;
  6.     IVariable texture_height;
  7.     IVariable texture_width;
  8.     private transient ResourceLocation animatedImage;
  9.     private transient int xOffset;
  10.     private transient int yOffset;
  11.     private transient int currentFrame = 0;
  12.     private transient int imageHeight;
  13.     private transient int imageWidth;
  14.     private transient int height;
  15.     private transient int width;
  16.     private int frames;
  17.  
  18.  
  19.     @Override
  20.     public void build(int x, int y, int page) {
  21.         xOffset = x;
  22.         yOffset = y;
  23.     }
  24.  
  25.     @Override
  26.     public void render(MatrixStack matrixStack, IComponentRenderContext context, float v, int i, int i1) {
  27.         if (context.getTicksInBook() % ticks_per_frame.asNumber().intValue() == 0) {
  28.             currentFrame++;
  29.         }
  30.         if (currentFrame >= frames) {
  31.             currentFrame = 0;
  32.         }
  33.         Minecraft.getInstance().getTextureManager().bindTexture(animatedImage);
  34.  
  35.         AbstractGui.drawTexture(matrixStack, xOffset, yOffset, 0, height * currentFrame, width / 4, height / 4, imageWidth, imageHeight);
  36.     }
  37.  
  38.     @Override
  39.     public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
  40.         image = lookup.apply(image);
  41.         ticks_per_frame = lookup.apply(ticks_per_frame);
  42.         this.animatedImage = new ResourceLocation(image.asString());
  43.         texture_height = lookup.apply(texture_height);
  44.         texture_width = lookup.apply(texture_width);
  45.         imageHeight = texture_height.asNumber().intValue();
  46.         imageWidth = texture_width.asNumber().intValue();
  47.         height = imageWidth;
  48.         width = imageWidth;
  49.         this.frames = imageHeight / imageWidth;
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement