use of org.spongepowered.common.interfaces.world.IMixinWorld in project SpongeCommon by SpongePowered.
the class MixinSlotCrafting method afterTake.
/**
* Create CraftItemEvent.Post result is also handled by
* {@link MixinContainer#redirectTransferStackInSlot} or
* {@link MixinContainer#redirectOnTakeThrow}
*/
@Inject(method = "onTake", cancellable = true, at = @At("RETURN"))
private void afterTake(EntityPlayer thePlayer, ItemStack stack, CallbackInfoReturnable<ItemStack> cir) {
if (((IMixinWorld) thePlayer.world).isFake()) {
return;
}
((IMixinContainer) thePlayer.openContainer).detectAndSendChanges(true);
((IMixinContainer) thePlayer.openContainer).setCaptureInventory(false);
Container container = thePlayer.openContainer;
Inventory craftInv = ((Inventory) container).query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
if (!(craftInv instanceof CraftingInventory)) {
SpongeImpl.getLogger().warn("Detected crafting without a InventoryCrafting!? Crafting Event will not fire.");
return;
}
// retain only last slot-transactions on output slot
SlotTransaction first = null;
List<SlotTransaction> capturedTransactions = ((IMixinContainer) container).getCapturedTransactions();
for (Iterator<SlotTransaction> iterator = capturedTransactions.iterator(); iterator.hasNext(); ) {
SlotTransaction trans = iterator.next();
Optional<SlotIndex> slotIndex = trans.getSlot().getInventoryProperty(SlotIndex.class);
if (slotIndex.isPresent() && slotIndex.get().getValue() == 0) {
iterator.remove();
if (first == null) {
first = trans;
}
}
}
ItemStackSnapshot craftedItem;
// if we got a transaction on the crafting-slot use this
if (first != null) {
capturedTransactions.add(first);
craftedItem = first.getOriginal().copy();
} else {
if (stack.isEmpty()) {
// if stack is empty this was probably shift-crafting so we use the captured itemstack instead
craftedItem = ItemStackUtil.snapshotOf(this.craftedStack);
} else {
craftedItem = ItemStackUtil.snapshotOf(stack);
}
}
CraftingInventory craftingInventory = (CraftingInventory) craftInv;
CraftItemEvent.Craft event = SpongeCommonEventFactory.callCraftEventPost(thePlayer, craftingInventory, craftedItem, this.lastRecipe, container, capturedTransactions);
((IMixinContainer) container).setLastCraft(event);
((IMixinContainer) container).setFirePreview(true);
this.craftedStack = null;
SlotTransaction last = new SlotTransaction(craftingInventory.getResult(), ItemStackSnapshot.NONE, ItemStackUtil.snapshotOf(this.getStack()));
List<SlotTransaction> previewTransactions = ((IMixinContainer) container).getPreviewTransactions();
CraftingRecipe newRecipe = Sponge.getRegistry().getCraftingRecipeRegistry().findMatchingRecipe(craftingInventory.getCraftingGrid(), ((World) player.world)).orElse(null);
SpongeCommonEventFactory.callCraftEventPre(thePlayer, craftingInventory, last, newRecipe, container, previewTransactions);
previewTransactions.clear();
}
use of org.spongepowered.common.interfaces.world.IMixinWorld in project SpongeCommon by SpongePowered.
the class EntityActivationRange method activateEntities.
/**
* Find what entities are in range of the players in the world and set
* active if in range.
*
* @param world The world to perform activation checks in
*/
public static void activateEntities(World world) {
if (((IMixinWorld) world).isFake()) {
return;
}
for (EntityPlayer player : world.playerEntities) {
int maxRange = 0;
for (Integer range : maxActivationRanges.values()) {
if (range > maxRange) {
maxRange = range;
}
}
maxRange = Math.min((((org.spongepowered.api.world.World) world).getViewDistance() << 4) - 8, maxRange);
((IModData_Activation) player).setActivatedTick(SpongeImpl.getServer().getTickCounter());
growBb(maxBB, player.getEntityBoundingBox(), maxRange, 256, maxRange);
int i = MathHelper.floor(maxBB.minX / 16.0D);
int j = MathHelper.floor(maxBB.maxX / 16.0D);
int k = MathHelper.floor(maxBB.minZ / 16.0D);
int l = MathHelper.floor(maxBB.maxZ / 16.0D);
for (int i1 = i; i1 <= j; ++i1) {
for (int j1 = k; j1 <= l; ++j1) {
WorldServer worldserver = (WorldServer) world;
Chunk chunk = ((IMixinChunkProviderServer) worldserver.getChunkProvider()).getLoadedChunkWithoutMarkingActive(i1, j1);
if (chunk != null) {
activateChunkEntities(player, chunk);
}
}
}
}
}
use of org.spongepowered.common.interfaces.world.IMixinWorld in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method callWorldLoadEvent.
private static LoadWorldEvent callWorldLoadEvent(Event event) {
LoadWorldEvent spongeEvent = (LoadWorldEvent) event;
// Since Forge only uses a single save handler, we need to make sure to pass the overworld's handler.
// This makes sure that mods dont attempt to save/read their data from the wrong location.
final net.minecraft.world.World minecraftWorld = (net.minecraft.world.World) spongeEvent.getTargetWorld();
((IMixinWorld) spongeEvent.getTargetWorld()).setCallingWorldEvent(true);
((IMixinChunkProviderServer) minecraftWorld.getChunkProvider()).setForceChunkRequests(true);
((IMixinEventBus) MinecraftForge.EVENT_BUS).post(new WorldEvent.Load(minecraftWorld), true);
((IMixinChunkProviderServer) minecraftWorld.getChunkProvider()).setForceChunkRequests(false);
((IMixinWorld) minecraftWorld).setCallingWorldEvent(false);
return spongeEvent;
}
use of org.spongepowered.common.interfaces.world.IMixinWorld in project SpongeForge by SpongePowered.
the class MixinEntityPlayer method wakeUpPlayer.
/**
* @author JBYoshi - November 23rd, 2015
* @reason implement SpongeAPI events.
*
* @param immediately Whether to be woken up immediately
* @param updateWorldFlag Whether to update the world
* @param setSpawn Whether the player has successfully set up spawn
*/
@Overwrite
public void wakeUpPlayer(boolean immediately, boolean updateWorldFlag, boolean setSpawn) {
IBlockState iblockstate = this.world.getBlockState(this.bedLocation);
Transform<World> newLocation = null;
if (this.bedLocation != null && iblockstate.getBlock().isBed(iblockstate, this.world, this.bedLocation, (Entity) (Object) this)) {
iblockstate.getBlock().setBedOccupied(this.world, this.bedLocation, ((EntityPlayer) (Object) this), false);
BlockPos blockpos = iblockstate.getBlock().getBedSpawnPosition(iblockstate, this.world, this.bedLocation, ((EntityPlayer) (Object) this));
if (blockpos == null) {
blockpos = ((EntityPlayer) (Object) this).bedLocation.up();
}
newLocation = this.getTransform().setPosition(new Vector3d(blockpos.getX() + 0.5F, blockpos.getY() + 0.1F, blockpos.getZ() + 0.5F));
}
SleepingEvent.Post post = null;
if (!((IMixinWorld) this.world).isFake()) {
try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(this);
post = SpongeEventFactory.createSleepingEventPost(Sponge.getCauseStackManager().getCurrentCause(), this.getWorld().createSnapshot(VecHelper.toVector3i(this.bedLocation)), Optional.ofNullable(newLocation), this, setSpawn);
Sponge.getEventManager().post(post);
if (post.isCancelled()) {
return;
}
ForgeEventFactory.onPlayerWakeup(((EntityPlayer) (Object) this), immediately, updateWorldFlag, setSpawn);
this.setSize(0.6F, 1.8F);
if (post.getSpawnTransform().isPresent()) {
this.setLocationAndAngles(post.getSpawnTransform().get());
}
}
} else {
ForgeEventFactory.onPlayerWakeup(((EntityPlayer) (Object) this), immediately, updateWorldFlag, setSpawn);
this.setSize(0.6F, 1.8F);
}
this.sleeping = false;
if (!this.world.isRemote && updateWorldFlag) {
this.world.updateAllPlayersSleepingFlag();
}
this.sleepTimer = immediately ? 0 : 100;
if (setSpawn) {
this.setSpawnPoint(this.bedLocation, false);
}
if (post != null) {
Sponge.getGame().getEventManager().post(SpongeEventFactory.createSleepingEventFinish(post.getCause(), this.getWorld().createSnapshot(VecHelper.toVector3i(this.bedLocation)), this));
}
}
use of org.spongepowered.common.interfaces.world.IMixinWorld in project SpongeCommon by SpongePowered.
the class MixinEntityFallingBlock method onWorldSetBlockToAir.
@Inject(method = "onUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;setBlockToAir(Lnet/minecraft/util/math/BlockPos;)Z", shift = At.Shift.AFTER), cancellable = true)
private void onWorldSetBlockToAir(CallbackInfo ci) {
final BlockPos pos = new BlockPos((EntityFallingBlock) (Object) this);
if (((IMixinWorld) this.world).isFake()) {
this.world.setBlockToAir(pos);
return;
}
// Ideally, at this point we should still be in the EntityTickState and only this block should
// be changing. What we need to do here is throw the block event specifically for setting air
// and THEN if this one cancels, we should kill this entity off, unless we want some duplication
// of falling blocks
final PhaseData currentPhaseData = PhaseTracker.getInstance().getCurrentPhaseData();
this.world.setBlockToAir(pos);
// By this point, we should have one captured block at least.
if (!TrackingUtil.processBlockCaptures(currentPhaseData.context.getCapturedBlocks(), currentPhaseData.state, currentPhaseData.context)) {
// So, it's been cancelled, we want to absolutely remove this entity.
// And we want to stop the entity update at this point.
this.setDead();
ci.cancel();
}
}
Aggregations