Unicopia/src/main/java/com/minelittlepony/unicopia/client/UnicopiaClient.java

149 lines
5.5 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.client;
2019-01-31 16:21:14 +01:00
2020-04-15 15:45:57 +02:00
import static com.minelittlepony.unicopia.EquinePredicates.MAGI;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.minelittlepony.common.event.ClientReadyCallback;
import com.minelittlepony.unicopia.Config;
import com.minelittlepony.unicopia.InteractionManager;
import com.minelittlepony.unicopia.Race;
2020-04-15 17:22:29 +02:00
import com.minelittlepony.unicopia.ability.Abilities;
2020-04-15 19:06:45 +02:00
import com.minelittlepony.unicopia.block.UBlocks;
2020-04-22 20:23:54 +02:00
import com.minelittlepony.unicopia.container.SpellbookResultSlot;
import com.minelittlepony.unicopia.ducks.Colourful;
2020-04-15 18:12:00 +02:00
import com.minelittlepony.unicopia.entity.player.Pony;
2020-04-15 15:45:57 +02:00
import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.magic.spell.SpellRegistry;
2020-04-22 20:23:54 +02:00
import com.minelittlepony.unicopia.mixin.client.DefaultTexturesRegistry;
import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.network.MsgRequestCapabilities;
import com.minelittlepony.unicopia.util.dummy.DummyClientPlayerEntity;
import com.mojang.authlib.GameProfile;
import net.fabricmc.api.ClientModInitializer;
2020-04-22 16:28:20 +02:00
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
import net.fabricmc.fabric.api.event.client.ClientTickCallback;
2020-04-15 15:45:57 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
2020-01-16 12:35:46 +01:00
import net.minecraft.client.MinecraftClient;
2020-04-15 15:45:57 +02:00
import net.minecraft.client.color.world.BiomeColors;
import net.minecraft.client.color.world.GrassColors;
2020-04-22 20:23:54 +02:00
import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.client.util.SpriteIdentifier;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.player.PlayerEntity;
2020-04-15 15:45:57 +02:00
import net.minecraft.item.BlockItem;
import net.minecraft.util.math.BlockPos;
2020-04-22 16:28:20 +02:00
import net.minecraft.world.BlockRenderView;
2020-04-16 00:44:58 +02:00
public class UnicopiaClient extends InteractionManager implements ClientModInitializer {
2020-04-15 15:55:18 +02:00
private final KeyBindingsHandler keyboard = new KeyBindingsHandler();
/**
* The race preferred by the client - as determined by mine little pony.
* Human if minelp was not installed.
*
* This is not neccessarily the _actual_ race used for the player,
* as the server may not allow certain race types, or the player may override
* this option in-game themselves.
*/
private static Race clientPlayerRace = getclientPlayerRace();
2020-04-22 16:28:20 +02:00
@Override
public void onInitializeClient() {
clientPlayerRace = getclientPlayerRace();
InteractionManager.instance = this;
ClientTickCallback.EVENT.register(this::tick);
2020-04-22 20:23:54 +02:00
ClientReadyCallback.EVENT.register(client -> Abilities.getInstance().getValues().forEach(keyboard::addKeybind));
2020-04-22 16:28:20 +02:00
2020-04-22 20:23:54 +02:00
DefaultTexturesRegistry.getDefaultTextures().add(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEX, SpellbookResultSlot.EMPTY_GEM_SLOT));
2020-04-22 16:28:20 +02:00
ColorProviderRegistry.ITEM.register((stack, tint) -> {
return getLeavesColor(((BlockItem)stack.getItem()).getBlock().getDefaultState(), null, null, tint);
}, UItems.apple_leaves);
ColorProviderRegistry.BLOCK.register(UnicopiaClient::getLeavesColor, UBlocks.apple_leaves);
ColorProviderRegistry.ITEM.register((stack, tint) -> {
if (MAGI.test(MinecraftClient.getInstance().player)) {
return SpellRegistry.instance().getSpellTintFromStack(stack);
}
return 0xFFFFFF;
}, UItems.spell, UItems.curse);
}
private void tick(MinecraftClient client) {
PlayerEntity player = client.player;
if (player != null && !player.removed) {
Race newRace = getclientPlayerRace();
if (newRace != clientPlayerRace) {
clientPlayerRace = newRace;
Channel.REQUEST_CAPABILITIES.send(new MsgRequestCapabilities(player, clientPlayerRace));
2020-04-22 16:28:20 +02:00
}
}
keyboard.onKeyInput();
}
2020-01-16 12:35:46 +01:00
@Override
@Nonnull
2020-01-16 16:46:24 +01:00
public PlayerEntity createPlayer(Entity observer, GameProfile profile) {
if (observer.world instanceof ClientWorld) {
return new DummyClientPlayerEntity((ClientWorld)observer.world, profile);
}
return super.createPlayer(observer, profile);
}
@Override
2020-01-16 16:46:24 +01:00
public boolean isClientPlayer(@Nullable PlayerEntity player) {
if (MinecraftClient.getInstance().player == player) {
return true;
}
if (MinecraftClient.getInstance().player == null || player == null) {
return false;
}
2020-04-15 18:12:00 +02:00
return Pony.equal(MinecraftClient.getInstance().player, player);
}
@Override
public int getViewMode() {
return MinecraftClient.getInstance().options.perspective;
}
2020-04-22 20:23:54 +02:00
private static Race getclientPlayerRace() {
if (!Config.getInstance().ignoresMineLittlePony()
&& MinecraftClient.getInstance().player != null) {
Race race = MineLPConnector.getPlayerPonyRace();
if (!race.isDefault()) {
return race;
}
}
return Config.getInstance().getPrefferedRace();
}
2020-04-22 16:28:20 +02:00
private static int getLeavesColor(BlockState state, @Nullable BlockRenderView world, @Nullable BlockPos pos, int tint) {
2020-04-15 15:45:57 +02:00
Block block = state.getBlock();
if (block instanceof Colourful) {
return ((Colourful)block).getCustomTint(state, tint);
2020-04-15 15:45:57 +02:00
}
if (world != null && pos != null) {
return BiomeColors.getGrassColor(world, pos);
}
return GrassColors.getColor(0.5D, 1);
}
}