Fixed DCP inventory, have no idea about MassiveCore

Essentials error is fixed
#106
This commit is contained in:
Norbi Peti 2019-10-27 02:33:13 +01:00
parent 88c1d100e9
commit 73fc4aedcc
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 293 additions and 38 deletions

View file

@ -1,6 +1,7 @@
package buttondevteam.discordplugin;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.playerfaker.DiscordInventory;
import buttondevteam.discordplugin.playerfaker.VCMDWrapper;
import discord4j.core.object.entity.MessageChannel;
import discord4j.core.object.entity.User;
@ -12,14 +13,19 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.PermissibleBase;
import org.bukkit.permissions.ServerOperator;
import org.mockito.Answers;
import org.mockito.MockSettings;
import org.mockito.Mockito;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.UUID;
import static org.mockito.Answers.RETURNS_DEFAULTS;
public abstract class DiscordConnectedPlayer extends DiscordSenderBase implements IMCPlayer<DiscordConnectedPlayer> {
private @Getter VCMDWrapper vanillaCmdListener;
@Getter
@ -37,7 +43,7 @@ public abstract class DiscordConnectedPlayer extends DiscordSenderBase implement
@Setter
private PermissibleBase perm;
private Location location = Bukkit.getWorlds().get(0).getSpawnLocation();
private Location location;
private final MinecraftChatModule module;
@ -50,6 +56,7 @@ public abstract class DiscordConnectedPlayer extends DiscordSenderBase implement
protected DiscordConnectedPlayer(User user, MessageChannel channel, UUID uuid, String mcname,
MinecraftChatModule module) {
super(user, channel);
location = Bukkit.getWorlds().get(0).getSpawnLocation();
origPerm = perm = new PermissibleBase(basePlayer = Bukkit.getOfflinePlayer(uuid));
name = mcname;
this.module = module;
@ -64,6 +71,15 @@ public abstract class DiscordConnectedPlayer extends DiscordSenderBase implement
}
}
/**
* For testing
*/
protected DiscordConnectedPlayer(User user, MessageChannel channel) {
super(user, channel);
module = null;
uniqueId = UUID.randomUUID();
}
public void setOp(boolean value) { //CraftPlayer-compatible implementation
this.origPerm.setOp(value);
this.perm.recalculatePermissions();
@ -161,7 +177,30 @@ public abstract class DiscordConnectedPlayer extends DiscordSenderBase implement
public static DiscordConnectedPlayer create(User user, MessageChannel channel, UUID uuid, String mcname,
MinecraftChatModule module) {
return Mockito.mock(DiscordConnectedPlayer.class, Mockito.withSettings()
.defaultAnswer(Answers.CALLS_REAL_METHODS).useConstructor(user, channel, uuid, mcname, module));
return Mockito.mock(DiscordConnectedPlayer.class,
getSettings().useConstructor(user, channel, uuid, mcname, module));
}
public static DiscordConnectedPlayer createTest() {
return Mockito.mock(DiscordConnectedPlayer.class, getSettings().useConstructor(null, null));
}
private static MockSettings getSettings() {
return Mockito.withSettings()
.defaultAnswer(invocation -> {
try {
if (!Modifier.isAbstract(invocation.getMethod().getModifiers()))
return invocation.callRealMethod();
if (PlayerInventory.class.isAssignableFrom(invocation.getMethod().getReturnType()))
return Mockito.mock(DiscordInventory.class, Mockito.withSettings().extraInterfaces(PlayerInventory.class));
if (Inventory.class.isAssignableFrom(invocation.getMethod().getReturnType()))
return new DiscordInventory();
return RETURNS_DEFAULTS.answer(invocation);
} catch (Exception e) {
System.err.println("Error in mocked player!");
e.printStackTrace();
return RETURNS_DEFAULTS.answer(invocation);
}
});
}
}

View file

