summaryrefslogtreecommitdiff
path: root/ihl/ServerProxy.java
blob: d22751cdf618f21f514cb93d19fb574f9fe5263f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package ihl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;
import cpw.mods.fml.common.network.FMLEventChannel;
import cpw.mods.fml.common.network.FMLNetworkEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import ihl.flexible_cable.NodeEntity;
import ihl.interfaces.INetworkListener;
import ihl.items_blocks.FlexibleCableItem;
import ihl.items_blocks.MachineBaseBlock.MachineType;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import net.minecraftforge.client.event.TextureStitchEvent.Pre;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
import net.minecraftforge.event.world.BlockEvent.PlaceEvent;

public class ServerProxy {

    protected static FMLEventChannel channel;
    public static final int updatePeriod = 1;
    protected Map<Integer, INetworkListener> entityList = new HashMap<Integer, INetworkListener>();
    protected Set<INetworkListener> entityServerList = new HashSet<INetworkListener>();
    protected Map<Integer, ByteBuf> delayedEntityDataPacket = new HashMap<Integer, ByteBuf>();
	public Map<Integer,Set<NodeEntity>> nodeEntityRegistry = new HashMap<Integer,Set<NodeEntity>>();
    
	public ServerProxy() {}
	
	public void load() throws ParserConfigurationException 
	{
    	if(channel==null)
    	{
    		channel = NetworkRegistry.INSTANCE.newEventDrivenChannel(IHLModInfo.MODID);
   	        channel.register(this);
    	}
	}
	public void spawnParticle(int particle, World world, double x, double y, double z, double mx, double my, double mz, float paticleScale){}
	
