-
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 all 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
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; | ||
import cn.nukkit.utils.ServerException; | ||
|
||
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; | ||
|
||
public EntityVillager(FullChunk chunk, CompoundTag nbt) { | ||
super(chunk, nbt); | ||
|
@@ -43,6 +57,134 @@ public String getName() { | |
public void initEntity() { | ||
super.initEntity(); | ||
this.setMaxHealth(20); | ||
this.inventory = new TradeInventory(this); | ||
this.recipes = new ArrayList<TradeInventoryRecipe>(); | ||
this.dataProperties.putLong(DATA_TRADING_PLAYER_EID, 0L); | ||
if(this.namedTag.contains("Offers")) { | ||
ListTag<CompoundTag> nbtRecipes = getListRecipes(); | ||
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 setTraderName(String name) { | ||
this.namedTag.putString("TraderName", name); | ||
} | ||
|
||
public String getTraderName() { | ||
if(!this.namedTag.contains("TraderName")) { | ||
this.namedTag.putString("TraderName", getNameTag()); | ||
} | ||
return this.namedTag.getString("TraderName"); | ||
} | ||
|
||
public void setTradeTier(int tier) { | ||
if(tier > 4){ | ||
throw new ServerException("Maximal tier is 4, but your tier is higher than 4"); | ||
} | ||
this.namedTag.putInt("TradeTier", tier); | ||
this.dataProperties.putInt(DATA_TRADE_TIER, tier); | ||
} | ||
|
||
public void setMaxTradeTier(int maxTier) { | ||
if(maxTier > 4) maxTier = 4; | ||
this.dataProperties.putInt(DATA_MAX_TRADE_TIER, maxTier); | ||
} | ||
|
||
public int getMaxTradeTier() { | ||
if(!this.dataProperties.exists(DATA_MAX_TRADE_TIER)) { | ||
this.setMaxTradeTier(4); | ||
} | ||
return this.dataProperties.getInt(DATA_MAX_TRADE_TIER); | ||
} | ||
|
||
public void setExperience(int experience) { | ||
this.dataProperties.putInt(DATA_TRADE_EXPERIENCE, experience); | ||
} | ||
|
||
public int getExperience() { | ||
if(!this.dataProperties.exists(DATA_TRADE_EXPERIENCE)) { | ||
setExperience(0); | ||
} | ||
return this.dataProperties.getInt(DATA_TRADE_EXPERIENCE); | ||
} | ||
|
||
public int getTradeTier() { | ||
if(!this.namedTag.contains("TradeTier")) { | ||
this.namedTag.putInt("TradeTier", 0); | ||
} | ||
return this.namedTag.getInt("TradeTier"); | ||
} | ||
|
||
public void setWilling(boolean value) { | ||
this.namedTag.putBoolean("Willing", value); | ||
} | ||
|
||
public boolean isWilling() { | ||
if(!this.namedTag.contains("Willing")) { | ||
this.namedTag.putBoolean("Willing", true); | ||
} | ||
return this.namedTag.getBoolean("Willing"); | ||
} | ||
|
||
public void cancelTradingWithPlayer() { | ||
this.setTradingWith(0L); | ||
} | ||
|
||
public void setTradingWith(long eid) { | ||
this.dataProperties.putLong(DATA_TRADING_PLAYER_EID, eid); | ||
} | ||
|
||
public boolean isTrading() { | ||
return this.dataProperties.getLong(DATA_TRADING_PLAYER_EID) != 0L; | ||
} | ||
|
||
@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.contains("Offers") && !isTrading()) { | ||
player.addWindow(this.getInventory()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public void addTradeRecipe(TradeInventoryRecipe recipe) { | ||
this.recipes.add(recipe); | ||
getListRecipes().add(recipe.toNBT()); | ||
} | ||
|
||
public List<TradeInventoryRecipe> getRecipes(){ | ||
return this.recipes; | ||
} | ||
|
||
public ListTag<CompoundTag> getListRecipes(){ | ||
if(!this.getOffers().contains("Recipes")) { | ||
this.getOffers().putList(new ListTag<CompoundTag>("Recipes")); | ||
} | ||
return this.getOffers().getList("Recipes", CompoundTag.class); | ||
} | ||
|
||
public CompoundTag getOffers() { | ||
if(!this.namedTag.contains("Offers")) { | ||
this.namedTag.putCompound("Offers", new CompoundTag("Offers")); | ||
} | ||
return this.namedTag.getCompound("Offers"); | ||
} | ||
|
||
public 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 +195,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,71 @@ | ||
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.nbt.tag.CompoundTag; | ||
import cn.nukkit.network.protocol.UpdateTradePacket; | ||
|
||
public class TradeInventory extends BaseInventory { | ||
|
||
public static final int TRADE_INPUT_A = 4; | ||
public static final int TRADE_INPUT_B = 5; | ||
public static final int TRADE_OUTPUT = 51; | ||
|
||
// mojang, what the heck?? | ||
public static final int FAKE_TRADE_INPUT = -30; // moves from fake ui (villager inventory) to player inventory | ||
public static final int FAKE_TRADE_OUTPUT = -31; // item is sent from player inventory to villager fake inv | ||
|
||
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.displayName = this.getHolder().getTraderName(); | ||
pk.player = who.getId(); | ||
pk.unknownVarInt1 = 0; | ||
try { | ||
pk.offers = NBTIO.write(this.getHolder().getOffers(),ByteOrder.LITTLE_ENDIAN, true); | ||
} catch(IOException ex) {} | ||
|
||
this.getHolder().setTradingWith(who.getId()); | ||
who.isTrading = true; | ||
|
||
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); | ||
} | ||
|
||
this.getHolder().cancelTradingWithPlayer(); | ||
who.isTrading = false; | ||
|
||
super.onClose(who); | ||
} | ||
|
||
public EntityVillager getHolder() { | ||
return (EntityVillager) this.holder; | ||
} | ||
|
||
} |
Oops, something went wrong.
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.