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

Melledy's avatar
Melledy committed
3
import emu.grasscutter.data.excels.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
import emu.grasscutter.game.props.ActionReason;
Melledy's avatar
Melledy committed
7
8
import emu.grasscutter.game.props.EntityIdType;
import emu.grasscutter.game.props.PlayerProperty;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.game.world.Scene;
Melledy's avatar
Melledy committed
10
11
12
13
14
15
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;
Melledy's avatar
Melledy committed
16
17
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
import emu.grasscutter.net.proto.InteractTypeOuterClass.InteractType;
Melledy's avatar
Melledy committed
18
19
20
21
22
23
24
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;
Melledy's avatar
Melledy committed
25
import emu.grasscutter.server.packet.send.PacketGadgetInteractRsp;
Melledy's avatar
Melledy committed
26
27
28
29
import emu.grasscutter.utils.Position;
import emu.grasscutter.utils.ProtoHelper;
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;

30
public class EntityItem extends EntityBaseGadget {
Melledy's avatar
Melledy committed
31
32
33
	private final Position pos;
	private final Position rot;
	
34
	private final GameItem item;
Melledy's avatar
Melledy committed
35
	private final long guid;
Kengxxiao's avatar
Kengxxiao committed
36
37
38

	private final boolean share;

39
	public EntityItem(Scene scene, Player player, ItemData itemData, Position pos, int count) {
40
41
		super(scene);
		this.id = getScene().getWorld().getNextEntityId(EntityIdType.GADGET);
Melledy's avatar
Melledy committed
42
43
		this.pos = new Position(pos);
		this.rot = new Position();
Kengxxiao's avatar
Kengxxiao committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
		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();
58
		this.item = new GameItem(itemData, count);
Kengxxiao's avatar
Kengxxiao committed
59
		this.share = share;
Melledy's avatar
Melledy committed
60
61
62
63
64
65
66
	}
	
	@Override
	public int getId() {
		return this.id;
	}
	
67
	private GameItem getItem() {
Melledy's avatar
Melledy committed
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
99
100
101
102
		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
103
104
105
	public boolean isShare() {
		return share;
	}
Melledy's avatar
Melledy committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	
	@Override
	public void onInteract(Player player, GadgetInteractReq interactReq) {
	    // check drop owner to avoid someone picked up item in others' world
        if (!this.isShare()) {
            int dropOwner = (int) (this.getGuid() >> 32);
            if (dropOwner != player.getUid()) {
                return;
            }
        }
        
        this.getScene().removeEntity(this);
        GameItem item = new GameItem(this.getItemData(), this.getCount());
        
        // Add to inventory
        boolean success = player.getInventory().addItem(item, ActionReason.SubfieldDrop);
        if (success) {
            if (!this.isShare()) { // not shared drop
                player.sendPacket(new PacketGadgetInteractRsp(this, InteractType.INTERACT_TYPE_PICK_ITEM));
            } else {
                this.getScene().broadcastPacket(new PacketGadgetInteractRsp(this, InteractType.INTERACT_TYPE_PICK_ITEM));
            }
        }
    }
Kengxxiao's avatar
Kengxxiao committed
130

Melledy's avatar
Melledy committed
131
132
133
134
135
136
137
138
139
140
141
	@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())
142
				.setEntityType(ProtEntityType.PROT_ENTITY_TYPE_GADGET)
Melledy's avatar
Melledy committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
				.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())
158
				.setBornType(GadgetBornType.GADGET_BORN_TYPE_IN_AIR)
Melledy's avatar
Melledy committed
159
160
161
162
163
164
165
166
				.setAuthorityPeerId(this.getWorld().getHostPeerId())
				.setIsEnableInteract(true);

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