use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project custom-items-gradle by knokko.
the class CustomItemNBT method getDurability.
/**
* Gets the remaining custom durability of this item stack. If no custom
* durability was stored in the custom item nbt, this method returns null.
*
* @throws UnsupportedOperationException If this item doesn't have custom item nbt
*/
public Long getDurability() throws UnsupportedOperationException {
assertOurNBT();
NBTTagCompound ourTag = getOurTag();
if (!ourTag.hasKey(DURABILITY))
return null;
return getOurTag().getLong(DURABILITY);
}
use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project custom-items-gradle by knokko.
the class Raytracer method raytrace.
/**
* <p>Performs a raytrace from {@code startLocation} towards {@code startLocation + vector}.
* The {@code vector} determines both the direction and the maximum distance of the raytrace!</p>
*
* <p>If an intersection with any block or entity was found, a RaytraceResult representing the intersection
* that is closest to {@code startLocation} will be returned. If no such intersection was found, this
* method will return null.</p>
*
* <p>Entities included in {@code entitiesToExclude} and dropped item entities will be ignored by
* the raytrace.</p>
*
* @param startLocation The location from which the raytrace will start
* @param vector The direction and maximum distance of the raytrace
* @param entitiesToExclude An array of entities that will be ignored by this raytrace, may contain null
* @return A RaytraceResult for the nearest intersection, or null if no intersection was found
*/
public static RaytraceResult raytrace(Location startLocation, Vector vector, Entity... entitiesToExclude) {
// Important variables
World world = startLocation.getWorld();
Vec3D rayStart = new Vec3D(startLocation.getX(), startLocation.getY(), startLocation.getZ());
Vec3D velocityVec = new Vec3D(vector.getX(), vector.getY(), vector.getZ());
Vec3D rayEnd = new Vec3D(rayStart.x + velocityVec.x, rayStart.y + velocityVec.y, rayStart.z + velocityVec.z);
CraftWorld craftWorld = (CraftWorld) world;
WorldServer nmsWorld = craftWorld.getHandle();
// Start with infinity to make sure that any other distance will be shorter
double nearestDistanceSq = Double.POSITIVE_INFINITY;
Vec3D intersectionPos = null;
// The block raytrace
MovingObjectPosition rayResult = nmsWorld.rayTrace(rayStart, rayEnd, true, true, false);
if (rayResult != null && rayResult.type == EnumMovingObjectType.BLOCK) {
double blockDistanceSq = rayResult.pos.distanceSquared(rayStart);
if (blockDistanceSq < vector.lengthSquared()) {
intersectionPos = rayResult.pos;
nearestDistanceSq = blockDistanceSq;
}
}
// The entity raytrace
AxisAlignedBB movementBB = new AxisAlignedBB(rayStart.x, rayStart.y, rayStart.z, rayEnd.x, rayEnd.y, rayEnd.z);
List<net.minecraft.server.v1_12_R1.Entity> nmsEntityList = nmsWorld.getEntities(null, movementBB);
net.minecraft.server.v1_12_R1.Entity intersectedEntity = null;
entityListLoop: for (net.minecraft.server.v1_12_R1.Entity nmsEntity : nmsEntityList) {
// It's currently convenient to ignore dropped items
if (nmsEntity instanceof EntityItem)
continue entityListLoop;
// Since the entities in entitiesToExclude could be null, it's important to call equals() on craftEntity
CraftEntity craftEntity = nmsEntity.getBukkitEntity();
for (Entity exclude : entitiesToExclude) if (craftEntity.equals(exclude))
continue entityListLoop;
// Check if we intersect this entity and check if the distance to it is smaller than the nearest distance so far
MovingObjectPosition entityIntersection = nmsEntity.getBoundingBox().b(rayStart, rayEnd);
if (entityIntersection != null) {
double distanceSq = rayStart.distanceSquared(entityIntersection.pos);
if (distanceSq < nearestDistanceSq) {
nearestDistanceSq = distanceSq;
intersectedEntity = nmsEntity;
intersectionPos = entityIntersection.pos;
}
}
}
// Determining the final result
if (nearestDistanceSq < Double.POSITIVE_INFINITY) {
Location hitLocation = new Location(world, intersectionPos.x, intersectionPos.y, intersectionPos.z);
if (intersectedEntity != null) {
return RaytraceResult.hitEntity(intersectedEntity.getBukkitEntity(), hitLocation);
} else {
return RaytraceResult.hitBlock(hitLocation);
}
} else {
return null;
}
}
use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project TheAPI by TheDevTec.
the class v1_12_R1 method processInvClickPacket.
@Override
public boolean processInvClickPacket(Player player, HolderGUI gui, Object provPacket) {
PacketPlayInWindowClick packet = (PacketPlayInWindowClick) provPacket;
int slot = packet.b();
if (slot == -999)
return false;
int id = packet.a();
int mouseClick = packet.c();
net.minecraft.server.v1_12_R1.InventoryClickType nmsType = packet.f();
InventoryClickType type = InventoryClickType.valueOf(nmsType.name());
Object container = gui.getContainer(player);
ItemStack item = asBukkitItem(packet.e());
if ((type == InventoryClickType.QUICK_MOVE || type == InventoryClickType.CLONE || type == InventoryClickType.THROW || item.getType() == Material.AIR) && item.getType() == Material.AIR)
item = asBukkitItem(getSlotItem(container, slot));
boolean cancel = false;
if (InventoryClickType.SWAP == type) {
item = player.getInventory().getItem(mouseClick);
mouseClick = 0;
cancel = true;
}
if (item == null)
item = new ItemStack(Material.AIR);
ItemStack before = player.getItemOnCursor();
ClickType clickType = BukkitLoader.buildClick(item, type, slot, mouseClick);
if (!cancel)
cancel = BukkitLoader.useItem(player, item, gui, slot, clickType);
if (!gui.isInsertable())
cancel = true;
int gameSlot = slot > gui.size() - 1 ? InventoryUtils.convertToPlayerInvSlot(slot - gui.size()) : slot;
if (!cancel)
cancel = gui.onIteractItem(player, item, clickType, gameSlot, slot < gui.size());
else
gui.onIteractItem(player, item, clickType, gameSlot, slot < gui.size());
int position = 0;
if (!cancel && type == InventoryClickType.QUICK_MOVE) {
ItemStack[] contents = slot < gui.size() ? player.getInventory().getStorageContents() : gui.getInventory().getStorageContents();
List<Integer> modified = slot < gui.size() ? InventoryUtils.shift(slot, player, gui, clickType, gui instanceof AnvilGUI ? DestinationType.PLAYER_INV_ANVIL : DestinationType.PLAYER_INV_CUSTOM_INV, null, contents, item) : InventoryUtils.shift(slot, player, gui, clickType, DestinationType.CUSTOM_INV, gui.getNotInterableSlots(player), contents, item);
if (!modified.isEmpty()) {
if (slot < gui.size()) {
boolean canRemove = !modified.contains(-1);
player.getInventory().setStorageContents(contents);
if (canRemove) {
gui.remove(gameSlot);
} else {
gui.getInventory().setItem(gameSlot, item);
}
} else {
boolean canRemove = !modified.contains(-1);
gui.getInventory().setStorageContents(contents);
if (canRemove) {
player.getInventory().setItem(gameSlot, null);
} else {
player.getInventory().setItem(gameSlot, item);
}
}
}
return true;
}
if (cancel) {
// MOUSE
BukkitLoader.getPacketHandler().send(player, packetSetSlot(-1, -1, asNMSItem(before)));
switch(type) {
case CLONE:
return true;
case SWAP:
case QUICK_MOVE:
case PICKUP_ALL:
// TOP
for (ItemStack cItem : gui.getInventory().getContents()) {
BukkitLoader.getPacketHandler().send(player, packetSetSlot(id, position++, asNMSItem(cItem)));
}
// BUTTON
player.updateInventory();
return true;
default:
BukkitLoader.getPacketHandler().send(player, packetSetSlot(id, slot, getSlotItem(container, slot)));
if (gui instanceof AnvilGUI) {
// TOP
for (ItemStack cItem : gui.getInventory().getContents()) {
if (position != slot)
BukkitLoader.getPacketHandler().send(player, packetSetSlot(id, position++, asNMSItem(cItem)));
}
// BUTTON
player.updateInventory();
}
return true;
}
} else {
if (gui instanceof AnvilGUI && slot == 2)
postToMainThread(() -> ((ContainerAnvil) container).shiftClick((EntityPlayer) getPlayer(player), slot));
}
return false;
}
use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project Autumn by joeengo.
the class ServerUtil method isIllegal.
public static boolean isIllegal(ItemStack item, boolean isInsideShulker) {
if (item == null)
return false;
net.minecraft.server.v1_12_R1.ItemStack nmscopy = CraftItemStack.asNMSCopy(item);
if (nmscopy.hasTag()) {
NBTTagCompound tag = nmscopy.getTag();
assert tag != null;
if (tag.get("AttributeModifiers") != null) {
return true;
}
if (tag.get("Unbreakable") != null) {
return true;
}
}
if (item.getDurability() > item.getType().getMaxDurability() && item.getType().getMaxDurability() > 0) {
return true;
}
;
try {
if (item.getEnchantments() != null) {
Map<Enchantment, Integer> ench = item.getEnchantments();
for (Map.Entry<Enchantment, Integer> entry : ench.entrySet()) {
Enchantment enchant = entry.getKey();
int enchantLvl = entry.getValue();
if (enchantLvl > enchant.getMaxLevel()) {
return true;
}
}
}
} catch (Exception e) {
}
if (MaterialHandler.isIllegalMaterial(item.getType()))
return true;
if ((isShulker(item) && Plugin.Instance.getConfig().getBoolean("Illegals.RevertShulkers"))) {
if (isInsideShulker)
return true;
for (ItemStack i : getShulkerInventory(item).getContents()) {
if (isIllegal(i, true)) {
return true;
}
}
}
if ((item.getAmount() > item.getMaxStackSize()) && Plugin.Instance.getConfig().getBoolean("Illegals.RevertOverstacks"))
return true;
return false;
}
use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project PaperDev by Kamillaova.
the class CraftItemStack method setItemMeta.
public static boolean setItemMeta(net.minecraft.server.v1_12_R1.ItemStack item, ItemMeta itemMeta) {
if (item == null) {
return false;
}
if (CraftItemFactory.instance().equals(itemMeta, null)) {
item.setTag(null);
return true;
}
if (!CraftItemFactory.instance().isApplicable(itemMeta, getType(item))) {
return false;
}
itemMeta = CraftItemFactory.instance().asMetaFor(itemMeta, getType(item));
if (itemMeta == null)
return true;
NBTTagCompound tag = new NBTTagCompound();
item.setTag(tag);
((CraftMetaItem) itemMeta).applyToItem(tag);
item.convertStack();
return true;
}
Aggregations