	public void spawnParticleFromServer(int particle, World world, double x, double y, double z, double mx, double my, double mz, float paticleScale)
	{
		ByteBuf bb = Unpooled.buffer(36); 
		ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(bb);
		try 
		{
			byteBufOutputStream.write(0);
			byteBufOutputStream.write(particle);
			byteBufOutputStream.writeFloat((float) x);
			byteBufOutputStream.writeFloat((float) y);
			byteBufOutputStream.writeFloat((float) z);
			byteBufOutputStream.writeFloat((float) mx);
			byteBufOutputStream.writeFloat((float) my);
			byteBufOutputStream.writeFloat((float) mz);
			byteBufOutputStream.writeFloat(paticleScale);
			channel.sendToAllAround(new FMLProxyPacket(byteBufOutputStream.buffer(),IHLModInfo.MODID), new TargetPoint(world.provider.dimensionId, x, y, z, 32d));
			byteBufOutputStream.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void playSoundEffectFromServer(int soundId, World world, double x, double y, double z, float volume, float pitch)
	{
		ByteBuf bb = Unpooled.buffer(36); 
		ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(bb);
		try 
		{
			byteBufOutputStream.write(3);
			byteBufOutputStream.write(soundId);
			byteBufOutputStream.writeFloat((float) x);
			byteBufOutputStream.writeFloat((float) y);
			byteBufOutputStream.writeFloat((float) z);
			byteBufOutputStream.writeFloat(volume);
			byteBufOutputStream.writeFloat(pitch);
			channel.sendToAllAround(new FMLProxyPacket(byteBufOutputStream.buffer(),IHLModInfo.MODID), new TargetPoint(world.provider.dimensionId, x, y, z, volume+32d));
			byteBufOutputStream.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void createExplosionEffectFromServer(World world, int x, int y, int z, float radius)
	{
		ByteBuf bb = Unpooled.buffer(36); 
		ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(bb);
		try 
		{
			byteBufOutputStream.write(2);
			byteBufOutputStream.writeInt(x);
			byteBufOutputStream.writeInt(y);
			byteBufOutputStream.writeInt(z);
			byteBufOutputStream.writeFloat(radius);
			channel.sendToAllAround(new FMLProxyPacket(byteBufOutputStream.buffer(),IHLModInfo.MODID), new TargetPoint(world.provider.dimensionId, x, y, z, radius+32d));
			byteBufOutputStream.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void registerIcons(Pre event) {}
	public void initBlockRenderer(){}
	public Object getRenderForEntityClass(Class<? extends Entity> entityClass) 
	{
		return null;
	}
	public int getGLDisplayList() {return -1;}

    public File getMinecraftDir()
    {
         return new File(".");
    }
	public int shareBlockRendererByMachineType(MachineType type) 
	{
		return 0;
	}

	public void addEntityToList(INetworkListener entity) 
	{
		this.entityList.put(entity.getId(), entity);
	}
	
    public void recieveDelayedDataPacket(INetworkListener listener)
    {
    	ByteBuf data = delayedEntityDataPacket.remove(listener.getId());
    	if(data!=null)
    	{
        	ByteBufInputStream byteBufInputStream = new ByteBufInputStream(data);
        	try 
        	{
				//byteBufInputStream.skipBytes(5);
				listener.recieveData(byteBufInputStream);
	            byteBufInputStream.close();
    			IHLMod.log.debug("Delayed data read.");
			} 
        	catch (IOException e) 
			{
				e.printStackTrace();
			}
    	}
    	else
    	{
			IHLMod.log.debug("Delayed data is null. Entity ID="+listener.getId());
    	}
    }

	public void sendFromServerToAll(FMLProxyPacket fmlProxyPacket) 
	{
		channel.sendToAll(fmlProxyPacket);
	}
	
	public void sendFromServerToPlayer(FMLProxyPacket fmlProxyPacket, EntityPlayerMP player) 
	{
		channel.sendTo(fmlProxyPacket, player);
	}
	
    @SubscribeEvent
	public void onPacketFromClientToServer(FMLNetworkEvent.ServerCustomPacketEvent event) throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
	{
    	ByteBuf data = event.packet.payload();
    	ByteBufInputStream byteBufInputStream = new ByteBufInputStream(data);
        switch(byteBufInputStream.read())
        {
        	case 0:
        		int playerEntityId = byteBufInputStream.readInt();
        		int worldDimensionId = byteBufInputStream.readInt();
        		int containerSlotNumber = byteBufInputStream.readInt();
        		int fieldValue = byteBufInputStream.readInt();
        		String fieldName = byteBufInputStream.readUTF();
        		EntityPlayerMP player = (EntityPlayerMP) MinecraftServer.getServer().worldServerForDimension(worldDimensionId).getEntityByID(playerEntityId);
        		ItemStack stack = ((Slot)player.openContainer.inventorySlots.get(containerSlotNumber)).getStack();
        		stack.stackTagCompound.setInteger(fieldName, fieldValue);
        		player.openContainer.detectAndSendChanges();
        		break;
        	case 1:
        		playerEntityId = byteBufInputStream.readInt();
        		worldDimensionId = byteBufInputStream.readInt();
        		int x = byteBufInputStream.readInt();
        		int y = byteBufInputStream.readInt();
        		int z = byteBufInputStream.readInt();
        		World world = MinecraftServer.getServer().worldServerForDimension(worldDimensionId);
        		TileEntity te = world.getTileEntity(x, y, z);
        		if(te!=null && !te.isInvalid())
        		{
            		NBTTagCompound nbt = new NBTTagCompound();
            		te.writeToNBT(nbt);
            		player = (EntityPlayerMP) world.getEntityByID(playerEntityId);
            		player.playerNetServerHandler.sendPacket(new S35PacketUpdateTileEntity(x,y,z,6,nbt));
        		}
        		break;
        	case 2:
        		worldDimensionId = byteBufInputStream.readInt();
        		x = byteBufInputStream.readInt();
        		y = byteBufInputStream.readInt();
        		z = byteBufInputStream.readInt();
        		world = MinecraftServer.getServer().worldServerForDimension(worldDimensionId);
        		te = world.getTileEntity(x, y, z);
        		if(te!=null && !te.isInvalid())
        		{
            		int value = byteBufInputStream.readInt();
        			fieldName = byteBufInputStream.readUTF();
        			te.getClass().getDeclaredField(fieldName).set(te, value);
        		}
        		break;
        }

        byteBufInputStream.close();

	}
	
    @SubscribeEvent
	public void onPlayerConnectedToServer(PlayerLoggedInEvent event)
    {
    	IHLMod.log.debug("player connected");
    	Iterator<INetworkListener> inli=this.entityServerList.iterator();
    	while(inli.hasNext())
    	{
    		INetworkListener inl = inli.next();
    		if(inl.isInvalid())
    		{
    			inli.remove();
    		}
    		else if(event.player instanceof EntityPlayerMP)
    		{
    			inl.registerAndSendData((EntityPlayerMP)event.player);
    		}
    	}
    }
    
	@SubscribeEvent
	public void onPlayerTeleport(PlayerChangedDimensionEvent event)
	{
		FlexibleCableItem.instance.onPlayerTeleport(event);
    	Iterator<INetworkListener> inli=this.entityServerList.iterator();
    	while(inli.hasNext())
    	{
    		INetworkListener inl = inli.next();
    		if(inl.isInvalid())
    		{
    			inli.remove();
    		}
    		else if(event.player instanceof EntityPlayerMP)
    		{
    			inl.registerAndSendData((EntityPlayerMP)event.player);
    		}
    	}
	}
	@SubscribeEvent
	public void onBlockBreak(BreakEvent event) {
		int x = event.x;
		int y = event.y;
		int z = event.z;
		ByteBuf bb = Unpooled.buffer(20); 
		ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(bb);
		try 
		{
			byteBufOutputStream.write(4);
			byteBufOutputStream.writeInt(x);
			byteBufOutputStream.writeInt(y);
			byteBufOutputStream.writeInt(z);
			channel.sendToAllAround(new FMLProxyPacket(byteBufOutputStream.buffer(),IHLModInfo.MODID), new TargetPoint(event.world.provider.dimensionId, x, y, z, 256d));
			byteBufOutputStream.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	@SubscribeEvent
	public void onBlockPlace(PlaceEvent event) {
		int x = event.x;
		int y = event.y;
		int z = event.z;
		ByteBuf bb = Unpooled.buffer(20); 
		ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(bb);
		try 
		{
			byteBufOutputStream.write(5);
			byteBufOutputStream.writeInt(x);
			byteBufOutputStream.writeInt(y);
			byteBufOutputStream.writeInt(z);
			channel.sendToAllAround(new FMLProxyPacket(byteBufOutputStream.buffer(),IHLModInfo.MODID), new TargetPoint(event.world.provider.dimensionId, x, y, z, 256d));
			byteBufOutputStream.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}

	
	public void addEntityToServerList(INetworkListener entity) 
	{
		this.entityServerList.add(entity);
	}

	public boolean renderTESpecialSelectionBox(TileEntity te, EntityPlayer player,	ItemStack currentItem, MovingObjectPosition target,	float partialTicks) {
		return false;
	}
	
	public void sendItemStackNBTTagFromClientToServerPlayer(EntityPlayer player, int slotNumber, String fieldName, int fieldValue){}

	public void createExplosionEffect(World world, int x, int y, int z, float radius){}
	
	public void requestTileEntityInitdataFromClientToServer(int x, int y, int z){}

}