-
-
Save wesserboy/dc7f1f88afb3b83aaac6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package wesserboy.challengeMod.network; | |
import io.netty.buffer.ByteBuf; | |
import java.util.ArrayList; | |
import java.util.List; | |
import net.minecraft.entity.player.EntityPlayerMP; | |
import net.minecraft.item.Item; | |
import net.minecraft.item.ItemStack; | |
import cpw.mods.fml.common.network.ByteBufUtils; | |
public class MessageSubRequester extends ChallengeMessageBase<MessageSubRequester>{ | |
public MessageSubRequester(){} | |
private String[] items; | |
public MessageSubRequester(String[] items){ | |
this.items = items; | |
} | |
@Override | |
public void fromBytes(ByteBuf buf) { | |
byte id = buf.readByte(); | |
int amount = 0; | |
switch(id){ | |
case 0: | |
amount = buf.readByte(); | |
break; | |
case 1: | |
amount = buf.readShort(); | |
break; | |
case 2: | |
amount = buf.readInt(); | |
break; | |
} | |
this.items = new String[amount]; | |
for(int i = 0; i < amount; i++){ | |
this.items[i] = ByteBufUtils.readUTF8String(buf); | |
} | |
System.out.println("Done reading stuff from bytebuffer"); | |
System.out.println(this.items == null); | |
} | |
@Override | |
public void toBytes(ByteBuf buf) { | |
//write how many items are being written (in a smart way) | |
int amount = items.length; | |
if(amount <= Byte.MAX_VALUE){ | |
buf.writeByte(0); //value is a byte | |
buf.writeByte(amount); | |
}else if(amount <= Short.MAX_VALUE){ | |
buf.writeByte(1); //value is a short | |
buf.writeShort(amount); | |
}else{ | |
buf.writeByte(2); //value is an integer | |
buf.writeInt(amount); | |
} | |
for(int i = 0; i < items.length; i++){ | |
ByteBufUtils.writeUTF8String(buf, items[i]); | |
} | |
System.out.println("Done storing stuff to bytebuffer"); | |
} | |
@Override | |
public void handleClientSide(MessageSubRequester message) { | |
System.out.println("Start handeling the Client"); | |
System.out.println(this.items == null); | |
for(int i = 0; i < this.items.length; i++){ | |
Item item = (Item) Item.itemRegistry.getObject(this.items[i]); | |
if(item != null && item.getCreativeTab() != null){ | |
List<ItemStack> subItems = new ArrayList<ItemStack>(); | |
item.getSubItems(item, null, subItems); | |
if(subItems.size() > 0){ | |
NetworkHandler.sendToServer(new MessageListAdder(subItems.toArray(new ItemStack[]{}))); | |
} | |
} | |
} | |
System.out.println("Done handeling the client"); | |
} | |
@Override | |
public void handleServerSide(MessageSubRequester message, EntityPlayerMP playerEntity) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment