2019-03-23 20:49:34 +01:00
|
|
|
package com.minelittlepony.client.render;
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2020-04-03 23:54:12 +02:00
|
|
|
import com.minelittlepony.api.pony.IPony;
|
2019-07-08 04:47:13 +02:00
|
|
|
import com.minelittlepony.client.MineLittlePony;
|
2020-04-16 23:35:28 +02:00
|
|
|
import com.minelittlepony.common.util.Color;
|
2019-11-23 18:28:42 +01:00
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
import net.minecraft.client.MinecraftClient;
|
2019-11-23 18:28:42 +01:00
|
|
|
import net.minecraft.client.render.OverlayTexture;
|
2019-11-29 19:11:31 +01:00
|
|
|
import net.minecraft.client.render.RenderLayer;
|
2019-11-23 18:28:42 +01:00
|
|
|
import net.minecraft.client.render.VertexConsumerProvider;
|
2019-05-27 17:59:15 +02:00
|
|
|
import net.minecraft.client.render.item.ItemRenderer;
|
|
|
|
import net.minecraft.client.render.model.json.ModelTransformation;
|
2019-11-30 11:14:24 +01:00
|
|
|
import net.minecraft.client.texture.SpriteAtlasTexture;
|
2019-11-23 18:28:42 +01:00
|
|
|
import net.minecraft.client.util.math.MatrixStack;
|
|
|
|
import net.minecraft.client.util.math.Vector3f;
|
2019-05-27 17:59:15 +02:00
|
|
|
import net.minecraft.entity.LivingEntity;
|
2019-11-30 17:49:04 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2018-05-26 23:37:50 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-07-20 20:16:54 +02:00
|
|
|
import net.minecraft.util.Arm;
|
2019-11-30 11:14:24 +01:00
|
|
|
import net.minecraft.util.Identifier;
|
2019-05-27 17:59:15 +02:00
|
|
|
import net.minecraft.util.UseAction;
|
2019-11-23 18:28:42 +01:00
|
|
|
import net.minecraft.world.World;
|
2019-05-27 17:59:15 +02:00
|
|
|
|
2018-05-26 23:37:50 +02:00
|
|
|
public class LevitatingItemRenderer {
|
|
|
|
|
2019-11-29 19:11:31 +01:00
|
|
|
static int tint;
|
2019-06-27 11:48:59 +02:00
|
|
|
private static boolean usingTransparency;
|
|
|
|
|
|
|
|
public static boolean usesTransparency() {
|
|
|
|
return usingTransparency;
|
2018-08-20 22:24:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-30 11:14:24 +01:00
|
|
|
public static RenderLayer getRenderLayer(Identifier texture) {
|
|
|
|
if (!usesTransparency()) {
|
|
|
|
return RenderLayer.getEntityTranslucent(texture);
|
|
|
|
}
|
|
|
|
return MagicGlow.getTintedTexturedLayer(texture, Color.r(tint), Color.g(tint), Color.b(tint), 0.8F);
|
|
|
|
}
|
|
|
|
|
2019-11-29 19:11:31 +01:00
|
|
|
public static RenderLayer getRenderLayer() {
|
2020-11-10 20:38:05 +01:00
|
|
|
return getRenderLayer(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE);
|
2019-11-29 19:11:31 +01:00
|
|
|
}
|
|
|
|
|
2018-05-26 23:37:50 +02:00
|
|
|
/**
|
|
|
|
* Renders a magical overlay over an item in third person.
|
|
|
|
*/
|
2020-01-08 20:17:05 +01:00
|
|
|
public void renderItemGlow(LivingEntity entity, ItemStack drop, ModelTransformation.Mode transform, Arm hand, int glowColor, MatrixStack stack, VertexConsumerProvider renderContext) {
|
2018-05-26 23:37:50 +02:00
|
|
|
setColor(glowColor);
|
2019-11-29 19:11:31 +01:00
|
|
|
stack.push();
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
ItemRenderer renderItem = MinecraftClient.getInstance().getItemRenderer();
|
2019-06-27 11:48:59 +02:00
|
|
|
|
2019-11-23 18:28:42 +01:00
|
|
|
stack.scale(1.1F, 1.1F, 1.1F);
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-11-23 18:28:42 +01:00
|
|
|
stack.translate(0.01F, 0.01F, 0.01F);
|
2019-12-10 17:11:26 +01:00
|
|
|
renderItem.renderItem(entity, drop, transform, hand == Arm.LEFT, stack, renderContext, entity.world, 0x0F00F0, OverlayTexture.DEFAULT_UV);
|
2019-11-23 18:28:42 +01:00
|
|
|
stack.translate(-0.02F, -0.02F, -0.02F);
|
2019-12-10 17:11:26 +01:00
|
|
|
renderItem.renderItem(entity, drop, transform, hand == Arm.LEFT, stack, renderContext, entity.world, 0x0F00F0, OverlayTexture.DEFAULT_UV);
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-11-23 18:28:42 +01:00
|
|
|
stack.pop();
|
2019-11-29 19:11:31 +01:00
|
|
|
unsetColor();
|
2018-05-26 23:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setColor(int glowColor) {
|
2019-11-29 19:11:31 +01:00
|
|
|
usingTransparency = true;
|
|
|
|
tint = glowColor;
|
2018-05-26 23:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void unsetColor() {
|
2019-11-29 19:11:31 +01:00
|
|
|
usingTransparency = false;
|
|
|
|
tint = 0;
|
2018-05-26 23:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders an item in first person optionally with a magical overlay.
|
|
|
|
*/
|
2020-01-08 20:17:05 +01:00
|
|
|
public void renderItemInFirstPerson(ItemRenderer itemRenderer, @Nullable LivingEntity entity, ItemStack stack, ModelTransformation.Mode transform, boolean left, MatrixStack matrix, VertexConsumerProvider renderContext, @Nullable World world, int lightUv) {
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-11-30 17:49:04 +01:00
|
|
|
if (entity instanceof PlayerEntity) {
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-11-30 17:49:04 +01:00
|
|
|
IPony pony = MineLittlePony.getInstance().getManager().getPony((PlayerEntity)entity);
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-11-30 17:49:04 +01:00
|
|
|
matrix.push();
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-11-30 17:49:04 +01:00
|
|
|
boolean doMagic = MineLittlePony.getInstance().getConfig().fpsmagic.get() && pony.getMetadata().hasMagic();
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-11-30 17:49:04 +01:00
|
|
|
if (doMagic) {
|
|
|
|
setupPerspective(itemRenderer, entity, stack, left, matrix);
|
|
|
|
}
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-12-10 17:11:26 +01:00
|
|
|
itemRenderer.renderItem(entity, stack, transform, left, matrix, renderContext, world, lightUv, OverlayTexture.DEFAULT_UV);
|
2019-03-24 18:55:15 +01:00
|
|
|
|
2019-11-30 17:49:04 +01:00
|
|
|
if (doMagic) {
|
|
|
|
setColor(pony.getMetadata().getGlowColor());
|
|
|
|
|
|
|
|
matrix.scale(1.1F, 1.1F, 1.1F);
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-11-30 17:49:04 +01:00
|
|
|
matrix.translate(0.015F, 0.01F, 0.01F);
|
2019-12-10 17:11:26 +01:00
|
|
|
itemRenderer.renderItem(entity, stack, transform, left, matrix, renderContext, world, lightUv, OverlayTexture.DEFAULT_UV);
|
2019-11-30 17:49:04 +01:00
|
|
|
matrix.translate(-0.03F, -0.02F, -0.02F);
|
2019-12-10 17:11:26 +01:00
|
|
|
itemRenderer.renderItem(entity, stack, transform, left, matrix, renderContext, world, lightUv, OverlayTexture.DEFAULT_UV);
|
2019-11-30 17:49:04 +01:00
|
|
|
|
|
|
|
unsetColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix.pop();
|
|
|
|
} else {
|
2019-12-10 17:11:26 +01:00
|
|
|
itemRenderer.renderItem(entity, stack, transform, left, matrix, renderContext, world, lightUv, OverlayTexture.DEFAULT_UV);
|
2019-11-30 17:49:04 +01:00
|
|
|
}
|
2018-05-26 23:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves held items to look like they're floating in the player's field.
|
|
|
|
*/
|
2019-11-23 18:28:42 +01:00
|
|
|
private void setupPerspective(ItemRenderer renderer, LivingEntity entity, ItemStack item, boolean left, MatrixStack stack) {
|
|
|
|
UseAction action = item.getUseAction();
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-06-11 12:22:58 +02:00
|
|
|
boolean doNormal = entity.getItemUseTime() <= 0 || action == UseAction.NONE || action == UseAction.CROSSBOW;
|
2018-05-26 23:37:50 +02:00
|
|
|
|
|
|
|
if (doNormal) { // eating, blocking, and drinking are not transformed. Only held items.
|
2020-04-17 00:41:59 +02:00
|
|
|
int sign = left ? 1 : -1;
|
|
|
|
float ticks = entity.age * sign;
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2020-04-17 00:41:59 +02:00
|
|
|
float floatAmount = -(float)Math.sin(ticks / 9F) / 40F;
|
|
|
|
float driftAmount = -(float)Math.cos(ticks / 6F) / 40F;
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2019-11-23 18:28:42 +01:00
|
|
|
boolean handHeldTool =
|
|
|
|
action == UseAction.BOW
|
2019-06-11 12:22:58 +02:00
|
|
|
|| action == UseAction.CROSSBOW
|
|
|
|
|| action == UseAction.BLOCK;
|
2018-05-26 23:37:50 +02:00
|
|
|
|
2020-04-17 00:41:59 +02:00
|
|
|
float distanceChange = handHeldTool ? -0.3F : -0.6F;
|
2018-08-22 12:31:24 +02:00
|
|
|
|
2020-04-17 00:41:59 +02:00
|
|
|
stack.translate(
|
|
|
|
driftAmount - floatAmount / 4F + distanceChange / 1.5F * sign,
|
|
|
|
floatAmount,
|
|
|
|
distanceChange);
|
|
|
|
|
|
|
|
if (!handHeldTool) { // bows have to point forwards
|
|
|
|
stack.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(sign * -60 + floatAmount));
|
|
|
|
stack.multiply(Vector3f.POSITIVE_Z.getDegreesQuaternion(sign * 30 + driftAmount));
|
2018-05-26 23:37:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|