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 */
017
018package net.tridentsdk.server.entity.living;
019
020import net.tridentsdk.base.BlockSnapshot;
021import net.tridentsdk.base.Position;
022import net.tridentsdk.base.Substance;
023import net.tridentsdk.entity.living.Enderman;
024import net.tridentsdk.entity.living.Player;
025import net.tridentsdk.entity.types.EntityType;
026import net.tridentsdk.event.entity.EntityDamageEvent;
027import net.tridentsdk.meta.nbt.CompoundTag;
028import net.tridentsdk.meta.nbt.ShortTag;
029import net.tridentsdk.server.data.MetadataType;
030import net.tridentsdk.server.data.ProtocolMetadata;
031import net.tridentsdk.server.entity.TridentLivingEntity;
032
033import java.util.UUID;
034
035public class TridentEnderman extends TridentLivingEntity implements Enderman {
036    private volatile BlockSnapshot carryingBlock;
037    private volatile boolean hostile = false;
038
039    public TridentEnderman(UUID id, Position spawnLocation) {
040        super(id, spawnLocation);
041    }
042
043    @Override
044    public void doLoad(CompoundTag tag) {
045        short carriedId = ((ShortTag) tag.getTag("carried")).value();
046
047        if(carriedId != 0) {
048            carryingBlock = BlockSnapshot.from(null, Substance.fromId((byte) carriedId),
049                    (byte) ((ShortTag) tag.getTag("carriedData")).value());
050        }
051    }
052
053    @Override
054    protected void doEncodeMeta(ProtocolMetadata protocolMeta) {
055        protocolMeta.setMeta(16, MetadataType.SHORT, (short) carryingBlock.type().id());
056        protocolMeta.setMeta(17, MetadataType.BYTE, carryingBlock.meta());
057        protocolMeta.setMeta(18, MetadataType.BYTE, hostile ? (byte) 1 : (byte) 0);
058    }
059
060    @Override
061    public BlockSnapshot carryingBlock() {
062        return carryingBlock;
063    }
064
065    @Override
066    public int endermiteCount() {
067        return 0;
068    }
069
070    @Override
071    public boolean isHostile() {
072        return hostile;
073    }
074
075    @Override
076    public EntityDamageEvent lastDamageEvent() {
077        return null;
078    }
079
080    @Override
081    public Player lastPlayerDamager() {
082        return null;
083    }
084
085    @Override
086    public EntityType type() {
087        return EntityType.ENDERMAN;
088    }
089}