Skip to content
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

refactor: reduce code duplication / migrate to JOML #31

Merged
merged 1 commit into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/org/terasology/logic/inventory/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package org.terasology.logic.inventory;

import org.joml.Vector3f;
import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.logic.inventory.events.BeforeItemPutInInventory;
import org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory;
import org.terasology.logic.inventory.events.DropItemRequest;
import org.terasology.logic.inventory.events.InventorySlotChangedEvent;
import org.terasology.logic.inventory.events.InventorySlotStackSizeChangedEvent;
import org.terasology.logic.players.LocalPlayer;

import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -443,4 +446,25 @@ static void adjustStackSize(EntityRef entity, int slot, int newCount) {
entity.send(new InventorySlotStackSizeChangedEvent(slot, oldSize, newCount));
}

/**
* Send a {@link DropItemRequest} to the player entity to drop the specified amount of items, configured with common
* values for the position and impulse vectors.
*
* @param item the item type to drop from the player's inventory
* @param count the number of items to drop
* @param localPlayer the local player to drop the items from
*/
public static void dropItems(EntityRef item, int count, LocalPlayer localPlayer) {
EntityRef playerEntity = localPlayer.getCharacterEntity();

Vector3f position = localPlayer.getViewPosition(new Vector3f());
Vector3f direction = localPlayer.getViewDirection(new Vector3f());
Vector3f newPosition = position.add(direction.mul(1.5f, 1.5f, 1.5f, new Vector3f()));

Vector3f impulseVector = new Vector3f(direction);
playerEntity.send(new DropItemRequest(item, playerEntity,
impulseVector,
newPosition,
count));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import org.terasology.input.MouseInput;
import org.terasology.logic.characters.CharacterComponent;
import org.terasology.logic.inventory.InventoryUtils;
import org.terasology.logic.inventory.events.DropItemRequest;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Vector3f;
import org.terasology.nui.BaseInteractionListener;
import org.terasology.nui.Canvas;
import org.terasology.nui.InteractionListener;
Expand Down Expand Up @@ -54,18 +51,7 @@ public boolean onMouseClick(NUIMouseClickEvent event) {
count = InventoryUtils.getStackCount(item); //Drop complete stack with left click
}

Vector3f position = localPlayer.getViewPosition();
Vector3f direction = localPlayer.getViewDirection();
Vector3f newPosition = new Vector3f(position.x + direction.x * 1.5f,
position.y + direction.y * 1.5f,
position.z + direction.z * 1.5f
);
//send DropItemRequest
Vector3f impulseVector = new Vector3f(direction);
playerEntity.send(new DropItemRequest(item, playerEntity,
JomlUtil.from(impulseVector),
JomlUtil.from(newPosition),
count));
InventoryUtils.dropItems(item, count, localPlayer);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import org.terasology.logic.inventory.InventoryComponent;
import org.terasology.logic.inventory.InventoryManager;
import org.terasology.logic.inventory.InventoryUtils;
import org.terasology.logic.inventory.events.DropItemRequest;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Vector3f;
import org.terasology.nui.databinding.ReadOnlyBinding;
import org.terasology.registry.In;
import org.terasology.rendering.nui.CoreScreenLayer;
Expand Down Expand Up @@ -114,31 +111,9 @@ The code below was originally taken from moveItemSmartly() in

inventoryManager.moveItemToSlots(getTransferEntity(), fromEntity, fromSlot, targetEntity, toSlots);



/*
The code below was taken from the InteractionListener in the
DropItemRegion.class and slightly modified to work here.

The code to drop an item right in front of the player that
was in that class was almost exactly what was needed here.
*/
EntityRef item = InventoryUtils.getItemAt(movingItem, 0);

int count = InventoryUtils.getStackCount(item);

Vector3f position = localPlayer.getViewPosition();
Vector3f direction = localPlayer.getViewDirection();
Vector3f newPosition = new Vector3f(position.x + direction.x * 1.5f,
position.y + direction.y * 1.5f,
position.z + direction.z * 1.5f
);

//send DropItemRequest
Vector3f impulseVector = new Vector3f(direction);
playerEntity.send(new DropItemRequest(item, playerEntity,
JomlUtil.from(impulseVector),
JomlUtil.from(newPosition),
count));
InventoryUtils.dropItems(item, count, localPlayer);
}
}