use of net.minecraft.server.level.ServerLevel in project Tropicraft by Tropicraft.
the class TropicraftSpawnEgg method useOn.
public InteractionResult useOn(UseOnContext context) {
Level world = context.getLevel();
if (world.isClientSide) {
return InteractionResult.SUCCESS;
} else {
ItemStack itemStack = context.getItemInHand();
BlockPos pos = context.getClickedPos();
Direction dir = context.getClickedFace();
BlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block == Blocks.SPAWNER) {
BlockEntity te = world.getBlockEntity(pos);
if (te instanceof SpawnerBlockEntity) {
BaseSpawner spawner = ((SpawnerBlockEntity) te).getSpawner();
EntityType<?> spawnType = typeIn.get();
spawner.setEntityId(spawnType);
te.setChanged();
world.sendBlockUpdated(pos, state, state, 3);
itemStack.shrink(1);
return InteractionResult.SUCCESS;
}
}
BlockPos spawnPos;
if (state.getCollisionShape(world, pos).isEmpty()) {
spawnPos = pos;
} else {
spawnPos = pos.relative(dir);
}
EntityType<?> type3 = typeIn.get();
if (type3.spawn((ServerLevel) world, itemStack, context.getPlayer(), spawnPos, MobSpawnType.SPAWN_EGG, true, !Objects.equals(pos, spawnPos) && dir == Direction.UP) != null) {
itemStack.shrink(1);
}
return InteractionResult.SUCCESS;
}
}
use of net.minecraft.server.level.ServerLevel in project MyPet by xXKeyleXx.
the class EntityMyPig method handlePlayerInteraction.
@Override
public InteractionResult handlePlayerInteraction(final net.minecraft.world.entity.player.Player entityhuman, InteractionHand enumhand, final ItemStack itemStack) {
if (enumhand == InteractionHand.OFF_HAND) {
if (itemStack != null) {
if (itemStack.getItem() == Items.LEAD) {
((ServerLevel) this.level).getChunkProvider().broadcastAndSend(this, new ClientboundSetEntityLinkPacket(this, null));
entityhuman.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
new BukkitRunnable() {
@Override
public void run() {
if (entityhuman instanceof ServerPlayer) {
// TODO
entityhuman.setItemInHand(InteractionHand.OFF_HAND, itemStack);
Player p = (Player) entityhuman.getBukkitEntity();
if (!p.isOnline()) {
p.saveData();
}
}
}
}.runTaskLater(MyPetApi.getPlugin(), 5);
}
}
return InteractionResult.CONSUME;
}
if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
return InteractionResult.CONSUME;
}
if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
if (itemStack.getItem() == Items.SADDLE && !getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
getMyPet().setSaddle(CraftItemStack.asBukkitCopy(itemStack));
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
itemStack.shrink(1);
if (itemStack.getCount() <= 0) {
entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
}
}
return InteractionResult.CONSUME;
} else if (itemStack.getItem() == Items.SHEARS && getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), CraftItemStack.asNMSCopy(getMyPet().getSaddle()));
entityitem.pickupDelay = 10;
entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
this.level.addFreshEntity(entityitem);
makeSound("entity.sheep.shear", 1.0F, 1.0F);
getMyPet().setSaddle(null);
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
try {
itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
} catch (Error e) {
// TODO REMOVE
itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
try {
CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
});
}
}
return InteractionResult.CONSUME;
} else if (Configuration.MyPet.Pig.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
itemStack.shrink(1);
if (itemStack.getCount() <= 0) {
entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
}
}
getMyPet().setBaby(false);
return InteractionResult.CONSUME;
}
}
return InteractionResult.PASS;
}
use of net.minecraft.server.level.ServerLevel in project MyPet by xXKeyleXx.
the class EntityMyEnderDragon method onLivingUpdate.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onLivingUpdate() {
super.onLivingUpdate();
if (Configuration.MyPet.EnderDragon.CAN_GLIDE) {
if (!this.onGround && this.getDeltaMovement().y() < 0.0D) {
this.setDeltaMovement(getDeltaMovement().multiply(1, 0.6D, 1));
}
}
if (!registered && this.valid) {
if (this.getCommandSenderWorld() instanceof ServerLevel) {
ServerLevel world = (ServerLevel) this.getCommandSenderWorld();
// The next part used to be prettier but... whilst it is listed everywhere I looked, world.dragonParts is just not... available?
// Mojang Field: dragonParts
Field dragonPartsField = ReflectionUtil.getField(ServerLevel.class, "ac");
Int2ObjectMap dragonParts = (Int2ObjectMap) ReflectionUtil.getFieldValue(dragonPartsField, world);
Arrays.stream(this.children).forEach(entityMyPetPart -> dragonParts.put(entityMyPetPart.getId(), entityMyPetPart));
ReflectionUtil.setFieldValue(dragonPartsField, world, dragonParts);
}
this.registered = true;
}
}
use of net.minecraft.server.level.ServerLevel in project MyPet by xXKeyleXx.
the class PlatformHelper method strikeLightning.
@Override
public void strikeLightning(Location loc, float distance) {
ServerLevel world = ((CraftWorld) loc.getWorld()).getHandle();
LightningBolt lightning = new LightningBolt(EntityType.LIGHTNING_BOLT, world);
lightning.setVisualOnly(true);
lightning.moveTo(loc.getX(), loc.getY(), loc.getZ(), 0.0F, 0.0F);
world.getCraftServer().getServer().getPlayerList().broadcast(null, loc.getX(), loc.getY(), loc.getZ(), distance, world.dimension(), new ClientboundAddEntityPacket(lightning));
world.getCraftServer().getServer().getPlayerList().broadcast(null, loc.getX(), loc.getY(), loc.getZ(), distance, world.dimension(), new ClientboundSoundPacket(SoundEvents.LIGHTNING_BOLT_THUNDER, SoundSource.WEATHER, loc.getX(), loc.getY(), loc.getZ(), distance, 1F));
}
use of net.minecraft.server.level.ServerLevel in project MyPet by xXKeyleXx.
the class EntityMyPig method handlePlayerInteraction.
@Override
public InteractionResult handlePlayerInteraction(final net.minecraft.world.entity.player.Player entityhuman, InteractionHand enumhand, final ItemStack itemStack) {
if (enumhand == InteractionHand.OFF_HAND) {
if (itemStack != null) {
if (itemStack.getItem() == Items.LEAD) {
((ServerLevel) this.level).getChunkSource().broadcastAndSend(this, new ClientboundSetEntityLinkPacket(this, null));
entityhuman.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
new BukkitRunnable() {
@Override
public void run() {
if (entityhuman instanceof ServerPlayer) {
// TODO
entityhuman.setItemInHand(InteractionHand.OFF_HAND, itemStack);
Player p = (Player) entityhuman.getBukkitEntity();
if (!p.isOnline()) {
p.saveData();
}
}
}
}.runTaskLater(MyPetApi.getPlugin(), 5);
}
}
return InteractionResult.CONSUME;
}
if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
return InteractionResult.CONSUME;
}
if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
if (itemStack.getItem() == Items.SADDLE && !getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
getMyPet().setSaddle(CraftItemStack.asBukkitCopy(itemStack));
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
itemStack.shrink(1);
if (itemStack.getCount() <= 0) {
entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
}
}
return InteractionResult.CONSUME;
} else if (itemStack.getItem() == Items.SHEARS && getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), CraftItemStack.asNMSCopy(getMyPet().getSaddle()));
entityitem.pickupDelay = 10;
entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
this.level.addFreshEntity(entityitem);
makeSound("entity.sheep.shear", 1.0F, 1.0F);
getMyPet().setSaddle(null);
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
try {
itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
} catch (Error e) {
// TODO REMOVE
itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
try {
CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
});
}
}
return InteractionResult.CONSUME;
} else if (Configuration.MyPet.Pig.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
itemStack.shrink(1);
if (itemStack.getCount() <= 0) {
entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
}
}
getMyPet().setBaby(false);
return InteractionResult.CONSUME;
}
}
return InteractionResult.PASS;
}
Aggregations