use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeCommon by SpongePowered.
the class MixinWorld method onGetClosestPlayerCheck.
@Redirect(method = "getClosestPlayer(DDDDLcom/google/common/base/Predicate;)Lnet/minecraft/entity/player/EntityPlayer;", at = @At(value = "INVOKE", target = "Lcom/google/common/base/Predicate;apply(Ljava/lang/Object;)Z", remap = false))
private boolean onGetClosestPlayerCheck(com.google.common.base.Predicate<net.minecraft.entity.Entity> predicate, Object entityPlayer) {
EntityPlayer player = (EntityPlayer) entityPlayer;
IMixinEntity mixinEntity = (IMixinEntity) player;
return predicate.apply(player) && !mixinEntity.isVanished();
}
use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeCommon by SpongePowered.
the class DisplayNameDataProcessor method set.
@Override
public DataTransactionResult set(DataHolder holder, DisplayNameData manipulator, MergeFunction function) {
if (holder instanceof IMixinEntity && !(holder instanceof Player)) {
final Optional<DisplayNameData> old = from(holder);
final DisplayNameData merged = checkNotNull(function).merge(old.orElse(null), manipulator);
final Text newValue = merged.displayName().get();
final ImmutableValue<Text> immutableValue = merged.displayName().asImmutable();
try {
((IMixinEntity) holder).setDisplayName(newValue);
if (old.isPresent()) {
return DataTransactionResult.successReplaceResult(old.get().displayName().asImmutable(), immutableValue);
}
return DataTransactionResult.successResult(immutableValue);
} catch (Exception e) {
SpongeImpl.getLogger().debug("An exception occurred when setting data: ", e);
return DataTransactionResult.errorResult(immutableValue);
}
}
if (holder instanceof ItemStack) {
final Optional<DisplayNameData> prevValue = from(holder);
final DisplayNameData merged = checkNotNull(function).merge(prevValue.orElse(null), manipulator);
final Text newValue = merged.displayName().get();
final ImmutableValue<Text> immutableValue = merged.displayName().asImmutable();
ItemStack stack = (ItemStack) holder;
if (stack.getItem() == Items.WRITTEN_BOOK) {
NbtDataUtil.getOrCreateCompound(stack).setString(NbtDataUtil.ITEM_BOOK_TITLE, SpongeTexts.toLegacy(newValue));
} else {
stack.setStackDisplayName(SpongeTexts.toLegacy(newValue));
}
if (prevValue.isPresent()) {
return DataTransactionResult.successReplaceResult(prevValue.get().displayName().asImmutable(), immutableValue);
}
return DataTransactionResult.successResult(immutableValue);
}
return DataTransactionResult.failResult(manipulator.getValues());
}
use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeCommon by SpongePowered.
the class SpongeMessageHandler method handleRequest.
public static void handleRequest(MessageTrackerDataRequest message, RemoteConnection connection, Platform.Type side) {
if (!(connection instanceof PlayerConnection)) {
return;
}
Player player = ((PlayerConnection) connection).getPlayer();
if (!player.hasPermission("sponge.debug.block-tracking")) {
return;
}
EntityPlayerMP sender = (EntityPlayerMP) player;
BlockPos pos = new BlockPos(message.x, message.y, message.z);
if (!sender.world.isBlockLoaded(pos)) {
return;
}
String ownerName;
String notifierName;
Optional<User> owner = Optional.empty();
Optional<User> notifier = Optional.empty();
if (message.type == 0) {
// block
IMixinChunk spongeChunk = (IMixinChunk) sender.world.getChunkFromBlockCoords(pos);
owner = spongeChunk.getBlockOwner(pos);
notifier = spongeChunk.getBlockNotifier(pos);
} else if (message.type == 1) {
// entity
Entity entity = sender.world.getEntityByID(message.entityId);
if (entity != null) {
IMixinEntity spongeEntity = (IMixinEntity) entity;
owner = spongeEntity.getCreatorUser();
notifier = spongeEntity.getNotifierUser();
}
}
ownerName = owner.map(User::getName).orElse("");
notifierName = notifier.map(User::getName).orElse("");
channel.sendTo(player, new MessageTrackerDataResponse(ownerName, notifierName));
}
use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeForge by SpongePowered.
the class MixinItemShears method itemInteractionForEntity.
/**
* @author gabizou - June 21st, 2016
* @reason Rewrites the forge handling of this to properly handle
* when sheared drops are captured by whatever current phase the
* {@link PhaseTracker} is in.
*
* Returns true if the item can be used on the given entity, e.g. shears on sheep.
*/
@Overwrite
@Override
public boolean itemInteractionForEntity(ItemStack itemstack, EntityPlayer player, EntityLivingBase entity, EnumHand hand) {
if (entity.world.isRemote) {
return false;
}
if (entity instanceof IShearable) {
IShearable target = (IShearable) entity;
BlockPos pos = new BlockPos(entity.posX, entity.posY, entity.posZ);
if (target.isShearable(itemstack, entity.world, pos)) {
List<ItemStack> drops = target.onSheared(itemstack, entity.world, pos, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, itemstack));
// Sponge Start - Handle drops according to the current phase
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData currentData = phaseTracker.getCurrentPhaseData();
final IPhaseState<?> currentState = currentData.state;
final PhaseContext<?> phaseContext = currentData.context;
final Random random = EntityUtil.fromNative(entity).getRandom();
final IMixinEntity mixinEntity = EntityUtil.toMixin(entity);
final double posX = entity.posX;
final double posY = entity.posY + 1.0F;
final double posZ = entity.posZ;
final Vector3d position = new Vector3d(posX, posY, posZ);
// Now the real fun begins.
for (ItemStack drop : drops) {
final ItemStack item;
if (!drop.isEmpty()) {
try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
// FIRST we want to throw the DropItemEvent.PRE
final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(drop);
final List<ItemStackSnapshot> original = new ArrayList<>();
original.add(snapshot);
Sponge.getCauseStackManager().pushCause(entity);
final DropItemEvent.Pre dropEvent = SpongeEventFactory.createDropItemEventPre(Sponge.getCauseStackManager().getCurrentCause(), ImmutableList.of(snapshot), original);
if (dropEvent.isCancelled()) {
continue;
}
// SECOND throw the ConstructEntityEvent
Transform<World> suggested = new Transform<>(mixinEntity.getWorld(), position);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), EntityTypes.ITEM, suggested);
SpongeImpl.postEvent(event);
item = event.isCancelled() ? null : ItemStackUtil.fromSnapshotToNative(dropEvent.getDroppedItems().get(0));
}
} else {
continue;
}
if (item == null) {
continue;
}
if (!item.isEmpty()) {
if (!currentState.ignoresItemPreMerging() && SpongeImpl.getGlobalConfig().getConfig().getOptimizations().doDropsPreMergeItemDrops()) {
if (currentState.tracksEntitySpecificDrops()) {
final Multimap<UUID, ItemDropData> multimap = phaseContext.getCapturedEntityDropSupplier().get();
final Collection<ItemDropData> itemStacks = multimap.get(entity.getUniqueID());
SpongeImplHooks.addItemStackToListForSpawning(itemStacks, ItemDropData.item(item).motion(new Vector3d((random.nextFloat() - random.nextFloat()) * 0.1F, random.nextFloat() * 0.05F, (random.nextFloat() - random.nextFloat()) * 0.1F)).position(new Vector3d(posX, posY, posZ)).build());
continue;
}
final List<ItemDropData> itemStacks = phaseContext.getCapturedItemStackSupplier().get();
SpongeImplHooks.addItemStackToListForSpawning(itemStacks, ItemDropData.item(item).position(new Vector3d(posX, posY, posZ)).motion(new Vector3d((random.nextFloat() - random.nextFloat()) * 0.1F, random.nextFloat() * 0.05F, (random.nextFloat() - random.nextFloat()) * 0.1F)).build());
continue;
}
EntityItem entityitem = new EntityItem(entity.world, posX, posY, posZ, item);
entityitem.setDefaultPickupDelay();
entityitem.motionY += random.nextFloat() * 0.05F;
entityitem.motionX += (random.nextFloat() - random.nextFloat()) * 0.1F;
entityitem.motionZ += (random.nextFloat() - random.nextFloat()) * 0.1F;
// FIFTH - Capture the entity maybe?
if (currentState.doesCaptureEntityDrops()) {
if (currentState.tracksEntitySpecificDrops()) {
// We are capturing per entity drop
phaseContext.getCapturedEntityItemDropSupplier().get().put(entity.getUniqueID(), entityitem);
} else {
// We are adding to a general list - usually for EntityPhase.State.DEATH
phaseContext.getCapturedItemsSupplier().get().add(entityitem);
}
// Return the item, even if it wasn't spawned in the world.
continue;
}
// FINALLY - Spawn the entity in the world if all else didn't fail
entity.world.spawnEntity(entityitem);
}
}
// Sponge End
itemstack.damageItem(1, entity);
}
return true;
}
return false;
}
use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeForge by SpongePowered.
the class MixinWorld_Activation method updateEntityWithOptionalForce.
@Overwrite
public void updateEntityWithOptionalForce(Entity entityIn, boolean forceUpdate) {
// Sponge start - area is handled in ActivationRange
// int i = MathHelper.floor_double(entityIn.posX);
// int j = MathHelper.floor_double(entityIn.posZ);
// boolean isForced = getPersistentChunks().containsKey(new net.minecraft.util.math.ChunkPos(i >> 4, j >> 4));
// int k = isForced ? 0 : 32;
// boolean canUpdate = !forceUpdate || this.isAreaLoaded(i - k, 0, j - k, i + k, 0, j + k, true);
boolean canUpdate = EntityActivationRange.checkIfActive(entityIn);
// Allow forge mods to force an update
if (!canUpdate)
canUpdate = net.minecraftforge.event.ForgeEventFactory.canEntityUpdate(entityIn);
if (!canUpdate) {
entityIn.ticksExisted++;
((IModData_Activation) entityIn).inactiveTick();
return;
}
// Sponge end
entityIn.lastTickPosX = entityIn.posX;
entityIn.lastTickPosY = entityIn.posY;
entityIn.lastTickPosZ = entityIn.posZ;
entityIn.prevRotationYaw = entityIn.rotationYaw;
entityIn.prevRotationPitch = entityIn.rotationPitch;
if (forceUpdate && entityIn.addedToChunk) {
++entityIn.ticksExisted;
// Sponge
++co.aikar.timings.TimingHistory.activatedEntityTicks;
if (entityIn.isRiding()) {
entityIn.updateRidden();
} else {
entityIn.onUpdate();
}
}
if (Double.isNaN(entityIn.posX) || Double.isInfinite(entityIn.posX)) {
entityIn.posX = entityIn.lastTickPosX;
}
if (Double.isNaN(entityIn.posY) || Double.isInfinite(entityIn.posY)) {
entityIn.posY = entityIn.lastTickPosY;
}
if (Double.isNaN(entityIn.posZ) || Double.isInfinite(entityIn.posZ)) {
entityIn.posZ = entityIn.lastTickPosZ;
}
if (Double.isNaN(entityIn.rotationPitch) || Double.isInfinite(entityIn.rotationPitch)) {
entityIn.rotationPitch = entityIn.prevRotationPitch;
}
if (Double.isNaN(entityIn.rotationYaw) || Double.isInfinite(entityIn.rotationYaw)) {
entityIn.rotationYaw = entityIn.prevRotationYaw;
}
int l = MathHelper.floor(entityIn.posX / 16.0D);
int i1 = MathHelper.floor(entityIn.posY / 16.0D);
int j1 = MathHelper.floor(entityIn.posZ / 16.0D);
if (!entityIn.addedToChunk || entityIn.chunkCoordX != l || entityIn.chunkCoordY != i1 || entityIn.chunkCoordZ != j1) {
// Sponge start - use cached chunk
final Chunk activeChunk = (Chunk) ((IMixinEntity) entityIn).getActiveChunk();
if (activeChunk != null) {
activeChunk.removeEntityAtIndex(entityIn, entityIn.chunkCoordY);
}
// Sponge end
final IMixinChunk newChunk = (IMixinChunk) ((IMixinChunkProviderServer) entityIn.world.getChunkProvider()).getLoadedChunkWithoutMarkingActive(l, j1);
final boolean isPositionDirty = entityIn.setPositionNonDirty();
if (newChunk == null || (!isPositionDirty && newChunk.isQueuedForUnload() && !newChunk.isPersistedChunk())) {
entityIn.addedToChunk = false;
} else {
((net.minecraft.world.chunk.Chunk) newChunk).addEntity(entityIn);
}
}
if (forceUpdate && entityIn.addedToChunk) {
for (Entity entity : entityIn.getPassengers()) {
if (!entity.isDead && entity.getRidingEntity() == entityIn) {
this.updateEntity(entity);
} else {
entity.dismountRidingEntity();
}
}
}
}
Aggregations