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

297 lines
9.2 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.core.sync.packets;
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;
2015-12-24 02:07:03 +01:00
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
2015-12-24 02:07:03 +01:00
2014-01-20 17:41:37 +01:00
import appeng.api.config.FuzzyMode;
import appeng.api.config.Settings;
2014-02-17 01:50:25 +01:00
import appeng.api.util.IConfigManager;
2014-09-20 22:20:00 +02:00
import appeng.api.util.IConfigurableObject;
import appeng.client.gui.implementations.GuiCraftingCPU;
import appeng.container.AEBaseContainer;
2014-01-20 17:41:37 +01:00
import appeng.container.implementations.ContainerCellWorkbench;
2014-06-29 11:06:07 +02:00
import appeng.container.implementations.ContainerCraftConfirm;
2014-06-28 07:20:12 +02:00
import appeng.container.implementations.ContainerCraftingCPU;
import appeng.container.implementations.ContainerCraftingStatus;
import appeng.container.implementations.ContainerLevelEmitter;
import appeng.container.implementations.ContainerNetworkTool;
2014-04-10 06:51:08 +02:00
import appeng.container.implementations.ContainerPatternTerm;
2014-01-26 07:44:50 +01:00
import appeng.container.implementations.ContainerPriority;
import appeng.container.implementations.ContainerQuartzKnife;
import appeng.container.implementations.ContainerSecurityStation;
import appeng.container.implementations.ContainerStorageBus;
import appeng.core.sync.AppEngPacket;
2014-02-09 02:34:52 +01:00
import appeng.core.sync.network.INetworkInfo;
import appeng.helpers.IMouseWheelItem;
2014-02-09 02:34:52 +01:00
public class PacketValueConfig extends AppEngPacket
{
private final String Name;
private final String Value;
// automatic.
2015-09-30 14:24:40 +02:00
public PacketValueConfig( final ByteBuf stream ) throws IOException
{
2015-09-30 14:24:40 +02:00
final DataInputStream dis = new DataInputStream( new ByteArrayInputStream( stream.array(), stream.readerIndex(), stream.readableBytes() ) );
2014-12-29 15:13:47 +01:00
this.Name = dis.readUTF();
this.Value = dis.readUTF();
2014-02-09 19:36:52 +01:00
// dis.close();
}
// api
2015-09-30 14:24:40 +02:00
public PacketValueConfig( final String name, final String value ) throws IOException
{
this.Name = name;
this.Value = value;
2015-09-30 14:24:40 +02:00
final ByteBuf data = Unpooled.buffer();
data.writeInt( this.getPacketID() );
2015-09-30 14:24:40 +02:00
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final DataOutputStream dos = new DataOutputStream( bos );
dos.writeUTF( name );
dos.writeUTF( value );
// dos.close();
data.writeBytes( bos.toByteArray() );
this.configureWrite( data );
}
@Override
2015-09-30 14:24:40 +02:00
public void serverPacketData( final INetworkInfo manager, final AppEngPacket packet, final EntityPlayer player )
{
2015-09-30 14:24:40 +02:00
final Container c = player.openContainer;
if( this.Name.equals( "Item" ) && ( ( player.getHeldItem( EnumHand.MAIN_HAND ) != null && player.getHeldItem(EnumHand.MAIN_HAND).getItem() instanceof IMouseWheelItem ) || ( player.getHeldItem( EnumHand.OFF_HAND ) != null && player.getHeldItem(EnumHand.OFF_HAND).getItem() instanceof IMouseWheelItem ) ) )
{
final EnumHand hand;
if( player.getHeldItem( EnumHand.MAIN_HAND ) != null && player.getHeldItem( EnumHand.MAIN_HAND ).getItem() instanceof IMouseWheelItem )
{
hand = EnumHand.MAIN_HAND;
}
else if( player.getHeldItem( EnumHand.OFF_HAND ) != null && player.getHeldItem( EnumHand.OFF_HAND ).getItem() instanceof IMouseWheelItem )
{
hand = EnumHand.OFF_HAND;
}
else
{
return;
}
final ItemStack is = player.getHeldItem( hand );
2015-09-30 14:24:40 +02:00
final IMouseWheelItem si = (IMouseWheelItem) is.getItem();
2014-12-29 15:13:47 +01:00
si.onWheel( is, this.Value.equals( "WheelUp" ) );
}
else if( this.Name.equals( "Terminal.Cpu" ) && c instanceof ContainerCraftingStatus )
{
2015-09-30 14:24:40 +02:00
final ContainerCraftingStatus qk = (ContainerCraftingStatus) c;
2014-12-29 15:13:47 +01:00
qk.cycleCpu( this.Value.equals( "Next" ) );
}
else if( this.Name.equals( "Terminal.Cpu" ) && c instanceof ContainerCraftConfirm )
{
2015-09-30 14:24:40 +02:00
final ContainerCraftConfirm qk = (ContainerCraftConfirm) c;
2014-12-29 15:13:47 +01:00
qk.cycleCpu( this.Value.equals( "Next" ) );
}
else if( this.Name.equals( "Terminal.Start" ) && c instanceof ContainerCraftConfirm )
2014-06-29 11:06:07 +02:00
{
2015-09-30 14:24:40 +02:00
final ContainerCraftConfirm qk = (ContainerCraftConfirm) c;
2014-06-29 11:06:07 +02:00
qk.startJob();
}
else if( this.Name.equals( "TileCrafting.Cancel" ) && c instanceof ContainerCraftingCPU )
2014-06-28 07:20:12 +02:00
{
2015-09-30 14:24:40 +02:00
final ContainerCraftingCPU qk = (ContainerCraftingCPU) c;
2014-06-28 07:20:12 +02:00
qk.cancelCrafting();
}
else if( this.Name.equals( "QuartzKnife.Name" ) && c instanceof ContainerQuartzKnife )
{
2015-09-30 14:24:40 +02:00
final ContainerQuartzKnife qk = (ContainerQuartzKnife) c;
2014-12-29 15:13:47 +01:00
qk.setName( this.Value );
}
else if( this.Name.equals( "TileStationSecurity.ToggleOption" ) && c instanceof ContainerSecurityStation )
2014-02-01 06:37:27 +01:00
{
final ContainerSecurityStation sc = (ContainerSecurityStation) c;
2014-12-29 15:13:47 +01:00
sc.toggleSetting( this.Value, player );
2014-02-01 06:37:27 +01:00
}
else if( this.Name.equals( "PriorityHost.Priority" ) && c instanceof ContainerPriority )
2014-01-26 07:44:50 +01:00
{
2015-09-30 14:24:40 +02:00
final ContainerPriority pc = (ContainerPriority) c;
2014-12-29 15:13:47 +01:00
pc.setPriority( Integer.parseInt( this.Value ), player );
2014-01-26 07:44:50 +01:00
}
else if( this.Name.equals( "LevelEmitter.Value" ) && c instanceof ContainerLevelEmitter )
{
2015-09-30 14:24:40 +02:00
final ContainerLevelEmitter lvc = (ContainerLevelEmitter) c;
2014-12-29 15:13:47 +01:00
lvc.setLevel( Long.parseLong( this.Value ), player );
}
else if( this.Name.startsWith( "PatternTerminal." ) && c instanceof ContainerPatternTerm )
2014-04-10 06:51:08 +02:00
{
2015-09-30 14:24:40 +02:00
final ContainerPatternTerm cpt = (ContainerPatternTerm) c;
if( this.Name.equals( "PatternTerminal.CraftMode" ) )
2014-04-10 06:51:08 +02:00
{
cpt.getPatternTerminal().setCraftingRecipe( this.Value.equals( "1" ) );
2014-04-10 06:51:08 +02:00
}
else if( this.Name.equals( "PatternTerminal.Encode" ) )
2014-04-10 06:51:08 +02:00
{
cpt.encode();
}
else if( this.Name.equals( "PatternTerminal.Clear" ) )
{
cpt.clear();
}
else if( this.Name.equals( "PatternTerminal.Substitute" ) )
{
cpt.getPatternTerminal().setSubstitution( this.Value.equals( "1" ) );
}
2014-04-10 06:51:08 +02:00
}
else if( this.Name.startsWith( "StorageBus." ) && c instanceof ContainerStorageBus )
{
2015-09-30 14:24:40 +02:00
final ContainerStorageBus ccw = (ContainerStorageBus) c;
if( this.Name.equals( "StorageBus.Action" ) )
{
if( this.Value.equals( "Partition" ) )
{
ccw.partition();
}
else if( this.Value.equals( "Clear" ) )
{
ccw.clear();
}
}
}
else if( this.Name.startsWith( "CellWorkbench." ) && c instanceof ContainerCellWorkbench )
2014-01-20 17:41:37 +01:00
{
2015-09-30 14:24:40 +02:00
final ContainerCellWorkbench ccw = (ContainerCellWorkbench) c;
if( this.Name.equals( "CellWorkbench.Action" ) )
2014-01-20 17:41:37 +01:00
{
if( this.Value.equals( "CopyMode" ) )
2014-04-07 01:05:09 +02:00
{
ccw.nextWorkBenchCopyMode();
2014-04-07 01:05:09 +02:00
}
else if( this.Value.equals( "Partition" ) )
2014-01-20 17:41:37 +01:00
{
ccw.partition();
}
else if( this.Value.equals( "Clear" ) )
2014-01-20 17:41:37 +01:00
{
ccw.clear();
}
}
else if( this.Name.equals( "CellWorkbench.Fuzzy" ) )
2014-01-20 17:41:37 +01:00
{
2014-12-29 15:13:47 +01:00
ccw.setFuzzy( FuzzyMode.valueOf( this.Value ) );
2014-01-20 17:41:37 +01:00
}
}
else if( c instanceof ContainerNetworkTool )
{
if( this.Name.equals( "NetworkTool" ) && this.Value.equals( "Toggle" ) )
{
( (ContainerNetworkTool) c ).toggleFacadeMode();
}
}
else if( c instanceof IConfigurableObject )
2014-02-17 01:50:25 +01:00
{
2015-09-30 14:24:40 +02:00
final IConfigManager cm = ( (IConfigurableObject) c ).getConfigManager();
2014-02-17 01:50:25 +01:00
2015-09-30 14:24:40 +02:00
for( final Settings e : cm.getSettings() )
2014-02-17 01:50:25 +01:00
{
if( e.name().equals( this.Name ) )
2014-02-17 01:50:25 +01:00
{
2015-09-30 14:24:40 +02:00
final Enum<?> def = cm.getSetting( e );
2014-02-17 01:50:25 +01:00
try
{
2014-12-29 15:13:47 +01:00
cm.putSetting( e, Enum.valueOf( def.getClass(), this.Value ) );
2014-02-17 01:50:25 +01:00
}
2015-09-30 14:24:40 +02:00
catch( final IllegalArgumentException err )
2014-02-17 01:50:25 +01:00
{
// :P
}
break;
}
}
}
}
@Override
2015-09-30 14:24:40 +02:00
public void clientPacketData( final INetworkInfo network, final AppEngPacket packet, final EntityPlayer player )
2014-02-17 01:50:25 +01:00
{
2015-09-30 14:24:40 +02:00
final Container c = player.openContainer;
2014-02-17 01:50:25 +01:00
if( this.Name.equals( "CustomName" ) && c instanceof AEBaseContainer )
{
( (AEBaseContainer) c ).setCustomName( this.Value );
}
else if( this.Name.startsWith( "SyncDat." ) )
2014-07-16 05:26:12 +02:00
{
( (AEBaseContainer) c ).stringSync( Integer.parseInt( this.Name.substring( 8 ) ), this.Value );
2014-07-16 05:26:12 +02:00
}
else if( this.Name.equals( "CraftingStatus" ) && this.Value.equals( "Clear" ) )
{
2015-09-30 14:24:40 +02:00
final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
if( gs instanceof GuiCraftingCPU )
2015-04-29 02:30:53 +02:00
{
( (GuiCraftingCPU) gs ).clearItems();
2015-04-29 02:30:53 +02:00
}
}
else if( c instanceof IConfigurableObject )
2014-02-17 01:50:25 +01:00
{
2015-09-30 14:24:40 +02:00
final IConfigManager cm = ( (IConfigurableObject) c ).getConfigManager();
2014-02-17 01:50:25 +01:00
2015-09-30 14:24:40 +02:00
for( final Settings e : cm.getSettings() )
2014-02-17 01:50:25 +01:00
{
if( e.name().equals( this.Name ) )
2014-02-17 01:50:25 +01:00
{
2015-09-30 14:24:40 +02:00
final Enum<?> def = cm.getSetting( e );
2014-02-17 01:50:25 +01:00
try
{
2014-12-29 15:13:47 +01:00
cm.putSetting( e, Enum.valueOf( def.getClass(), this.Value ) );
2014-02-17 01:50:25 +01:00
}
2015-09-30 14:24:40 +02:00
catch( final IllegalArgumentException err )
2014-02-17 01:50:25 +01:00
{
// :P
}
break;
}
}
}
}
}