Торговля через GUI жителя при клике на предмет

Версия Minecraft
1.7.10
1,007
36
206
Каком образом можно сделать торговлю с жителем (но без жителя), просто кликнув с предметом в руке? Естественно со своим списком продаваемых предметов.
 

timaxa007

Модератор
5,831
409
672
Открывать свои GuiContainer и Container, через player.openGui типа так:
Код:
player.openGui(MyMod.instance, IDgui, player.worldObj, (int)player.posX, (int)player.posY, (int)player.posZ);
MyMod - главный класс.
instance - инстанция твоего мода.
IDgui - ID твоего GuiContainer и Container.
player.worldObj - мир в котором открывается твой Gui.
(int)player.posX, (int)player.posY, (int)player.posZ) - координаты в int типе, можно просто по нулям координаты открывать, так как открываем через предмет, а не через блок.
 
1,007
36
206
Но у меня нет GuiContainer и Container, я сделал моба в которого засунул GUI жителя(и задал ему свои товары).

Вот код моба
Код:
public class EntityGaiaNPCCreeperGirl extends EntityMobMerchant {

   public EntityGaiaNPCCreeperGirl(World var1) {
      super(var1);
   }

   protected String getLivingSound() {
      return "gaia:passive_say";
   }

   protected String getHurtSound() {
      return "gaia:passive_hurt";
   }

   protected String getDeathSound() {
      return "gaia:passive_death";
   }

   protected void dropFewItems(boolean par1, int par2) {
      if(par1 && (this.rand.nextInt(1) == 0 || this.rand.nextInt(1 + par2) > 0)) {
         this.entityDropItem(new ItemStack(GaiaItem.SpawnCardCreeperGirl, 1, 1), 0.0F);
      }

   }

   public void addRecipies(MerchantRecipeList recipes) {
      recipes.add(new Trade(new ItemStack(GaiaItem.MiscCurrency, 1, 1), new ItemStack(Items.gunpowder, 1, 0)));
      recipes.add(new Trade(new ItemStack(Items.gunpowder, 1, 0), new ItemStack(Items.string, 2, 0)));
      recipes.add(new Trade(new ItemStack(Items.gunpowder, 1, 0), new ItemStack(Items.spider_eye, 1, 0)));
      recipes.add(new Trade(new ItemStack(Items.gunpowder, 1, 0), new ItemStack(Items.rotten_flesh, 4, 0)));
      recipes.add(new Trade(new ItemStack(Items.gunpowder, 1, 0), new ItemStack(Items.bone, 2, 0)));
      recipes.add(new Trade(new ItemStack(Items.string, 4, 0), new ItemStack(Items.gunpowder, 1, 0)));
      recipes.add(new Trade(new ItemStack(Items.spider_eye, 2, 0), new ItemStack(Items.gunpowder, 1, 0)));
      recipes.add(new Trade(new ItemStack(Items.rotten_flesh, 6, 0), new ItemStack(Items.gunpowder, 1, 0)));
      recipes.add(new Trade(new ItemStack(Items.bone, 4, 0), new ItemStack(Items.gunpowder, 1, 0)));
      recipes.add(new Trade(new ItemStack(Items.slime_ball, 2, 0), new ItemStack(Items.gunpowder, 1, 0)));
      recipes.add(new Trade(new ItemStack(Items.ender_pearl, 1, 0), new ItemStack(Items.gunpowder, 2, 0)));
   }
}
Вот код с GUI жителя
Код:
public abstract class EntityMobMerchant extends EntityVillager implements INpc, IMerchant {

   private int randomTickDivider;
   private Village villageObj;
   private String lastBuyingPlayer;
   private EntityPlayer buyingPlayer;
   private MerchantRecipeList buyingList;
   private int timeUntilReset;
   private boolean needsInitilization;
   private int wealth;
   private String buyersName;
   private float buying;


   public EntityMobMerchant(World var1) {
      super(var1);
      this.setSize(1.0F, 2.0F);
      this.randomTickDivider = 0;
      this.villageObj = null;
   }

   protected void applyEntityAttributes() {
      super.applyEntityAttributes();
      this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue((double)EntityAttributes.maxHealth1);
      this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue((double)EntityAttributes.moveSpeed1);
   }

   public boolean isAIEnabled() {
      return true;
   }

   protected boolean canDespawn() {
      return false;
   }

   protected String getLivingSound() {
      return null;
   }

   protected String getDeathSound() {
      return null;
   }

   protected String getHurtSound() {
      return null;
   }

   protected void updateAITick() {
      if(this.randomTickDivider-- <= 0) {
         this.randomTickDivider = 70 + this.rand.nextInt(50);
         this.villageObj = this.worldObj.villageCollectionObj.findNearestVillage(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posY), MathHelper.floor_double(this.posZ), 32);
         if(this.villageObj == null) {
            this.detachHome();
         } else {
            this.villageObj.setDefaultPlayerReputation(30);
         }
      }

