-
Notifications
You must be signed in to change notification settings - Fork 419
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 2 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,27 @@ | ||
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>(); | ||
private int tradeTier = 0; | ||
|
||
public EntityVillager(FullChunk chunk, CompoundTag nbt) { | ||
super(chunk, nbt); | ||
|
@@ -43,7 +57,54 @@ public String getName() { | |
public void initEntity() { | ||
super.initEntity(); | ||
this.setMaxHealth(20); | ||
this.inventory = new TradeInventory(this); | ||
} | ||
|
||
public void setTradeTier(int tier) { | ||
this.tradeTier = tier; | ||
} | ||
|
||
public int getTradeTier() { | ||
return this.tradeTier; | ||
} | ||
|
||
@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(recipes.size() > 0) { | ||
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); | ||
} | ||
|
||
public CompoundTag getOffers() { | ||
CompoundTag nbt = new CompoundTag(); | ||
nbt.putList(recipesToNbt()); | ||
nbt.putList(getTierExpRequirements()); | ||
return nbt; | ||
} | ||
|
||
private ListTag<CompoundTag> recipesToNbt() { | ||
ListTag<CompoundTag> tag = new ListTag<CompoundTag>("Recipes"); | ||
for(TradeInventoryRecipe recipe : this.recipes) { | ||
tag.add(recipe.toNBT()); | ||
} | ||
return tag; | ||
} | ||
|
||
private ListTag<CompoundTag> getTierExpRequirements() { | ||
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", 60)); | ||
tag.add(new CompoundTag().putInt("3", 160)); | ||
tag.add(new CompoundTag().putInt("4", 310)); | ||
return tag; | ||
} | ||
|
||
public boolean isBaby() { | ||
return this.getDataFlag(DATA_FLAGS, DATA_FLAG_BABY); | ||
|
@@ -53,4 +114,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 = true; | ||
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; | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
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,104 @@ | ||
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; | ||
} | ||
|
||
} |
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.