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.netty.packet;
019
020import io.netty.buffer.ByteBuf;
021import net.tridentsdk.reflect.FastClass;
022import net.tridentsdk.server.netty.ClientConnection;
023import net.tridentsdk.util.TridentLogger;
024
025/**
026 * @author The TridentSDK Team
027 */
028public abstract class OutPacket implements Packet {
029    private final FastClass fastClass;
030
031    public OutPacket() {
032        this.fastClass = FastClass.get(this.getClass());
033    }
034
035    @Override
036    public PacketDirection direction() {
037        return PacketDirection.OUT;
038    }
039
040    /**
041     * Sets the field name with said value
042     *
043     * @param name  Name of field you wish to set
044     * @param value Value you wish to set the field to
045     * @return OutPacket instance
046     */
047    public OutPacket set(String name, Object value) {
048        this.fastClass.fieldBy(name).set(this, value);
049        return this;
050    }
051
052    /**
053     * {@inheritDoc}
054     *
055     * <p>Cannot be decoded</p>
056     */
057    @Override
058    public Packet decode(ByteBuf buf) {
059        TridentLogger.get().error(new UnsupportedOperationException(this.getClass().getName() + " cannot be decoded!"));
060        return null;
061    }
062
063    /**
064     * {@inheritDoc}
065     *
066     * <p>Cannot be received</p>
067     */
068    @Override
069    public void handleReceived(ClientConnection connection) {
070        TridentLogger.get().error(new UnsupportedOperationException(
071                this.getClass().getName() + " is a client-bound packet therefor cannot be handled!"));
072    }
073}