use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.
the class SpongePacketHandler method init.
public static void init(final SpongeChannelManager registry) {
SpongePacketHandler.channel = registry.createChannel(ResourceKey.sponge("default"), PacketChannel.class);
SpongePacketHandler.channel.registerTransactional(RequestBlockTrackerDataPacket.class, TrackerDataResponsePacket.class, 0).setRequestHandler(EngineConnectionTypes.SERVER_PLAYER, (requestPacket, connection, response) -> {
final ServerPlayer player = connection.player();
if (!player.hasPermission("sponge.debug.block-tracking")) {
return;
}
final net.minecraft.server.level.ServerPlayer sender = (net.minecraft.server.level.ServerPlayer) player;
final BlockPos pos = new BlockPos(requestPacket.x, requestPacket.y, requestPacket.z);
if (!sender.level.hasChunkAt(pos)) {
return;
}
final LevelChunkBridge levelChunkBridge = (LevelChunkBridge) sender.level.getChunkAt(pos);
final Optional<UUID> owner = levelChunkBridge.bridge$getBlockCreatorUUID(pos);
final Optional<UUID> notifier = levelChunkBridge.bridge$getBlockNotifierUUID(pos);
response.success(SpongePacketHandler.createTrackerDataResponse(owner, notifier));
});
SpongePacketHandler.channel.registerTransactional(RequestEntityTrackerDataPacket.class, TrackerDataResponsePacket.class, 1).setRequestHandler(EngineConnectionTypes.SERVER_PLAYER, (requestPacket, connection, response) -> {
final ServerPlayer player = connection.player();
if (!player.hasPermission("sponge.debug.entity-tracking")) {
return;
}
final net.minecraft.server.level.ServerPlayer sender = (net.minecraft.server.level.ServerPlayer) player;
final Entity entity = sender.level.getEntity(requestPacket.entityId);
if (!(entity instanceof CreatorTrackedBridge)) {
return;
}
final CreatorTrackedBridge creatorTrackedBridge = (CreatorTrackedBridge) entity;
final Optional<UUID> owner = creatorTrackedBridge.tracker$getCreatorUUID();
final Optional<UUID> notifier = creatorTrackedBridge.tracker$getNotifierUUID();
response.success(SpongePacketHandler.createTrackerDataResponse(owner, notifier));
});
SpongePacketHandler.channel.register(ChangeViewerEnvironmentPacket.class, 3).addHandler(ClientSideConnection.class, (packet, connection) -> {
final ClientLevel world = Minecraft.getInstance().level;
if (world == null) {
return;
}
final DimensionType dimensionType = SpongeCommon.server().registryAccess().dimensionTypes().get(packet.dimensionLogic);
((LevelBridge) world).bridge$adjustDimensionLogic(dimensionType);
});
}
use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.
the class EntityMixin method impl$throwDropItemConstructEvent.
/**
* @return
* @author gabizou - January 30th, 2016
* @author blood - May 12th, 2016
* @author gabizou - June 2nd, 2016
* <p>
* TODO from i509VCB: gabizou's remider to refactor this code here
* @reason Rewrites the method entirely for several reasons:
* 1) If we are in a forge environment, we do NOT want forge to be capturing the item entities, because we handle them ourselves
* 2) If we are in a client environment, we should not perform any sort of processing whatsoever.
* 3) This method is entirely managed from the standpoint where our events have final say, as per usual.
*/
@Inject(method = "spawnAtLocation(Lnet/minecraft/world/item/ItemStack;F)Lnet/minecraft/world/entity/item/ItemEntity;", at = @At("HEAD"), cancellable = true)
public void impl$throwDropItemConstructEvent(final ItemStack stack, final float offsetY, final CallbackInfoReturnable<ItemEntity> cir) {
if (stack.isEmpty()) {
cir.setReturnValue(null);
return;
}
if (((LevelBridge) this.level).bridge$isFake()) {
return;
}
// Now the real fun begins.
final ItemStack item;
final double posX = this.shadow$position().x;
final double posY = this.shadow$position().y + offsetY;
final double posZ = this.shadow$position().z;
// FIRST we want to throw the DropItemEvent.PRE
final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(stack);
final List<ItemStackSnapshot> original = new ArrayList<>();
original.add(snapshot);
// We want to frame ourselves here, because of the two events we have to throw, first for the drop item event, then the constructentityevent.
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
// Perform the event throws first, if they return false, return null
item = SpongeCommonEventFactory.throwDropItemAndConstructEvent((Entity) (Object) this, posX, posY, posZ, snapshot, original, frame);
if (item == null || item.isEmpty()) {
cir.setReturnValue(null);
return;
}
final ItemEntity entityitem = new ItemEntity(this.level, posX, posY, posZ, item);
entityitem.setDefaultPickUpDelay();
this.level.addFreshEntity(entityitem);
cir.setReturnValue(entityitem);
}
}
use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.
the class LevelChunkMixin method impl$setTrackedUUID.
private void impl$setTrackedUUID(final BlockPos pos, final UUID uuid, final PlayerTracker.Type type, final BiConsumer<PlayerTracker, Integer> consumer) {
if (((LevelBridge) this.level).bridge$isFake()) {
return;
}
final PrimaryLevelDataBridge worldInfo = (PrimaryLevelDataBridge) this.level.getLevelData();
final int index = uuid == null ? -1 : worldInfo.bridge$getIndexForUniqueId(uuid);
if (pos.getY() <= 255) {
final short blockPos = Constants.Sponge.blockPosToShort(pos);
this.impl$computePlayerTracker(this.impl$trackedShortBlockPositions, blockPos, index, type, consumer);
return;
}
final int blockPos = Constants.Sponge.blockPosToInt(pos);
this.impl$computePlayerTracker(this.impl$trackedIntBlockPositions, blockPos, index, type, consumer);
}
use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.
the class LevelChunkMixin method bridge$addTrackedBlockPosition.
@Override
public void bridge$addTrackedBlockPosition(final Block block, final BlockPos pos, final UUID uuid, final PlayerTracker.Type trackerType) {
if (((LevelBridge) this.level).bridge$isFake()) {
return;
}
if (!PhaseTracker.getInstance().getPhaseContext().tracksCreatorsAndNotifiers()) {
// Don't track chunk gen
return;
}
// Update TE tracking cache
// We must always check for a TE as a mod block may not implement ITileEntityProvider if a TE exists
// Note: We do not check SpongeImplHooks.hasBlockTileEntity(block, state) as neighbor notifications do not include blockstate.
final BlockEntity blockEntity = this.blockEntities.get(pos);
if (blockEntity != null) {
if (blockEntity instanceof CreatorTrackedBridge) {
final CreatorTrackedBridge trackedBlockEntity = (CreatorTrackedBridge) blockEntity;
if (trackerType == PlayerTracker.Type.NOTIFIER) {
if (Objects.equals(trackedBlockEntity.tracker$getNotifierUUID().orElse(null), uuid)) {
return;
}
trackedBlockEntity.tracker$setTrackedUUID(PlayerTracker.Type.NOTIFIER, uuid);
} else {
if (Objects.equals(trackedBlockEntity.tracker$getCreatorUUID().orElse(null), uuid)) {
return;
}
trackedBlockEntity.tracker$setTrackedUUID(PlayerTracker.Type.CREATOR, uuid);
}
}
}
if (trackerType == PlayerTracker.Type.CREATOR) {
this.impl$setTrackedUUID(pos, uuid, trackerType, (pt, idx) -> {
pt.creatorindex = idx;
pt.notifierIndex = idx;
});
} else {
this.impl$setTrackedUUID(pos, uuid, trackerType, (pt, idx) -> pt.notifierIndex = idx);
}
}
use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.
the class AbstractArrowMixin method onProjectileHit.
/**
* Collide impact event post for plugins to cancel impact.
*/
@Inject(method = "onHitBlock", at = @At("HEAD"), cancellable = true)
private void onProjectileHit(final BlockHitResult hitResult, final CallbackInfo ci) {
if (!((LevelBridge) this.level).bridge$isFake() && hitResult.getType() != HitResult.Type.MISS) {
if (SpongeCommonEventFactory.handleCollideImpactEvent((AbstractArrow) (Object) this, this.impl$getProjectileSource(), hitResult)) {
this.shadow$playSound(SoundEvents.ARROW_HIT, 1.0F, 1.2F / (this.random.nextFloat() * 0.2F + 0.9F));
// Make it almost look like it collided with something
final BlockHitResult blockraytraceresult = (BlockHitResult) hitResult;
final BlockState blockstate = this.level.getBlockState(blockraytraceresult.getBlockPos());
this.lastState = blockstate;
final Vec3 vec3d = blockraytraceresult.getLocation().subtract(this.shadow$getX(), this.shadow$getY(), this.shadow$getZ());
this.shadow$setDeltaMovement(vec3d);
final Vec3 vec3d1 = vec3d.normalize().scale(0.05F);
this.shadow$setPos(this.shadow$getX() - vec3d1.x, this.shadow$getY() - vec3d1.y, this.shadow$getZ() - vec3d1.z);
this.inGround = true;
this.shakeTime = 7;
this.shadow$setCritArrow(false);
this.shadow$setPierceLevel((byte) 0);
this.shadow$setShotFromCrossbow(false);
this.resetPiercedEntities();
ci.cancel();
}
}
}
Aggregations