use of net.minecraft.world.item.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class FishingHelperImpl method getRandomReward.
public ItemStack getRandomReward(FishingHook hook, ResourceLocation key) {
ServerLevel worldServer = (ServerLevel) hook.level;
LootContext.Builder playerFishEvent2 = new LootContext.Builder(worldServer);
LootTables registry = ((ServerLevel) hook.level).getServer().getLootTables();
// registry.getLootTable(key).getLootContextParameterSet()
LootContext info = playerFishEvent2.withOptionalParameter(LootContextParams.ORIGIN, new Vec3(hook.getX(), hook.getY(), hook.getZ())).withOptionalParameter(LootContextParams.TOOL, new ItemStack(Items.FISHING_ROD)).create(LootContextParamSets.FISHING);
List<ItemStack> itemStacks = registry.get(key).getRandomItems(info);
return itemStacks.get(worldServer.random.nextInt(itemStacks.size()));
}
use of net.minecraft.world.item.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class DenizenNetworkManagerImpl method processEquipmentForPacket.
public boolean processEquipmentForPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
if (FakeEquipCommand.overrides.isEmpty()) {
return false;
}
try {
if (packet instanceof ClientboundSetEquipmentPacket) {
int eid = ((ClientboundSetEquipmentPacket) packet).getEntity();
Entity ent = player.level.getEntity(eid);
if (ent == null) {
return false;
}
FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
if (override == null) {
return false;
}
List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>(((ClientboundSetEquipmentPacket) packet).getSlots());
ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(eid, equipment);
for (int i = 0; i < equipment.size(); i++) {
Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack> pair = equipment.get(i);
ItemStack use = pair.getSecond();
switch(pair.getFirst()) {
case MAINHAND:
use = override.hand == null ? use : CraftItemStack.asNMSCopy(override.hand.getItemStack());
break;
case OFFHAND:
use = override.offhand == null ? use : CraftItemStack.asNMSCopy(override.offhand.getItemStack());
break;
case CHEST:
use = override.chest == null ? use : CraftItemStack.asNMSCopy(override.chest.getItemStack());
break;
case HEAD:
use = override.head == null ? use : CraftItemStack.asNMSCopy(override.head.getItemStack());
break;
case LEGS:
use = override.legs == null ? use : CraftItemStack.asNMSCopy(override.legs.getItemStack());
break;
case FEET:
use = override.boots == null ? use : CraftItemStack.asNMSCopy(override.boots.getItemStack());
break;
}
equipment.set(i, new Pair<>(pair.getFirst(), use));
}
oldManager.send(newPacket, genericfuturelistener);
return true;
} else if (packet instanceof ClientboundEntityEventPacket) {
Entity ent = ((ClientboundEntityEventPacket) packet).getEntity(player.level);
if (!(ent instanceof net.minecraft.world.entity.LivingEntity)) {
return false;
}
FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
if (override == null || (override.hand == null && override.offhand == null)) {
return false;
}
if (((ClientboundEntityEventPacket) packet).getEventId() != (byte) 55) {
return false;
}
List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>();
ItemStack hand = override.hand != null ? CraftItemStack.asNMSCopy(override.hand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getMainHandItem();
ItemStack offhand = override.offhand != null ? CraftItemStack.asNMSCopy(override.offhand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getOffhandItem();
equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.MAINHAND, hand));
equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.OFFHAND, offhand));
ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(ent.getId(), equipment);
oldManager.send(newPacket, genericfuturelistener);
return true;
} else if (packet instanceof ClientboundContainerSetContentPacket) {
FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
if (override == null) {
return false;
}
int window = ((ClientboundContainerSetContentPacket) packet).getContainerId();
if (window != 0) {
return false;
}
NonNullList<ItemStack> items = (NonNullList<ItemStack>) ((ClientboundContainerSetContentPacket) packet).getItems();
if (override.head != null) {
items.set(5, CraftItemStack.asNMSCopy(override.head.getItemStack()));
}
if (override.chest != null) {
items.set(6, CraftItemStack.asNMSCopy(override.chest.getItemStack()));
}
if (override.legs != null) {
items.set(7, CraftItemStack.asNMSCopy(override.legs.getItemStack()));
}
if (override.boots != null) {
items.set(8, CraftItemStack.asNMSCopy(override.boots.getItemStack()));
}
if (override.offhand != null) {
items.set(45, CraftItemStack.asNMSCopy(override.offhand.getItemStack()));
}
if (override.hand != null) {
items.set(player.getInventory().selected + 36, CraftItemStack.asNMSCopy(override.hand.getItemStack()));
}
ClientboundContainerSetContentPacket newPacket = new ClientboundContainerSetContentPacket(window, ((ClientboundContainerSetContentPacket) packet).getStateId(), items, ((ClientboundContainerSetContentPacket) packet).getCarriedItem());
oldManager.send(newPacket, genericfuturelistener);
return true;
} else if (packet instanceof ClientboundContainerSetSlotPacket) {
FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
if (override == null) {
return false;
}
int window = ((ClientboundContainerSetSlotPacket) packet).getContainerId();
if (window != 0) {
return false;
}
int slot = ((ClientboundContainerSetSlotPacket) packet).getSlot();
org.bukkit.inventory.ItemStack item = null;
if (slot == 5 && override.head != null) {
item = override.head.getItemStack();
} else if (slot == 6 && override.chest != null) {
item = override.chest.getItemStack();
} else if (slot == 7 && override.legs != null) {
item = override.legs.getItemStack();
} else if (slot == 8 && override.boots != null) {
item = override.boots.getItemStack();
} else if (slot == 45 && override.offhand != null) {
item = override.offhand.getItemStack();
} else if (slot == player.getInventory().selected + 36 && override.hand != null) {
item = override.hand.getItemStack();
}
if (item == null) {
return false;
}
ClientboundContainerSetSlotPacket newPacket = new ClientboundContainerSetSlotPacket(window, ((ClientboundContainerSetSlotPacket) packet).getStateId(), slot, CraftItemStack.asNMSCopy(item));
oldManager.send(newPacket, genericfuturelistener);
return true;
}
} catch (Throwable ex) {
Debug.echoError(ex);
}
return false;
}
use of net.minecraft.world.item.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class DenizenNetworkManagerImpl method processEquipmentForPacket.
public boolean processEquipmentForPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
if (FakeEquipCommand.overrides.isEmpty()) {
return false;
}
try {
if (packet instanceof ClientboundSetEquipmentPacket) {
int eid = ((ClientboundSetEquipmentPacket) packet).getEntity();
Entity ent = player.level.getEntity(eid);
if (ent == null) {
return false;
}
FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
if (override == null) {
return false;
}
List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>(((ClientboundSetEquipmentPacket) packet).getSlots());
ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(eid, equipment);
for (int i = 0; i < equipment.size(); i++) {
Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack> pair = equipment.get(i);
ItemStack use = pair.getSecond();
switch(pair.getFirst()) {
case MAINHAND:
use = override.hand == null ? use : CraftItemStack.asNMSCopy(override.hand.getItemStack());
break;
case OFFHAND:
use = override.offhand == null ? use : CraftItemStack.asNMSCopy(override.offhand.getItemStack());
break;
case CHEST:
use = override.chest == null ? use : CraftItemStack.asNMSCopy(override.chest.getItemStack());
break;
case HEAD:
use = override.head == null ? use : CraftItemStack.asNMSCopy(override.head.getItemStack());
break;
case LEGS:
use = override.legs == null ? use : CraftItemStack.asNMSCopy(override.legs.getItemStack());
break;
case FEET:
use = override.boots == null ? use : CraftItemStack.asNMSCopy(override.boots.getItemStack());
break;
}
equipment.set(i, new Pair<>(pair.getFirst(), use));
}
oldManager.send(newPacket, genericfuturelistener);
return true;
} else if (packet instanceof ClientboundEntityEventPacket) {
Entity ent = ((ClientboundEntityEventPacket) packet).getEntity(player.level);
if (!(ent instanceof net.minecraft.world.entity.LivingEntity)) {
return false;
}
FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(ent.getUUID(), player.getBukkitEntity());
if (override == null || (override.hand == null && override.offhand == null)) {
return false;
}
if (((ClientboundEntityEventPacket) packet).getEventId() != (byte) 55) {
return false;
}
List<Pair<net.minecraft.world.entity.EquipmentSlot, ItemStack>> equipment = new ArrayList<>();
ItemStack hand = override.hand != null ? CraftItemStack.asNMSCopy(override.hand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getMainHandItem();
ItemStack offhand = override.offhand != null ? CraftItemStack.asNMSCopy(override.offhand.getItemStack()) : ((net.minecraft.world.entity.LivingEntity) ent).getOffhandItem();
equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.MAINHAND, hand));
equipment.add(new Pair<>(net.minecraft.world.entity.EquipmentSlot.OFFHAND, offhand));
ClientboundSetEquipmentPacket newPacket = new ClientboundSetEquipmentPacket(ent.getId(), equipment);
oldManager.send(newPacket, genericfuturelistener);
return true;
} else if (packet instanceof ClientboundContainerSetContentPacket) {
FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
if (override == null) {
return false;
}
int window = ((ClientboundContainerSetContentPacket) packet).getContainerId();
if (window != 0) {
return false;
}
NonNullList<ItemStack> items = (NonNullList<ItemStack>) ((ClientboundContainerSetContentPacket) packet).getItems();
if (override.head != null) {
items.set(5, CraftItemStack.asNMSCopy(override.head.getItemStack()));
}
if (override.chest != null) {
items.set(6, CraftItemStack.asNMSCopy(override.chest.getItemStack()));
}
if (override.legs != null) {
items.set(7, CraftItemStack.asNMSCopy(override.legs.getItemStack()));
}
if (override.boots != null) {
items.set(8, CraftItemStack.asNMSCopy(override.boots.getItemStack()));
}
if (override.offhand != null) {
items.set(45, CraftItemStack.asNMSCopy(override.offhand.getItemStack()));
}
if (override.hand != null) {
items.set(player.getInventory().selected + 36, CraftItemStack.asNMSCopy(override.hand.getItemStack()));
}
ClientboundContainerSetContentPacket newPacket = new ClientboundContainerSetContentPacket(window, ((ClientboundContainerSetContentPacket) packet).getStateId(), items, ((ClientboundContainerSetContentPacket) packet).getCarriedItem());
oldManager.send(newPacket, genericfuturelistener);
return true;
} else if (packet instanceof ClientboundContainerSetSlotPacket) {
FakeEquipCommand.EquipmentOverride override = FakeEquipCommand.getOverrideFor(player.getUUID(), player.getBukkitEntity());
if (override == null) {
return false;
}
int window = ((ClientboundContainerSetSlotPacket) packet).getContainerId();
if (window != 0) {
return false;
}
int slot = ((ClientboundContainerSetSlotPacket) packet).getSlot();
org.bukkit.inventory.ItemStack item = null;
if (slot == 5 && override.head != null) {
item = override.head.getItemStack();
} else if (slot == 6 && override.chest != null) {
item = override.chest.getItemStack();
} else if (slot == 7 && override.legs != null) {
item = override.legs.getItemStack();
} else if (slot == 8 && override.boots != null) {
item = override.boots.getItemStack();
} else if (slot == 45 && override.offhand != null) {
item = override.offhand.getItemStack();
} else if (slot == player.getInventory().selected + 36 && override.hand != null) {
item = override.hand.getItemStack();
}
if (item == null) {
return false;
}
ClientboundContainerSetSlotPacket newPacket = new ClientboundContainerSetSlotPacket(window, ((ClientboundContainerSetSlotPacket) packet).getStateId(), slot, CraftItemStack.asNMSCopy(item));
oldManager.send(newPacket, genericfuturelistener);
return true;
}
} catch (Throwable ex) {
Debug.echoError(ex);
}
return false;
}
use of net.minecraft.world.item.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class FishingHelperImpl method getResult.
@Override
public org.bukkit.inventory.ItemStack getResult(FishHook fishHook, CatchType catchType) {
ItemStack result = null;
FishingHook nmsHook = ((CraftFishHook) fishHook).getHandle();
if (catchType == CatchType.DEFAULT) {
float f = ((CraftWorld) fishHook.getWorld()).getHandle().random.nextFloat();
int i = EnchantmentHelper.getMobLooting(nmsHook.getPlayerOwner());
int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.FISHING_LUCK, nmsHook.getPlayerOwner());
float f1 = 0.1F - (float) i * 0.025F - (float) j * 0.01F;
float f2 = 0.05F + (float) i * 0.01F - (float) j * 0.01F;
f1 = Mth.clamp(f1, 0.0F, 1.0F);
f2 = Mth.clamp(f2, 0.0F, 1.0F);
if (f < f1) {
result = catchRandomJunk(nmsHook);
} else {
f -= f1;
if (f < f2) {
result = catchRandomTreasure(nmsHook);
} else {
result = catchRandomFish(nmsHook);
}
}
} else if (catchType == CatchType.JUNK) {
result = catchRandomJunk(nmsHook);
} else if (catchType == CatchType.TREASURE) {
result = catchRandomTreasure(nmsHook);
} else if (catchType == CatchType.FISH) {
result = catchRandomFish(nmsHook);
}
if (result != null) {
return CraftItemStack.asBukkitCopy(result);
} else {
return null;
}
}
use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class ForgeInternalHandler method onEntityJoinWorld.
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onEntityJoinWorld(EntityJoinWorldEvent event) {
Entity entity = event.getEntity();
if (entity.getClass().equals(ItemEntity.class)) {
ItemStack stack = ((ItemEntity) entity).getItem();
Item item = stack.getItem();
if (item.hasCustomEntity(stack)) {
Entity newEntity = item.createEntity(event.getWorld(), entity, stack);
if (newEntity != null) {
entity.discard();
event.setCanceled(true);
var executor = LogicalSidedProvider.WORKQUEUE.get(event.getWorld().isClientSide ? LogicalSide.CLIENT : LogicalSide.SERVER);
executor.tell(new TickTask(0, () -> event.getWorld().addFreshEntity(newEntity)));
}
}
}
}
Aggregations