-
Notifications
You must be signed in to change notification settings - Fork 421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic villager trades #1819
Open
Ragnok123
wants to merge
4
commits into
CloudburstMC:master
Choose a base branch
from
Ragnok123:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Basic villager trades #1819
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
package cn.nukkit.entity.passive; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import cn.nukkit.Player; | ||
import cn.nukkit.entity.Entity; | ||
import cn.nukkit.entity.EntityAgeable; | ||
import cn.nukkit.entity.EntityCreature; | ||
import cn.nukkit.inventory.Inventory; | ||
import cn.nukkit.inventory.InventoryHolder; | ||
import cn.nukkit.inventory.TradeInventory; | ||
import cn.nukkit.inventory.TradeInventoryRecipe; | ||
import cn.nukkit.item.Item; | ||
import cn.nukkit.level.format.FullChunk; | ||
import cn.nukkit.nbt.tag.CompoundTag; | ||
import cn.nukkit.nbt.tag.ListTag; | ||
|
||
public class EntityVillager extends EntityCreature implements EntityNPC, EntityAgeable { | ||
public class EntityVillager extends EntityCreature implements InventoryHolder, EntityNPC, EntityAgeable { | ||
|
||
public static final int NETWORK_ID = 115; | ||
private TradeInventory inventory; | ||
private List<TradeInventoryRecipe> recipes = new ArrayList<TradeInventoryRecipe>(); | ||
|
||
public EntityVillager(FullChunk chunk, CompoundTag nbt) { | ||
super(chunk, nbt); | ||
|
@@ -43,6 +56,67 @@ public String getName() { | |
public void initEntity() { | ||
super.initEntity(); | ||
this.setMaxHealth(20); | ||
this.inventory = new TradeInventory(this); | ||
if(this.namedTag.getCompound("Offers") != null) { | ||
ListTag<CompoundTag> nbtRecipes = this.namedTag.getCompound("Offers").getList("Recipes", CompoundTag.class); | ||
for(CompoundTag nbt : nbtRecipes.getAll()) { | ||
recipes.add(TradeInventoryRecipe.toNBT(nbt)); | ||
} | ||
} else { | ||
CompoundTag nbt = new CompoundTag("Offers"); | ||
nbt.putList(new ListTag<CompoundTag>("Recipes")); | ||
nbt.putList(getDefaultTierExpRequirements()); | ||
this.namedTag.putCompound("Offers", nbt); | ||
} | ||
} | ||
|
||
public void setTradeTier(int tier) { | ||
this.namedTag.putInt("TradeTier", tier); | ||
} | ||
|
||
public int getTradeTier() { | ||
return this.namedTag.getInt("TradeTier"); | ||
} | ||
|
||
public void setWilling(boolean value) { | ||
this.namedTag.putBoolean("Willing", value); | ||
} | ||
|
||
public boolean isWilling() { | ||
return this.namedTag.getBoolean("Willing"); | ||
} | ||
|
||
@Override | ||
public boolean onInteract(Player player, Item item) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Google Translate)
|
||
if(this.namedTag.getCompound("Offers") != null) { | ||
player.addWindow(this.getInventory()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public void addTradeRcipe(TradeInventoryRecipe recipe) { | ||
PetteriM1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.recipes.add(recipe); | ||
ListTag<CompoundTag> nbtRecipes = this.getOffers().getList("Recipes", CompoundTag.class); | ||
nbtRecipes.add(recipe.toNBT()); | ||
} | ||
|
||
public List<TradeInventoryRecipe> getRecipes(){ | ||
return this.recipes; | ||
} | ||
|
||
public CompoundTag getOffers() { | ||
return this.namedTag.getCompound("Offers"); | ||
} | ||
|
||
private ListTag<CompoundTag> getDefaultTierExpRequirements() { | ||
ListTag<CompoundTag> tag = new ListTag<CompoundTag>("TierExpRequirements"); | ||
tag.add(new CompoundTag().putInt("0", 0)); | ||
tag.add(new CompoundTag().putInt("1", 10)); | ||
tag.add(new CompoundTag().putInt("2", 70)); | ||
tag.add(new CompoundTag().putInt("3", 150)); | ||
tag.add(new CompoundTag().putInt("4", 250)); | ||
return tag; | ||
} | ||
|
||
public boolean isBaby() { | ||
|
@@ -53,4 +127,9 @@ public void setBaby(boolean baby) { | |
this.setDataFlag(DATA_FLAGS, DATA_FLAG_BABY, baby); | ||
this.setScale(baby ? 0.5f : 1); | ||
} | ||
|
||
@Override | ||
public Inventory getInventory() { | ||
return this.inventory; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cn.nukkit.inventory; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteOrder; | ||
|
||
import cn.nukkit.Player; | ||
import cn.nukkit.entity.passive.EntityVillager; | ||
import cn.nukkit.item.Item; | ||
import cn.nukkit.nbt.NBTIO; | ||
import cn.nukkit.network.protocol.UpdateTradePacket; | ||
|
||
public class TradeInventory extends BaseInventory { | ||
|
||
public TradeInventory(InventoryHolder holder) { | ||
super(holder, InventoryType.TRADING); | ||
} | ||
|
||
public void onOpen(Player who) { | ||
super.onOpen(who); | ||
|
||
UpdateTradePacket pk = new UpdateTradePacket(); | ||
pk.windowId = (byte) who.getWindowId(this); | ||
pk.windowType = (byte) InventoryType.TRADING.getNetworkType(); | ||
pk.isWilling = this.getHolder().isWilling(); | ||
pk.screen2 = true; | ||
pk.trader = this.getHolder().getId(); | ||
pk.tradeTier = this.getHolder().getTradeTier(); | ||
pk.player = who.getId(); | ||
try { | ||
pk.offers = NBTIO.write(this.getHolder().getOffers(),ByteOrder.LITTLE_ENDIAN); | ||
} catch(IOException ex) {} | ||
|
||
who.dataPacket(pk); | ||
} | ||
|
||
public void onClose(Player who) { | ||
for(int i = 0; i <= 1; i++) { | ||
Item item = getItem(i); | ||
if(who.getInventory().canAddItem(item)) { | ||
who.getInventory().addItem(item); | ||
} else { | ||
who.dropItem(item); | ||
} | ||
this.clear(i); | ||
} | ||
|
||
super.onClose(who); | ||
} | ||
|
||
public EntityVillager getHolder() { | ||
return (EntityVillager) this.holder; | ||
} | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
src/main/java/cn/nukkit/inventory/TradeInventoryRecipe.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package cn.nukkit.inventory; | ||
|
||
import cn.nukkit.item.Item; | ||
import cn.nukkit.nbt.NBTIO; | ||
import cn.nukkit.nbt.tag.CompoundTag; | ||
import lombok.Getter; | ||
|
||
public class TradeInventoryRecipe { | ||
|
||
public static int A_ITEM = 0; | ||
public static int B_ITEM = 1; | ||
@Getter | ||
private Item sellItem; | ||
@Getter | ||
private Item buyItem; | ||
@Getter | ||
private Item secondBuyItem; | ||
|
||
private int tier = -1; | ||
private int maxUses = 999; | ||
private int buyCountA = 0; | ||
private int buyCountB = 0; | ||
private int uses = 0; | ||
private int demand = 0; | ||
private int rewardsExp = 0; | ||
private int traderExp = 0; | ||
private float priceMultiplierA = 0F; | ||
private float priceMultiplierB = 0F; | ||
|
||
|
||
public TradeInventoryRecipe(Item sellItem, Item buyItem) { | ||
this(sellItem, buyItem, Item.get(0)); | ||
} | ||
|
||
public TradeInventoryRecipe(Item sellItem, Item buyItem, Item secondBuyItem) { | ||
this.sellItem = sellItem; | ||
this.buyItem = buyItem; | ||
this.secondBuyItem = secondBuyItem; | ||
} | ||
|
||
public TradeInventoryRecipe setTier(int tier) { | ||
this.tier = tier; | ||
return this; | ||
} | ||
|
||
public TradeInventoryRecipe setMaxUses(int maxUses) { | ||
this.maxUses = maxUses; | ||
return this; | ||
} | ||
|
||
public TradeInventoryRecipe setBuyCount(int count, int type) { | ||
switch(type) { | ||
case 0: | ||
this.buyCountA = count; | ||
break; | ||
case 1: | ||
this.buyCountB = count; | ||
break; | ||
} | ||
this.buyCountA = count; | ||
return this; | ||
} | ||
|
||
public TradeInventoryRecipe setDemand(int demand) { | ||
this.demand = demand; | ||
return this; | ||
} | ||
|
||
public TradeInventoryRecipe setMultiplier(float multiplier, int type) { | ||
switch(type) { | ||
case 0: | ||
this.priceMultiplierA = multiplier; | ||
break; | ||
case 1: | ||
this.priceMultiplierB = multiplier; | ||
break; | ||
} | ||
return this; | ||
} | ||
|
||
public TradeInventoryRecipe setRewardExp(int reward) { | ||
this.rewardsExp = reward; | ||
return this; | ||
} | ||
|
||
public CompoundTag toNBT() { | ||
CompoundTag nbt = new CompoundTag(); | ||
nbt.putCompound("buyA", NBTIO.putItemHelper(buyItem, -1)); | ||
nbt.putCompound("buyB", NBTIO.putItemHelper(secondBuyItem,-1)); | ||
nbt.putCompound("sell", NBTIO.putItemHelper(sellItem, -1)); | ||
nbt.putInt("tier", tier); | ||
nbt.putInt("buyCountA", buyCountA); | ||
nbt.putInt("buyCountB", buyCountB); | ||
nbt.putInt("uses", uses); | ||
nbt.putInt("maxUses", maxUses); | ||
nbt.putInt("rewardExp", rewardsExp); | ||
nbt.putInt("demand", demand); | ||
nbt.putInt("traderExp", traderExp); | ||
nbt.putFloat("priceMultiplierA", priceMultiplierA); | ||
nbt.putFloat("priceMultiplierB", priceMultiplierB); | ||
return nbt; | ||
} | ||
|
||
public static TradeInventoryRecipe toNBT(CompoundTag nbt) { | ||
TradeInventoryRecipe recipe = new TradeInventoryRecipe(NBTIO.getItemHelper(nbt.getCompound("sell")), NBTIO.getItemHelper(nbt.getCompound("buyA")), NBTIO.getItemHelper(nbt.getCompound("buyB"))) | ||
.setTier(nbt.getInt("tier")) | ||
.setBuyCount(nbt.getInt("buyCountA"), A_ITEM) | ||
.setBuyCount(nbt.getInt("buyCountB"), B_ITEM) | ||
.setMaxUses(nbt.getInt("maxUses")) | ||
.setMultiplier(nbt.getInt("priceMultiplierA"), A_ITEM) | ||
.setMultiplier(nbt.getInt("priceMultiplierB"), B_ITEM) | ||
.setDemand(nbt.getInt("demand")) | ||
.setRewardExp(nbt.getInt("rewardExp")); | ||
return recipe; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about implementing InventoryHolder here, since it has no physical inventory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or does it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Villager trading is inventory and since villager contains trade recipes, i find them as inventory holders.