EntityItem.java 4.83 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
package emu.grasscutter.game.entity;

import emu.grasscutter.data.def.ItemData;
4
import emu.grasscutter.game.inventory.GameItem;
Melledy's avatar
Melledy committed
5
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
6
7
import emu.grasscutter.game.props.EntityIdType;
import emu.grasscutter.game.props.PlayerProperty;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.world.Scene;
Melledy's avatar
Melledy committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import emu.grasscutter.net.proto.AbilitySyncStateInfoOuterClass.AbilitySyncStateInfo;
import emu.grasscutter.net.proto.AnimatorParameterValueInfoPairOuterClass.AnimatorParameterValueInfoPair;
import emu.grasscutter.net.proto.EntityAuthorityInfoOuterClass.EntityAuthorityInfo;
import emu.grasscutter.net.proto.EntityClientDataOuterClass.EntityClientData;
import emu.grasscutter.net.proto.EntityRendererChangedInfoOuterClass.EntityRendererChangedInfo;
import emu.grasscutter.net.proto.GadgetBornTypeOuterClass.GadgetBornType;
import emu.grasscutter.net.proto.MotionInfoOuterClass.MotionInfo;
import emu.grasscutter.net.proto.PropPairOuterClass.PropPair;
import emu.grasscutter.net.proto.ProtEntityTypeOuterClass.ProtEntityType;
import emu.grasscutter.net.proto.SceneEntityAiInfoOuterClass.SceneEntityAiInfo;
import emu.grasscutter.net.proto.SceneEntityInfoOuterClass.SceneEntityInfo;
import emu.grasscutter.net.proto.SceneGadgetInfoOuterClass.SceneGadgetInfo;
import emu.grasscutter.net.proto.VectorOuterClass.Vector;
import emu.grasscutter.utils.Position;
import emu.grasscutter.utils.ProtoHelper;
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;

26
public class EntityItem extends EntityBaseGadget {
Melledy's avatar
Melledy committed
27
28
29
	private final Position pos;
	private final Position rot;
	
30
	private final GameItem item;
Melledy's avatar
Melledy committed
31
	private final long guid;
Kengxxiao's avatar
Kengxxiao committed
32
33
34

	private final boolean share;

35
	public EntityItem(Scene scene, Player player, ItemData itemData, Position pos, int count) {
36
37
		super(scene);
		this.id = getScene().getWorld().getNextEntityId(EntityIdType.GADGET);
Melledy's avatar
Melledy committed
38
39
		this.pos = new Position(pos);
		this.rot = new Position();
Kengxxiao's avatar
Kengxxiao committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
		this.guid = player == null ? scene.getWorld().getHost().getNextGameGuid() : player.getNextGameGuid();
		this.item = new GameItem(itemData, count);
		this.share = true;
	}

	// In official game, some drop items are shared to all players, and some other items are independent to all players
	// For example, if you killed a monster in MP mode, all players could get drops but rarity and number of them are different
	// but if you broke regional mine, when someone picked up the drop then it disappeared
	public EntityItem(Scene scene, Player player, ItemData itemData, Position pos, int count, boolean share) {
		super(scene);
		this.id = getScene().getWorld().getNextEntityId(EntityIdType.GADGET);
		this.pos = new Position(pos);
		this.rot = new Position();
		this.guid = player == null ? scene.getWorld().getHost().getNextGameGuid() : player.getNextGameGuid();
54
		this.item = new GameItem(itemData, count);
Kengxxiao's avatar
Kengxxiao committed
55
		this.share = share;
Melledy's avatar
Melledy committed
56
57
58
59
60
61
62
	}
	
	@Override
	public int getId() {
		return this.id;
	}
	
63
	private GameItem getItem() {
Melledy's avatar
Melledy committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
		return this.item;
	}

	public ItemData getItemData() {
		return this.getItem().getItemData();
	}

	public long getGuid() {
		return guid;
	}

	public int getCount() {
		return this.getItem().getCount();
	}
	
	@Override
	public int getGadgetId() {
		return this.getItemData().getGadgetId();
	}

	@Override
	public Position getPosition() {
		return this.pos;
	}

	@Override
	public Position getRotation() {
		return this.rot;
	}
	
	@Override
	public Int2FloatOpenHashMap getFightProperties() {
		return null;
	}

Kengxxiao's avatar
Kengxxiao committed
99
100
101
102
	public boolean isShare() {
		return share;
	}

Melledy's avatar
Melledy committed
103
104
105
106
107
108
109
110
111
112
113
	@Override
	public SceneEntityInfo toProto() {
		EntityAuthorityInfo authority = EntityAuthorityInfo.newBuilder()
				.setAbilityInfo(AbilitySyncStateInfo.newBuilder())
				.setRendererChangedInfo(EntityRendererChangedInfo.newBuilder())
				.setAiInfo(SceneEntityAiInfo.newBuilder().setIsAiOpen(true).setBornPos(Vector.newBuilder()))
				.setBornPos(Vector.newBuilder())
				.build();
		
		SceneEntityInfo.Builder entityInfo = SceneEntityInfo.newBuilder()
				.setEntityId(getId())
114
				.setEntityType(ProtEntityType.PROT_ENTITY_GADGET)
Melledy's avatar
Melledy committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
				.setMotionInfo(MotionInfo.newBuilder().setPos(getPosition().toProto()).setRot(getRotation().toProto()).setSpeed(Vector.newBuilder()))
				.addAnimatorParaList(AnimatorParameterValueInfoPair.newBuilder())
				.setEntityClientData(EntityClientData.newBuilder())
				.setEntityAuthorityInfo(authority)
				.setLifeState(1);
		
		PropPair pair = PropPair.newBuilder()
				.setType(PlayerProperty.PROP_LEVEL.getId())
				.setPropValue(ProtoHelper.newPropValue(PlayerProperty.PROP_LEVEL, 1))
				.build();
		entityInfo.addPropList(pair);
		
		SceneGadgetInfo.Builder gadgetInfo = SceneGadgetInfo.newBuilder()
				.setGadgetId(this.getItemData().getGadgetId())
				.setTrifleItem(this.getItem().toProto())
130
				.setBornType(GadgetBornType.GADGET_BORN_IN_AIR)
Melledy's avatar
Melledy committed
131
132
133
134
135
136
137
138
				.setAuthorityPeerId(this.getWorld().getHostPeerId())
				.setIsEnableInteract(true);

		entityInfo.setGadget(gadgetInfo);
		
		return entityInfo.build();
	}
}