akSource

ayamitsu氏のMessageクラス改善案

Aug 12th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. package ayamitsu.popupvillagerhead.network;
  2.  
  3. import ayamitsu.popupvillagerhead.PopupVillagerHead;
  4. import cpw.mods.fml.common.network.simpleimpl.IMessage;
  5. import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
  6. import cpw.mods.fml.common.network.simpleimpl.MessageContext;
  7. import io.netty.buffer.ByteBuf;
  8. import net.minecraft.entity.Entity;
  9. import net.minecraft.entity.passive.EntityVillager;
  10. import net.minecraft.world.World;
  11.  
  12. import java.util.List;
  13.  
  14. /**
  15.  * Created by ayamitsu0321 on 2014/08/12.
  16.  */
  17. public class MessagePopupHead implements IMessage, IMessageHandler<MessagePopupHead, IMessage> {
  18.  
  19.     public int entityId = 0;
  20.     public boolean data = false;
  21.     public boolean sent = false;
  22.  
  23.     public MessagePopupHead() {}
  24.  
  25.     public MessagePopupHead(EntityVillager villager) {
  26.         this.entityId = villager.getEntityId();
  27.         this.data = villager.getEntityData().getBoolean("PoppedupHead");
  28.     }
  29.  
  30.     @Override
  31.     public void fromBytes(ByteBuf buf) {
  32.         this.entityId = buf.readInt();
  33.         this.data = buf.readBoolean();
  34.         System.out.println("puts " + this.entityId + this.data);
  35.     }
  36.  
  37.     @Override
  38.     public void toBytes(ByteBuf buf) {
  39.         buf.writeInt(this.entityId);
  40.         buf.writeBoolean(this.data);
  41.         System.out.println("puts toBytes");
  42.     }
  43.  
  44.     @Override
  45.     public IMessage onMessage(MessagePopupHead message, MessageContext ctx) {
  46.         World world = PopupVillagerHead.proxy.getWorldClient();
  47.         @SuppressWarnings("unchecked")
  48.         List<Entity> entityList = world.getLoadedEntityList();
  49.         for (Entity entity : entityList) {
  50.             System.out.println("check " + entity.getEntityId());
  51.             //引数のmessageから変数を取る。これ、クラス分けたほうが事故が少ないかも。
  52.             if (entity.getEntityId() == message.entityId) {
  53.                 //こっちも引数のmessageから変数を取る。
  54.                 entity.getEntityData().setBoolean("PoppedupHead", message.data);
  55.                 System.out.println("puts setBoolean");
  56.             }
  57.         }
  58.         return null;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment