001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.tridentsdk.server.entity;
018
019import net.tridentsdk.base.Position;
020import net.tridentsdk.entity.traits.Tameable;
021import net.tridentsdk.server.data.MetadataType;
022import net.tridentsdk.server.data.ProtocolMetadata;
023import net.tridentsdk.server.player.TridentPlayer;
024import net.tridentsdk.util.TridentLogger;
025
026import java.util.UUID;
027
028public abstract class TridentTameable extends TridentBreedable implements Tameable {
029
030    protected volatile byte tameData;
031    protected volatile UUID owner;
032
033    protected TridentTameable(UUID id, Position spawnLocation) {
034        super(id, spawnLocation);
035
036        this.tameData = 0;
037    }
038
039    @Override
040    protected void doEncodeMeta(ProtocolMetadata protocolMeta) {
041        protocolMeta.setMeta(16, MetadataType.BYTE, tameData);
042        protocolMeta.setMeta(17, MetadataType.STRING,
043                owner == null ? "" : TridentPlayer.getPlayer(owner).name());
044    }
045
046    @Override
047    public boolean isSitting() {
048        return (tameData & 1) == 1;
049    }
050
051    @Override
052    public UUID owner() {
053        return owner;
054    }
055
056    @Override
057    public boolean isTamed() {
058        return (tameData & 4) == 4;
059    }
060
061    public void setTame(final UUID owner) {
062        if(TridentPlayer.getPlayer(owner) == null) {
063            TridentLogger.get().error(new IllegalArgumentException("No player found with provided UUID!"));
064            return;
065        }
066
067        TridentTameable.this.owner = owner;
068        tameData |= 4;
069    }
070}