      if(this.timeUntilReset > 0 && this.timeUntilReset <= 0) {
         if(this.buyingList.size() > 1) {
            Iterator iterator = this.buyingList.iterator();
            if(this.needsInitilization) {
               while(iterator.hasNext()) {
                  MerchantRecipe merchantrecipe = (MerchantRecipe)iterator.next();
                  if(merchantrecipe.isRecipeDisabled()) {
                     merchantrecipe.func_82783_a(this.rand.nextInt(6) + this.rand.nextInt(6) + 2);
                  }
               }
            }

            this.addDefaultEquipmentAndRecipies(75);
            this.needsInitilization = false;
            if(this.villageObj != null && this.lastBuyingPlayer != null) {
               this.villageObj.setReputationForPlayer(this.lastBuyingPlayer, 30);
            }
         }

         this.addPotionEffect(new PotionEffect(Potion.regeneration.id, 200, 0));
      }

      super.updateAITick();
   }

   public boolean interact(EntityPlayer par1EntityPlayer) {
      ItemStack var2 = par1EntityPlayer.inventory.getCurrentItem();
      boolean var3 = var2 != null && var2.getItem() == Items.spawn_egg;
      if(!var3 && this.isEntityAlive() && !this.isTrading() && !this.isChild()) {
         if(!this.worldObj.isRemote) {
            this.setCustomer(par1EntityPlayer);
            String name = this.getCustomNameTag();
            if(null == name || name.length() < 1) {
               name = this.getCommandSenderName();
            }

            par1EntityPlayer.displayGUIMerchant(this, name);
         }

         return true;
      } else {
         return super.interact(par1EntityPlayer);
      }
   }

   public abstract void addRecipies(MerchantRecipeList var1);

   public void writeEntityToNBT(NBTTagCompound var1) {
      super.writeEntityToNBT(var1);
      var1.setInteger("Profession", this.getProfession());
      var1.setInteger("Riches", this.wealth);
      if(this.buyingList != null) {
         var1.setTag("Offers", this.buyingList.getRecipiesAsTags());
      }

   }

   public void readEntityFromNBT(NBTTagCompound var1) {
      super.readEntityFromNBT(var1);
      this.setProfession(var1.getInteger("Profession"));
      this.wealth = var1.getInteger("Riches");
      if(var1.hasKey("Offers")) {
         NBTTagCompound var2 = var1.getCompoundTag("Offers");
         if(!(this instanceof EntityGaiaNPCCreeperGirl) && !(this instanceof EntityGaiaNPCTrader)) {
            this.buyingList = new MerchantRecipeList(var2);
         } else {
            this.buyingList = new TradeList(var2);
         }
      }

   }

   public void useRecipe(MerchantRecipe var1) {
      var1.incrementToolUses();
      if(var1.hasSameIDsAs((MerchantRecipe)this.buyingList.get(this.buyingList.size() - 1))) {
         this.timeUntilReset = 40;
         this.needsInitilization = true;
         if(this.buyingPlayer != null) {
            this.buyersName = this.buyingPlayer.getCommandSenderName();
         } else {
            this.buyersName = null;
         }
      }

      if(var1.getItemToBuy().getItem() == Items.emerald) {
         this.wealth += var1.getItemToBuy().stackSize;
      }

   }

   public void verifySellingItem(ItemStack par1ItemStack) {}

   public MerchantRecipeList getRecipes(EntityPlayer var1) {
      if(this.buyingList == null) {
         this.addDefaultEquipmentAndRecipies(75);
      }

      return this.buyingList;
   }

   private void addDefaultEquipmentAndRecipies(int par1) {
      if(this.buyingList != null) {
         this.buying = MathHelper.sqrt_float((float)this.buyingList.size()) * 0.2F;
      } else {
         this.buying = 0.0F;
      }

      MerchantRecipeList rec = new MerchantRecipeList();
      this.addRecipies(rec);
      if(this.buyingList == null) {
         this.buyingList = new MerchantRecipeList();
      }

      for(int var3 = 0; var3 < par1 && var3 < rec.size(); ++var3) {
         this.buyingList.add((MerchantRecipe)rec.get(var3));
      }

   }
}
 
7,099
324
1,510
Почитай туторы, иностранные и в разделе учебник на тему gui
 
7,099
324
1,510
к тому же палевный: на фоне гуи и другим игрокам будет виден житель
 

timaxa007

Модератор
5,831
409
672
junkil335, да, можно. Точно не знаю, но примерно:
Если моб спавнится игроком, то этому игроку открывается GUI. (player.openGui(...))
Нужно дать объект в аргумент/переменную этого моба для GUI и удалять этого моба, когда закрывается GUI (отправлять пакет в методе закрытия GUI на серверную сторону ид в мире этого моба, чтобы по этому иду вычислить которому мобу принадлежит ид и этого моба убить). [Если GUI ванильное, то тут придётся использовать эвент(-ы)]
 
7,099
324
1,510
Dahaka написал(а):
Не, ну я понимаю еще, если бы это было подобие джина. Может у автора именно такая задумка?
junkil335 написал(а):
Каком образом можно сделать торговлю с жителем (но без жителя), просто кликнув с предметом в руке?
Вроде, ему просто нужен торгующий предмет
 
Сверху