@ -0,0 +1,208 @@
package buttondevteam.discordplugin.playerfaker;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import java.util.*;
import java.util.stream.IntStream;
public class DiscordInventory implements Inventory {
private ItemStack[] items = new ItemStack[27];
private List<ItemStack> itemStacks = Arrays.asList(items);
@Getter
@Setter
public int maxStackSize;
private static ItemStack emptyStack = new ItemStack(Material.AIR, 0);
@Override
public int getSize() {
return items.length;
}
@Override
public String getName() {
return "Discord inventory";
}
@Override
public ItemStack getItem(int index) {
if (index >= items.length)
return emptyStack;
else
return items[index];
}
@Override
public void setItem(int index, ItemStack item) {
if (index < items.length)
items[index] = item;
}
@Override
public HashMap<Integer, ItemStack> addItem(ItemStack... items) throws IllegalArgumentException {
return IntStream.range(0, items.length).collect(HashMap::new, (map, i) -> map.put(i, items[i]), HashMap::putAll); //Pretend that we can't add anything
}
@Override
public HashMap<Integer, ItemStack> removeItem(ItemStack... items) throws IllegalArgumentException {
return IntStream.range(0, items.length).collect(HashMap::new, (map, i) -> map.put(i, items[i]), HashMap::putAll); //Pretend that we can't add anything
}
@Override
public ItemStack[] getContents() {
return items;
}
@Override
public void setContents(ItemStack[] items) throws IllegalArgumentException {
this.items = items;
}
@Override
public ItemStack[] getStorageContents() {
return items;
}
@Override
public void setStorageContents(ItemStack[] items) throws IllegalArgumentException {
this.items = items;
}
@SuppressWarnings("deprecation")
@Override
public boolean contains(int materialId) {
return itemStacks.stream().anyMatch(is -> is.getType().getId() == materialId);
}
@Override
public boolean contains(Material material) throws IllegalArgumentException {
return itemStacks.stream().anyMatch(is -> is.getType() == material);
}
@Override
public boolean contains(ItemStack item) {
return itemStacks.stream().anyMatch(is -> is.getType() == item.getType() && is.getAmount() == item.getAmount());
}
@SuppressWarnings("deprecation")
@Override
public boolean contains(int materialId, int amount) {
return itemStacks.stream().anyMatch(is -> is.getType().getId() == materialId && is.getAmount() == amount);
}
@Override
public boolean contains(Material material, int amount) throws IllegalArgumentException {
return itemStacks.stream().anyMatch(is -> is.getType() == material && is.getAmount() == amount);
}
@Override
public boolean contains(ItemStack item, int amount) { //Not correct implementation but whatever
return itemStacks.stream().anyMatch(is -> is.getType() == item.getType() && is.getAmount() == amount);
}
@Override
public boolean containsAtLeast(ItemStack item, int amount) {
return false;
}
@Override
public HashMap<Integer, ? extends ItemStack> all(int materialId) {
return new HashMap<>();
}
@Override
public HashMap<Integer, ? extends ItemStack> all(Material material) throws IllegalArgumentException {
return new HashMap<>();
}
@Override
public HashMap<Integer, ? extends ItemStack> all(ItemStack item) {
return new HashMap<>();
}
@Override
public int first(int materialId) {
return -1;
}
@Override
public int first(Material material) throws IllegalArgumentException {
return -1;
}
@Override
public int first(ItemStack item) {
return -1;
}
@Override
public int firstEmpty() {
return -1;
}
@Override
public void remove(int materialId) {
}
@Override
public void remove(Material material) throws IllegalArgumentException {
}
@Override
public void remove(ItemStack item) {
}
@Override
public void clear(int index) {
if (index < items.length)
items[index] = null;
}
@Override
public void clear() {
Arrays.fill(items, null);
}
@Override
public List<HumanEntity> getViewers() {
return Collections.emptyList();
}
@Override
public String getTitle() {
return "Discord inventory";
}
@Override
public InventoryType getType() {
return InventoryType.CHEST;
}
@Override
public InventoryHolder getHolder() {
return null;
}
@SuppressWarnings("NullableProblems")
@Override
public ListIterator<ItemStack> iterator() {
return itemStacks.listIterator();
}
@Override
public ListIterator<ItemStack> iterator(int index) {
return itemStacks.listIterator(index);
}
@Override
public Location getLocation() {
return null;
}
}

View file

@ -1,8 +1,11 @@
package buttondevteam.DiscordPlugin;
import buttondevteam.discordplugin.DiscordConnectedPlayer;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Player;
/**
* Unit test for simple App.
@ -29,6 +32,11 @@ public class AppTest extends TestCase {
* Rigourous Test :-)
*/
public void testApp() {
assertTrue(true);
Player dcp = DiscordConnectedPlayer.createTest();
double h = dcp.getMaxHealth();
System.out.println(h);
Damageable d = (Damageable) dcp;
System.out.println(d.getMaxHealth());
}
}