Applied-Energistics-2-tiler.../core/sync/packets/PacketValueConfig.java

185 lines
4.6 KiB
Java
Raw Normal View History

package appeng.core.sync.packets;
2014-02-09 02:34:52 +01:00
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
2014-02-09 04:48:04 +01:00
import java.io.ByteArrayInputStream;
2014-02-09 04:41:49 +01:00
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
2014-01-20 17:41:37 +01:00
import appeng.api.config.FuzzyMode;
2014-02-17 01:50:25 +01:00
import appeng.api.util.IConfigManager;
import appeng.api.util.IConfigureableObject;
import appeng.container.AEBaseContainer;
2014-01-20 17:41:37 +01:00
import appeng.container.implementations.ContainerCellWorkbench;
import appeng.container.implementations.ContainerLevelEmitter;
2014-01-26 07:44:50 +01:00
import appeng.container.implementations.ContainerPriority;
import appeng.container.implementations.ContainerQuartzKnife;
2014-02-01 06:37:27 +01:00
import appeng.container.implementations.ContainerSecurity;
import appeng.container.implementations.ContainerStorageBus;
import appeng.core.sync.AppEngPacket;
2014-02-09 02:34:52 +01:00
import appeng.core.sync.network.INetworkInfo;
public class PacketValueConfig extends AppEngPacket
{
final public String Name;
final public String Value;
// automatic.
2014-02-09 02:34:52 +01:00
public PacketValueConfig(ByteBuf stream) throws IOException {
2014-02-09 19:36:52 +01:00
DataInputStream dis = new DataInputStream( new ByteArrayInputStream( stream.array(), stream.readerIndex(), stream.readableBytes() ) );
2014-02-09 02:34:52 +01:00
Name = dis.readUTF();
Value = dis.readUTF();
2014-02-09 19:36:52 +01:00
// dis.close();
}
@Override
2014-02-09 02:34:52 +01:00
public void serverPacketData(INetworkInfo manager, AppEngPacket packet, EntityPlayer player)
{
Container c = player.openContainer;
if ( Name.equals( "QuartzKnife.Name" ) && c instanceof ContainerQuartzKnife )
{
ContainerQuartzKnife qk = (ContainerQuartzKnife) c;
qk.setName( Value );
return;
}
else if ( Name.equals( "TileSecurity.ToggleOption" ) && c instanceof ContainerSecurity )
2014-02-01 06:37:27 +01:00
{
ContainerSecurity sc = (ContainerSecurity) c;
sc.toggleSetting( Value, player );
return;
}
else if ( Name.equals( "PriorityHost.Priority" ) && c instanceof ContainerPriority )
2014-01-26 07:44:50 +01:00
{
ContainerPriority pc = (ContainerPriority) c;
pc.setPriority( Integer.parseInt( Value ), player );
return;
}
else if ( Name.equals( "LevelEmitter.Value" ) && c instanceof ContainerLevelEmitter )
{
ContainerLevelEmitter lvc = (ContainerLevelEmitter) c;
lvc.setLevel( Long.parseLong( Value ), player );
return;
}
else if ( Name.startsWith( "StorageBus." ) && c instanceof ContainerStorageBus )
{
ContainerStorageBus ccw = (ContainerStorageBus) c;
if ( Name.equals( "StorageBus.Action" ) )
{
if ( Value.equals( "Partition" ) )
{
ccw.partition();
}
else if ( Value.equals( "Clear" ) )
{
ccw.clear();
}
}
}
2014-01-20 17:41:37 +01:00
else if ( Name.startsWith( "CellWorkbench." ) && c instanceof ContainerCellWorkbench )
{
ContainerCellWorkbench ccw = (ContainerCellWorkbench) c;
if ( Name.equals( "CellWorkbench.Action" ) )
{
if ( Value.equals( "Partition" ) )
{
ccw.partition();
}
else if ( Value.equals( "Clear" ) )
{
ccw.clear();
}
}
else if ( Name.equals( "CellWorkbench.Fuzzy" ) )
{
ccw.setFuzzy( FuzzyMode.valueOf( Value ) );
}
}
2014-02-17 01:50:25 +01:00
else if ( c instanceof IConfigureableObject )
{
IConfigManager cm = ((IConfigureableObject) c).getConfigManager();
for (Enum e : cm.getSettings())
{
if ( e.name().equals( Name ) )
{
Enum def = cm.getSetting( e );
try
{
cm.putSetting( e, Enum.valueOf( def.getClass(), Value ) );
}
catch (IllegalArgumentException err)
{
// :P
}
break;
}
}
}
}
@Override
public void clientPacketData(INetworkInfo network, AppEngPacket packet, EntityPlayer player)
{
Container c = player.openContainer;
if ( Name.equals( "CustomName" ) && c instanceof AEBaseContainer )
{
((AEBaseContainer) c).customName = Value;
}
else if ( c instanceof IConfigureableObject )
2014-02-17 01:50:25 +01:00
{
IConfigManager cm = ((IConfigureableObject) c).getConfigManager();
for (Enum e : cm.getSettings())
{
if ( e.name().equals( Name ) )
{
Enum def = cm.getSetting( e );
try
{
cm.putSetting( e, Enum.valueOf( def.getClass(), Value ) );
}
catch (IllegalArgumentException err)
{
// :P
}
break;
}
}
}
}
// api
public PacketValueConfig(String Name, String Value) throws IOException {
this.Name = Name;
this.Value = Value;
2014-02-09 02:34:52 +01:00
ByteBuf data = Unpooled.buffer();
data.writeInt( getPacketID() );
2014-02-09 04:41:49 +01:00
ByteArrayOutputStream bos = new ByteArrayOutputStream();
2014-02-09 02:34:52 +01:00
DataOutputStream dos = new DataOutputStream( bos );
2014-02-09 19:36:52 +01:00
dos.writeUTF( Name );
dos.writeUTF( Value );
// dos.close();
2014-02-09 02:34:52 +01:00
2014-02-09 04:41:49 +01:00
data.writeBytes( bos.toByteArray() );
2014-02-09 02:34:52 +01:00
configureWrite( data );
}
}